perliedman / geojson-path-finder

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

findPath method returning null #28

Closed egemenokten closed 6 years ago

egemenokten commented 6 years ago

Hi @perliedman I copied a part of your network.json file for my geoJson object and my points in this file. Here is the code

var geojson = require('./network.json');
var PathFinder = require('geojson-path-finder');
var start = {
    "type": "Feature",
    "properties": {},
    "geometry": {
      "type": "Point",
      "coordinates": [
            8.44846912,
            59.49097094
        ]
    }
}
var finish = {
    "type": "Feature",
    "properties": {},
    "geometry": {
      "type": "Point",
      "coordinates": [
            8.43052838,
            59.48310541
        ]
    }
}
var pathFinder = new PathFinder(geojson);
var path = pathFinder.findPath(start, finish)

But findPath method returning null. What am i doing wrong, can you help me ?

perliedman commented 6 years ago

Hard to tell without seeing the network, but a first thing is to ensure that the start and finish points are both in the network. That is, both must be coordinates in some segment (and of course the segments have to be connected to be able to find a route between them).

For an application with arbitrary start and finish points, I usually first look up the points in the network closest to the route's desired start and finish, to make sure they're always in the network. One way to this is to use Turf's nearest module.

egemenokten commented 6 years ago

Thanks @perliedman ! My segments wasn't connected. This solved my problem.