@Test public void testReadFromInputStream() throws Exception { String input = "{\n" + "\"type\": \"FeatureCollection\",\n" + "\"features\": [\n" + "{ \"type\": \"Feature\", \"properties\": { \"LAT\": 46.066667, \"LON\": 11.116667, \"CITY\": \"Trento\", \"NUMBER\": 140, \"YEAR\": 2002 }, \"bbox\": [ 11.117, 46.067, 11.117, 46.067 ], \"geometry\": { \"type\": \"Point\", \"coordinates\": [ 11.117, 46.067 ] } },\n" + "{ \"type\": \"Feature\", \"properties\": { \"LAT\": 44.9441, \"LON\": -93.0852, \"CITY\": \"St Paul\", \"NUMBER\": 125, \"YEAR\": 2003 }, \"bbox\": [ -93.085, 44.944, -93.085, 44.944 ], \"geometry\": { \"type\": \"Point\", \"coordinates\": [ -93.085, 44.944 ] } },\n" + "{ \"type\": \"Feature\", \"properties\": { \"LAT\": 13.752222, \"LON\": 100.493889, \"CITY\": \"Bangkok\", \"NUMBER\": 150, \"YEAR\": 2004 }, \"bbox\": [ 100.494, 13.752, 100.494, 13.752 ], \"geometry\": { \"type\": \"Point\", \"coordinates\": [ 100.494, 13.752 ] } },\n" + "{ \"type\": \"Feature\", \"properties\": { \"LAT\": 45.420833, \"LON\": -75.69, \"CITY\": \"Ottawa\", \"NUMBER\": 200, \"YEAR\": 2004 }, \"bbox\": [ -75.69, 45.421, -75.69, 45.421 ], \"geometry\": { \"type\": \"Point\", \"coordinates\": [ -75.69, 45.421 ] } },\n" + "{ \"type\": \"Feature\", \"properties\": { \"LAT\": 44.9801, \"LON\": -93.251867, \"CITY\": \"Minneapolis\", \"NUMBER\": 350, \"YEAR\": 2005 }, \"bbox\": [ -93.252, 44.98, -93.252, 44.98 ], \"geometry\": { \"type\": \"Point\", \"coordinates\": [ -93.252, 44.98 ] } },\n" + "{ \"type\": \"Feature\", \"properties\": { \"LAT\": 46.519833, \"LON\": 6.6335, \"CITY\": \"Lausanne\", \"NUMBER\": 560, \"YEAR\": 2006 }, \"bbox\": [ 6.633, 46.52, 6.633, 46.52 ], \"geometry\": { \"type\": \"Point\", \"coordinates\": [ 6.633, 46.52 ] } },\n" + "{ \"type\": \"Feature\", \"properties\": { \"LAT\": 48.428611, \"LON\": -123.365556, \"CITY\": \"Victoria\", \"NUMBER\": 721, \"YEAR\": 2007 }, \"bbox\": [ -123.366, 48.429, -123.366, 48.429 ], \"geometry\": { \"type\": \"Point\", \"coordinates\": [ -123.366, 48.429 ] } },\n" + "{ \"type\": \"Feature\", \"properties\": { \"LAT\": -33.925278, \"LON\": 18.423889, \"CITY\": \"Cape Town\", \"NUMBER\": 550, \"YEAR\": 2008 }, \"bbox\": [ 18.424, -33.925, 18.424, -33.925 ], \"geometry\": { \"type\": \"Point\", \"coordinates\": [ 18.424, -33.925 ] } },\n" + "{ \"type\": \"Feature\", \"properties\": { \"LAT\": -33.859972, \"LON\": 151.21111, \"CITY\": \"Sydney\", \"NUMBER\": 436, \"YEAR\": 2009 }, \"bbox\": [ 151.211, -33.86, 151.211, -33.86 ], \"geometry\": { \"type\": \"Point\", \"coordinates\": [ 151.211, -33.86 ] } }\n" + "]\n" + "}"; try (GeoJSONReader reader = new GeoJSONReader(new ByteArrayInputStream(input.getBytes()))) { FeatureCollection features = reader.getFeatures(); assertNotNull(features); assertEquals("wrong number of features read", 9, features.size()); } }
public FeatureCollection getFeatures() throws IOException { if (!isConnected()) { throw new IOException("not connected to " + url.toExternalForm()); } ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JtsModule()); List<SimpleFeature> features = new ArrayList<>(); builder = null; while (!parser.isClosed()) { JsonToken token = parser.nextToken(); if (token == null) { break; } if (JsonToken.FIELD_NAME.equals(token) && "features".equalsIgnoreCase(parser.currentName())) { token = parser.nextToken(); if (!JsonToken.START_ARRAY.equals(token) || token == null) { break; } while (parser.nextToken() == JsonToken.START_OBJECT) { ObjectNode node = mapper.readTree(parser); SimpleFeature feature = getNextFeature(node); features.add(feature); } } } if (isSchemaChanged()) { List<SimpleFeature> nFeatures = new ArrayList<>(features.size()); for (SimpleFeature feature : features) { if (feature.getFeatureType() != schema) { SimpleFeature nFeature = DataUtilities.reType(schema, feature); nFeatures.add(nFeature); } else { nFeatures.add(feature); } } return DataUtilities.collection(nFeatures); } return DataUtilities.collection(features); }
GeoJSONReader implements AutoCloseable { public FeatureCollection getFeatures() throws IOException { if (!isConnected()) { throw new IOException("not connected to " + url.toExternalForm()); } ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JtsModule()); List<SimpleFeature> features = new ArrayList<>(); builder = null; while (!parser.isClosed()) { JsonToken token = parser.nextToken(); if (token == null) { break; } if (JsonToken.FIELD_NAME.equals(token) && "features".equalsIgnoreCase(parser.currentName())) { token = parser.nextToken(); if (!JsonToken.START_ARRAY.equals(token) || token == null) { break; } while (parser.nextToken() == JsonToken.START_OBJECT) { ObjectNode node = mapper.readTree(parser); SimpleFeature feature = getNextFeature(node); features.add(feature); } } } if (isSchemaChanged()) { List<SimpleFeature> nFeatures = new ArrayList<>(features.size()); for (SimpleFeature feature : features) { if (feature.getFeatureType() != schema) { SimpleFeature nFeature = DataUtilities.reType(schema, feature); nFeatures.add(nFeature); } else { nFeatures.add(feature); } } return DataUtilities.collection(nFeatures); } return DataUtilities.collection(features); } }
GeoJSONReader implements AutoCloseable { public FeatureCollection getFeatures() throws IOException { if (!isConnected()) { throw new IOException("not connected to " + url.toExternalForm()); } ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JtsModule()); List<SimpleFeature> features = new ArrayList<>(); builder = null; while (!parser.isClosed()) { JsonToken token = parser.nextToken(); if (token == null) { break; } if (JsonToken.FIELD_NAME.equals(token) && "features".equalsIgnoreCase(parser.currentName())) { token = parser.nextToken(); if (!JsonToken.START_ARRAY.equals(token) || token == null) { break; } while (parser.nextToken() == JsonToken.START_OBJECT) { ObjectNode node = mapper.readTree(parser); SimpleFeature feature = getNextFeature(node); features.add(feature); } } } if (isSchemaChanged()) { List<SimpleFeature> nFeatures = new ArrayList<>(features.size()); for (SimpleFeature feature : features) { if (feature.getFeatureType() != schema) { SimpleFeature nFeature = DataUtilities.reType(schema, feature); nFeatures.add(nFeature); } else { nFeatures.add(feature); } } return DataUtilities.collection(nFeatures); } return DataUtilities.collection(features); } GeoJSONReader(URL url);  GeoJSONReader(InputStream is); }
GeoJSONReader implements AutoCloseable { public FeatureCollection getFeatures() throws IOException { if (!isConnected()) { throw new IOException("not connected to " + url.toExternalForm()); } ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JtsModule()); List<SimpleFeature> features = new ArrayList<>(); builder = null; while (!parser.isClosed()) { JsonToken token = parser.nextToken(); if (token == null) { break; } if (JsonToken.FIELD_NAME.equals(token) && "features".equalsIgnoreCase(parser.currentName())) { token = parser.nextToken(); if (!JsonToken.START_ARRAY.equals(token) || token == null) { break; } while (parser.nextToken() == JsonToken.START_OBJECT) { ObjectNode node = mapper.readTree(parser); SimpleFeature feature = getNextFeature(node); features.add(feature); } } } if (isSchemaChanged()) { List<SimpleFeature> nFeatures = new ArrayList<>(features.size()); for (SimpleFeature feature : features) { if (feature.getFeatureType() != schema) { SimpleFeature nFeature = DataUtilities.reType(schema, feature); nFeatures.add(nFeature); } else { nFeatures.add(feature); } } return DataUtilities.collection(nFeatures); } return DataUtilities.collection(features); } GeoJSONReader(URL url);  GeoJSONReader(InputStream is); boolean isConnected(); static SimpleFeature parseFeature(String json); FeatureCollection getFeatures(); FeatureIterator<SimpleFeature> getIterator(); FeatureType getSchema(); void setSchema(SimpleFeatureType schema); boolean isSchemaChanged(); void setSchemaChanged(boolean schemaChanged); @Override void close(); }
GeoJSONReader implements AutoCloseable { public FeatureCollection getFeatures() throws IOException { if (!isConnected()) { throw new IOException("not connected to " + url.toExternalForm()); } ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JtsModule()); List<SimpleFeature> features = new ArrayList<>(); builder = null; while (!parser.isClosed()) { JsonToken token = parser.nextToken(); if (token == null) { break; } if (JsonToken.FIELD_NAME.equals(token) && "features".equalsIgnoreCase(parser.currentName())) { token = parser.nextToken(); if (!JsonToken.START_ARRAY.equals(token) || token == null) { break; } while (parser.nextToken() == JsonToken.START_OBJECT) { ObjectNode node = mapper.readTree(parser); SimpleFeature feature = getNextFeature(node); features.add(feature); } } } if (isSchemaChanged()) { List<SimpleFeature> nFeatures = new ArrayList<>(features.size()); for (SimpleFeature feature : features) { if (feature.getFeatureType() != schema) { SimpleFeature nFeature = DataUtilities.reType(schema, feature); nFeatures.add(nFeature); } else { nFeatures.add(feature); } } return DataUtilities.collection(nFeatures); } return DataUtilities.collection(features); } GeoJSONReader(URL url);  GeoJSONReader(InputStream is); boolean isConnected(); static SimpleFeature parseFeature(String json); FeatureCollection getFeatures(); FeatureIterator<SimpleFeature> getIterator(); FeatureType getSchema(); void setSchema(SimpleFeatureType schema); boolean isSchemaChanged(); void setSchemaChanged(boolean schemaChanged); @Override void close(); static final String GEOMETRY_NAME; }