Extends Leaflet Routing Machine with support for pgRouting.
Some brief instructions follow below, but the Leaflet Routing Machine tutorial on alternative routers is recommended.
Install nodejs/iojs, clone this repository and execute:
npm install
./scripts/build.sh
Put the script after Leaflet and Leaflet Routing Machine has been loaded.
To use with for example Browserify:
npm install --save lrm-pgrouting
createdb -U postgres muko
psql -U postgres -d muko -c "CREATE EXTENSION postgis;"
psql -U postgres -d muko -c "CREATE EXTENSION pgrouting;"
psql -U postgres -d muko -f data/muko_2po_4pgr.sql
psql -U postgres -d muko -f sql/routing_via_points.sql
- Add following lines into
geoserver/WEB-INF/web.xml
-<web-app>
node.
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
See http://stackoverflow.com/questions/22363192/cors-tomcat-geoserver for more details.
- Restart Tomcat service.
- Access to http://localhost:8080/geoserver.
- Click "Workspaces", then "Add new workspace".
- Fill the form with:
- Name:
pgrouting
- Namespace URI:
http://pgrouting.org
- Press the "Submit" button.
- Click "Stores", then "Add new Store".
- Choose "PostGIS" and fill the form with:
- Basic Store Info:
- Workspace:
pgrouting
- Data Source Name:
muko
- Workspace:
- Connection Parameters:
- host:
localhost
- port:
5432
- database:
muko
- schema:
public
- user:
postgres
- password: ``
- host:
- Press the "Save" button.
- Click "Layers", then "Add a new resource".
- Select the newly created workspace and store pair:
pgrouting:muko
- Click "Configure new SQL view...".
- Name the view
muko
and fill the "SQL statements" with:
SELECT
seq,
gid,
name,
heading,
cost,
geom,
distance,
point_type AS "pointType"
FROM
routing.viaPoints(
'%points%',
'muko_2po_4pgr',
'id',
'geom_way',
'osm_name',
'source',
'target',
'cost',
'reverse_cost',
4326
)
ORDER BY seq
- In the "SQL view parameters", click "Guess parameters from SQL", then fill with:
- Name:
points
- Default value: ``
- Validation regular expression:
^[\d\.,\|]+$
- In the "Attributes", click "Refresh", then change
geom
column "Type" toLineString
and "SRID" to4326
. - Press the "Save" button.
- In "Edit Layer" page, fill with:
- Coordinate Reference Systems:
- Declared SRS:
EPSG:4326
- SRS handling:
Force declared
- Declared SRS:
- Bounding Boxes:
- Lat/Lon Bounding Box:
- Min X:
-180
- Min Y:
-90
- Max X:
180
- Max Y:
90
- Min X:
- Lat/Lon Bounding Box:
- Press the "Save" button.
There's a single class exported by this module, L.Routing.PgRouting
. It implements the IRouter
interface. Use it to replace Leaflet Routing Machine's default OSRM router implementation:
var L = require('leaflet');
require('leaflet-routing-machine');
require('lrm-pgrouting'); // This will tack on the class to the L.Routing namespace
L.Routing.control({
router: new L.Routing.PgRouting('your GeoServer WFS layer type name'),
}).addTo(map);
Note that you will need to pass an existing GeoServer WFS layer type name to the constructor.