Install:
npm i osm-road-graph
Usage examples:
Initialize:
;; const roadGraph = RoadGraph;
Ways finding:
Find way by coords:
const coords1 = lat: 5368350 lng: 2383437;const coords2 = lat: 5367722 lng: 2382298; const result = roadGraph;
Find way between road graph nodes (works faster than counting by coords):
const node1 = roadGraphnodes5;const node2 = roadGraphnodes100; const result = roadGraph;
Result:
distance: 7355 polyline: lat: 5368350 lng: 2383437 ... lat: 5367722 lng: 2382298
Distance matrix calculation:
Basic:
let points = lat: 5367719 lng: 23823 lat: 5368384 lng: 2383443 lat: 5368817 lng: 2384796 ;const distanceMatrix = roadGraph;
Results - Distance matrix:
point_object_1: key: point_object_2 distance: 1234 ... ...
Extended:
makeDistanceMatrix params:
points // Any collection of objects which have coords gettingCoordsFunc = null // A function which must returns {lat, lng} from object distanceLimit = Infinity // Maximal distance which can be added into distance matrix gettingPointIdentificatorFunc = null // A function which returns key from object (by default is same object) updatingPointFunc = null // A function for collection object modifying
Example:
let points = name: "Castle" coords: lat: 5367719 lng: 23823 name: "City Square" coords: lat: 5368384 lng: 2383443 name: "Zoo" coords: lat: 5368817 lng: 2384796 ;const gettingCoordsFunc = pcoords;const gettingPointIdentificatorFunc = pname;const updatingPointFunc = { if !currentPointdistances currentPointdistances = ; currentPointdistances;};const distanceMatrix = roadGraph;
Results - Distance matrix:
'Castle': key: 'City Square' distance: 1202 'City Square': key: 'Castle' distance: 1202 key: 'Zoo' distance: 1242 'Zoo': key: 'City Square' distance: 1242
Results - Modified points object:
name: 'Castle' coords: lat: 5367719 lng: 23823 distances: point: "City Square" distance: 1202 ... ...
Todo:
- Add function for generating distance matrix.
- Jump through single-way-out nodes.
- Prolong a found path to the nearest roads intersection if the destination is on the other side of the road (for vehicles).