Java Code Examples for com.csvreader.CsvReader#readRecord()

The following examples show how to use com.csvreader.CsvReader#readRecord() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CsvUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns the CSV file represented by the given input stream as a
 * list of string arrays.
 *
 * @param in the {@link InputStream} representing the CSV file.
 * @param ignoreFirstRow whether to ignore the first row.
 * @return a list of string arrays.
 * @throws IOException
 */
public static List<String[]> readCsvAsList( InputStream in, boolean ignoreFirstRow )
    throws IOException
{
    CsvReader reader = getReader( in );

    if ( ignoreFirstRow )
    {
        reader.readRecord();
    }

    List<String[]> lines = new ArrayList<>();

    while ( reader.readRecord() )
    {
        lines.add( reader.getValues() );
    }

    return lines;
}
 
Example 2
Source File: CSVMatrixReaderTest.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
@Test
public void get() {
    try {
        CsvReader e = new CsvReader("src/test/resources/aml/iomatrix/testcsv.csv");
        e.readRecord();
        String val = e.get(0);
        Assert.assertEquals("1494574053", val);
        String val2 = e.get(100500);
        Assert.assertEquals("", val2);
        String val3 = e.get("SOME_COLUMN");
        Assert.assertEquals("", val3);
        
        System.out.println("finished");
        
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}
 
Example 3
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private List<OrganisationUnitGroup> orgUnitGroupsFromCsv( CsvReader reader )
    throws IOException
{
    List<OrganisationUnitGroup> list = new ArrayList<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            OrganisationUnitGroup object = new OrganisationUnitGroup();
            setIdentifiableObject( object, values );
            object.setAutoFields();
            object.setShortName( getSafe( values, 3, object.getName(), 50 ) );
            list.add( object );
        }
    }

    return list;
}
 
Example 4
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private List<CategoryOptionGroup> categoryOptionGroupsFromCsv( CsvReader reader )
    throws IOException
{
    List<CategoryOptionGroup> list = new ArrayList<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            CategoryOptionGroup object = new CategoryOptionGroup();
            setIdentifiableObject( object, values );
            object.setShortName( getSafe( values, 3, object.getName(), 50 ) );
            list.add( object );
        }
    }

    return list;
}
 
Example 5
Source File: DefaultDataValueSetService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
@Transactional
public ImportSummary saveDataValueSetCsv( InputStream in, ImportOptions importOptions, JobConfiguration id )
{
    try
    {
        in = StreamUtils.wrapAndCheckCompressionFormat( in );
        CsvReader csvReader = CsvUtils.getReader( in );

        if ( importOptions == null || importOptions.isFirstRowIsHeader() )
        {
            csvReader.readRecord(); // Ignore the first row
        }

        DataValueSet dataValueSet = new StreamingCsvDataValueSet( csvReader );
        return saveDataValueSet( importOptions, id, dataValueSet );
    }
    catch ( Exception ex )
    {
        log.error( DebugUtils.getStackTrace( ex ) );
        notifier.clear( id ).notify( id, ERROR, "Process failed: " + ex.getMessage(), true );
        return new ImportSummary( ImportStatus.ERROR, "The import process failed: " + ex.getMessage() );
    }
}
 
Example 6
Source File: ReadDocument.java    From Benchmark with GNU General Public License v3.0 5 votes vote down vote up
public static AarhusTrafficObservation getStreamData(String dataFile) throws NumberFormatException, IOException {
	CsvReader streamData = new CsvReader(String.valueOf(dataFile));
	streamData.readHeaders();
	// Reads csv document for traffic metadata
	try {
		CsvReader metaData = new CsvReader("dataset/MetaData/trafficMetaData.csv");
		metaData.readHeaders();
		AarhusTrafficObservation data = new AarhusTrafficObservation();
		while (streamData.readRecord()) {

			while (metaData.readRecord()) {
				if (streamData.get("REPORT_ID").equals(metaData.get("REPORT_ID"))) {
					data = new AarhusTrafficObservation(Double.parseDouble(streamData.get("REPORT_ID")),
							Double.parseDouble(streamData.get("avgSpeed")), Double.parseDouble(streamData
									.get("vehicleCount")), Double.parseDouble(streamData.get("avgMeasuredTime")),
							0, 0, metaData.get("POINT_1_STREET"), metaData.get("POINT_1_CITY"),
							Double.parseDouble(metaData.get("POINT_1_LAT")), Double.parseDouble(metaData
									.get("POINT_1_LNG")), metaData.get("POINT_2_STREET"),
							metaData.get("POINT_2_CITY"), Double.parseDouble(metaData.get("POINT_2_LAT")),
							Double.parseDouble(metaData.get("POINT_2_LNG")), metaData.get("POINT_1_COUNTRY"),
							metaData.get("POINT_2_COUNTRY"), metaData.get("TIMESTAMP"));
					Double distance = Double.parseDouble(metaData.get("DISTANCE_IN_METERS"));
					data.setEstimatedTime(distance / data.getAverageSpeed());
					data.setCongestionLevel(data.getVehicle_count() / distance);
				}
			}

		}
		streamData.close();
		metaData.close();
		return data;
	} catch (Exception e) {

		e.printStackTrace();
	}
	return null;
}
 
Example 7
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private List<IndicatorGroup> indicatorGroupMembersFromCsv( CsvReader reader )
    throws IOException
{
    CachingMap<String, IndicatorGroup> uidMap = new CachingMap<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            String groupUid = values[0];
            String memberUid = values[1];

            IndicatorGroup persistedGroup = indicatorGroupService.getIndicatorGroupByUid( groupUid );

            if ( persistedGroup != null )
            {

                IndicatorGroup group = uidMap.get( groupUid, () -> {
                    IndicatorGroup nonPersistedGroup = new IndicatorGroup();
                    nonPersistedGroup.setUid( persistedGroup.getUid() );
                    nonPersistedGroup.setName( persistedGroup.getName() );
                    return nonPersistedGroup;
                } );

                Indicator member = new Indicator();
                member.setUid( memberUid );
                group.addIndicator( member );
            }
        }
    }
    return new ArrayList<>( uidMap.values() );
}
 
Example 8
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private List<DataElementGroup> dataElementGroupMembersFromCsv( CsvReader reader )
    throws IOException
{
    CachingMap<String, DataElementGroup> uidMap = new CachingMap<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            String groupUid = values[0];
            String memberUid = values[1];

            DataElementGroup persistedGroup = dataElementGroupService.getDataElementGroupByUid( groupUid );

            if ( persistedGroup != null )
            {
                DataElementGroup group = uidMap.get( groupUid, () -> {
                    DataElementGroup nonPersistedGroup = new DataElementGroup();
                    nonPersistedGroup.setUid( persistedGroup.getUid() );
                    nonPersistedGroup.setName( persistedGroup.getName() );
                    return nonPersistedGroup;
                } );

                DataElement member = new DataElement();
                member.setUid( memberUid );
                group.addDataElement( member );
            }
        }
    }

    return new ArrayList<>( uidMap.values() );
}
 
Example 9
Source File: RulesDbClientImpl.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public Collection<RuleId> listRules(String workflowId, RuleAttributeSet attributeSet) {
    Objects.requireNonNull(workflowId);

    String path = RULES_PREFIX + workflowId + "/data.csv";
    Map<String, String> query = ImmutableMap.of("start", "0",
                                                "count", "-1",
                                                "headers", "true",
                                                "cols", "algoType,contingencyId,indexType");

    try {
        CsvReader csvReader = new CsvReader(httpClient.getHttpRequest(new HistoDbUrl(config, path, query)), ',', StandardCharsets.UTF_8);
        try {
            csvReader.setSafetySwitch(false);
            csvReader.readHeaders();

            List<RuleId> ruleIds = new ArrayList<>();

            while (csvReader.readRecord()) {
                String[] values = csvReader.getValues();
                ruleIds.add(new RuleId(RuleAttributeSet.valueOf(values[0]),
                            new SecurityIndexId(values[1], SecurityIndexType.fromLabel(values[2]))));
            }

            return ruleIds;
        } finally {
            csvReader.close();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 10
Source File: CSPARQLAarhusTrafficStream.java    From Benchmark with GNU General Public License v3.0 5 votes vote down vote up
public CSPARQLAarhusTrafficStream(String uri, String txtFile, EventDeclaration ed, Date start, Date end)
		throws IOException {
	super(uri);
	logger.info("IRI: " + this.getIRI().split("#")[1] + ed.getInternalQos());
	this.startDate = start;
	this.endDate = end;
	messageCnt = 0;
	byteCnt = 0;
	this.txtFile = txtFile;
	this.ed = ed;
	streamData = new CsvReader(String.valueOf(txtFile));
	streamData.setTrimWhitespace(false);
	streamData.setDelimiter(',');
	streamData.readHeaders();
	metaData = new CsvReader("dataset/MetaData/trafficMetaData.csv");
	metaData.readHeaders();
	streamData.readRecord();
	while (metaData.readRecord()) {
		if (streamData.get("REPORT_ID").equals(metaData.get("REPORT_ID"))) {
			// p1Street = metaData.get("POINT_1_STREET");
			// p1City = metaData.get("POINT_1_CITY");
			// p1Lat = metaData.get("POINT_1_LAT");
			// p1Lon = metaData.get("POINT_1_LNG");
			// p1Country = metaData.get("POINT_2_COUNTRY");
			// p2Street = metaData.get("POINT_2_STREET");
			// p2City = metaData.get("POINT_2_CITY");
			// p2Lat = metaData.get("POINT_2_LAT");
			// p2Lon = metaData.get("POINT_2_LNG");
			// p2Country = metaData.get("POINT_2_COUNTRY");
			distance = metaData.get("DISTANCE_IN_METERS");
			if (ed instanceof TrafficReportService)
				((TrafficReportService) ed).setDistance(Integer.parseInt(distance));

			// timestamp = metaData.get("TIMESTAMP");
			// id = metaData.get("extID");
			metaData.close();
			break;
		}
	}
}
 
Example 11
Source File: Utils.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
public static List readHistoDataFromCsv(Path inFilePath, char delimiter) throws NumberFormatException, IOException {
    ArrayList retList = new ArrayList();

    int numberOfCSVLines = countLines(inFilePath);
    numberOfCSVLines = numberOfCSVLines - 1; //take into account header

    CsvReader cvsReader = new CsvReader(inFilePath.toString());
    cvsReader.setDelimiter(delimiter);
    int i = 0;
    cvsReader.readHeaders();


    String[] headers = cvsReader.getHeaders();
    retList.add(headers);

    double[][] mlDouble = new double[numberOfCSVLines][headers.length];

    while (cvsReader.readRecord()) {
        String[] rows = cvsReader.getValues();
        int j = 0;
        for (String col : rows) {
            if (j > 1) {

                mlDouble[i][j - 2] = (col != null) ? Double.parseDouble(col) : 0.0;
            }
            j++;
        }
        i++;
        if (i >= numberOfCSVLines) {
            break;
        }

    }
    retList.add(mlDouble);
    return retList;
}
 
Example 12
Source File: CQELSAarhusTrafficStream.java    From Benchmark with GNU General Public License v3.0 5 votes vote down vote up
public CQELSAarhusTrafficStream(ExecContext context, String uri, String txtFile, EventDeclaration ed, Date start,
		Date end) throws IOException {
	super(context, uri);
	this.startDate = start;
	this.endDate = end;
	messageCnt = 0;
	byteCnt = 0;
	this.txtFile = txtFile;
	this.ed = ed;
	streamData = new CsvReader(String.valueOf(txtFile));
	streamData.setTrimWhitespace(false);
	streamData.setDelimiter(',');
	streamData.readHeaders();
	metaData = new CsvReader("dataset/MetaData/trafficMetaData.csv");
	metaData.readHeaders();
	streamData.readRecord();
	while (metaData.readRecord()) {
		if (streamData.get("REPORT_ID").equals(metaData.get("REPORT_ID"))) {
			// p1Street = metaData.get("POINT_1_STREET");
			// p1City = metaData.get("POINT_1_CITY");
			// p1Lat = metaData.get("POINT_1_LAT");
			// p1Lon = metaData.get("POINT_1_LNG");
			// p1Country = metaData.get("POINT_2_COUNTRY");
			// p2Street = metaData.get("POINT_2_STREET");
			// p2City = metaData.get("POINT_2_CITY");
			// p2Lat = metaData.get("POINT_2_LAT");
			// p2Lon = metaData.get("POINT_2_LNG");
			// p2Country = metaData.get("POINT_2_COUNTRY");
			distance = metaData.get("DISTANCE_IN_METERS");
			// timestamp = metaData.get("TIMESTAMP");
			// id = metaData.get("extID");
			metaData.close();
			break;
		}
	}
}
 
Example 13
Source File: CQELSAarhusTrafficStream.java    From Benchmark with GNU General Public License v3.0 4 votes vote down vote up
public CQELSAarhusTrafficStream(ExecContext context, String uri, String txtFile, EventDeclaration ed)
		throws IOException {
	super(context, uri);
	String fileName = "";
	if (this.getURI().split("#").length > 1)
		fileName = this.getURI().split("#")[1];
	else
		fileName = this.getURI();
	// logFile = new File("resultlog/" + fileName + ".csv");
	// fw = new FileWriter(logFile);
	// cw = new CsvWriter(fw, ',');
	// cw.write("time");
	// cw.write("message_cnt");
	// cw.write("byte_cnt");
	// cw.endRecord();
	// cw.flush();
	messageCnt = 0;
	byteCnt = 0;
	this.txtFile = txtFile;
	this.ed = ed;
	// time1 = time.getTime();
	streamData = new CsvReader(String.valueOf(txtFile));
	streamData.setTrimWhitespace(false);
	streamData.setDelimiter(',');
	streamData.readHeaders();
	// streamData.skipRecord();
	metaData = new CsvReader("dataset/MetaData/trafficMetaData.csv");
	metaData.readHeaders();
	streamData.readRecord();
	while (metaData.readRecord()) {
		if (streamData.get("REPORT_ID").equals(metaData.get("REPORT_ID"))) {
			// p1Street = metaData.get("POINT_1_STREET");
			// p1City = metaData.get("POINT_1_CITY");
			// p1Lat = metaData.get("POINT_1_LAT");
			// p1Lon = metaData.get("POINT_1_LNG");
			// p1Country = metaData.get("POINT_2_COUNTRY");
			// p2Street = metaData.get("POINT_2_STREET");
			// p2City = metaData.get("POINT_2_CITY");
			// p2Lat = metaData.get("POINT_2_LAT");
			// p2Lon = metaData.get("POINT_2_LNG");
			// p2Country = metaData.get("POINT_2_COUNTRY");
			distance = metaData.get("DISTANCE_IN_METERS");
			// timestamp = metaData.get("TIMESTAMP");
			// id = metaData.get("extID");
			// stream(n(RDFFileManager.defaultPrefix + streamData.get("REPORT_ID")),
			// n(RDFFileManager.ctPrefix + "hasETA"), n(data.getEstimatedTime() + ""));
			// System.out.println("metadata: " + metaData.toString());
			metaData.close();
			break;
		}
	}
}
 
Example 14
Source File: FileOperation.java    From Benchmark with GNU General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
	// File trafficRawDirectory = new File("streams");
	// File[] trafficRawFiles = trafficRawDirectory.listFiles();
	// for (File tr : trafficRawFiles) {
	// System.out.println("renaming: " + tr.toPath());
	// File newTr;
	// if (tr.getName().contains("traffic")) {
	// newTr = new File("streams" + File.separator + "AarhusT"
	// + tr.getName().substring(1, tr.getName().length()));
	// tr.renameTo(newTr);
	// } else if (tr.getName().contains("pollution")) {
	// newTr = new File("streams" + File.separator + "AarhusP"
	// + tr.getName().substring(1, tr.getName().length()));
	// tr.renameTo(newTr);
	// }
	//
	// // Files.copy(tr.toPath(), newTr.toPath());
	// }
	CsvReader streamData = new CsvReader("dataset/aarhus_parking.csv");
	streamData.setTrimWhitespace(false);
	streamData.setDelimiter(',');
	streamData.readHeaders();

	List<String> streamNames = new ArrayList<String>();
	streamNames.add("NORREPORT");
	streamNames.add("BUSGADEHUSET");
	streamNames.add("BRUUNS");
	streamNames.add("SKOLEBAKKEN");
	streamNames.add("SCANDCENTER");
	streamNames.add("SALLING");
	streamNames.add("MAGASIN");
	streamNames.add("KALKVAERKSVEJ");

	Map<String, CsvWriter> writerMap = new HashMap<String, CsvWriter>();
	for (String s : streamNames)
		try {
			// use FileWriter constructor that specifies open for appending
			String outputFile = "dataset/AarhusParkingData-" + s + ".stream";
			boolean alreadyExists = new File(outputFile).exists();
			CsvWriter csvOutput = new CsvWriter(new FileWriter(outputFile, true), ',');

			// if the file didn't already exist then we need to write out the header line
			if (!alreadyExists) {
				csvOutput.write("vehiclecount");
				csvOutput.write("updatetime");
				csvOutput.write("_id");
				csvOutput.write("totalspaces");
				csvOutput.write("garagecode");
				csvOutput.write("streamtime");
				csvOutput.endRecord();
			}
			// }
			writerMap.put(s, csvOutput);
			// csvOutput.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	while (streamData.readRecord()) {
		String code = streamData.get("garagecode");
		writerMap.get(code).write(streamData.get("vehiclecount"));
		writerMap.get(code).write(streamData.get("updatetime"));
		writerMap.get(code).write(streamData.get("_id"));
		writerMap.get(code).write(streamData.get("totalspaces"));
		writerMap.get(code).write(streamData.get("garagecode"));
		writerMap.get(code).write(streamData.get("streamtime"));
		writerMap.get(code).endRecord();
	}
	for (CsvWriter csv : writerMap.values())
		csv.close();
}
 
Example 15
Source File: CSPARQLAarhusTrafficStream.java    From Benchmark with GNU General Public License v3.0 4 votes vote down vote up
public CSPARQLAarhusTrafficStream(String uri, String txtFile, EventDeclaration ed) throws IOException {
	super(uri);
	String fileName = "";
	if (this.getIRI().split("#").length > 1)
		fileName = this.getIRI().split("#")[1];
	else
		fileName = this.getIRI();
	// logFile = new File("resultlog/" + fileName + ".csv");
	// fw = new FileWriter(logFile);
	// cw = new CsvWriter(fw, ',');
	// cw.write("time");
	// cw.write("message_cnt");
	// cw.write("byte_cnt");
	// cw.endRecord();heh
	// cw.flush();
	messageCnt = 0;
	byteCnt = 0;
	this.txtFile = txtFile;
	this.ed = ed;
	// time1 = time.getTime();
	streamData = new CsvReader(String.valueOf(txtFile));
	streamData.setTrimWhitespace(false);
	streamData.setDelimiter(',');
	streamData.readHeaders();
	// streamData.skipRecord();
	metaData = new CsvReader("dataset/MetaData/trafficMetaData.csv");
	metaData.readHeaders();
	streamData.readRecord();
	while (metaData.readRecord()) {
		if (streamData.get("REPORT_ID").equals(metaData.get("REPORT_ID"))) {
			// p1Street = metaData.get("POINT_1_STREET");
			// p1City = metaData.get("POINT_1_CITY");
			// p1Lat = metaData.get("POINT_1_LAT");
			// p1Lon = metaData.get("POINT_1_LNG");
			// p1Country = metaData.get("POINT_2_COUNTRY");
			// p2Street = metaData.get("POINT_2_STREET");
			// p2City = metaData.get("POINT_2_CITY");
			// p2Lat = metaData.get("POINT_2_LAT");
			// p2Lon = metaData.get("POINT_2_LNG");
			// p2Country = metaData.get("POINT_2_COUNTRY");
			distance = metaData.get("DISTANCE_IN_METERS");
			if (ed instanceof TrafficReportService)
				((TrafficReportService) ed).setDistance(Integer.parseInt(distance));

			// timestamp = metaData.get("TIMESTAMP");
			// id = metaData.get("extID");
			// stream(n(RDFFileManager.defaultPrefix + streamData.get("REPORT_ID")),
			// n(RDFFileManager.ctPrefix + "hasETA"), n(data.getEstimatedTime() + ""));
			// System.out.println("metadata: " + metaData.toString());
			metaData.close();
			break;
		}
	}
}
 
Example 16
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private List<OrganisationUnit> orgUnitsFromCsv( CsvReader reader )
    throws IOException
{
    List<OrganisationUnit> list = new ArrayList<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            OrganisationUnit object = new OrganisationUnit();
            setIdentifiableObject( object, values );
            String parentUid = getSafe( values, 3, null, 230 ); // Could be UID, code, name
            object.setShortName( getSafe( values, 4, object.getName(), 50 ) );
            object.setDescription( getSafe( values, 5, null, null ) );
            object.setOpeningDate( getMediumDate( getSafe( values, 6, "1970-01-01", null ) ) );
            object.setClosedDate( getMediumDate( getSafe( values, 7, null, null ) ) );
            object.setComment( getSafe( values, 8, null, null ) );
            setGeometry( object, FeatureType.valueOf( getSafe( values, 9, "NONE", 50 ) ),
                getSafe( values, 10, null, null ) );
            object.setUrl( getSafe( values, 11, null, 255 ) );
            object.setContactPerson( getSafe( values, 12, null, 255 ) );
            object.setAddress( getSafe( values, 13, null, 255 ) );
            object.setEmail( getSafe( values, 14, null, 150 ) );
            object.setPhoneNumber( getSafe( values, 15, null, 150 ) );
            object.setAutoFields();

            if ( parentUid != null )
            {
                OrganisationUnit parent = new OrganisationUnit();
                parent.setUid( parentUid );
                object.setParent( parent );
            }

            list.add( object );
        }
    }

    return list;
}
 
Example 17
Source File: Utils.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public static void generateFakeInputFile(String folderName, long startTimeInSec, long finishTimeInSec, String generatingFile) throws IOException
{
	File folder = new File(folderName);

	if ( folder.exists() )
	{
		File[] files = folder.listFiles();

           Arrays.sort(files, new FileSpecialComparator(true, false));

           if(files.length == 0) {
               return;
           }

           int index = files.length - 1;
		for ( ; index >= 0; --index )
		{
			long startInterval = determineStartInterval(files[index].getName());

               if(startInterval < startTimeInSec * 1000000) {
                   break;
               }
		}

           if(index < 0) {
               index = 0;
           }

		BufferedWriter writer = new BufferedWriter(new FileWriter(generatingFile));


		boolean firstRecordWritten = false;

		for ( int i = index; i < files.length; ++i )
		{
			if ( isDebugged )
			{
				logger.debug(files[i].getName());
			}

			CsvReader reader = new CsvReader(folderName + File.separator + files[i].getName());
			reader.setSafetySwitch(false);

			reader.readRecord();

			if ( i == index )
			{
				writer.write(reader.getRawRecord());
				writer.newLine();
			}


			if ( !firstRecordWritten || i == files.length - 1)
			{
				String lastRecord = "";

				while ( reader.readRecord() )
				{
					long timestamp = Long.parseLong(reader.getValues()[0]);

					lastRecord = reader.getRawRecord();

					if ( !firstRecordWritten )
					{
                           if(timestamp >= startTimeInSec * 1000000 && (timestamp < finishTimeInSec * 1000000 || finishTimeInSec == Long.MAX_VALUE)) {
                               writer.write(reader.getRawRecord());
                               writer.newLine();
                               firstRecordWritten = true;
                           } else if(timestamp >= startTimeInSec * 1000000) {
                               break;
                           }
                       }
				}

				writer.write(lastRecord);
			}

			reader.close();
		}

		writer.close();

       } else {
           throw new EPSCommonException("Could not find [" + folderName + "] folder with files");
       }
}
 
Example 18
Source File: CSVMappingGenerator.java    From GeoTriples with Apache License 2.0 4 votes vote down vote up
public void run() throws IOException {
	CsvReader reader = new CsvReader(new FileInputStream(pathToShapefile), Charset.defaultCharset());
	reader.setDelimiter(separator);
	reader.setSafetySwitch(false);

	reader.readHeaders();
	// Iterate the rows

	Path p = Paths.get(pathToShapefile);
	String tmp = p.getFileName().toString();
	String typeName = tmp.substring(0, tmp.lastIndexOf('.'));
	triplesMaps.put(typeName, "");
	triplesMaps.put(typeName, triplesMaps.get(typeName) + printTriplesMap(typeName));
	triplesMaps.put(typeName, triplesMaps.get(typeName) + printLogicalSource(typeName));
	triplesMaps.put(typeName, triplesMaps.get(typeName) + printSubjectMap(baseURI, typeName));

	boolean hasgeometry = false;
	String typeNameGeo = typeName + "_Geometry";
	WKTReader wktReader = new WKTReader();
	String geometry_identifier = null;
	boolean has_record = reader.readRecord();
	for (String header : reader.getHeaders()) {
		String identifier = header;
		if (identifier.equalsIgnoreCase("the_geom")) {
			geometry_identifier = identifier;
			hasgeometry = true;
			continue;
		}
		if (has_record) {
			try {
				if (wktReader.read(reader.get(identifier)) != null) {
					hasgeometry = true;
					geometry_identifier = identifier;
					continue;
				}
			} catch (ParseException e1) {
				System.err.println(reader.get(identifier));
			}
		}

		String datatype = TranslateDataTypeToXSD("String");
		triplesMaps.put(typeName,
				triplesMaps.get(typeName) + printPredicateObjectMap(identifier, identifier, datatype, typeName));

	}

	if (hasgeometry) {
		triplesMaps
				.put(typeName,
						triplesMaps.get(typeName) + printPredicateObjectMap(true, "hasGeometry",
								baseURI + (baseURI.endsWith("/") ? "" : "/") + typeName
										+ "/Geometry/{GeoTriplesID}",
								null, null, "ogc", null, typeName, true, false));
		triplesMaps.put(typeNameGeo, "");
		triplesMaps.put(typeNameGeo, triplesMaps.get(typeNameGeo) + printTriplesMap(typeNameGeo));
		triplesMaps.put(typeNameGeo, triplesMaps.get(typeNameGeo) + printLogicalSource(typeName));
		triplesMaps.put(typeNameGeo,
				triplesMaps.get(typeNameGeo) + printSubjectMap(baseURI, typeName, "ogc", true));
		triplesMaps.put(typeNameGeo,
				triplesMaps.get(typeNameGeo) + printGEOPredicateObjectMaps(geometry_identifier));
	}
	printmapping();
	printontology();
}
 
Example 19
Source File: Utils.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public static void generateInputFile(String folderName, long startTimeInSec, long finishTimeInSec, String generatingFile) throws IOException
{
	File folder = new File(folderName);

	if ( folder.exists() )
	{
		File[] files = folder.listFiles();

           Arrays.sort(files, new FileSpecialComparator(true, false));

           if(files.length == 0) {
               return;
           }

           int index = files.length - 1;
		for ( ; index >= 0; --index )
		{
			long startInterval = determineStartInterval(files[index].getName());

               if(startInterval < startTimeInSec * 1000000) {
                   break;
               }
		}

           if(index < 0) {
               index = 0;
           }



		BufferedWriter writer = new BufferedWriter(new FileWriter(generatingFile));

		for ( int i = index; i < files.length; ++i )
		{
			if ( isDebugged )
			{
				logger.debug(files[i].getName());
			}

			CsvReader reader = new CsvReader(folderName + File.separator + files[i].getName());
			reader.setSafetySwitch(false);

			reader.readRecord();

			if ( i == index )
			{
				writer.write(reader.getRawRecord());
				writer.newLine();
			}

			while ( reader.readRecord() )
			{
				long timestamp = Long.parseLong(reader.getValues()[0]);

                   if(timestamp >= startTimeInSec * 1000000 && (timestamp < finishTimeInSec * 1000000 || finishTimeInSec == Long.MAX_VALUE)) {
                       writer.write(reader.getRawRecord());
                       writer.newLine();
                   } else if(timestamp >= startTimeInSec * 1000000) {
                       break;
                   }
               }

			reader.close();
		}

		writer.close();

       } else {
           throw new EPSCommonException("Could not find [" + folderName + "] folder with files");
       }
}
 
Example 20
Source File: Utils.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public static long determineStartSecFromParsedFile(String parsedFileName) throws IOException
{
	CsvReader parsedReader = new CsvReader(parsedFileName);

	parsedReader.setSafetySwitch(false);

	parsedReader.readHeaders();

	parsedReader.readRecord();

	long startSec = Long.parseLong(parsedReader.getValues()[0]);

	parsedReader.close();

	return startSec;
}