• Stars
    star
    270
  • Rank 146,990 (Top 3 %)
  • Language
    TypeScript
  • License
    ISC License
  • Created about 8 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Find shortest path through a network of GeoJSON

GeoJSON Path Finder

Build status

Find shortest paths through a network of GeoJSON.

Given a network of GeoJSON LineStrings, GeoJSON Path Finder will find the shortest path between two points in the network. This might be useful for automatic route searches in smaller networks, where setting up a real route planner like OSRM is too much work, or you simply need to do everything on the client.

See the GeoJSON Path Finder demo.

Upgrade notice Version 2.0 has been released, which is a TypeScript rewrite - you can still use the module from plain JavaScript, of course. This version also contains some breaking changes regarding option naming; for most common use cases, everything will work as before.

Breaking changes:

  • option precision is now named tolerance
  • option keyFn is now named key
  • option weightFn is now named weight
  • option edgeDataReduceFn is now named edgeDataReducer
  • option edgeDataSeed is now a function taking the properties of the start node

Installing

npm install --save geojson-path-finder

API

Detailed (and somewhat experimental) API Docs

Create a path finding object:

import PathFinder from "geojson-path-finder";
import geojson from "./network.json";

const pathFinder = new PathFinder(geojson);

The GeoJSON object should be a FeatureCollection of LineString features. The network will be built into a topology, so that lines that start and end, or cross, at the same coordinate are joined such that you can find a path from one feature to the other.

To find the shortest path between two coordinates:

var path = pathFinder.findPath(start, finish);

Where start and finish are two GeoJSON point features. Note that both points have to be vertices in the routing network; if they are not, no route will be found.

If a route can be found, an object with two properties: path and weight is returned, where path is the coordinates the path runs through, and weight is the total weight (distance in kilometers, if you use the default weight function) of the path.

As a convenience, the function pathToGeoJSON is also exported, it converts the result of a findPath call to a GeoJSON linestring:

import PathFinder, { pathToGeoJSON } from "geojson-path-finder";
const pathFinder = new PathFinder(geojson);
const pathLineString = pathToGeoJSON(pathFinder.findPath(start, finish));

(If findPath does not find a path, pathToGeoJSON will also return undefined.)

PathFinder options

The PathFinder constructor takes an optional seconds parameter containing options that you can use to control the behaviour of the path finder. Available options:

  • weight controls how the weight (or cost) of travelling between two vertices is calculated; by default, the geographic distance between the coordinates is calculated and used as weight; see Weight functions below for details
  • tolerance (default 1e-5) controls the tolerance for how close vertices in the GeoJSON can be before considered being the same vertice; you can say that coordinates closer than this will be snapped together into one coordinate
  • edgeDataReducer can optionally be used to store data present in the GeoJSON on each edge of the routing graph; typically, this can be used for storing things like street names; if specified, the reduced data is present on found paths under the edgeDatas property
  • edgeDataSeed is a function returning taking a network feature's properties as argument and returning the seed used when reducing edge data with the edgeDataReducer above

Weight functions

By default, the cost of going from one node in the network to another is determined simply by the geographic distance between the two nodes. This means that, by default, shortest paths will be found. You can however override this by providing a cost calculation function through the weight option:

const pathFinder = new PathFinder(geojson, {
  weight: function (a, b, props) {
    const dx = a[0] - b[0];
    const dy = a[1] - b[1];
    return Math.sqrt(dx * dx + dy * dy);
  },
});

The weight function is passed two coordinate arrays (in GeoJSON axis order), as well as the feature properties that are associated with this feature, and should return either:

  • a numeric value for the cost of travelling between the two coordinates; in this case, the cost is assumed to be the same going from a to b as going from b to a; as cost of 0 means the edge can't be used
  • an object with two properties: forward and backward; in this case, forward denotes the cost of going from a to b, and backward the cost of going from b to a; setting either to 0 will prevent taking that direction, the segment will be a oneway.
  • undefined is the same as setting the weight to 0: this edge can't be used

More Repositories

1

leaflet-routing-machine

Control for routing in Leaflet
JavaScript
1,024
star
2

leaflet-realtime

Put realtime data on a Leaflet map
JavaScript
720
star
3

leaflet-control-geocoder

A simple geocoder form to locate places. Easily extended to multiple data providers.
TypeScript
510
star
4

reproject

Change, convert, transform, reproject GeoJSON between different projections / CRS
JavaScript
212
star
5

query-overpass

Make queries to OpenStreetMap's overpass API and output as GeoJSON
JavaScript
147
star
6

raytracing-in-one-weekend

Rust implementation of the book Ray Tracing in One Weekend
Rust
94
star
7

tiled-maps

A short, free and open introduction to the concept of tiled maps
HTML
61
star
8

shadow-mapper

Generate maps of sun and shade from OpenStreetMap data
Python
56
star
9

elevation-service

Elevation data for your GeoJSON as a micro service
JavaScript
46
star
10

node-hgt

Query hgt files (typically SRTM elevation data) with performance
JavaScript
45
star
11

geojson-elevation

Add juicy elevation data to your fresh GeoJSON
JavaScript
38
star
12

ocad2geojson

JavaScript OCAD file reader and exporter for GeoJSON, SVG and Mapbox Style Spec
JavaScript
32
star
13

lrm-graphhopper

Support for GraphHopper in Leaflet Routing Machine
JavaScript
30
star
14

leaflet-underneath

Find interesting features is in your map using Mapbox Vector Tiles data
JavaScript
30
star
15

openlayers-routing-machine

A complete user interface for routing within an OpenLayers map
HTML
18
star
16

geojson2obj

Convert GeoJSON into Wavefront OBJ format
JavaScript
17
star
17

osm-slope

Calculate slope (climb and descent) for highway ways in OpenStreetMap data.
JavaScript
17
star
18

leaflet-touch-helper

Make it easy to touch things in Leaflet
JavaScript
17
star
19

svenska-landskap

Sveriges landskap som öppen geodata i GeoJSON
Shell
13
star
20

terrain-obj

Convert elevation data (typically SRTM elevation data, HGT files) to Wavefront OBJ 3D models
JavaScript
12
star
21

gis-nerd-tools

Simple but awesome web based tools for GIS enthusiasts
JavaScript
10
star
22

o-scout

A web based app for orienteering course setting and exploration
JavaScript
9
star
23

osmwave

Convert OpenStreetMap buildings and roads into Wavefront OBJ 3D models.
C++
7
star
24

leaflet-multi-style

Quickly add multiple styles to GeoJSON data
JavaScript
7
star
25

leaflet-raster

A simple Leaflet plugin for powerful raster functions
JavaScript
7
star
26

raster-blaster

Blast multi-band rasters to a canvas with speed and style.
JavaScript
6
star
27

cykelbanor

Bike routing for Sweden
JavaScript
5
star
28

gps-log-viewer

Web UI from gps-log
JavaScript
5
star
29

standstill

Find locations where there has been no movement, a stop, within a GeoJSON track, typically recorded from a GPS
JavaScript
5
star
30

fly-route

Fly over a map route using Mapbox GL (small demo)
JavaScript
4
star
31

course-sketcher

Web based course setting for orienteering
Vue
4
star
32

lrm-mapbox

DEPRECATED. Support for Mapbox directions API v4 in Leaflet Routing Machine
JavaScript
4
star
33

openlayers-tilejson

Create map or tile layer from TileJSON, with projection support
JavaScript
4
star
34

osm2obj

Generate Wavefront OBJ 3D models from OpenStreetMap XML or JSON
JavaScript
4
star
35

css-rankme-slack-bot

Slack bot to inform about important stats from Counter Strike Source's Rankme plugin
Python
3
star
36

brochure

Simple GeoJSON viewer based on Leaflet
JavaScript
3
star
37

lrm-svelte

Experiment making a Leaflet plugin using the Svelte framework
JavaScript
3
star
38

maptime-gbg-201602

"Let's learn Leaflet.js" presentation for Maptime GBG 2016-02
JavaScript
3
star
39

local-proj

Find a suitable local projection from GeoJSON data
JavaScript
3
star
40

jsprit.web

Server side of a simple Vechile Routing Problem solver built on jsprit
Java
2
star
41

mapbox-gl-routing-machine

A complete user interface for routing with Mapbox GL JS
JavaScript
2
star
42

roundabound

Simplistic log rotation utility
Python
2
star
43

ocad2tiles

Create raster images and tiles (image pyramids) from OCAD map files
JavaScript
2
star
44

advent-of-code

My solutions for Advent of Code
Python
2
star
45

svg-control-descriptions

IOF orienteering control descriptions as SVG
JavaScript
2
star
46

tradportalen-export

Export tree observations from Trädportalen
Shell
1
star
47

turfjs-gbgnodejs

Short introduction to turf.js @ gbgnodejs meetup
JavaScript
1
star
48

drone

Enjoy Mapbox's cloudless atlas
JavaScript
1
star
49

godwit

Minimalistic database migration tool
Python
1
star
50

vixel-test

Toying around with making voxel landscapes from height maps
JavaScript
1
star
51

memory-game

Very basic memory game I made for my kids
JavaScript
1
star
52

gps-track-import

Simple import of tracks for GPS
Python
1
star