@Test public void testBuildFeatureType() { String input = buildInputString("lat,lon,quux,morx\n"); CSVFileState fileState = new CSVFileState(input, "foo", WGS84); 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()); CoordinateReferenceSystem crs = featureType.getCoordinateReferenceSystem(); assertEquals("Unknown crs", WGS84, crs); }
private SimpleFeatureType buildFeatureType() { SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setName(csvFileState.getTypeName()); builder.setCRS(csvFileState.getCrs()); CsvReader csvReader = null; Map<String, Class<?>> typesFromData = null; try { csvReader = csvFileState.openCSVReader(); headers = csvReader.getHeaders(); typesFromData = findMostSpecificTypesFromData(csvReader, headers); } catch (IOException e) { throw new RuntimeException("Failure reading csv file", e); } finally { if (csvReader != null) { csvReader.close(); } } if (csvFileState.getNamespace() != null) { builder.setNamespaceURI(csvFileState.getNamespace()); } boolean validLat = false; boolean validLon = false; boolean seenLat = false; boolean seenLon = false; String latSpelling = null; String lonSpelling = null; for (String col : headers) { Class<?> type = typesFromData.get(col); if ("lat".equalsIgnoreCase(col)) { seenLat = true; latSpelling = col; if (type == Double.class || type == Integer.class) { validLat = true; } } else if ("lon".equalsIgnoreCase(col) || "lng".equalsIgnoreCase(col)) { seenLon = true; lonSpelling = col; if (type == Double.class || type == Integer.class) { validLon = true; } } else { builder.add(col, type); } } if (validLat && validLon) { builder.add(GEOMETRY_COLUMN, Point.class); } else { if (seenLat) { builder.add(latSpelling, typesFromData.get("lat")); } if (seenLon) { builder.add(lonSpelling, typesFromData.get("lon")); } } return builder.buildFeatureType(); }
CSVLatLonStrategy implements CSVStrategy { private SimpleFeatureType buildFeatureType() { SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setName(csvFileState.getTypeName()); builder.setCRS(csvFileState.getCrs()); CsvReader csvReader = null; Map<String, Class<?>> typesFromData = null; try { csvReader = csvFileState.openCSVReader(); headers = csvReader.getHeaders(); typesFromData = findMostSpecificTypesFromData(csvReader, headers); } catch (IOException e) { throw new RuntimeException("Failure reading csv file", e); } finally { if (csvReader != null) { csvReader.close(); } } if (csvFileState.getNamespace() != null) { builder.setNamespaceURI(csvFileState.getNamespace()); } boolean validLat = false; boolean validLon = false; boolean seenLat = false; boolean seenLon = false; String latSpelling = null; String lonSpelling = null; for (String col : headers) { Class<?> type = typesFromData.get(col); if ("lat".equalsIgnoreCase(col)) { seenLat = true; latSpelling = col; if (type == Double.class || type == Integer.class) { validLat = true; } } else if ("lon".equalsIgnoreCase(col) || "lng".equalsIgnoreCase(col)) { seenLon = true; lonSpelling = col; if (type == Double.class || type == Integer.class) { validLon = true; } } else { builder.add(col, type); } } if (validLat && validLon) { builder.add(GEOMETRY_COLUMN, Point.class); } else { if (seenLat) { builder.add(latSpelling, typesFromData.get("lat")); } if (seenLon) { builder.add(lonSpelling, typesFromData.get("lon")); } } return builder.buildFeatureType(); } }
CSVLatLonStrategy implements CSVStrategy { private SimpleFeatureType buildFeatureType() { SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setName(csvFileState.getTypeName()); builder.setCRS(csvFileState.getCrs()); CsvReader csvReader = null; Map<String, Class<?>> typesFromData = null; try { csvReader = csvFileState.openCSVReader(); headers = csvReader.getHeaders(); typesFromData = findMostSpecificTypesFromData(csvReader, headers); } catch (IOException e) { throw new RuntimeException("Failure reading csv file", e); } finally { if (csvReader != null) { csvReader.close(); } } if (csvFileState.getNamespace() != null) { builder.setNamespaceURI(csvFileState.getNamespace()); } boolean validLat = false; boolean validLon = false; boolean seenLat = false; boolean seenLon = false; String latSpelling = null; String lonSpelling = null; for (String col : headers) { Class<?> type = typesFromData.get(col); if ("lat".equalsIgnoreCase(col)) { seenLat = true; latSpelling = col; if (type == Double.class || type == Integer.class) { validLat = true; } } else if ("lon".equalsIgnoreCase(col) || "lng".equalsIgnoreCase(col)) { seenLon = true; lonSpelling = col; if (type == Double.class || type == Integer.class) { validLon = true; } } else { builder.add(col, type); } } if (validLat && validLon) { builder.add(GEOMETRY_COLUMN, Point.class); } else { if (seenLat) { builder.add(latSpelling, typesFromData.get("lat")); } if (seenLon) { builder.add(lonSpelling, typesFromData.get("lon")); } } return builder.buildFeatureType(); } CSVLatLonStrategy(CSVFileState csvFileState); }
CSVLatLonStrategy implements CSVStrategy { private SimpleFeatureType buildFeatureType() { SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setName(csvFileState.getTypeName()); builder.setCRS(csvFileState.getCrs()); CsvReader csvReader = null; Map<String, Class<?>> typesFromData = null; try { csvReader = csvFileState.openCSVReader(); headers = csvReader.getHeaders(); typesFromData = findMostSpecificTypesFromData(csvReader, headers); } catch (IOException e) { throw new RuntimeException("Failure reading csv file", e); } finally { if (csvReader != null) { csvReader.close(); } } if (csvFileState.getNamespace() != null) { builder.setNamespaceURI(csvFileState.getNamespace()); } boolean validLat = false; boolean validLon = false; boolean seenLat = false; boolean seenLon = false; String latSpelling = null; String lonSpelling = null; for (String col : headers) { Class<?> type = typesFromData.get(col); if ("lat".equalsIgnoreCase(col)) { seenLat = true; latSpelling = col; if (type == Double.class || type == Integer.class) { validLat = true; } } else if ("lon".equalsIgnoreCase(col) || "lng".equalsIgnoreCase(col)) { seenLon = true; lonSpelling = col; if (type == Double.class || type == Integer.class) { validLon = true; } } else { builder.add(col, type); } } if (validLat && validLon) { builder.add(GEOMETRY_COLUMN, Point.class); } else { if (seenLat) { builder.add(latSpelling, typesFromData.get("lat")); } if (seenLon) { builder.add(lonSpelling, typesFromData.get("lon")); } } return builder.buildFeatureType(); } CSVLatLonStrategy(CSVFileState csvFileState); @Override SimpleFeatureType getFeatureType(); @Override CSVIterator iterator(); @Override SimpleFeature createFeature(String recordId, String[] csvRecord); }
CSVLatLonStrategy implements CSVStrategy { private SimpleFeatureType buildFeatureType() { SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setName(csvFileState.getTypeName()); builder.setCRS(csvFileState.getCrs()); CsvReader csvReader = null; Map<String, Class<?>> typesFromData = null; try { csvReader = csvFileState.openCSVReader(); headers = csvReader.getHeaders(); typesFromData = findMostSpecificTypesFromData(csvReader, headers); } catch (IOException e) { throw new RuntimeException("Failure reading csv file", e); } finally { if (csvReader != null) { csvReader.close(); } } if (csvFileState.getNamespace() != null) { builder.setNamespaceURI(csvFileState.getNamespace()); } boolean validLat = false; boolean validLon = false; boolean seenLat = false; boolean seenLon = false; String latSpelling = null; String lonSpelling = null; for (String col : headers) { Class<?> type = typesFromData.get(col); if ("lat".equalsIgnoreCase(col)) { seenLat = true; latSpelling = col; if (type == Double.class || type == Integer.class) { validLat = true; } } else if ("lon".equalsIgnoreCase(col) || "lng".equalsIgnoreCase(col)) { seenLon = true; lonSpelling = col; if (type == Double.class || type == Integer.class) { validLon = true; } } else { builder.add(col, type); } } if (validLat && validLon) { builder.add(GEOMETRY_COLUMN, Point.class); } else { if (seenLat) { builder.add(latSpelling, typesFromData.get("lat")); } if (seenLon) { builder.add(lonSpelling, typesFromData.get("lon")); } } return builder.buildFeatureType(); } CSVLatLonStrategy(CSVFileState csvFileState); @Override SimpleFeatureType getFeatureType(); @Override CSVIterator iterator(); @Override SimpleFeature createFeature(String recordId, String[] csvRecord);  }