8000 Fix GeoJSONReader to parse null and empty coordinates as empty geometry by JamesRTaylor · Pull Request #687 · locationtech/jts · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix GeoJSONReader to parse null and empty coordinates as empty geometry #687

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 1 commit into from
Feb 9, 2021
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 @@ -15,6 +15,7 @@
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -319,17 +320,17 @@ private Geometry createPolygon(Map<String, Object> geometryMap,
List<List<List<Number>>> ringsList = (List<List<List<Number>>>) geometryMap
.get(GeoJsonConstants.NAME_COORDINATES);

if (ringsList == null || ringsList.isEmpty()) {
return geometryFactory.createPolygon();
}

List<CoordinateSequence> rings = new ArrayList<CoordinateSequence>();

for (List<List<Number>> coordinates : ringsList) {

rings.add(createCoordinateSequence(coordinates));
}

if (rings.isEmpty()) {
throw new IllegalArgumentException("Polygon specified with no rings.");
}

LinearRing outer = geometryFactory.createLinearRing(rings.get(0));
LinearRing[] inner = null;
if (rings.size() > 1) {
Expand Down Expand Up @@ -433,6 +434,9 @@ private GeometryFactory getGeometryFactory(Map<String, Object> geometryMap)
private CoordinateSequence createCoordinateSequence(
List<List<Number>> coordinates) {
CoordinateSequence result = null;
if (coordinates == null) {
coordinates = Collections.EMPTY_LIST;
}

result = new CoordinateArraySequence(coordinates.size());

Expand All @@ -455,6 +459,10 @@ private CoordinateSequence createCoordinateSequence(
}

private CoordinateSequence createCoordinate(List<Number> ordinates) {
if (ordinates == null || ordinates.size() == 0) {
return new CoordinateArraySequence(0);
}

CoordinateSequence result = new CoordinateArraySequence(1);

if (ordinates.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

public class GeoJsonReaderTest extends GeometryTestCase {

public GeoJsonReader geoJsonRdr;
private GeoJsonReader geoJsonRdr;

public GeoJsonReaderTest(String name) {
super(name);
Expand All @@ -37,14 +37,49 @@ public void testEmptyArray() throws ParseException {
public void testEmptyObject() throws ParseException {
runParseEx("{}");
}


public void testEmptyCoordinatesPoint() throws ParseException {
runTest("{\"type\":\"Point\",\"coordinates\":[]}", "POINT EMPTY");
}

public void testNullCoordinatesPoint() throws ParseException {
runTest("{\"type\":\"Point\",\"coordinates\":null}", "POINT EMPTY");
}

public void testEmptyCoordinatesLineString() throws ParseException {
runTest("{\"type\":\"LineString\",\"coordinates\":[]}", "LINESTRING EMPTY");
}

public void testNullCoordinatesLineString() throws ParseException {
runTest("{\"type\":\"LineString\",\"coordinates\":null}", "LINESTRING EMPTY");
}

public void testEmptyCoordinatesPolygon() throws ParseException {
runTest("{\"type\":\"Polygon\",\"coordinates\":[]}", "POLYGON EMPTY");
}

public void testNullCoordinatesPolygon() throws ParseException {
runTest("{\"type\":\"Polygon\",\"coordinates\":null}", "POLYGON EMPTY");
}

private void runParseEx(String json) {
try {
Geometry geom = geoJsonRdr.read(json);
fail();
}
catch (ParseException ex) {
assertTrue(true);
}
}

private void runTest(String geojson, String expectedWkt) throws ParseException {
runTest(geojson, expectedWkt, 0, false);
}

private void runTest(String geojson, String expectedWkt, int srid, boolean encodeCRS) throws ParseException {
Geometry expectedGeom = read(expectedWkt);
expectedGeom.setSRID(srid);
Geometry geom = geoJsonRdr.read(geojson);
assertEquals(expectedGeom, geom);
}

}
0