Skip to content

Commit

Permalink
FlatGeobuf writing: in SPATIAL_INDEX=NO mode, deal with empty geometr…
Browse files Browse the repository at this point in the history
…ies as if there were null

- this fixes the crash when writing an empty polygon. Fixes OSGeo#11419
- this avoids a reading error when writing an empty line
  • Loading branch information
rouault committed Dec 2, 2024
1 parent 5e173b5 commit e1f2404
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions autotest/ogr/ogr_flatgeobuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1612,3 +1612,24 @@ def test_ogr_flatgeobuf_arrow_stream_numpy_datetime_as_string(tmp_vsimem):
assert batch["datetime"][1] == b"2022-05-31T12:34:56.789Z"
assert batch["datetime"][2] == b"2022-05-31T12:34:56"
assert batch["datetime"][3] == b"2022-05-31T12:34:56+12:30"


def test_ogr_flatgeobuf_write_empty_geometries_no_spatial_index(tmp_vsimem):
# Verify empty geom handling without spatial index
out_filename = str(tmp_vsimem / "out.fgb")
with ogr.GetDriverByName("FlatGeobuf").CreateDataSource(out_filename) as ds:
lyr = ds.CreateLayer(
"test", geom_type=ogr.wkbPolygon, options=["SPATIAL_INDEX=NO"]
)

f = ogr.Feature(lyr.GetLayerDefn())
f.SetGeometry(ogr.CreateGeometryFromWkt("POLYGON EMPTY"))
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
lyr.CreateFeature(f)

with gdal.OpenEx(out_filename) as ds:
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
g = f.GetGeometryRef()
assert g is None
2 changes: 1 addition & 1 deletion ogr/ogrsf_frmts/flatgeobuf/ogrflatgeobuflayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2295,7 +2295,7 @@ OGRErr OGRFlatGeobufLayer::ICreateFeature(OGRFeature *poNewFeature)
// the size of the FlatBuffer, but WKB might be a good approximation.
// Takes an extra security margin of 10%
flatbuffers::Offset<FlatGeobuf::Geometry> geometryOffset = 0;
if (ogrGeometry != nullptr)
if (ogrGeometry && !ogrGeometry->IsEmpty())
{
const auto nWKBSize = ogrGeometry->WkbSize();
if (nWKBSize > feature_max_buffer_size - nWKBSize / 10)
Expand Down

0 comments on commit e1f2404

Please sign in to comment.