Skip to content

Commit

Permalink
GeoJSON reader: accept a {"type": "Polygon","coordinates": []} as a r…
Browse files Browse the repository at this point in the history
…epresentation of POLYGON EMPTY (fixes duckdb/duckdb-spatial#273)

This was what we exported already, which also happens to validate
against http://geojson.org/schema/Polygon.json but on import,
we insisted on havin a [[]] coordinates array (which does not validate
the schema)
  • Loading branch information
rouault committed Mar 9, 2024
1 parent bd9f525 commit c7e2ca8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
12 changes: 9 additions & 3 deletions autotest/ogr/ogr_geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -1906,7 +1906,7 @@ def test_ogr_geojson_50():


###############################################################################
# Test writing empty geometries
# Test writing and reading empty geometries


def test_ogr_geojson_51():
Expand Down Expand Up @@ -1949,8 +1949,6 @@ def test_ogr_geojson_51():
data = gdal.VSIFReadL(1, 10000, fp).decode("ascii")
gdal.VSIFCloseL(fp)

gdal.Unlink("/vsimem/ogr_geojson_51.json")

assert '{ "id": 1 }, "geometry": null' in data

assert (
Expand Down Expand Up @@ -1982,6 +1980,14 @@ def test_ogr_geojson_51():
in data
)

ds = ogr.Open("/vsimem/ogr_geojson_51.json")
lyr = ds.GetLayer(0)
for f in lyr:
if f.GetFID() >= 2:
assert f.GetGeometryRef().IsEmpty()

gdal.Unlink("/vsimem/ogr_geojson_51.json")


###############################################################################
# Test NULL type detection
Expand Down
11 changes: 5 additions & 6 deletions ogr/ogrsf_frmts/geojson/ogrgeojsonreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2952,7 +2952,6 @@ OGRPolygon *OGRGeoJSONReadPolygon(json_object *poObj, bool bRaw)
if (poObjPoints == nullptr)
{
poPolygon = new OGRPolygon();
poPolygon->addRingDirectly(new OGRLinearRing());
}
else
{
Expand All @@ -2968,11 +2967,7 @@ OGRPolygon *OGRGeoJSONReadPolygon(json_object *poObj, bool bRaw)
i < nRings && nullptr != poPolygon; ++i)
{
poObjPoints = json_object_array_get_idx(poObjRings, i);
if (poObjPoints == nullptr)
{
poPolygon->addRingDirectly(new OGRLinearRing());
}
else
if (poObjPoints != nullptr)
{
OGRLinearRing *poRing =
OGRGeoJSONReadLinearRing(poObjPoints);
Expand All @@ -2983,6 +2978,10 @@ OGRPolygon *OGRGeoJSONReadPolygon(json_object *poObj, bool bRaw)
}
}
}
else
{
poPolygon = new OGRPolygon();
}
}

return poPolygon;
Expand Down

0 comments on commit c7e2ca8

Please sign in to comment.