0.3.0 • Published 8 years ago
node-pgrouting v0.3.0
node-pgrouting
A simple interface to pgRouting.
Features:
- Multi-cost
- Filters
- Aggregation of identical sections based on its properties
- Multi-snapping
- routing AND isocurve
How to use ?
Routing
const pgr = require("node-pgrouting")(conf);
let geojeson_results = await pgr.routing({from:"46,1",to:"47,2","type":"duration", "avoid":"toll"});- The
routingreturn a Promise, so you can use the async/await like above or the Promise itself. typeandavoiddepend on data structure (see Data structure)
Isocurve
const pgr = require("node-pgrouting")(conf);
let geojeson_results = await pgr.isocurve({from:"46,1","type":"duration", "avoid":"toll", "values":"3600,7200"});- The
isocurvereturn a Promise, so you can use the async/await like above or the Promise itself. typeandavoiddepend on data structure (see Data structure)fromorto
Capabilities (data structure)
const pgr = require("node-pgrouting")(conf);
let types = await pgr.getTypes();- The
getTypesreturn a Promise, so you can use the async/await like above or the Promise itself. typesis an array of available cost based on data structure (see Data structure)
const pgr = require("node-pgrouting")(conf);
let filters = await pgr.getFilters();- The
getFiltersreturn a Promise, so you can use the async/await like above or the Promise itself. filtersis an array of available filter based on data structure (see Data structure)
const pgr = require("node-pgrouting")(conf);
let properties = await pgr.getProperties();- The
getPropertiesreturn a Promise, so you can use the async/await like above or the Promise itself. propertiesis an array of available feature properties based on data structure (see Data structure)
PgRouting version
const pgr = require("node-pgrouting")(conf);
let version = await pgr.pgVersion();- The
pgVersionreturn a Promise, so you can use the async/await like above or the Promise itself. pgVersionis the version of PgRouting extension, not PosGIS or Postgresql version.
Close connection to the database
const pgr = require("node-pgrouting")(conf);
...your stuff...
pgr.close();Configuration
The conf object let you configure the connection to the database and some routing options :
- Connection parameters: node-pgrouting use pg as an interface to PostgreSQL database, with the
confobject as parameters like host, port, user, password and database. You can use same environment variables as libpq too : see pg documentation for more details. - table: : table that contains the network. You can use the environment variable PGTABLE too. table can contain a schema. (Default: edge)
- maxSnappingDistance: when process the routing, node-pgrouting needs to connect your start and end point to closest edge of the network within maxSnappingDistance meters.
- snappingRatio: allow to snap not only the nearest point but also all near points with a distance difference lower than snappingRatio (=(distance-min(distance))/min(distance)). (Default: 0 (no ratio)).
Data structure
Requirements: pgRouting concepts.
The data structure determine the routing capabilites. Some attributes are reserved:
- id: an uniq identifier of the network section
- source, target: for pgRouting topology
- cost_TYPE and reverse_cost_TYPE: the cost of the section when perform routing to minimize TYPE. You can provides as many type as wanted.
- filter_FILTER: true to avoid this section by use avoid: 'FILTER' in routing params. You can provides as many filter as wanted.
- the_geom: geometry of the section, with 4326 SRID.
- seq: use in routing response.
All other properties are used to identify the section. If multiple sections have the same properties, they are grouped in the routing response.
Example:
| id | name | source | target | cost_distance | reverse_cost_distance | cost_duration | reverse_cost_duration | cost_consumption | reverse_cost_consumption | filter_toll | cost | filter_highway |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 10693 | D86 | 65 | 66 | 397.550220811875 | 397.550220811875 | 20.4454399274679 | 20.4454399274679 | 0.0238530132487125 | 0.0238530132487125 | f | Free | f |
| 7711 | A1/E15-E19 | 6497 | 6914 | 3369.1892947396 | 3369.1892947396 | 101.075678842188 | 101.075678842188 | 0.303227036526564 | 0.303227036526564 | t | Paying | t |
| 11326 | A10 | 71 | 72 | 702.986479543753 | 702.986479543753 | 21.0895943863126 | 21.0895943863126 | 0.0632687831589378 | 0.0632687831589378 | f | Free | t |
| 7885 | D3 | 45 | 46 | 1238.94362175843 | 1238.94362175843 | 49.557744870337 | 49.557744870337 | 0.0867260535230898 | 0.0867260535230898 | f | Free | f |
| 7663 | D317 | 6819 | 6872 | 5334.01082992236 | 5334.01082992236 | 274.320556967436 | 274.320556967436 | 0.320040649795342 | 0.320040649795342 | f | Free | f |
| 7799 | N104 | 6789 | 46 | 3921.77709540926 | 3921.77709540926 | 117.653312862278 | 117.653312862278 | 0.352959938586834 | 0.352959938586834 | f | Free | f |
This table provides:
- 3 types: distance, duration, consumption
- 2 filters: toll, highway
- 2 properties: name, cost
Demonstration/Development
- Start demo/dev environmnent
bash dev/up.sh- Use the GUI or the REST service:
- http://127.0.0.1:8080, graphical interface : use the right click to define start and end point.
- http://127.0.0.1:8080/app/version
- http://127.0.0.1:8080/app/capabilities
- http://127.0.0.1:8080/app/route?from=46,1&to=47,2&type=duration&avoid=toll
- Perform unit tests:
docker exec -i -t pgr_node_1 grunt test- Cleanup Dev env
bash dev/down.sh