perliedman / geojson-path-finder

Find shortest path through a network of GeoJSON
https://www.liedman.net/geojson-path-finder/
ISC License
300 stars 86 forks source link

Add compact option which can be used to prevent graph compacting #61

Closed nickw1 closed 4 years ago

nickw1 commented 4 years ago

This relates to issue #27.

Am using geojson-path-finder in my OpenTrailView project (opentrailview.org) which is a StreetView-like application for hiking trails. Panoramas are auto-linked using underlying OpenStreetMap data in the form of GeoJSON, and geojson-path-finder is used to find routes between adjacent panoramas to allow such linking.

The distance between panoramas is typically small (maybe between 10 and 100 metres) and only a small area of GeoJSON is downloaded to calculate the routing. I found that with unmodified geojson-path-finder, the graph would be compacted leading to incorrect routing between adjacent panoramas. Modifiying the precision parameter didn't appear to solve the problem.

By making this small modification (allowing compacting to be turned off in cases like this where the graph is very small by setting the 'compact' option to false), the problem disappeared.

Usage is simple:

var pathFinder = new PathFinder(json, { compact: false } );

Obviously I can see why compacting is desirable in larger graphs, but in this specific use case, I need to route over a very small graph and keeping the graph uncompacted is vital to obtain the correct results.

perliedman commented 4 years ago

Nice! Although I would be even happier with a fix to whatever the issue is with the compactor, this seems reasonable.

I made some small changes so that any falsy value for options.compact is accepted, hope it looks alright to you!

nickw1 commented 4 years ago

OK thanks, yes fine with your change! Will try and look into the compactor issues if I have the time, but fairly busy at the moment so I thought I'd just submit this small fix.