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

turf.dissolve erroring with oddly shaped polygons #2420

Open
charlierudolph opened this issue Mar 31, 2023 · 3 comments · May be fixed by #2729
Open

turf.dissolve erroring with oddly shaped polygons #2420

charlierudolph opened this issue Mar 31, 2023 · 3 comments · May be fixed by #2729

Comments

@charlierudolph
Copy link

charlierudolph commented Mar 31, 2023

Currently hitting the following error when using turf.dissolve

Error: Unable to complete output ring starting at [54.52907798654607, -2.5983186383573154]. Last matching segment found ends at [54.61818656347785, -2.5731465650016876].
    at Function.factory (/Users/charlesrudolph/Code/test/node_modules/polygon-clipping/dist/polygon-clipping.cjs.js:1252:21)
    at Operation.run (/Users/charlesrudolph/Code/test/node_modules/polygon-clipping/dist/polygon-clipping.cjs.js:1776:30)
    at union (/Users/charlesrudolph/Code/test/node_modules/polygon-clipping/dist/polygon-clipping.cjs.js:1792:20)
    at Module.dissolve (file:///Users/charlesrudolph/Code/test/node_modules/@turf/dissolve/dist/es/index.js:42:31)
    at file:///Users/charlesrudolph/Code/test/index.js:9:18
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

Code to reproduce:

import {cellToBoundary} from "h3-js"
import * as turf from "@turf/turf"

const h3Indexes = ['857bb583fffffff', '857bb58bfffffff']
const polygons = h3Indexes.map(x => cellToBoundary(x, true))
const turfPolygons = polygons.map(x => turf.polygon([x]))
turf.dissolve(turf.featureCollection(turfPolygons)) // <-- throws

With dependencies (Node 18.14):

  • @turf/turf: 6.5.0
  • h3-js: 4.1.0

For reference the turfPolygons value is:

[
  {
    "type": "Feature",
    "properties": {},
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [54.674471560746106, -2.669669459403366],
          [54.66827243056676, -2.590808846502601]
          [54.61818656347785, -2.5731465650016876],
          [54.59479733372542, -2.5582148982951005],
          [54.52907798654607, -2.5983186383573154],
          [54.533564730328536, -2.68329182348515],
          [54.58040270977906, -2.713147336590615],
          [54.60545643615633, -2.7220126907386035],
          [54.674471560746106, -2.669669459403366]
        ]
      ]
    }
  },
  {
    "type": "Feature",
    "properties": {},
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [54.59029423717366, -2.4733591205361],
          [54.59479733372542, -2.5582148982951005],
          [54.61818656347788, -2.573146565001703],
          [54.66827243056676, -2.590808846502601],
          [54.73723247700517, -2.538498282632361],
          [54.730995571183335, -2.4597687064449976],
          [54.655899262660675, -2.433377660227877],
          [54.59029423717366, -2.4733591205361]
        ]
      ]
    }
  }
]

And this is what the indicies look like in kepler.gl:

plotted

@twelch
Copy link
Collaborator

twelch commented Apr 13, 2023

You're going to find open issues reporting this already. It's likely a floating point precision issue with the underlying clipping library.

What happens if you use turf-truncate to reduce the precision?

@charlierudolph
Copy link
Author

Ended up solving this a different way (using h3-js cellsToMultiPolygon for different resolutions and and then turf/union to join those) and thus not hitting this anymore

@smallsaucepan
Copy link
Member

Is there anything for us to address here @twelch? Is there an existing accuracy issue we could link to before closing this?

@cmurphy23 cmurphy23 linked a pull request Oct 9, 2024 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants