Mapbox系列之矢量切片工具Tippecanoe

Posted GeoWin_CAU

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mapbox系列之矢量切片工具Tippecanoe相关的知识,希望对你有一定的参考价值。

Tippecanoe(学习笔记)

Builds vector tilesets from large (or small) collections of GeoJSON or Geobuf features, like these.

矢量瓦片集创建工具,将大量(或小量)的GeoJSON或Geobuf要素集生成矢量瓦片。

Intent(目的)

The goal of Tippecanoe is to enable making a scale-independent(尺度无关、多尺度) view of your data, so that at any level from the entire world to a single building, you can see the density and texture(密度和结构) of the data rather than a simplification from dropping supposedly unimportant features(丢弃可能不重要的要素) or clustering or aggregating (分类归并)them.

If you give it all of OpenStreetMap and zoom out, it should give you back something that looks like "All Streets" rather than something that looks like an Interstate road atlas(洲际公路地图集).

If you give it all the building footprints in Los Angeles and zoom out far enough that most individual buildings are no longer discernable(可识别的), you should still be able to see the extent and variety of development in every neighborhood, not just the largest downtown buildings.

If you give it a collection of years of tweet locations, you should be able to see the shape and relative popularity of every point of interest and every significant travel corridor(走廊).

Installation(安装)

The easiest way to install tippecanoe on OSX is with Homebrew:

$ brew install tippecanoe

Usage(使用)

$ tippecanoe -o file.mbtiles [options] [file.json file.geobuf ...]

If no files are specified, it reads GeoJSON from the standard input. If multiple files are specified, each is placed in its own layer.

The GeoJSON features need not be wrapped in a FeatureCollection. You can concatenate (连接)multiple GeoJSON features or files together, and it will parse out the features and ignore whatever other objects it encounters(遇见)

Try this first

If you aren't sure what options to use, try this:

$ tippecanoe -o out.mbtiles -zg --drop-densest-as-needed in.geojson

The -zg option will make Tippecanoe choose a maximum zoom level that should be high enough to reflect the precision of the original data. (If it turns out still not to be as detailed as you want, use -z manually with a higher number.)

If the tiles come out too big, the --drop-densest-as-needed option will make Tippecanoe try dropping what should be the least visible features at each zoom level. (If it drops too many features, use -x to leave out some feature attributes that you didn't really need.)

Examples(案例)

Create a tileset of TIGER roads for Alameda County, to zoom level 13, with a custom layer name and description:

$ tippecanoe -o alameda.mbtiles -l alameda -n "Alameda County from TIGER" -z13 tl_2014_06001_roads.json

Create a tileset of all TIGER roads, at only zoom level 12, but with higher detail than normal, with a custom layer name and description, and leaving out the LINEARID and RTTYP attributes:

$ cat tiger/tl_2014_*_roads.json | tippecanoe -o tiger.mbtiles -l roads -n "All TIGER roads, one zoom" -z12 -Z12 -d14 -x LINEARID -x RTTYP

Options

There are a lot of options. A lot of the time you won't want to use any of them other than -o output.mbtiles to name the output file, and probably -f to delete the file that already exists with that name.

If you aren't sure what the right maxzoom is for your data, -zg will guess one for you based on the density of features.

If you are mapping point features, you will often want to use -Bg to automatically choose a base zoom level for dot dropping. If that doesn't work out for you, try -r1 --drop-fraction-as-needed to turn off the normal dot dropping and instead only drop features if the tiles get too big.

If you are mapping points or polygons, you will often want to use --drop-densest-as-needed to drop some of them if necessary to make the low zoom levels work.

If your features have a lot of attributes, use -y to keep only the ones you really need.

If your input is formatted as newline-delimited GeoJSON, use -P to make input parsing a lot faster.

Output tileset

  • -o file.mbtiles or --output=file.mbtiles: Name the output file.
  • -e directory or --output-to-directory=directory: Write tiles to the specified directory instead of to an mbtiles file.
  • -f or --force: Delete the mbtiles file if it already exists instead of giving an error
  • -F or --allow-existing: Proceed (without deleting existing data) if the metadata or tiles table already exists or if metadata fields can't be set. You probably don't want to use this.

Tileset description and attribution

  • -n name or --name=name: Human-readable name for the tileset (default file.json)
  • -A text or --attribution=text: Attribution (html) to be shown with maps that use data from this tileset.
  • -N description or --description=description: Description for the tileset (default file.mbtiles)

Input files and layer names

  • name.json or name.geojson: Read the named GeoJSON input file into a layer called name.
  • name.geobuf or name.geobuf: Read the named Geobuf input file into a layer called name.
  • -l name or --layer=name: Use the specified layer name instead of deriving a name from the input filename or output tileset. If there are multiple input files specified, the files are all merged into the single named layer, even if they try to specify individual names with -L.
  • -L name:file.json or --named-layer=name:file.json: Specify layer names for individual files. If your shell supports it, you can use a subshell redirect like -L name:<(cat dir/*.json) to specify a layer name for the output of streamed input.

Parallel processing of input

  • -P or --read-parallel: Use multiple threads to read different parts of each GeoJSON input file at once. This will only work if the input is line-delimited JSON with each Feature on its own line, because it knows nothing of the top-level structure around the Features. Spurious "EOF" error messages may result otherwise. Performance will be better if the input is a named file that can be mapped into memory rather than a stream that can only be read sequentially.

If the input file begins with the RFC 8142 record separator, parallel processing of input will be invoked automatically, splitting at record separators rather than at all newlines.

Parallel processing will also be automatic if the input file is in Geobuf format.

Projection of input

  • -s projection or --projection=projection: Specify the projection of the input data. Currently supported are EPSG:4326(WGS84, the default) and EPSG:3857 (Web Mercator). In general you should use WGS84 for your input files if at all possible.

Zoom levels

  • -z zoom or --maximum-zoom=zoom: Maxzoom: the highest zoom level for which tiles are generated (default 14)
  • -zg or --maximum-zoom=g: Guess what is probably a reasonable maxzoom based on the spacing of features.
  • -Z zoom or --minimum-zoom=zoom: Minzoom: the lowest zoom level for which tiles are generated (default 0)
  • -ae or --extend-zooms-if-still-dropping: Increase the maxzoom if features are still being dropped at that zoom level. The detail and simplification options that ordinarily apply only to the maximum zoom level will apply both to the originally specified maximum zoom and to any levels added beyond that.

Tile resolution

  • -d detail or --full-detail=detail: Detail at max zoom level (default 12, for tile resolution of 2^12=4096)
  • -D detail or --low-detail=detail: Detail at lower zoom levels (default 12, for tile resolution of 2^12=4096)
  • -m detail or --minimum-detail=detail: Minimum detail that it will try if tiles are too big at regular detail (default 7)

All internal math is done in terms of a 32-bit tile coordinate system, so 1/(2^32) of the size of Earth, or about 1cm, is the smallest distinguishable distance. If maxzoom + detail > 32, no additional resolution is obtained than by using a smallermaxzoom or detail.

Filtering feature attributes

  • -x name or --exclude=name: Exclude the named properties from all features
  • -y name or --include=name: Include the named properties in all features, excluding all those not explicitly named
  • -X or --exclude-all: Exclude all properties and encode only geometries
  • -Tattribute:type or --attribute-type=attribute:type: Coerce the named feature attribute to be of the specified type. The type may be stringfloatint, or bool. If the type is bool, then original attributes of 0 (or, if numeric, 0.0, etc.), falsenull, or the empty string become false, and otherwise become true. If the type is float or int and the original attribute was non-numeric, it becomes 0. If the type is int and the original attribute was floating-point, it is rounded to the nearest integer.
  • -j filter or --feature-filter=filter: Check features against a per-layer filter (as defined in the Mapbox GL Style Specification) and only include those that match. Any features in layers that have no filter specified will be passed through. Filters for the layer "*" apply to all layers.
  • -J filter-file or --feature-filter-file=filter-file: Like -j, but read the filter from a file.

Example: to find the Natural Earth countries with low scalerank but high LABELRANK:

tippecanoe -z5 -o filtered.mbtiles -j ' "ne_10m_admin_0_countries": [ "all", [ "<", "scalerank", 3 ], [ ">", "LABELRANK", 5 ] ] ' ne_10m_admin_0_countries.geojson

Dropping a fixed fraction of features by zoom level

  • -r rate or --drop-rate=rate: Rate at which dots are dropped at zoom levels below basezoom (default 2.5). If you use -rg, it will guess a drop rate that will keep at most 50,000 features in the densest tile. You can also specify a marker-width with -rgwidth to allow fewer features in the densest tile to compensate for the larger marker, or -rfnumber to allow at most number features in the densest tile.
  • -B zoom or --base-zoom=zoom: Base zoom, the level at and above which all points are included in the tiles (default maxzoom). If you use -Bg, it will guess a zoom level that will keep at most 50,000 features in the densest tile. You can also specify a marker-width with -Bgwidth to allow fewer features in the densest tile to compensate for the larger marker, or -Bfnumber to allow at most number features in the densest tile.
  • -al or --drop-lines: Let "dot" dropping at lower zooms apply to lines too
  • -ap or --drop-polygons: Let "dot" dropping at lower zooms apply to polygons too

Dropping a fraction of features to keep under tile size limits

  • -as or --drop-densest-as-needed: If a tile is too large, try to reduce it to under 500K by increasing the minimum spacing between features. The discovered spacing applies to the entire zoom level.
  • -ad or --drop-fraction-as-needed: Dynamically drop some fraction of features from each zoom level to keep large tiles under the 500K size limit. (This is like -pd but applies to the entire zoom level, not to each tile.)
  • -an or --drop-smallest-as-needed: Dynamically drop the smallest features (physically smallest: the shortest lines or the smallest polygons) from each zoom level to keep large tiles under the 500K size limit. This option will not work for point features.
  • -aN or --coalesce-smallest-as-needed: Dynamically combine the smallest features (physically smallest: the shortest lines or the smallest polygons) from each zoom level into other nearby features to keep large tiles under the 500K size limit. This option will not work for point features, and will probably not help very much with LineStrings. It is mostly intended for polygons, to maintain the full original area covered by polygons while still reducing the feature count somehow. The attributes of the small polygons are not preserved into the combined features, only their geometry.
  • -pd or --force-feature-limit: Dynamically drop some fraction of features from large tiles to keep them under the 500K size limit. It will probably look ugly at the tile boundaries. (This is like -ad but applies to each tile individually, not to the entire zoom level.) You probably don't want to use this.

Dropping tightly overlapping features

  • -g gamma or --gamma=_gamma_: Rate at which especially dense dots are dropped (default 0, for no effect). A gamma of 2 reduces the number of dots less than a pixel apart to the square root of their original number.
  • -aG or --increase-gamma-as-needed: If a tile is too large, try to reduce it to under 500K by increasing the -g gamma. The discovered gamma applies to the entire zoom level. You probably want to use --drop-densest-as-needed instead.

Line and polygon simplification

  • -S scale or --simplification=scale: Multiply the tolerance for line and polygon simplification by scale. The standard tolerance tries to keep the line or polygon within one tile unit of its proper location. You can probably go up to about 10 without too much visible difference.
  • -ps or --no-line-simplification: Don't simplify lines and polygons
  • -pS or --simplify-only-low-zooms: Don't simplify lines and polygons at maxzoom (but do simplify at lower zooms)
  • -pt or --no-tiny-polygon-reduction: Don't combine the area of very small polygons into small squares that represent their combined area.

Attempts to improve shared polygon boundaries

  • -ab or --detect-shared-borders: In the manner of TopoJSON, detect borders that are shared between multiple polygons and simplify them identically in each polygon. This takes more time and memory than considering each polygon individually.
  • -aL or --grid-low-zooms: At all zoom levels below maxzoom, snap all lines and polygons to a stairstep grid instead of allowing diagonals. You will also want to specify a tile resolution, probably -D8. This option provides a way to display continuous parcel, gridded, or binned data at low zooms without overwhelming the tiles with tiny polygons, since features will either get stretched out to the grid unit or lost entirely, depending on how they happened to be aligned in the original data. You probably don't want to use this.

Controlling clipping to tile boundaries

  • -b pixels or --buffer=pixels: Buffer size where features are duplicated from adjacent tiles. Units are "screen pixels"—1/256th of the tile width or height. (default 5)
  • -pc or --no-clipping: Don't clip features to the size of the tile. If a feature overlaps the tile's bounds or buffer at all, it is included completely. Be careful: this can produce very large tilesets, especially with large polygons.
  • -pD or --no-duplication: As with --no-clipping, each feature is included intact instead of cut to tile boundaries. In addition, it is included only in a single tile per zoom level rather than potentially in multiple copies. Clients of the tileset must check adjacent tiles (possibly some distance away) to ensure they have all features.

Reordering features within each tile

  • -pi or --preserve-input-order: Preserve the original input order of features as the drawing order instead of ordering geographically. (This is implemented as a restoration of the original order at the end, so that dot-dropping is still geographic, which means it also undoes -ao).
  • -ao or --reorder: Reorder features to put ones with the same properties in sequence, to try to get them to coalesce. You probably don't want to use this.
  • -ac or --coalesce: Coalesce adjacent line and polygon features that have the same properties. You probably don't want to use this.
  • -ar or --reverse: Try reversing the directions of lines to make them coalesce and compress better. You probably don't want to use this.

Adding calculated attributes

  • -ag or --calculate-feature-density: Add a new attribute, tippecanoe_feature_density, to each feature, to record how densely features are spaced in that area of the tile. You can use this attribute in the style to produce a glowing effect where points are densely packed. It can range from 0 in the sparsest areas to 255 in the densest.

Trying to correct bad source geometry

  • -aw or --detect-longitude-wraparound: Detect when adjacent points within a feature jump to the other side of the world, and try to fix the geometry.

Setting or disabling tile size limits