Build up a pyramid of Mapbox vector
tiles by aggregating quantitative
data into grids at lower zooms.
Motivation
Say you have a dataset of polygons that have some kind of density data
(population, vegetation, ...), and you want to build an interactive map with
it. Vector tiles are great for this--especially with
mapbox-gl steadily maturing.
But if your data is at a fine resolution and you don't want to be limited to
very high zoom levels, you're stuck using standard simplification techniques.
(Or, much better, the rather badass and blazingly fast simplification and point
dropping techniques offered by
tippecanoe). For many cases, this works
great, but it's not ideal here: for instance, in simplification many small,
high-density polygons get dropped, even though these are often important
features.
This tool is an alternative to simplification: using a grid whose resolution
varies with zoom level, aggregate the quantitative features of interest, so
that you can visualize the spatial distribution of your data at any scale.
To start, you'll need an mbtiles file containing the original feature data at
some (high) zoom level. If you've got the data in, say, a shapefile or
PostGIS, you can use Mapbox Studio to create a source and then export to
MBTiles -- just set the min and max zoom to something high enough.
CLI
Let's say you've got the data in data.mbtiles, at zoom 12 in a layer called
'foo', and each polygon in this layer has a field called density. Then, you
can build the grid pyramid above this base layer with:
Starting at zoom 11 and going down to zoom 1, this will build a 16x16 grid in
each tile, aggregating the data from the zoom level above. The aggregations
are defined by the --aggregations parameters. Each one is of the form:
layer:aggregationFunction(field), where aggregationFunction can
be any of the built-in aggregations available in
geojson-polygon-aggregate.
So, in this case, we'll end up with a grid where each box has a density
property, which is the (correctly weighted) mean of the densities of the
polygons from the previous (higher) zoom level that fall within that box.
With other aggregations, other stats. For instance, we could have done:
# first use count() to find out the number of polygons from the original# dataset being aggregated into each grid box at z11vt-grid input.mbtiles output.mbtiles --basezoom 12 --minzoom 11 --gridsize 16 \ --aggregations 'foo:areaWeightedMean(density)' 'foo:count(numzones)'# now, for z10 and below, sum the countsvt-grid input.mbtiles output.mbtiles --basezoom 12 --minzoom 11 --gridsize 16 \ --aggregations 'foo:areaWeightedMean(density)' 'foo:sum(numzones)'
Node
You can have a little more flexibility with aggregations (and post-aggregation
functions) by using vt-grid programmatically:
var path = require('path')var vtGrid = require('vt-grid')var reducers = require('geojson-polygon-aggregate/reducers')if (require.main === module) { vtGrid('/path/to/output.mbtiles', 'path/to/input.mbtiles', { minzoom: 1, basezoom: 10, aggregations: __filename, // this can be any file that exports an `aggregations` object like the one below postAggregations: __filename // same for this }, function (err) { if (err) { throw err } console.log('Finished!')
opts.basezoomnumber The zoom level at which to find the initial data
opts.inputTilesArray= An array of [z, x, y] tile coordinates to start with
opts.gridsizenumber Number of grid squares per tile
opts.aggregations(Object|string) If an object, then it maps layer names to aggregation objects, which themselves map field names to geojson-polygon-aggregate aggregation function names. Each worker will construct the actual aggregation function from geojson-polygon-aggregate by passing it the field name as an argument. If a string, then it's the path of a module that exports a layer to aggregation object map (see #grid for details).
opts.postAggregationsstring= Path to a module mapping layer names to postAggregations objects. See #grid for details.
opts.jobsnumber The number of jobs to run in parallel.