turf icon indicating copy to clipboard operation
turf copied to clipboard

@turf/[email protected] - Unable to clone FeatureCollection containing features with null geometry

Open chrispahm opened this issue 3 years ago • 1 comments

When trying to clone a GeoJSON FeatureCollection that contains null geometries, @turf/clone (v. 6.5.0) throws "Cannot read properties of null (reading 'type')".

The error originates from the cloneGeometry function https://github.com/Turfjs/turf/blob/2dd2ef88c44d847989fa0b82089f3f81fe433b83/packages/turf-clone/index.ts#L61

which could be changed to something like the following

if (geojson.geometry == null) {
  cloned.geometry = null;
} else {
  cloned.geometry = cloneGeometry(geojson.geometry);
}

in order to resolve the bug. I can create a PR if desired!

Here's an Observable notebook reproducing the behaviour: https://observablehq.com/@chrispahm/turf-clone-unable-to-clone-featurecollection-containing-f

chrispahm avatar Jul 25 '22 12:07 chrispahm

Thanks for the detailed report - I think because null geometries are valid in the GeoJSON spec this is a bug. Feel free to raise a PR or perhaps one of us can look into it if we get a moment.

JamesLMilner avatar Jul 25 '22 19:07 JamesLMilner