Turfjs / turf

A modular geospatial engine written in JavaScript and TypeScript

Home Page:https://turfjs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't create a union between two masks

rjbrockbundy opened this issue · comments

I am trying to merge two turf masks into a single polygon with turf.union. However I keep getting it to return null.

My two masks look like this:

{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-75.778706,45.311839],[-75.762915,45.311839],[-75.762915,45.323789],[-75.778706,45.323789],[-75.778706,45.311839]],[[-75.786602,45.296747],[-75.722062,45.296747],[-75.722062,45.329341],[-75.786602,45.329341],[-75.786602,45.296747]]]}}
{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-75.74575,45.296747],[-75.728928,45.296747],[-75.728928,45.31715],[-75.74575,45.31715],[-75.74575,45.296747]],[[-75.786602,45.296747],[-75.722062,45.296747],[-75.722062,45.329341],[-75.786602,45.329341],[-75.786602,45.296747]]]}}

I am using turf v6.5

Is this possible? I've tried passing union the geojson, just the features (which is in the json I've provided), the geometry, and just the coordinates but everytime I either get an error or null.

Ideally my end goal is just to have a polygon that has multiple holes cut out of it, so if this is not the best way to do that please let me know.

Thanks!

These two polygons don't seem to be valid geojson, the first and last coordinates should have the same values to close the polygon ring.

Is the input to your earlier steps to produce the masks valid?

Separately, the difference function can be used to subtract out polygons from a larger polygon to produce holes but I'm not sure what your use case is.

These are two polygons each with a hole cut out it, with the "base" polygon remaining the same and then two different "cut" polygons. The base polygon is the same in both in geometry.coordinates[1]:

[[-75.786602,45.296747],[-75.722062,45.296747],[-75.722062,45.329341],[-75.786602,45.329341],[-75.786602,45.296747]]

The actual geoJSON is a FeatureCollection:

{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-75.748839,45.300249],[-75.730129,45.300249],[-75.730129,45.31534],[-75.748839,45.31534],[-75.748839,45.300249]],[[-75.777677,45.293486],[-75.717599,45.293486],[-75.717599,45.320289],[-75.777677,45.320289],[-75.777677,45.293486]]]}}]}
{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-75.777677,45.307251],[-75.755534,45.307251],[-75.755534,45.320289],[-75.777677,45.320289],[-75.777677,45.307251]],[[-75.777677,45.293486],[-75.717599,45.293486],[-75.717599,45.320289],[-75.777677,45.320289],[-75.777677,45.293486]]]}}]}

The masks are correct as I am displaying them propely on my leaflet map, just these two are seperate and I wish to merge them into one.

Hi @rjbrockbundy. Ran your geojson through a validator and get an error about the "right hand rule". So it might be that the direction of one of your rectangles is incorrect (these both run clockwise):

two-rectangles

Tools like Turf use the direction (or winding) of the polygon to figure out which side is meant to be the outside. It might look ok in Leaflet because they have some extra checks to try to give you something that looks good.

Try running your polygons through turf-rewind, and it should change it to the below (outer ring is now anticlockwise):

donut

Give that a try and let us know if it helps. If not, we can dig a little further.

A good (strict) geojson validator: https://geojsonlint.com/

Closing for now in lieu of further feedback. Feel free to reopen if the suggestion above doesn't solve the problem.