8000 add support for GeoJSON's Feature and FeatureCollection types by danishnawab · Pull Request #837 · locationtech/jts · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add support for GeoJSON's Feature and FeatureCollection types #837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
public class GeoJsonConstants {

public static final String NAME_GEOMETRY = "geometry";
public static final String NAME_FEATURES = "features";
public static final String NAME_GEOMETRIES = "geometries";
public static final String NAME_CRS = "crs";
public static final String NAME_PROPERTIES = "properties";
Expand All @@ -32,5 +34,7 @@ public class GeoJsonConstants {
public static final String NAME_MULTIPOLYGON = "MultiPolygon";
public static final String NAME_MULTILINESTRING = "MultiLineString";
public static final String NAME_MULTIPOINT = "MultiPoint";
public static final String NAME_FEATURE = "Feature";
public static final String NAME_FEATURECOLLECTION = "FeatureCollection";

}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ private Geometry create(Map<String, Object> geometryMap,
} else if (GeoJsonConstants.NAME_GEOMETRYCOLLECTION.equals(type)) {
result = createGeometryCollection(geometryMap, geometryFactory);

} else if (GeoJsonConstants.NAME_FEATURE.equals(type)) {
result = createFeature(geometryMap, geometryFactory);

} else if (GeoJsonConstants.NAME_FEATURECOLLECTION.equals(type)) {
result = createFeatureCollection(geometryMap, geometryFactory);

} else {
throw new ParseException(
"Could not parse Geometry from GeoJson string. Unsupported 'type':"
Expand All @@ -177,6 +183,36 @@ private Geometry create(Map<String, Object> geometryMap,
return result;
}

private Geometry createFeatureCollection(Map<String, Object> geometryMap,
GeometryFactory geometryFactory) throws ParseException {
try {
@SuppressWarnings("unchecked")
List<Map<String, Object>> features = (List<Map<String, Object>>) geometryMap.get(GeoJsonConstants.NAME_FEATURES);

Geometry[] geometries = new Geometry[features.size()];
int i = 0;
for (Map<String, Object> featureMap : features) {
geometries[i] = createFeature(featureMap, geometryFactory);
++i;
}

return geometryFactory.createGeometryCollection(geometries);
} catch (RuntimeException e) {
throw new ParseException("Could not parse FeatureCollection from GeoJson string.", e);
}
}

private Geometry createFeature(Map<String, Object> geometryMap,
GeometryFactory geometryFactory) throws ParseException {
try {
@SuppressWarnings("unchecked")
Map<String, Object> innerGeometryMap = (Map<String, Object>) geometryMap.get(GeoJsonConstants.NAME_GEOMETRY);
return create(innerGeometryMap, geometryFactory);
} catch (RuntimeException e) {
throw new ParseException("Could not parse Feature from GeoJson string.", e);
}
}

private Geometry createGeometryCollection(Map<String, Object> geometryMap,
GeometryFactory geometryFactory) throws ParseException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@

package org.locationtech.jts.io.geojson;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.Point;
CBE5 import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.ParseException;

import test.jts.GeometryTestCase;

import java.util.Arrays;

public class GeoJsonReaderTest extends GeometryTestCase {

private GeoJsonReader geoJsonRdr;
Expand Down Expand Up @@ -62,6 +68,43 @@ public void testNullCoordinatesPolygon() throws ParseException {
runTest("{\"type\":\"Polygon\",\"coordinates\":null}", "POLYGON EMPTY");
}

public void testEmptyFeatureCollection() throws ParseException {
runTest("{ \"type\": \"FeatureCollection\", \"features\": [] }", "GEOMETRYCOLLECTION EMPTY");
}

public void testFeatureCollection() throws ParseException {
final String featureCollectionTemplate = "{ \"type\": \"FeatureCollection\", \"features\": [ %s, %s ] }";
String polygonFeature = "{ \"type\": \"Feature\", \"geometry\": { \"type\": \"Polygon\", \"coordinates\": [ [ [ 10, 20, 0 ], [ 11, 21, 0 ], [ 10, 20, 0 ] ] ] }, \"properties\": { \"name\": \"Some polygonGeometry property\" } }";
String pointFeature = "{ \"type\": \"Feature\", \"geometry\": { \"type\": \"Point\", \"coordinates\": [ 12, 13, 1 ] }, \"properties\": { \"name\": \"Some point property\" } }";
final String featureCollection = String.format(featureCollectionTemplate, polygonFeature, pointFeature);

final Geometry geometryCollection = geoJsonRdr.read(featureCollection);
assertEquals(GeometryCollection.TYPENAME_GEOMETRYCOLLECTION, geometryCollection.getGeometryType());
assertEquals(2, geometryCollection.getNumGeometries());

final Geometry polygon = geometryCollection.getGeometryN(0);
assertEquals(GeometryCollection.TYPENAME_POLYGON, polygon.getGeometryType());
assertEquals(Polygon.class, polygon.getClass());
assertEquals(1, polygon.getNumGeometries());
final Coordinate[] polygonCoordinates = polygon.getCoordinates();
assertEquals(3, polygonCoordinates.length);
final Coordinate[] expectedPolygonCoordinates = {
new Coordinate(10, 20, 0),
new Coordinate(11, 21, 0),
new Coordinate(10, 20, 0)
};
assertTrue(Arrays.equals(expectedPolygonCoordinates, polygonCoordinates));

final Geometry point = geometryCollection.getGeometryN(1);
assertEquals(GeometryCollection.TYPENAME_POINT, point.getGeometryType());
assertEquals(Point.class, point.getClass());
assertEquals(1, point.getNumGeometries());
final Coordinate[] pointCoordinates = point.getCoordinates();
assertEquals(1, pointCoordinates.length);
final Coordinate[] expectedPointCoordinates = {new Coordinate(12, 13, 1)};
assertTrue(Arrays.equals(expectedPointCoordinates, pointCoordinates));
}

private void runParseEx(String json) {
try {
Geometry geom = geoJsonRdr.read(json);
Expand Down
0