question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mask - option for islands in holes to be pulled out as separate MultiPolygon exterior rings

See original GitHub issue

The mask function takes an input polygon and inverts it such all areas outside the input polygon now form the inside of the output.

When you pass mask a Polygon with a hole (an exterior ring and an interior ring) like this:

polygon with a hole

https://gist.github.com/andrewharvey/971b9db1900a62efa7635f6a6d337ae7

It will simply add a new global exterior ring and make the input rings interior rings.

Although I’m not 100% sure if supported by the GeoJSON spec many applications which support GeoJSON allow for a GeoJSON Polygon with an interior ring inside another interior ring to be interpreted as an island inside a hole inside a polygon.

I think the winding order of the interior rings should be used to spell out if the ring should be a hole or island, although this seems to make no difference in Leaflet.

So in Leaflet this works and visually creates the mask you expect.

island in a hole in a polygon

https://gist.github.com/andrewharvey/c092a6c4932a4d94ecb6cd59dc935b90

However not all applications support this for example earcut does not: https://github.com/mapbox/earcut/issues/94.

I propose mask should have an option for these islands within holes to be pulled out as separate exterior rings as part of a MultiPolygon geometry type rather than inside the Polygon type which is what earcut asks for.

I’m working on a PR at the moment to do this, appreciate any thoughts on the issue.

It should also fix the issue where you can’t round trip that second example back to the first by again applying the mask.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

1reaction
andrewharveycommented, Mar 12, 2018

They should be part of a MuliPolygon instead, like earcut is doing if I understand correctly.

If that’s the case then the current output of turf.mask is wrong and it looks okay in Leaflet by luck, in which case what I propose shouldn’t be an option it should be the default behaviour.

0reactions
waissbluthcommented, Feb 14, 2020

This is a simple test case that fails to produce a valid MultiPolygon for both difference @ 6.0.1 and mask @ 5.1.5, and instead produce an invalid Polygon. difference @ 5.1.5 produces proper output.

const world = {
  "type": "Polygon",
  "coordinates": [ [ [ -180, -90 ], [  180, -90 ], [  180,  90 ], [ -180,  90 ], [ -180, -90 ] ] ]
};

const hole = {
  "type": "Polygon",
  "coordinates": [
    [ [ -10,  50 ], [ -40,  20 ], [  70, -30 ],  [ -10,  50 ] ],
    [ [ -5,  30 ], [ -20, 20 ], [  35, 3  ], [ -5,  30 ] ]
  ]
};

console.log(turf.difference(world, hole));
console.log(turf.mask(hole));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove/Fill holes in Polygons from a MultiPolygon using ...
The idea seems to be simple: Get the list of polygons, get the exterior rings and create new polygons from those. Inner rings...
Read more >
Mask features—ArcGIS Pro | Documentation
Masking is a technique used to clarify dense or detailed map content by having the features of one layer hide, or mask, features...
Read more >
Turf.js | Advanced geospatial analysis
Takes any type of polygon and an optional mask and returns a polygon exterior ring with holes. Arguments. Argument, Type, Description. polygon, ( ......
Read more >
Softimage User's Guide: Converting Curves to Polygon Meshes
This option is recursive; it will create holes within polygon "islands" within ... islands created by separate outer contour curves when converting curves....
Read more >
How To: Create holes in polygons
Later versions of ArcGIS may contain different functionality, ... The hole is removed from the area of the exterior polygon.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found