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

[intersect function] it seems bugged in a strange way #1195

Closed
RaynalHugo opened this issue Dec 22, 2017 · 3 comments
Closed

[intersect function] it seems bugged in a strange way #1195

RaynalHugo opened this issue Dec 22, 2017 · 3 comments

Comments

@RaynalHugo
Copy link

Hi,
@crubier and I are trying to use your library but we encountered a very strange behavior from the function intersect.

What we did :

  • create a LineString named : Line1. Here is the gist.
  • create a buffer around this line with the buffer function. We named this BufferedLine. Here is the gist.
  • create a polygon which contains the BufferedLine. We called it Polygon1. Here is the gist.
  • Here is a gist of the situation.
  • Now, we called the function intersect(BufferedLine, Polygon1). It should return the shape of BufferedLine as it is contained in Polygon1. However, it returns null.

We created a test with jest to show this:
Bug.js.zip

/* @flow */

import { lineString, interpolate, buffer, intersect } from "@turf/turf";

export const Line1 = {
  type: "Feature",
  properties: {},
  geometry: {
    type: "LineString",
    coordinates: [
      [-1.520082950592041, 47.28430930649756],
      [-1.5163064002990723, 47.2820530712768]
    ]
  }
};

const BufferedLine = buffer(Line1, 10, { units: "meters", steps: 10 });

const Polygon1 = {
  type: "Feature",
  properties: {},
  geometry: {
    type: "Polygon",
    coordinates: [
      [
        [-1.520082950592041, 47.28469504030638],
        [-1.5205228328704834, 47.28418557996312],
        [-1.5160918235778809, 47.281660039816714],
        [-1.5158557891845703, 47.28214041120473],
        [-1.520082950592041, 47.28469504030638]
      ]
    ]
  }
};

const hardcodedBufferedLine = {
  geometry: {
    coordinates: [
      [
        [-1.5162470291855608, 47.28209880486835],
        [-1.5162350179821558, 47.28209006851059],
        [-1.5162257499611578, 47.282079910368694],
        [-1.5162195812874169, 47.28206872081364],
        [-1.5162167490196052, 47.282056929853255],
        [-1.5162173620001864, 47.28204499060729],
        [-1.5162213966726608, 47.282033361894314],
        [-1.5162286979868271, 47.28202249059961],
        [-1.5162389853572777, 47.282012794501505],
        [-1.5162518634461344, 47.28200464621637],
        [-1.516266837355665, 47.281998358878994],
        [-1.5162833316469233, 47.28199417410893],
        [-1.5163007124535464, 47.28199225272506],
        [-1.5163183118408838, 47.281992668565344],
        [-1.5163354534743494, 47.281995405649184],
        [-1.5163514786105825, 47.28200035879167],
        [-1.5163657714125842, 47.28200733764573],
        [-1.520142321705553, 47.28426357481694],
        [-1.520154332908958, 47.28427231081577],
        [-1.5201636009299557, 47.2842824685367],
        [-1.5201697696036964, 47.284293657623515],
        [-1.5201726018715083, 47.284305448085384],
        [-1.5201719888909269, 47.28431738682121],
        [-1.5201679542184527, 47.284329015032135],
        [-1.5201606529042861, 47.284339885852866],
        [-1.520150365533836, 47.28434958152446],
        [-1.5201374874449793, 47.28435772944842],
        [-1.5201225135354488, 47.28436401650539],
        [-1.52010601924419, 47.284368201088],
        [-1.520088638437567, 47.28437012238558],
        [-1.5200710390502294, 47.284369706564],
        [-1.5200538974167639, 47.284366969602985],
        [-1.520037872280531, 47.284362016682074],
        [-1.520023579478529, 47.28435503813864],
        [-1.5162470291855608, 47.28209880486835]
      ]
    ],
    type: "Polygon"
  },
  properties: {},
  type: "Feature"
};

const hardcodedFixedBufferedLine = {
  geometry: {
    coordinates: [
      [
        [-1.5162470291855608, 47.28209880486835],
        [-1.5162350179821558, 47.28209006851059],
        [-1.5162257499611578, 47.282079910368694],
        [-1.5162195812874169, 47.28206872081364],
        [-1.5162167490196052, 47.282056929853255],
        [-1.5162173620001864, 47.28204499060729],
        [-1.5162213966726608, 47.282033361894314],
        [-1.5162286979868271, 47.28202249059961],
        [-1.5162389853572777, 47.282012794501505],
        [-1.5162518634461344, 47.28200464621637],
        [-1.516266837355665, 47.281998358878994],
        [-1.5162833316469233, 47.28199417410893],
        [-1.5163007124535464, 47.28199225272506],
        [-1.5163183118408838, 47.281992668565344],
        [-1.5163354534743494, 47.281995405649184],
        [-1.5163514786105825, 47.28200035879167],
        [-1.5163657714125842, 47.28200733764573],
        [-1.520142321705553, 47.28426357481694],
        [-1.520154332908958, 47.28427231081577],
        [-1.5201636009299557, 47.2842824685367],
        [-1.5201697696036964, 47.284293657623515],
        [-1.5201726018715083, 47.284305448085384],
        [-1.5201719888909269, 47.28431738682121],
        [-1.5201679542184527, 47.284329015032135],
        [-1.5201606529042861, 47.284339885852866],
        [-1.520150365533836, 47.28434958152446],
        [-1.5201374874449793, 47.28435772944842],
        [-1.5201225135354488, 47.28436401650539],
        [-1.52010601924419, 47.284368201088],
        [-1.520088638437567, 47.28437012238558],
        [-1.5200710390502294, 47.284369706564],
        [-1.5200538974167639, 47.284366969602985],
        //[-1.520037872280531, 47.284362016682074], //This is the magic line :O
        [-1.520023579478529, 47.28435503813864],
        [-1.5162470291855608, 47.28209880486835]
      ]
    ],
    type: "Polygon"
  },
  properties: {},
  type: "Feature"
};

describe("Turf", () => {
  // Testing if buffer of line is equal to my hardcoded BufferedLine ==> true
  test("hardcoded buffered line equal buffer of line", () => {
    expect(BufferedLine).toEqual(hardcodedBufferedLine);
  });

  //Testing if intersection of polygon1 and the BufferedLine is not null ==> false, it shouldn't
  test("intersect polygon with hardcoded buffered line", () => {
    expect(intersect(hardcodedBufferedLine, Polygon1)).not.toBeFalsy();
  });

  //Testing if intersection of polygon1 and the fixed buffered line is not null ==> true, wtf ?!
  test("intersect polygon with hardcoded fixed buffered line work", () => {
    expect(intersect(hardcodedFixedBufferedLine, Polygon1)).not.toBeFalsy();
  });
});

We use some hard coded data to simplify the problem.

  • The first test show that the hard coded version of the BufferedLine was identical to the one generated with the buffer function. It succeeds.
  • The second test shows that intersect(hardcodedBufferedLine, Polygon1) returns null. It shouldn't
  • The third test is quite funny : we found that commenting one line of code solved the problem. In hardcodedFixedBufferedLine we commented the 3rd line from the end of the field coordinates in geometry. And now, intersect(hardcodedFixedBufferedLine, Polygon1) is correctly returning the shape of the BufferedLine. We have absolutely no idea why.

We had some fun while testing this, however, the "solution" we found is not very reproducible :P
If someone has a better solution, we are interested.

@DenisCarriere
Copy link
Member

@Hugosama Thanks for providing a very detailed test case.

At the moment @turf/intersect is using JSTS which we are going to be phasing out in favor of @w8r's martinez.

As a solution, could you attempt the same comparisons using martinez and see if you get the same expected results or null values.

We are not going to be providing a fix for jsts, so this will most likely not be resolved anytime soon in any of the 5.x releases. This will most likely be resolved in the next major release 6.x when we replace jsts with martinez.

CC: @rowanwins & @w8r has been working hard to improve martinez for us to import it in TurfJS.

@rowanwins
Copy link
Member

Just a note @Hugosama that you'll need to look at this branch of martinez, we haven't quite managed to merge it back into master yet

@tmcw
Copy link
Collaborator

tmcw commented Jun 25, 2018

Closing; #88 is the tracking issue for JSTS.

@tmcw tmcw closed this as completed Jun 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants