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.intersect raises "uncaught exception: [object Object]" on valid geometries #820

Closed
2 tasks done
vicvolk opened this issue Jun 28, 2017 · 12 comments
Closed
2 tasks done
Assignees
Milestone

Comments

@vicvolk
Copy link

vicvolk commented Jun 28, 2017

To-Do

Added by @DenisCarriere

  • Returning a Feature<null>instead of returning undefined.
  • Adding @turf/truncate to inputs.

To reproduce this error message (which I believe should not be raised), just run this code:

turf.intersect({"type": "Feature", "geometry":{"type":"Polygon","coordinates": [[[4869493.170354112,7598816.20365591],[4869512.279611183,7595796.941038646],[4873926.5179946525,7596007.14286643],[4873716.316166868,7598013.614858915],[4871671.62566024,7598758.875884696],[4869493.170354112,7598816.20365591]]]}},{"type": "Feature", "geometry":{"type":"Polygon","coordinates":[[[4894725.4051467,7624972.8484957],[4894725.4051469,7624972.8530041],[4894614.9242472,7624825.5926316],[4894398.2837739,7624987.6762962],[4894450.8149275,7625056.9040792],[4894668.7026646,7625096.7545718],[4894671.4728847,7625081.2756834],[4894674.9876194,7625066.9804281],[4894678.7586548,7625055.2998143],[4894682.9867902,7625043.5046946],[4894688.1291364,7625031.5950619],[4894694.8416008,7625017.8373286],[4894699.3280398,7625009.9516021],[4894705.0417681,7625000.4468024],[4894710.069852,7624992.8887655],[4894715.3264873,7624985.6742709],[4894721.1517534,7624977.9994939],[4894725.4051467,7624972.8484957]]]}})

The whole problem is with the second feature and I'm not sure what may be wrong with it.

@vicvolk vicvolk changed the title turf.intersect raises "uncaught exception: [object Object]" turf.intersect raises "uncaught exception: [object Object]" on valid geometries Jun 28, 2017
@DenisCarriere
Copy link
Member

DenisCarriere commented Jun 28, 2017

Your coordinates should be in decimal degrees (WGS84 projection).

Please don't hesitate to reopen this issue if this is not resolved after the coordinate conversion is done.

@vicvolk
Copy link
Author

vicvolk commented Jun 28, 2017

Thanks! Did not know that. I will check it in a minute

@DenisCarriere
Copy link
Member

👍 I'm assuming that jsts doesn't play well with other projections, especially if the crs is not defined. You can "easily" use QGIS or any other tool to convert your UTM or Mercator based data into WGS84.

@vicvolk
Copy link
Author

vicvolk commented Jun 28, 2017

I managed to reproduce this error with WGS84 coordinates. Here is a new test case:

turf.intersect({"type": "Feature", "geometry":{"type":"Polygon","coordinates": [[[43.741513133300735,56.20222135938059],[43.74237144018551,56.18856400988486],[43.77756202246089,56.187608768581725],[43.77927863623042,56.19639609165836],[43.763142466796836,56.20212587032091],[43.741513133300735,56.20222135938059]]]}},{"type": "Feature", "geometry":{"type":"Polygon","coordinates":[[[43.970066430113974,56.332893840607994],[43.970066430115764,56.33289386305967],[43.96907396330773,56.33216050512385],[43.96712784882449,56.3329676824026],[43.9675997442062,56.33331243182769],[43.96955706305079,56.33351088302814],[43.96958194836136,56.33343379993835],[43.969613521760365,56.3333626110674],[43.96964739754773,56.3333044427381],[43.96968537953426,56.3332457040905],[43.96973157401614,56.333186395086614],[43.969791873109784,56.333117882577056],[43.96983217547703,56.3330786121781],[43.969883502771644,56.3330312788475],[43.969928670817815,56.332993640236595],[43.96997589197615,56.33295771241299],[43.97002822123187,56.332919492368774],[43.970066430113974,56.332893840607994]]]}})

@vicvolk
Copy link
Author

vicvolk commented Jun 28, 2017

I have a long list of real world features and turf.intersect works perfect, until it hits this second feature. And it does not really matter what the first feature is, it may be absolutely arbitrary.

@vicvolk
Copy link
Author

vicvolk commented Jun 28, 2017

It seems like I have no rights to reopen this issue.

@DenisCarriere
Copy link
Member

🤦‍♂️ Ops.. strange you can't reopen (my bad).

@DenisCarriere DenisCarriere reopened this Jun 28, 2017
@DenisCarriere
Copy link
Member

Oh I get it... looks like the intersection is "empty".

http://geojson.io/#id=gist:anonymous/1c584a17eac2f8f16c2cd4b6fa3fe664&map=11/56.2606/43.8558

I'm also getting a jsts related error:

Error
    at sn.l (/Users/mac/Github/turf/packages/turf-intersect/node_modules/jsts/dist/jsts.min.js:6:424)
    at new sn (/Users/mac/Github/turf/packages/turf-intersect/node_modules/jsts/dist/jsts.min.js:6:17532)
    at un.checkValid (/Users/mac/Github/turf/packages/turf-intersect/node_modules/jsts/dist/jsts.min.js:12:7761)
    at ln.checkValid (/Users/mac/Github/turf/packages/turf-intersect/node_modules/jsts/dist/jsts.min.js:12:8294)
    at Function.ln.checkValid (/Users/mac/Github/turf/packages/turf-intersect/node_modules/jsts/dist/jsts.min.js:12:8556)
    at ii.computeOverlay (/Users/mac/Github/turf/packages/turf-intersect/node_modules/jsts/dist/jsts.min.js:14:4)
    at ii.getResultGeometry (/Users/mac/Github/turf/packages/turf-intersect/node_modules/jsts/dist/jsts.min.js:13:31486)
    at Function.ii.overlayOp (/Users/mac/Github/turf/packages/turf-intersect/node_modules/jsts/dist/jsts.min.js:14:2252)
    at si.getResultGeometry (/Users/mac/Github/turf/packages/turf-intersect/node_modules/jsts/dist/jsts.min.js:14:5597)
    at Function.si.overlayOp (/Users/mac/Github/turf/packages/turf-intersect/node_modules/jsts/dist/jsts.min.js:14:5935)

@stebogit
Copy link
Collaborator

stebogit commented Aug 3, 2017

@vicvolk I read on the jsts docs that

  • In some cases you might get a TopologyException thrown as an Error. This is expected if a calculation fails due to precision issues. To resolve this issue try reducing precision in the input and at the same time make sure the input is valid as defined by the OGC Simple Features specification. To reduce precision GeometryPrecisionReducer can be used.

Don't know exactly how to use GeometryPrecisionReducer, but using @turf/truncate on your polygons before applying @turf/intersect seems to solve the issue.
Please note that GeoJSON specs require coordinates with max 6 decimals anyway.

turf.intersect({"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":
[[[43.741513,56.202221],[43.742371,56.188564],[43.777562,56.187608],
[43.779278,56.196396],[43.763142,56.202125],[43.741513,56.202221]]]}},
{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":
[[[43.970066,56.332893],[43.970066,56.332893],[43.969073,56.332160],
[43.967127,56.332967],[43.967599,56.333312],[43.969557,56.333510],
[43.969581,56.333433],[43.969613,56.333362],[43.969647,56.333304],
[43.969685,56.333245],[43.969731,56.333186],[43.969791,56.333117],
[43.969832,56.333078],[43.969883,56.333031],[43.969928,56.332993],
[43.969975,56.332957],[43.970028,56.332919],[43.970066,56.332893]]]}});
// undefined

@DenisCarriere should we truncate the inputs of @turf/intersect?
I noticed also that intersect returns undefined if there is no intersection between polygons; this means the test is in practice skipped, so we don't have an actual check on the output.
Should we modify the module's output returning null instead? I find it more appropriate because it's an actual value representing "nothing", and also it's a valid JSON so we could have null in the geojson output file and perform the test also on empty intersections.

@DenisCarriere
Copy link
Member

DenisCarriere commented Aug 3, 2017

With our recent @turf/helpers null geometry fix, we can definitely use Feature<null> for this module.

  • 👍 Returning a Feature<null>instead of returning undefined.
  • 👍 Adding @turf/truncate to inputs.

🤔 Don't know if we should apply @turf/truncate by default, might be best to give that option to the user if he wants to apply truncate or not.

@stebogit
Copy link
Collaborator

stebogit commented Aug 3, 2017

What I meant was applying it to the inputs (with no mutation), in order to avoid this issue with jsts. After all, any additional decimal after the 6th is irrelevant here, no?
I already have the fix btw, I'll commit later this evening or tomorrow

@DenisCarriere
Copy link
Member

Is this error happening all the time?

Yes I know that we would be adding it to the inputs, but wouldn't that slow things down if it's not needed.

jsts is a mess, so I'm 👍 if we included the truncate fix directly in the module, that way we prevent those 5% errors that would normally occur from coordinate precision.

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

3 participants