w8r / martinez

Martinez-Rueda polygon clipping algorithm, does boolean operation on polygons (multipolygons, polygons with holes etc): intersection, union, difference, xor
https://w8r.github.io/martinez/demo/#geo
MIT License
698 stars 77 forks source link

Return values - Polygon vs MultiPolygon #5

Closed AtiX closed 7 years ago

AtiX commented 8 years ago

Hi,

I have a question regarding the return values of the operations - more specifically, in what cases is a MultiPolygon (since the API docs specify <Geometry> as either Polygon or MultiPolygon) returned?

Take this example looking at the difference operation:

diff

The big polygon is split in two by the smaller polygon - so far so good. However, the return value is one Polygon with a hole (with the hole not being within the outline, which makes not that much sense):

returnValue = [
  [[0,0],[2,0],[2,1],[0,1],[0,0]], //Left polygon (first array = outline)
  [[3,0],[4,0],[4,1],[3,1],[3,0]]  //Right polygon (all subsequent arrays = holes)
]

I did expect a MultiPolygon return value, since the two polygons aren't connected to each other anymore, so a value like:

returnValue = [
  //First polygon with only an outline
  [
    [[0,0],[2,0],[2,1],[0,1],[0,0]] 
  ],
  //Second polygon with only an outline
  [
    [[3,0],[4,0],[4,1],[3,1],[3,0]]
  ]
]

Is this a known limitation / a bug / should that be like this?

Thanks :smile: !

fresidue commented 8 years ago

I'm kind of guessing here, but i think it is meant to be like this, and that the algorithm does not explicitly distinguish between holes and multiple polygons. The return value is simply an array of linear rings and it's up to the user to interpret the meaning of them. In your case, one can in effect think of the return value as an invalid polygon where the second LR is a hole which lies outside the first LR...

I agree, a more structured response would be nice, but may not be within the purview of this module

w8r commented 8 years ago

@fresidue, exactly, thanks for the explanation. We will be looking into this issue a bit later, cause actually, the algorithm is capable of maintaining, so to say, the geometry depth, but now it's buggy, to say the least.

DenisCarriere commented 7 years ago

👍 +1 @AtiX comments, noticed the same issue.

and it's up to the user to interpret the meaning of them.

@fresidue I disagree, the user can't distinguish the inner & outers, especially if the outer is not in the first position and the inners & outers are mixed (also trying to achieve this with minimal performance loss).

Example If you had your source polygon with an inner, and the target did a difference operation, then you would receive 3 polygons (linestrings) in no particular order.

https://gist.github.com/5a8496e2ffc77dfe47c7ffbed8a8fb59

image

The results "look" good as a single Polygon (Geometry is malformed), however there's not an easy way to convert it to a MultiPolygon or a collection of individual Polygons.

https://gist.github.com/625b82196e437560e48e4ae7acb51e20

image

CC: https://github.com/Turfjs/turf/pull/644

w8r commented 7 years ago

The changes are released in 0.2.0

DenisCarriere commented 7 years ago

🎉 Woot!