GeoJSONReader2 { public static SimpleFeature toFeature(Map<String, Object> json, SimpleFeatureType schema) { try { return toFeature(json, new SimpleFeatureBuilder(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 public void testPointFeature() throws Exception { Map<String, Object> json = loadJSONResource("point.json"); CoordinateReferenceSystem crs84 = CRS.decode("EPSG:4326", true); SimpleFeatureType schema = GeoJSONSchemaDetector.getSchema(json, crs84); SimpleFeature f = GeoJSONReader2.toFeature(json, schema); assertEquals(2, f.getProperties().size()); Point geom = (Point) f.getDefaultGeometry(); Coordinate c = geom.getCoordinate(); assertEquals(125.6, c.x, 1e6); assertEquals(10.1, c.y, 1e6); String name = (String) f.getAttribute("name"); assertEquals("Dinagat Islands", name); }
@Test public void testLineString() throws Exception { Map<String, Object> json = loadJSONResource("linestring.json"); CoordinateReferenceSystem crs84 = CRS.decode("EPSG:4326", true); SimpleFeatureType schema = GeoJSONSchemaDetector.getSchema(json, crs84); SimpleFeature f = GeoJSONReader2.toFeature(json, schema); assertEquals(2, f.getProperties().size()); LineString geom = (LineString) f.getDefaultGeometry(); CoordinateSequence cs = geom.getCoordinateSequence(); assertEquals(2, cs.size()); Coordinate c = cs.getCoordinate(0); assertEquals(125.6, c.x, 1e6); assertEquals(10.1, c.y, 1e6); c = cs.getCoordinate(1); assertEquals(10.1, c.x, 1e6); assertEquals(125.6, c.y, 1e6); String name = (String) f.getAttribute("name"); assertEquals("Dinagat Islands", name); }
@Test public void testPolygon() throws Exception { Map<String, Object> json = loadJSONResource("polygon.json"); CoordinateReferenceSystem crs84 = CRS.decode("EPSG:4326", true); SimpleFeatureType schema = GeoJSONSchemaDetector.getSchema(json, crs84); SimpleFeature f = GeoJSONReader2.toFeature(json, schema); assertEquals(2, f.getProperties().size()); Polygon geom = (Polygon) f.getDefaultGeometry(); CoordinateSequence cs = geom.getExteriorRing().getCoordinateSequence(); assertEquals(5, cs.size()); Coordinate c = cs.getCoordinate(0); assertEquals(125.6, c.x, 1e6); assertEquals(10.1, c.y, 1e6); c = cs.getCoordinate(4); assertEquals(10.1, c.x, 1e6); assertEquals(125.6, c.y, 1e6); assertEquals(1, geom.getNumInteriorRing()); String name = (String) f.getAttribute("name"); assertEquals("Dinagat Islands", name); }