turf icon indicating copy to clipboard operation
turf copied to clipboard

Creating a polygon with empty array of LinearRings doesn't throw an error

Open daniel-hauser opened this issue 5 years ago • 3 comments

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 avatar Jan 20 '20 14:01 daniel-hauser

@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

stebogit avatar Jan 21 '20 03:01 stebogit

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

daniel-hauser avatar Jan 21 '20 08:01 daniel-hauser

This is related to our handling of geojson typescript types, throwing it into 7.0.0

mfedderly avatar Aug 02 '21 21:08 mfedderly