Skip to content

Handle empty geojson coordinate arrays correctly - #17615

Merged
ahocevar merged 4 commits into
openlayers:mainfrom
ahocevar:geojson-empty-coordinates
Sep 4, 2026
Merged

ahocevar merged 4 commits into
openlayers:mainfrom
ahocevar:geojson-empty-coordinates

Conversation

@ahocevar

@ahocevar ahocevar commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

#15388 fixed a valid concern, but in the incorrect way. According to the GeoJSON spec:

A GeoJSON Geometry object of any type other than "GeometryCollection" has a member with the name "coordinates". The value of the "coordinates" member is an array. The structure of the elements in this array is determined by the type of geometry. GeoJSON processors MAY interpret Geometry objects with empty "coordinates" arrays as null objects.

What we currently do is produce geometries with empty coordinates arrays, but the GeoJSON spec says

A position is the fundamental geometry construct. The "coordinates" member of a Geometry object is composed of either:

  • one position in the case of a Point geometry,
  • an array of positions in the case of a LineString or MultiPoint geometry,
  • an array of LineString or linear ring (see Section 3.1.6) coordinates in the case of a Polygon or MultiLineString geometry, or
  • an array of Polygon coordinates in the case of a MultiPolygon geometry.

A position is an array of numbers. There MUST be two or more elements.

So it does not make much sense to create geometries with empty coordinates arrays from GeoJSON with empty coordinates arrays.

The fix this pull request proposes as alternative is to return null geomertries, according to the spec.

@github-actions

Copy link
Copy Markdown

@RobertOrthofer

RobertOrthofer commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

I think gracefully handling such geometries as null instead of generating degenerate internal geometries is the way to go. Just the one code comment from me.

@ahocevar

ahocevar commented Sep 4, 2026

Copy link
Copy Markdown
Member Author

@RobertOrthofer Thanks for the review. I don't see the code comment you mentioned though.

Comment thread src/ol/format/GeoJSON.js Resolved
@ahocevar

ahocevar commented Sep 4, 2026

Copy link
Copy Markdown
Member Author

Sharp eye, @RobertOrthofer. I added a commit which handles that case properly.

@RobertOrthofer RobertOrthofer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good from my side

@ahocevar
ahocevar force-pushed the geojson-empty-coordinates branch from 4f3a506 to 8225ade Compare September 4, 2026 14:24
@ahocevar

ahocevar commented Sep 4, 2026

Copy link
Copy Markdown
Member Author

Thanks, @RobertOrthofer. I also added a regression test for the geometry collection case.

@ahocevar
ahocevar merged commit 3305db7 into openlayers:main Sep 4, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants