Turf: Union call fails with complex polygon and line string intersection attempt

Created on 1 Apr 2017  路  5Comments  路  Source: Turfjs/turf

I have found what appears to be an issue with the union call crashing. The result is basically the node script I am running crashes and prints 'debug'??. I have create a gist that replicates the issue https://gist.github.com/rheh/397308b280775ad70dedd140a27fd7ce.

The command and output I get follows:

ray@Pluto:~/badfir$ node bad.js 

/home/ray/badfir/node_modules/turf-intersect/node_modules/jsts/dist/jsts.min.js:14
this.resultGeom=this.computeGeometry(this.resultPointList,this.resultLineList,this.resultPolyList,t)},labelIncompleteNode:function(t,e){var n=this.ptLocator.locate(t.getCoordinate(),this.arg[e].getGeometry());t.getLabel().setLocation(e,n)},copyPoints:function(t){for(var e=this.arg[t].getNodeIterator();e.hasNext();){var n=e.next(),i=this.graph.addNode(n.getCoordinate());i.setLabel(t,n.getLabel().getLocation(t))}},findResultAreaEdges:function(t){for(var e=this.graph.getEdgeEnds().iterator();e.hasNext();){var n=e.next(),i=n.getLabel();i.isArea()&&!n.isInteriorAreaEdge()&&ii.isResultOfOp(i.getLocation(0,cn.RIGHT),i.getLocation(1,cn.RIGHT),t)&&n.setInResult(!0)}},computeLabelsFromDepths:function(){for(var t=this.edgeList.iterator();t.hasNext();){var e=t.next(),n=e.getLabel(),i=e.getDepth();if(!i.isNull()){i.normalize();for(var r=0;2>r;r++)n.isNull(r)||!n.isArea()||i.isNull(r)||(0===i.getDelta(r)?n.toLine(r):(f.isTrue(!i.isNull(r,c
[object Object]

A work around I've used is to call turf.simplify on the polygon before passing this to union. Although, I would like to understand what the issue is so I can avoid it. Is my data malformed? I suspect not as the sample plots the various geojson tools online, such as google-developers.appspot.com/maps/documentation/utils/geojson/. The disadvantage of the simplify call is that my intersection may miss as the shape become 'flatter'

To see the script working with the simplify in switch the variables on line 4489.

My node version is v4.2.6.
The version of turf is 3.0.14.

question

All 5 comments

Hi @rheh

Unfortuantly the union module is a little unstable sometimes depending on the input geometries, we're currently trying to remove the dependency on jsts which is the source for the union operation.

Odds are if simpliying works then your vertices are too dense for jsts to work for some reason... It might also be worth checking if your polygons contain self-intersections using turf/kinks

Hope that helps.

Cheers
Rowan

@rheh Have you tried using @turf/line-intersect instead? This might solve your issue.

var lineIntersect = require('@turf/line-intersect')
var intersects =  turf.lineIntersect(linestring, poly)
//=poly
//=linestring
//=intersects (points)

image

@rowanwins the polygon does indeed have 'kinks'. One assumes this means the data is malformed and the cause of the crash?

image

@DenisCarriere thanks, that works just fine with the complex polygon - even without the simplify,

Closing this issue in favor of @turf/line-intersect solution.

Was this page helpful?
0 / 5 - 0 ratings