Turfjs / turf

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

ShortestPath & geoJSON #1283

Open ngoluan opened 6 years ago

ngoluan commented 6 years ago

Do you have any tips on how to use this with a land geojson? I'm trying to plot a course between two points and avoid land masses, which is represented by a GeometryCollection of Polygons. But, it throws me an invalid obstacles error probably because the geometry is a multipolygon. So, I try to flatten it using turf.flatten but now I'm stuck in a massive loop.

image

You can find the geojson file at: http://www.luanngo.ca/apps/game/res/land_sm.json Thanks!

    var start = [-11.777343749999979,40.44694705960049];
    var end = [62.22656250000001,19.97334878611059];
    $.get("http://www.luanngo.ca/apps/game/res/land_sm.json").done(function(data){
        data=turf.flatten(data);
        var options = {
            obstacles: data
        };
        var path = turf.shortestPath(start, end, options);

    });
rowanwins commented 6 years ago

Hi @ngoluan

Sorry for the slow reply. Unfortunately the shortest path module needs a bit of love at the moment, I've been working on a complete rewrite but it's taking a little while.

I would've thought flattening the data would've been sufficient but apparently not, the only other thing I can think in the short term is try bumping up the resolution option to 1000 and see if that makes a difference.

If you need additional help perhaps throw together a jsfiddle or runkit snippet, it just saves in the amount of time we have to spend time getting things setup to test :)

Thanks

ngoluan commented 6 years ago

Thanks for the response! I'll give it a try and will come back if I have any issues.

ngoluan commented 6 years ago

Hi @rowanwins

I'm one step closer, thanks to your tip. But, when I try it, turf.shortestPath produced a straight line, rather one that avoids obstacles.

Here's the jsfiddle. Do you have any tips?

https://jsfiddle.net/luan_ngo/s42rsog0/

viktorfa commented 1 year ago

Hi @rowanwins

I'm one step closer, thanks to your tip. But, when I try it, turf.shortestPath produced a straight line, rather one that avoids obstacles.

Here's the jsfiddle. Do you have any tips?

https://jsfiddle.net/luan_ngo/s42rsog0/

You have to set a lower resolution. For the example you provided, the resolution has to be 1 for it to make a sensible path.

It may seem like maps have different resolutions, as a resolution at about 40 was enough for me on another map I tried. Note that a much lower resolution means that the path finding algorithm will be much slower. (n² I think)

If you want a curved line, you can use bezierSpline on the path.

https://jsfiddle.net/mwrhzpcx/14/

rowanwins commented 1 year ago

Just dropping a note that I did eventually put together this visibility-graph module which is probably a better option in alot of cases