CSVLatLonStrategy extends CSVStrategy { @Override protected SimpleFeatureType buildFeatureType() { String[] headers; Map<String, Class<?>> typesFromData; CsvReader csvReader = null; try { csvReader = csvFileState.openCSVReader(); headers = csvReader.getHeaders(); typesFromData = CSVStrategy.findMostSpecificTypesFromData(csvReader, headers); } catch (IOException e) { throw new RuntimeException(e); } finally { if (csvReader != null) { csvReader.close(); } } SimpleFeatureTypeBuilder builder = CSVStrategy.createBuilder(csvFileState, headers, typesFromData); boolean validLat = false; boolean validLon = false; String latSpelling = null; String lonSpelling = null; for (String col : headers) { Class<?> type = typesFromData.get(col); if (isLatitude(col)) { latSpelling = col; if (CSVStrategy.isNumeric(type)) { validLat = true; } } else if (isLongitude(col)) { lonSpelling = col; if (CSVStrategy.isNumeric(type)) { validLon = true; } } } if (validLat && validLon) { List<String> csvHeaders = Arrays.asList(headers); int index = csvHeaders.indexOf(latSpelling); AttributeTypeBuilder builder2 = new AttributeTypeBuilder(); builder2.setCRS(DefaultGeographicCRS.WGS84); builder2.binding(Point.class); AttributeDescriptor descriptor = builder2.buildDescriptor(GEOMETRY_COLUMN); builder.add(index, descriptor); builder.remove(latSpelling); builder.remove(lonSpelling); } return builder.buildFeatureType(); } CSVLatLonStrategy(CSVFileState csvFileState); @Override void createSchema(SimpleFeatureType featureType); @Override String[] encode(SimpleFeature feature); @Override SimpleFeature decode(String recordId, String[] csvRecord);  }
@Test public void testBuildFeatureType() { String input = CSVTestStrategySupport.buildInputString("lat,lon,quux,morx\n"); CSVFileState fileState = new CSVFileState(input, "foo"); CSVLatLonStrategy strategy = new CSVLatLonStrategy(fileState); SimpleFeatureType featureType = strategy.getFeatureType(); assertEquals("Invalid attribute count", 3, featureType.getAttributeCount()); assertEquals("Invalid featuretype name", "foo", featureType.getName().getLocalPart()); assertEquals("Invalid name", "foo", featureType.getTypeName()); List<AttributeDescriptor> attrs = featureType.getAttributeDescriptors(); assertEquals("Invalid number of attributes", 3, attrs.size()); List<String> attrNames = new ArrayList<String>(2); for (AttributeDescriptor attr : attrs) { if (!(attr instanceof GeometryDescriptor)) { attrNames.add(attr.getName().getLocalPart()); } } Collections.sort(attrNames); assertEquals("Invalid property descriptor", "morx", attrNames.get(0)); assertEquals("Invalid property descriptor", "quux", attrNames.get(1)); GeometryDescriptor geometryDescriptor = featureType.getGeometryDescriptor(); GeometryType geometryType = geometryDescriptor.getType(); assertEquals("Invalid geometry name", "location", geometryType.getName().getLocalPart()); }