CsvFile implements CsvSourceParser, CsvSource { @Override public Object[] parseRecords(final int offset, final int numRecords, final String rowName) throws IOException { AttributeDescriptor attributeDescriptor = simpleFeatureType.getDescriptor(rowName); int expectedTokenCount = simpleFeatureType.getAttributeCount(); expectedTokenCount += hasFeatureId ? 1 : 0; int rowIndex = simpleFeatureType.getAttributeDescriptors().indexOf(attributeDescriptor); int tokenIndex = rowIndex + (hasFeatureId ? 1 : 0); List<Object> values = new ArrayList<>(numRecords); skipToLine(offset); String line; long featureCount = offset; while ((numRecords == -1 || featureCount < offset + numRecords) && (line = stream.readLine()) != null) { String[] tokens = getTokens(line); if (tokens == null) { break; } if (tokens.length != expectedTokenCount) { continue; } featureCount++; String token = tokens[tokenIndex]; try { Object value = null; if (!VectorDataNodeIO.NULL_TEXT.equals(token)) { value = converters[rowIndex].parse(token); } values.add(value); } catch (ConversionException e) { BeamLogManager.getSystemLogger().warning(String.format("Problem in '%s': %s", csv.getPath(), e.getMessage())); } bytePositionForOffset.put(featureCount, stream.getStreamPosition()); } return values.toArray(); } private  CsvFile(String csv); private  CsvFile(File csv, CoordinateReferenceSystem crs); static CsvSourceParser createCsvSourceParser(String csv); @Override void checkReadingFirstRecord(); @Override Object[] parseRecords(final int offset, final int numRecords, final String rowName); @Override void parseRecords(int offset, int numRecords); @Override CsvSource parseMetadata(); @Override void close(); @Override int getRecordCount(); @Override SimpleFeature[] getSimpleFeatures(); @Override SimpleFeatureType getFeatureType(); @Override Map<String, String> getProperties(); static final String DEFAULT_HEADER_NAME; }
@Test public void testParseRecords() throws Exception { final String simpleFormatExample = getClass().getResource("reader/simple_format_example.txt").getFile(); final CsvSourceParser parser = CsvFile.createCsvSourceParser(simpleFormatExample); final CsvSource csvSource = parser.parseMetadata(); parser.parseRecords(0, 3); SimpleFeature[] features = csvSource.getSimpleFeatures(); assertEquals(4, csvSource.getRecordCount()); assertEquals(3, features.length); SimpleFeature feature = features[0]; assertEquals(7, feature.getAttributeCount()); assertEquals("" + 26, feature.getID()); assertEquals(String.class, feature.getAttribute(0).getClass()); assertEquals(Float.class, feature.getAttribute(1).getClass()); assertEquals(Float.class, feature.getAttribute(2).getClass()); assertEquals(ProductData.UTC.class, feature.getAttribute(3).getClass()); assertEquals(Float.class, feature.getAttribute(4).getClass()); assertEquals(Float.class, feature.getAttribute(5).getClass()); assertEquals(ProductData.UTC.class, feature.getAttribute(6).getClass()); feature = features[1]; assertEquals(7, feature.getAttributeCount()); assertEquals("" + 28, feature.getID()); assertEquals(String.class, feature.getAttribute(0).getClass()); assertEquals(Float.class, feature.getAttribute(1).getClass()); assertEquals(Float.class, feature.getAttribute(2).getClass()); assertEquals(ProductData.UTC.class, feature.getAttribute(3).getClass()); assertEquals(Float.class, feature.getAttribute(4).getClass()); assertEquals(Float.class, feature.getAttribute(5).getClass()); assertEquals(null, feature.getAttribute(6)); feature = features[2]; assertEquals(7, feature.getAttributeCount()); assertEquals("" + 371, feature.getID()); assertEquals(String.class, feature.getAttribute(0).getClass()); assertEquals(Float.class, feature.getAttribute(1).getClass()); assertEquals(Float.class, feature.getAttribute(2).getClass()); assertEquals(null, feature.getAttribute(3)); assertEquals(Float.class, feature.getAttribute(4).getClass()); assertEquals(Float.class, feature.getAttribute(5).getClass()); assertEquals(ProductData.UTC.class, feature.getAttribute(6).getClass()); assertEquals("AMRU1", features[0].getAttribute(0)); assertEquals("AMRU1", features[1].getAttribute(0)); assertEquals("AMRU2", features[2].getAttribute(0)); assertEquals(new GeoPos(30.0f, 50.0f), new GeoPos((Float)features[0].getAttribute(1), (Float)features[0].getAttribute(2))); assertEquals(new GeoPos(30.0f, 50.0f), new GeoPos((Float)features[1].getAttribute(1), (Float)features[1].getAttribute(2))); assertEquals(new GeoPos(40.0f, 120.0f), new GeoPos((Float)features[2].getAttribute(1), (Float)features[2].getAttribute(2))); assertEquals(ProductData.UTC.parse("2010-06-01 12:45:00", "yyyy-MM-dd HH:mm:ss").getAsDate().getTime(), ((ProductData.UTC)features[0].getAttribute(3)).getAsDate().getTime()); assertEquals(ProductData.UTC.parse("2010-06-01 12:48:00", "yyyy-MM-dd HH:mm:ss").getAsDate().getTime(), ((ProductData.UTC)features[1].getAttribute(3)).getAsDate().getTime()); assertEquals(null, features[2].getAttribute(3)); assertEquals(Float.NaN, features[0].getAttribute(4)); assertEquals(18.3f, features[1].getAttribute(4)); assertEquals(10.6f, features[2].getAttribute(5)); assertEquals(ProductData.UTC.parse("2011-06-01 10:45:00", "yyyy-MM-dd HH:mm:ss").getAsDate().getTime(), ((ProductData.UTC)features[0].getAttribute(6)).getAsDate().getTime()); }