mapbox / tile-count

A tool for accumulating point counts by tile
BSD 2-Clause "Simplified" License
32 stars 10 forks source link

tile-count

A tool for accumulating point counts by tile.

Installation

Tile-count requires sqlite3 and libpng.

$ sudo apt-get install libsqlite3-dev libpng-dev  # Linux
$ brew install libpng                             # MacOS
$ make install

Creating a count

tile-count-create [-q] [-s binsize] -o out.count [file.csv ...] [file.json ...]

If the input is CSV, it is a list of records in the form:

lon,lat

or

lon,lat,count

If it is JSON, any array of two or more numbers will be treated as a longitude-latitude pair. This includes GeoJSON Points as well as the points that make up GeoJSON MultiPoints, LineStrings, MultiLineStrings, Polygons, and MultiPolygons. Beware that it also includes anything else that might be mistaken for a longitude-latitude pair.

The input is first streamed into the internal format specified below (minus the header) and then sorted and merged into the same format in quadkey order, with adjacent duplicates summed.

Merging counts

tile-count-merge [-q] [-s binsize] -o out.count [-F] in1.count [in2.count ...]

Produces a new count file from the specified count files, summing the counts for any points duplicated between the two.

Decoding counts

tile-count-decode in.count ...

Outputs the lon,lat,count CSV that would recreate in.count.

Tiling

tile-count-tile [options] -o out.mbtiles in.count
tile-count-tile [options] -o out.mbtiles in.mbtiles [ … in.mbtiles ]

The features in the mbtiles are a grid of squares with a density attribute indicating how many original points were accumulated into that binned point, normalized according the densest point in the zoom level.

If you are merging existing .mbtiles files, they all have to have been created with the same minzoom, maxzoom, and detail. The merged output can be either bitmap or vector as desired. The .mbtiles files being merged should be separated spatially, not temporally, because merging does not recalculate the reference brightness where tilesets overlap. Use tile-count-merge to combine data sets that are for the same area.

Output tileset

Zoom levels

You must specify either -z (maxzoom) or -s (bin size) if you are creating a new tileset instead of merging existing tilesets. The maxzoom plus the detail always equals the bin size.

Level bucketing

Bitmap tiles

Vector tiles

Tile size

Miscellaneous controls

Relationship between bin size, maxzoom, and detail

What exactly the "detail" parameter means is often the source of confusion. It is the difference between the maxzoom and the bin size.

So, for example, if you have data with a bin size of 23 and want to tile it with a maxzoom of 16, you should specify a detail of 7, because 16+7=23.

Within each tile, the resolution of the tile is 2^detail, so if you specify a detail of 7, each tile will be a 128x128 grid of pixels or features, because 2^7=128.

It is often more useful to work backward from the bin size to the maxzoom: if you have data with a bin size of 24, and you want 256x256 tiles, 2^8=256 so you should specify a detail of 8, and the maxzoom will be 16 because 24-8=16.

Internal file format

The .count files contain a header for versioning and identification followed (currently) by a simple list of 12-byte records containing: