Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: turf/difference fails on a shared edge #1393

Closed
PizzaBrandon opened this issue May 22, 2018 · 5 comments
Closed

Bug: turf/difference fails on a shared edge #1393

PizzaBrandon opened this issue May 22, 2018 · 5 comments
Assignees
Milestone

Comments

@PizzaBrandon
Copy link
Contributor

PizzaBrandon commented May 22, 2018

It appears difference is failing due to a bug in a supporting library: w8r/martinez#35

Here is a runnable test that exhibits the behavior

const difference = require('@turf/difference');
tape('Difference failure', (t) => {
  const polygon1 = {
    'type': 'Polygon',
    'coordinates': [
      [[ 10, 10 ], [ 80, 10 ], [ 80, 80 ], [ 10, 80 ], [ 10, 10 ]]
    ]
  };

  const polygon2 = {
    'type': 'Polygon',
    'coordinates': [
      [[ 20, 10 ], [ 30, 10 ], [ 30, 80 ], [ 20, 80 ], [ 20, 10 ]]
    ]
  };

  const result = difference(polygon1, polygon2);
});
// Expected result to be a multi-polygon
// Actual fails with exception `Each LinearRing of a Polygon must have 4 or more Positions.`

This failure happens since martinez returns a malformed polygon when two edges overlap. It returns a "good" difference and as well as a two- or three-point polygon. Could this be detected and handled prior to handing off to helper.polygon, which is where the exception occurs?

(Hoping creating a tracking issue between the two libraries helps find a common solution faster.)

@PizzaBrandon
Copy link
Contributor Author

Additional note, there is a larger bug in martinez where it returns an incorrect result for the above test, but at least need to make sure difference doesn't exception.

@w8r
Copy link

w8r commented May 27, 2018

linking the issue here w8r/martinez#81

@plepe
Copy link
Contributor

plepe commented Jun 17, 2018

Happens to me as well, here even shorter code:

const difference = require('@turf/difference')

let result = turf.difference(
  {"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[16.2,48.0],[16.6,48.0],[16.6,48.1],[16.2,48.1],[16.2,48.0]]]}},
  {"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[16.4,48.0],[16.6,48.0],[16.6,48.1],[16.4,48.1],[16.4,48.0]]]}}
)
console.log(JSON.stringify(result))

@HansonYip
Copy link

HansonYip commented Jan 5, 2019

I also found a similar problem about shared edge.

var p1 = [[[0,0],[2,2],[4,2],[2,0],[0,0]]];
var p2 = [[[1,0],[2,1],[3,1],[2,0],[1,0]]];
var differenced = difference(p1, p2);

But the result is

[[[[0,0],[1,0],[2,0],[2,1],[3,1],[4,2],[2,2],[0,0]]]]

image

@rowanwins
Copy link
Member

Resolved in v6.2.0 onwards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants