Turfjs / turf

A modular geospatial engine written in JavaScript and TypeScript
https://turfjs.org/
MIT License
9.39k stars 944 forks source link

booleanEqual() sometimes outputs false by internal cleanCoords() #2406

Open naoak opened 1 year ago

naoak commented 1 year ago

Both of these polygons have the same shape and are in the same location. The length of the coordinates is also the same. The only difference is the order of the coordinates.

A:

{
  "type": "Feature", 
  "properties": {},
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [1, 3], [3, 3], [3, 1], [3, -3], [-3, -3], [-3, 3], [1, 3]
      ]
    ]
  }
}

B:

{
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [-3, -3], [-3, 3], [1, 3], [3, 3], [3, 1], [3, -3], [-3, -3]
      ]
    ]
  }
}

cleanCoords() drops [3,1] from A, whereas it does [1,3] and [3,1] from B. Therefore, the lengths of the coordinates are different and the result of booleanEqual is false. In polygon A, [1,3] is at the starting point, so it seems to make that difference.

smallsaucepan commented 5 days ago

This is an interesting case. cleanCoords effectively refuses to clean a redundant point if it's the start or end point of a polygon.

Taking example A above as polygon a-b-c-d-e-f-a and running cleanCoords:

clean-coords

Giving us a-b-d-e-f-a. Technically though, that polygon is really only defined by b-d-e-f-b, so a is still a redundant point.

On paper I would say this is a bug in cleanCoords. There shouldn't be any protected status offered to the start or end point of a polygon.