GeoJSONReader2 { public static SimpleFeatureCollection toFeatureCollection(Map<String, Object> json, SimpleFeatureType schema) { try { return toFeatureCollection(json, schema, null); } catch (MismatchedDimensionException | TransformException ignore) { throw new RuntimeException("Something bad happened"); } }  static SimpleFeatureCollection toFeatureCollection(Map<String, Object> json, SimpleFeatureType schema); static SimpleFeatureCollection toFeatureCollection(Map<String, Object> json,
            SimpleFeatureType schema, MathTransform transform); static SimpleFeature toFeature(Map<String, Object> json, SimpleFeatureType schema); static SimpleFeature toFeature(Map<String, Object> json, SimpleFeatureType schema,
            MathTransform transform); static Geometry toGeometry(Map<String, Object> geometry); static Point toPoint(Map<String, Object> geometry); static LineString toLineString(Map<String, Object> geometry); static Polygon toPolygon(Map<String, Object> geometry); static MultiPoint toMultiPoint(Map<String, Object> geometry); static MultiLineString toMultiLineString(Map<String, Object> geometry); static MultiPolygon toMultiPolygon(Map<String, Object> geometry); static GeometryCollection toGeometryCollection(Map<String, Object> geometry);  }
@Test @SuppressWarnings("unchecked") public void testDeeplyComplex() throws Exception { Map<String, Object> json = loadJSONResource("featureCollectionDeeplyComplex.json"); CoordinateReferenceSystem crs84 = CRS.decode("EPSG:4326", true); SimpleFeatureType schema = GeoJSONSchemaDetector.getSchema(json, crs84); SimpleFeatureCollection fc = GeoJSONReader2.toFeatureCollection(json, schema); try (SimpleFeatureIterator it = fc.features()) { assertTrue(it.hasNext()); SimpleFeature f1 = it.next(); assertFalse(it.hasNext()); assertNotNull(f1); assertEquals("ABC_112", f1.getID()); assertEquals("bar", f1.getAttribute("foo")); Map<String, Object> a = (Map<String, Object>) f1.getAttribute("a"); List<Object> foo = (List<Object>) a.get("foo"); Map<String, Object> fooThe3rd = (Map<String, Object>) foo.get(2); Map<String, Object> qux = (Map<String, Object>) fooThe3rd.get("qux"); assertEquals("sure", qux.get("deep")); Geometry expectedGeometry = new WKTReader().read("POINT (21.3587384 61.3939013)"); assertEquals(expectedGeometry, f1.getDefaultGeometry()); } }
@Test public void testSimpleArrayComplexProperty() throws Exception { Map<String, Object> json = loadJSONResource("featureCollectionSimpleArray.json"); CoordinateReferenceSystem crs84 = CRS.decode("EPSG:4326", true); SimpleFeatureType schema = GeoJSONSchemaDetector.getSchema(json, crs84); SimpleFeatureCollection fc = GeoJSONReader2.toFeatureCollection(json, schema); try (SimpleFeatureIterator it = fc.features()) { assertTrue(it.hasNext()); SimpleFeature f1 = it.next(); assertFalse(it.hasNext()); assertNotNull(f1); assertEquals("ABC_123", f1.getID()); assertEquals("bar", f1.getAttribute("foo")); List<Integer> expectedSimpleArray = Arrays.asList(1, 2, 3); assertThat(expectedSimpleArray, Is.is(f1.getAttribute("mySimpleIntArray"))); Geometry expectedGeometry = new WKTReader().read("POINT (21.3743445 61.3764872)"); assertEquals(expectedGeometry, f1.getDefaultGeometry()); } }
@Test public void testComplexProperties() throws Exception { Map<String, Object> json = loadJSONResource("featureCollectionComplex.json"); CoordinateReferenceSystem crs84 = CRS.decode("EPSG:4326", true); SimpleFeatureType schema = GeoJSONSchemaDetector.getSchema(json, crs84); SimpleFeatureCollection fc = GeoJSONReader2.toFeatureCollection(json, schema); try (SimpleFeatureIterator it = fc.features()) { SimpleFeature f1 = it.hasNext() ? it.next() : null; SimpleFeature f2 = it.hasNext() ? it.next() : null; assertFalse(it.hasNext()); assertNotNull(f1); assertNotNull(f2); assertEquals("P_10000001", f1.getID()); assertEquals("P_10000002", f2.getID()); assertEquals(10000001, f1.getAttribute("placeId")); assertEquals(3, f1.getAttribute("placeVersionId")); assertEquals(1010110, f1.getAttribute("placeType")); assertEquals("M3233D4", f1.getAttribute("tm35MapSheet")); assertEquals("2008-12-05T22:00:00Z", f1.getAttribute("placeCreationTime")); assertNull(f1.getAttribute("placeNameDeletionTime")); Geometry expected = new WKTReader().read("POINT (21.3587384 61.3939013)"); assertEquals(expected, f1.getDefaultGeometry()); @SuppressWarnings("unchecked") List<Map<String, Object>> placenames = (List<Map<String, Object>>) f1.getAttribute("name"); assertEquals(1, placenames.size()); Map<String, Object> placename = placenames.get(0); assertEquals(40000001, placename.get("placeNameId")); assertEquals(1, placename.get("placeNameVersionId")); assertEquals("Isokloppa", placename.get("spelling")); assertEquals("fin", placename.get("language")); } }
@Test public void testMultipleGeometries() throws Exception { Map<String, Object> json = loadJSONResource("featureCollectionMultipleGeometries.json"); CoordinateReferenceSystem crs84 = CRS.decode("EPSG:4326", true); SimpleFeatureType schema = GeoJSONSchemaDetector.getSchema(json, crs84); SimpleFeatureCollection fc = GeoJSONReader2.toFeatureCollection(json, schema); try (SimpleFeatureIterator it = fc.features()) { SimpleFeature f = it.hasNext() ? it.next() : null; assertFalse(it.hasNext()); assertNotNull(f); assertEquals("feature.0", f.getID()); WKTReader wkt = new WKTReader(); assertEquals(wkt.read("LINESTRING (1.1 1.2, 1.3 1.4)"), f.getAttribute("otherGeometry")); assertEquals(wkt.read("POINT (0.1 0.1)"), f.getDefaultGeometry()); } }
@Test public void testEmptyFeatureCollection() throws Exception { Map<String, Object> json = loadJSONResource("featureCollectionEmpty.json"); CoordinateReferenceSystem crs84 = CRS.decode("EPSG:4326", true); SimpleFeatureType schema = GeoJSONSchemaDetector.getSchema(json, crs84); assertNull(schema); SimpleFeatureCollection fc = GeoJSONReader2.toFeatureCollection(json, schema); try (SimpleFeatureIterator it = fc.features()) { assertFalse(it.hasNext()); } }
@Test public void testDifferentIdTypes() throws Exception { Map<String, Object> json = loadJSONResource("featureCollectionDifferentId.json"); CoordinateReferenceSystem crs84 = CRS.decode("EPSG:4326", true); SimpleFeatureType schema = GeoJSONSchemaDetector.getSchema(json, crs84); assertNotNull(schema); SimpleFeatureCollection fc = GeoJSONReader2.toFeatureCollection(json, schema); try (SimpleFeatureIterator it = fc.features()) { it.hasNext(); SimpleFeature f1 = it.next(); it.hasNext(); SimpleFeature f2 = it.next(); it.hasNext(); SimpleFeature f3 = it.next(); it.hasNext(); SimpleFeature f4 = it.next(); assertNotNull(f1.getID()); assertEquals(10000001, f1.getAttribute("placeId")); assertNotNull(f2.getID()); assertEquals(10000002, f2.getAttribute("placeId")); assertEquals("123", f3.getID()); assertEquals(10000003, f3.getAttribute("placeId")); assertEquals("ABC_321", f4.getID()); assertEquals(10000004, f4.getAttribute("placeId")); } }