Turfjs / turf

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

booleanIntersects between v6 and v7 return different values #2700

Open aaronkyle opened 3 months ago

aaronkyle commented 3 months ago

Please provide the following when reporting an issue:

I was using Turf@7 when I discovered this issue. I can verify that it doesn't happen with Turf@6.

Here are two Observable notebooks with the same code, but different Turf.js versions:

With Turf@6, my bounding box allows me to filter a source data file by objects correctly.

Screenshot from 2024-08-12 20-25-15

In Turf@7, I am seeing returns from the opposite side of the globe.

Screenshot from 2024-08-12 20-24-37

smallsaucepan commented 3 months ago

Hi @aaronkyle. Thanks for reporting this. Can't actually view your examples unless I log in, which I'd rather not have to :) Can you post to a https://jsfiddle.net instead?

Can you also please attach the Geojson data going in to and returned from booleanIntersects? And if not a JSFiddle, a snippet of the surrounding code as well.

aaronkyle commented 3 months ago

Hi @smallsaucepan . Thank you for looking into this. I apologize that I missed making one of those notebooks public. I fixed them so that you can review without logging in. The source .geojson is 40MB, so rather large to attach, however it is included as a file attachment on the notebook. I'll also link to it from a GitHub repo.

To decrease all the clutter from the notebook where I discovered the issue, I have created a minimal notebook example here: https://observablehq.com/d/ae03f49d4dd9b456

In this example, I load both versions of Turf (6 and 7) and use them in otherwise identical functions, where they return different outputs, namely:

function getVisibleFeatures_turf6(geojson, boundingBox) {
  if (!geojson || !geojson.features || !boundingBox) {
    throw new Error('Invalid arguments');
  }

  // Create a turf bounding box polygon
  const bboxPolygon = turf6.bboxPolygon([
    boundingBox.west,
    boundingBox.south,
    boundingBox.east,
    boundingBox.north
  ]);

  // Filter features based on whether they intersect with the bounding box polygon
  const visibleFeatures = geojson.features.filter(feature => {
    // Check if the feature intersects with the bounding box polygon
    return turf6.booleanIntersects(bboxPolygon, feature);
  });

  // Return the filtered GeoJSON object
  return {
    type: 'FeatureCollection',
    features: visibleFeatures
  };
}

(repeats for Turf7)

These outputs are attached (and can also be pulled from the notebook). visibleFeatures_turf7.json visibleFeatures_turf6.json

Here's the link to the source .geojson: https://github.com/aaronkyle/kba-table-view/blob/master/src/kba-2022-10-poly-simp.geojson

Hope this helps!

aaronkyle commented 3 months ago

Looking back at the returned features, the geometries do indeed have negative values, so my original assumption about what is happening to cause these differences is incorrect. I'll update the title of this issue.