Skip to content

turf.difference adds unnecessary points to coordinates when one polygon is on the edge of another polygon. #2928

@dmytro-y-gaf

Description

@dmytro-y-gaf

Hello team! Thank you for your hard work!

There is an issue adding extra point to result of turn.difference when one polygon is on the edge of another polygon.

Trying to find the difference for these 2 shapes:

Image

Actual result:

Image

Expected result:

Image

Code to reproduce:

const initialArea = turf.polygon([
  [
    [-72.696428, 41.797527],
    [-72.712274, 41.785347],
    [-72.66441, 41.751403],
    [-72.635534, 41.778325],
    [-72.696428, 41.797527]
  ]
]);
const subtractArea = turf.polygon([
  [
    [
      -72.700578,
      41.794337
    ],
    [
      -72.692501,
      41.791197
    ],
    [
      -72.699563,
      41.785815
    ],
    [
      -72.692501,
      41.782889
    ],
    [
      -72.681386,
      41.792784
    ],
    [
      -72.696428,
      41.797527
    ],
    [
      -72.700578,
      41.794337
    ]
  ]
]);
const featureCollection = turf.featureCollection([
  initialArea,
  subtractArea,
]);
const difference = turf.difference(featureCollection);
console.log(">>>difference", JSON.stringify(difference));

Actual result of difference:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -72.712274,
              41.785347
            ],
            [
              -72.66441,
              41.751403
            ],
            [
              -72.635534,
              41.778325
            ],
            [
              -72.68138622124394,
              41.79278380304014
            ],
            [
              -72.692501,
              41.782889
            ],
            [
              -72.699563,
              41.785815
            ],
            [
              -72.692501,
              41.791197
            ],
            [
              -72.700578,
              41.794337
            ],
            [
              -72.696428,
              41.797527
            ],
            [
              -72.712274,
              41.785347
            ]
          ]
        ]
      }
    }
  ]
}

Expected result

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -72.712274,
              41.785347
            ],
            [
              -72.66441,
              41.751403
            ],
            [
              -72.635534,
              41.778325
            ],
            [
              -72.68138622124394,
              41.79278380304014
            ],
            [
              -72.692501,
              41.782889
            ],
            [
              -72.699563,
              41.785815
            ],
            [
              -72.692501,
              41.791197
            ],
            [
              -72.700578,
              41.794337
            ],
            // Wrong one.
            // [
            //   -72.696428,
            //   41.797527
            // ],
            [
              -72.712274,
              41.785347
            ]
          ]
        ]
      }
    }
  ]
}

When doing it multiple times it may have more artifacts:

Image

Link to playground: https://jsfiddle.net/9d1baeno/1/

Looking forward for your reply!

Best regards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions