GeoJSONReader2 { public static Geometry toGeometry(Map<String, Object> geometry) { String geomType = GeoJSONUtil.getString(geometry, GeoJSON.TYPE); if (geomType == null) { throw new IllegalArgumentException("Invalid geometry type"); } switch (geomType) { case GeoJSON.POINT: return toPoint(geometry); case GeoJSON.LINESTRING: return toLineString(geometry); case GeoJSON.POLYGON: return toPolygon(geometry); case GeoJSON.MULTI_POINT: return toMultiPoint(geometry); case GeoJSON.MULTI_LINESTRING: return toMultiLineString(geometry); case GeoJSON.MULTI_POLYGON: return toMultiPolygon(geometry); case GeoJSON.GEOMETRY_COLLECTION: return toGeometryCollection(geometry); } throw new IllegalArgumentException("Invalid geometry type"); }  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 testMultiPoint() throws Exception { Map<String, Object> json = loadJSONResource("multipoint.json"); MultiPoint geom = (MultiPoint) GeoJSONReader2.toGeometry(json); assertEquals(2, geom.getNumGeometries()); Point p1 = (Point) geom.getGeometryN(0); Point p2 = (Point) geom.getGeometryN(1); Coordinate c1 = p1.getCoordinate(); Coordinate c2 = p2.getCoordinate(); assertEquals(100.0, c1.x, 1e6); assertEquals(0.0, c1.y, 1e6); assertEquals(101.0, c2.x, 1e6); assertEquals(1.0, c2.y, 1e6); }
@Test public void testMultiLineString() throws Exception { Map<String, Object> json = loadJSONResource("multilinestring.json"); MultiLineString geom = (MultiLineString) GeoJSONReader2.toGeometry(json); assertEquals(2, geom.getNumGeometries()); LineString ls1 = (LineString) geom.getGeometryN(0); LineString ls2 = (LineString) geom.getGeometryN(1); assertEquals(2, ls1.getNumPoints()); assertEquals(2, ls2.getNumPoints()); CoordinateSequence cs2 = ls2.getCoordinateSequence(); Coordinate c2 = cs2.getCoordinate(1); assertEquals(103.0, c2.x, 1e6); assertEquals(3.0, c2.y, 1e6); }
@Test public void testMultiPolygon() throws Exception { Map<String, Object> json = loadJSONResource("multipolygon.json"); MultiPolygon geom = (MultiPolygon) GeoJSONReader2.toGeometry(json); assertEquals(2, geom.getNumGeometries()); Polygon p1 = (Polygon) geom.getGeometryN(0); Polygon p2 = (Polygon) geom.getGeometryN(1); assertEquals(0, p1.getNumInteriorRing()); CoordinateSequence cs1 = p1.getExteriorRing().getCoordinateSequence(); assertEquals(5, cs1.size()); Coordinate c1 = cs1.getCoordinate(0); assertEquals(102.0, c1.x, 1e6); assertEquals(2.0, c1.y, 1e6); assertEquals(1, p2.getNumInteriorRing()); CoordinateSequence cs2 = p2.getInteriorRingN(0).getCoordinateSequence(); assertEquals(5, cs2.size()); Coordinate c2 = cs2.getCoordinate(3); assertEquals(100.8, c2.x, 1e6); assertEquals(0.8, c2.y, 1e6); }
@Test public void testGeometryCollection() throws Exception { Map<String, Object> json = loadJSONResource("geometrycollection.json"); GeometryCollection geom = (GeometryCollection) GeoJSONReader2.toGeometry(json); assertEquals(2, geom.getNumGeometries()); Point p = (Point) geom.getGeometryN(0); Coordinate c = p.getCoordinate(); assertEquals(100.0, c.x, 1e6); assertEquals(0.0, c.y, 1e6); LineString ls = (LineString) geom.getGeometryN(1); CoordinateSequence cs = ls.getCoordinateSequence(); assertEquals(2, cs.size()); c = cs.getCoordinate(0); assertEquals(101.0, c.x, 1e6); assertEquals(0.0, c.y, 1e6); c = cs.getCoordinate(1); assertEquals(102.0, c.x, 1e6); assertEquals(1.0, c.y, 1e6); }