/** * Each element in the geometries array of a GeometryCollection is one of * the geometry objects described above: * * { "type": "GeometryCollection", "geometries": [ { "type": "Point", * "coordinates": [100.0, 0.0] }, { "type": "LineString", "coordinates": [ * [101.0, 0.0], [102.0, 1.0] ] } ]} * * @param jp * * @throws IOException * @throws SQLException * @return GeometryCollection */ private GeometryCollection parseGeometryCollection(JsonParser jp) throws IOException, SQLException { jp.nextToken(); // FIELD_NAME geometries String coordinatesField = jp.getText(); if (coordinatesField.equalsIgnoreCase(GeoJsonField.GEOMETRIES)) { jp.nextToken();//START array //jp.nextToken();//START object ArrayList<Geometry> geometries = new ArrayList<Geometry>(); while (jp.getCurrentToken() != JsonToken.END_ARRAY) { geometries.add(parseGeometry(jp)); } jp.nextToken();//END_OBJECT } geometry return GF.createGeometryCollection(geometries.toArray(new Geometry[0])); } else { throw new SQLException("Malformed GeoJSON file. Expected 'geometries', found '" + coordinatesField + "'"); } }
/** * Each element in the geometries array of a GeometryCollection is one of * the geometry objects described above: * * { "type": "GeometryCollection", "geometries": [ { "type": "Point", * "coordinates": [100.0, 0.0] }, { "type": "LineString", "coordinates": [ * [101.0, 0.0], [102.0, 1.0] ] } ]} * * @param jp * * @throws IOException * @throws SQLException * @return GeometryCollection */ private GeometryCollection parseGeometryCollection(JsonParser jp) throws IOException, SQLException { jp.nextToken(); // FIELD_NAME geometries String coordinatesField = jp.getText(); if (coordinatesField.equalsIgnoreCase(GeoJsonField.GEOMETRIES)) { jp.nextToken();//START array //jp.nextToken();//START object ArrayList<Geometry> geometries = new ArrayList<Geometry>(); while (jp.getCurrentToken() != JsonToken.END_ARRAY) { geometries.add(parseGeometry(jp)); } jp.nextToken();//END_OBJECT } geometry return GF.createGeometryCollection(geometries.toArray(new Geometry[geometries.size()])); } else { throw new SQLException("Malformed GeoJSON file. Expected 'geometries', found '" + coordinatesField + "'"); } }
/** * Each element in the geometries array of a GeometryCollection is one of * the geometry objects described above: * * { "type": "GeometryCollection", "geometries": [ { "type": "Point", * "coordinates": [100.0, 0.0] }, { "type": "LineString", "coordinates": [ * [101.0, 0.0], [102.0, 1.0] ] } ]} * * @param jp * * @throws IOException * @throws SQLException * @return GeometryCollection */ private GeometryCollection parseGeometryCollection(JsonParser jp) throws IOException, SQLException { jp.nextToken(); // FIELD_NAME geometries String coordinatesField = jp.getText(); if (coordinatesField.equalsIgnoreCase(GeoJsonField.GEOMETRIES)) { jp.nextToken();//START array //jp.nextToken();//START object ArrayList<Geometry> geometries = new ArrayList<Geometry>(); while (jp.getCurrentToken() != JsonToken.END_ARRAY) { geometries.add(parseGeometry(jp)); } jp.nextToken();//END_OBJECT } geometry return GF.createGeometryCollection(geometries.toArray(new Geometry[geometries.size()])); } else { throw new SQLException("Malformed GeoJSON file. Expected 'geometries', found '" + coordinatesField + "'"); } }
/** * Convert a geojson geometry to geometry. * * @param geojson * @return * @throws java.io.IOException * @throws java.sql.SQLException */ public static Geometry geomFromGeoJSON(String geojson) throws IOException, SQLException { if (geojson == null) { return null; } if (jsFactory == null) { jsFactory = new JsonFactory(); jsFactory.configure(JsonParser.Feature.ALLOW_COMMENTS, true); jsFactory.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); jsFactory.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true); reader = new GJGeometryReader(new GeometryFactory()); } JsonParser jp = jsFactory.createParser(geojson); return reader.parseGeometry(jp); } }
/** * Convert a geojson geometry to geometry. * * @param geojson * @return * @throws java.io.IOException * @throws java.sql.SQLException */ public static Geometry geomFromGeoJSON(String geojson) throws IOException, SQLException { if (geojson == null) { return null; } if (jsFactory == null) { jsFactory = new JsonFactory(); jsFactory.configure(JsonParser.Feature.ALLOW_COMMENTS, true); jsFactory.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); jsFactory.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true); reader = new GJGeometryReader(new GeometryFactory()); } JsonParser jp = jsFactory.createParser(geojson); return reader.parseGeometry(jp); } }
/** * Convert a geojson geometry to geometry. * * @param geojson * @return * @throws java.io.IOException * @throws java.sql.SQLException */ public static Geometry geomFromGeoJSON(String geojson) throws IOException, SQLException { if (geojson == null) { return null; } if (jsFactory == null) { jsFactory = new JsonFactory(); jsFactory.configure(JsonParser.Feature.ALLOW_COMMENTS, true); jsFactory.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); jsFactory.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true); reader = new GJGeometryReader(new GeometryFactory()); } JsonParser jp = jsFactory.createParser(geojson); return reader.parseGeometry(jp); } }