Make the API nicer

This commit is contained in:
Andreas Hocevar 2017-08-13 20:58:09 -04:00
parent 8cdd7d5f8f
commit 522d33f118
3 changed files with 26 additions and 28 deletions

View File

@ -11,42 +11,39 @@ We are using mapboxGL in [The Capital Planning Platform](http://capitalplanning.
Install
`npm install geojson2mvt`
`geojson2mvt` takes an array of geojson objects and a config object, and builds the the pyramid in the specified output directory
`geojson2mvt` takes a config object with the GeoJSONs to encode and other options, and builds the pyramid in the specified output directory
```
var fs = require('fs');
var geojson2mvt = require('geojson2mvt');
var options = {
layers: {
layer0: JSON.parse(fs.readFileSync('bus_routes.geojson', "utf8")),
layer1: JSON.parse(fs.readFileSync('stops.geojson', "utf8"))
},
rootDir: 'tiles',
bbox : [40.426042,-74.599228,40.884448,-73.409958], //[south,west,north,east]
zoom : {
min : 8,
max : 18,
},
layerNames: ['layer0', 'layer1']
}
};
var geoJsons = [
JSON.parse(fs.readFileSync('bus_routes.geojson', "utf8")),
JSON.parse(fs.readFileSync('stops.geojson', "utf8")),
]
// build the static tile pyramid
geojson2mvt(geoJsons, options);
geojson2mvt(options);
```
Check out `/example` for a test project that you can try locally
## Options
`layers` - Object<string,Object> (required) - GeoJSONs to create a vector tileset from. Keys are the layer names that will be used to access data from the respective GeoJSON when displaying data from the MVT.
`rootDir` - string (required) - the filepath of the directory that will be the root of the file pyramid. It will be created if it doesn't exist.
`bbox` - array (required) - array of lat/lon bounds like `[s,w,n,e]`
`zoom` - object (required) - object with `min` and `max` properties for the desired zoom levels in the tile pyramid
`layerNames` - Array<string> (required) - names of the layers in the vector tile that the data will be stored in. Names are in the same order as the provided `geoJsons`. When displaying data from an MVT, you must specify which layer to use.
## Backwards compatibility
Instead of providing an array of `geoJsons`, a single geoJson can also be provided. In that case, the `options` have to contain a `layerName` property with the name for the imported geoJson as value.
Instead of providing a single config object, you can provide two arguments: a geoJson and config object without a `layers` property, but instead with a `layerName` property for the name for the imported geoJson in the MVT.

View File

@ -2,17 +2,16 @@ var fs = require('fs');
var geojson2mvt = require('../src');
var options = {
layers: {
layer0: JSON.parse(fs.readFileSync('bus_routes.geojson', "utf8")),
layer1: JSON.parse(fs.readFileSync('stops.geojson', "utf8"))
},
rootDir: 'tiles',
bbox : [40.426042,-74.599228,40.884448,-73.409958], //[south,west,north,east]
zoom : {
min : 8,
max : 18,
},
layerNames: ['lines', 'stops'],
}
};
var lines = JSON.parse(fs.readFileSync('bus_routes.geojson', "utf8"));
var stops = JSON.parse(fs.readFileSync('stops.geojson', "utf8"));
// build the static tile pyramid
geojson2mvt([lines, stops], options);
geojson2mvt(options);

View File

@ -4,19 +4,21 @@ var geojsonvt = require('geojson-vt');
var helpers = require('./helpers.js');
var geojson2mvt = function(geoJsons, options) {
var geojson2mvt = function(options) {
var layerNames = options.layerNames;
if (!Array.isArray(geoJsons)) {
geoJsons = [geoJsons];
layerNames = [options.layerName];
if (arguments.length == 2) {
var geoJson = options;
options = arguments[1];
options.layers = {};
options.layers[options.layerName] = geoJson;
}
var i = 0, ii = geoJsons.length;
var layerNames = Object.keys(options.layers);
var i = 0, ii = layerNames.length;
var tileIndex = new Array(ii);
for (; i < ii; ++i) {
tileIndex[i] = geojsonvt(geoJsons[i], {
tileIndex[i] = geojsonvt(options.layers[layerNames[i]], {
maxZoom: options.zoom.max,
indexMaxZoom: options.zoom.max,
indexMaxPoints: 0