guidance-geojson
Advanced tools
Comparing version 0.1.1 to 0.2.0
65
index.js
@@ -7,3 +7,11 @@ var polyline = require('polyline'); | ||
module.exports = guidance; | ||
module.exports.stylePrep = stylePrep; | ||
module.exports.styleRoute = styleRoute; | ||
/** | ||
* Convert a Mapbox Directions response to a GeoJSON FeatureCollection that can be styled with Mapbox Studio. | ||
* | ||
* @return {object} GeoJSON | ||
* @param {object} response - A Mapbox Directions response object. | ||
*/ | ||
function guidance(response) { | ||
@@ -219,1 +227,58 @@ if (response.code) { | ||
} | ||
/** | ||
* Prepare a Mapbox GL style for loading into Mapbox GL prior to having route data from guidance-geojson added. Useful for initializing a map prior to having a dynamic route added to it. | ||
* | ||
* @return {object} A Mapbox GL style object | ||
* @param {object} style - A Mapbox GL style object. | ||
* @param {string} [prefix=route] - Layer IDs matching this string prefix will be snagged for styling route data. Defaults to 'route'. | ||
*/ | ||
function stylePrep(style, prefix) { | ||
// Clone style object | ||
style = JSON.parse(JSON.stringify(style)); | ||
var routeLayers = []; | ||
prefix = prefix || 'route'; | ||
// Stash layers and source ID to use on style metadata object | ||
style.metadata = style.metadata || {}; | ||
style.metadata['guidanceRoute'] = routeLayers; | ||
// Filter layers to those not styling route data | ||
var filtered = []; | ||
style.layers.forEach(function(layer, i) { | ||
if (layer.id.indexOf(prefix) === 0) { | ||
routeLayers.push({ | ||
layer: layer, | ||
before: style.layers[i-1] ? style.layers[i-1].id : undefined | ||
}); | ||
} else { | ||
filtered.push(layer); | ||
} | ||
}); | ||
style.layers = filtered; | ||
return style; | ||
} | ||
/** | ||
* Add guidance geojson from a route to a map for styling. The map style must already be prepared via the `stylePrep()` function. | ||
* | ||
* @param {object} mapboxgl - A reference to the Mapbox GL library. | ||
* @param {object} map - A Mapbox GL map object. | ||
* @param {object} route - A Mapbox Directions response (will be converted automatically) or guidance GeoJSON for such a route. | ||
*/ | ||
function styleRoute(mapboxgl, map, route) { | ||
var style = map.getStyle(); | ||
if (!style.metadata || !style.metadata.guidanceRoute) throw new Error('metadata.guidanceRoute not found. Did you run stylePrep() on your style object?'); | ||
if (route.type !== 'FeatureCollection') route = guidance(route); | ||
map.addSource('route-guidance', new mapboxgl.GeoJSONSource({ data: route })); | ||
style.metadata.guidanceRoute.forEach(function(item) { | ||
item.layer['source'] = 'route-guidance'; | ||
delete item.layer['source-layer']; | ||
map.addLayer(item.layer, item.before) | ||
}); | ||
} | ||
{ | ||
"name": "guidance-geojson", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"dependencies": { | ||
@@ -10,4 +10,3 @@ "minimist": "1.2.x", | ||
"turf-bearing": "1.0.x", | ||
"turf-line-distance": "1.0.x", | ||
"turf-line-slice": "^1.3.4" | ||
"turf-line-distance": "1.0.x" | ||
}, | ||
@@ -14,0 +13,0 @@ "bin": { |
@@ -5,1 +5,50 @@ guidance-geojson | ||
```js | ||
var guidanceGeoJSON = require('guidance-geojson'); | ||
var geojson = guidanceGeoJSON(directionsResponse); | ||
// console.log(geojson); | ||
``` | ||
### Styling Guidance GeoJSON | ||
Guidance GeoJSON provides a FeatureCollection with the following feature types: | ||
- `segments` are LineString features that together make up the route to be traveled. | ||
- `labels` are Point features for maneuver points where the user needs to make a turn. | ||
- `waypoints` are Point features for starting, final, or intermediate destinations. | ||
Each feature has the following properties exposed for styling in Mapbox Studio: | ||
property | segment | label | waypoint | ||
--- | --- | --- | --- | ||
`type` | `segment` | `label` | `waypoint` | ||
`bearing` | `null` | `0-360` | `null` | ||
`modifier` | `null` | TBD | `null` | ||
`name` | `null` | Name of next road | Name of waypoint road | ||
`waypoint` | `null` | `null` | `"first"|"last"|null` | ||
@TODO: provide example Guidance GeoJSON file for styling in studio as a static route. | ||
### Integrating Guidance GeoJSON dynamically with a Mapbox GL JS map | ||
Once you have created your map style you can have it dynamically style a route. To do so, however, you will need to use the `stylePrep()` and `styleRoute()` methods to omit the layers in your style when loading your map and then apply those styles to your dynamic guidance GeoJSON once loaded: | ||
```js | ||
var guidanceGeoJSON = require('guidance-geojson'); | ||
var mapboxgl = require('mapbox-gl'); | ||
var directionsResponse = require('./stored-directions-response.json'); | ||
var map = new mapboxgl.Map({ | ||
container: 'map', | ||
// Defers showing all layers from `style` beginning with `route` | ||
style: guidanceGeoJSON.stylePrep(style, 'route') | ||
}); | ||
map.on('style.load', function () { | ||
var route = guidanceGeoJSON(directionsResponse); | ||
// Restores style layers and adjusts dynamically to style `route` GeoJSON | ||
guidanceGeoJSON.styleRoute(mapboxgl, map, route); | ||
}); | ||
``` | ||
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
6
54
2
0
12824
7
261
- Removedturf-line-slice@^1.3.4
- Removedturf-line-slice@1.3.4(transitive)
- Removedturf-linestring@1.0.2(transitive)