turf
turf copied to clipboard
Creating a polygon with empty array of LinearRings doesn't throw an error
Creating a polygon with empty array of LinearRings doesn't throw an error
const { polygon } = require('@turf/helpers');
console.log(polygon([]).geometry) // {coordinates: [], type: "Polygon"}
Expected: an error is thrown Actual: returns a valid polygon with empty coordinates
- [x] Snippet of source code or for complex examples use jsfiddle.
@daniel-hauser I'm not sure an error should actually be expected, since technically the GeoJSON specs (to which Turf tries to conform) don't require the LinearRing array to be non-empty: https://tools.ietf.org/html/rfc7946#section-3.1.6
According to section 3.1:
GeoJSON processors MAY interpret Geometry objects with empty "coordinates" arrays as null objects
Given that the polygon
function does not support null
as input and throwes, the same should be with an empty array
Moreover, In section 3.1.8 and section 3.3 you can see the following clarification:
It is possible for this array to be empty.
So we can argue that empty arrays are allowed only if the spec explicitly allowed them
If we choose to implement it, this is probably good to also improve the error thrown for null
geometry
const { polygon } = require('@turf/helpers');
console.log(polygon(null).geometry) // TypeError: Cannot read property 'length' of null
I'll be happy to open a PR once you have a decision
This is related to our handling of geojson typescript types, throwing it into 7.0.0