使用Tippecanoe工具处理大数据量的矢量数据切片

Tippecanoe是Mapbox的一个开源切片工具,项目地址:https://github.com/mapbox/tippecanoe,Mapbox常规的切片方法tilelive-copy参见另一篇博客。Tippecanoe主要在处理大数据量时有很大的优势,具有很高的效率,并且有很多参数可以控制。Tippecanoe只能处理GeoJSON,因此在切片前需要将矢量数据转换为GeoJSON,推荐使用ogr2ogr工具转换。切片以后的格式为mbtiles,可自行导入mongodb等数据库。

一、在Linux系统配置GDAL

ogr2ogr工具在GDAL中,Linux安装GDAL需要分别安装PROJ.4、GEOS和GDAL,本实验采用的proj-4.8.0geos-3.3.8gdal-1.10.0,点击可直接进入下载地址。安装前请确保系统已经安装gcc、g++、make,没有请自行安装。 
分别解压下载的压缩文件,在文件夹依次运行:

$ ./configure
$ make 
$ make install

安装完成以后命令默认路径在/usr/local/bin,库文件在/usr/local/lib,源码在/usr/local/include。此时运行gdalinfo:

$ gdalinfo

这里写图片描述

出现以上结果说明安装成功。 
注意: 如果出现提示共享库找不到,按一下方法处理: 
1、修改/etc/ld.so.conf文件,将共享库的路径“/usr/local/lib”加入进去。 
2、运行ldconfig 命令使其生效。凡是增加了新的共享库,都需要运行一下ldconfig 命令。

二、将数据转换为GeoJSON

本实验是从postgis数据库转换,转换命令为:

$ ogr2ogr -f "GeoJSON" ./filename.json PG:"host=localhost dbname=timeline user=ms password=ms" -sql "select * from tablename" -t_srs="epsg:4326"

其他命令可参考:https://morphocode.com/using-ogr2ogr-convert-data-formats-geojson-postgis-esri-geodatabase-shapefiles/

为了避免一个一个转,可以写成shell进行批处理:

#!bin/sh
for layer in "layer1" "layer2" "layer3" "layer4"
do
  echo "$layer convert start"
  ogr2ogr -f "GeoJSON" ./$layer.json PG:"host=localhost dbname=timeline user=ms password=ms" -sql "select * from $layer" -t_srs="epsg:4326"
  echo "$layer convert successful"
done

三、安装tippecanoe

从github下载源码:https://github.com/mapbox/tippecanoe/releases 
依次输入命令进行编译安装:

$ make
$ make install

四、 切片

$ tippecanoe -z 14 -Z 12 -ps -Bg -o river_live.mbtiles *.json

-Bg 是自动抽吸,避免单个瓦片要素数量过多 
*.json 是当前文件夹下的所有json文件,会为每个文件自动创建一个图层,图层名为文件名

五、mbtiles导入到mongodb

本项目矢量瓦片需要在mongodb进行管理,需要将mbtiles导入到mongodb,此时可以使用tilelive-copy进行导入,速度很快。

$ tilelive-copy mbtiles:///path/to/mbtiles/file.mbtiles "foxgis+mongodb://localhost/zootop?tileset_id=****&owner=****" --timeout=200000000

最后,如果在加载矢量切片后,渲染出现三角形的空洞,例如如下效果:

这里写图片描述

这是因为tippecanoe自动抽吸,精简了部分线坐标,可在切片时加-ps选项,禁止简化线要素。

$ tippecanoe -z 14 -Z 12 -ps -Bg -o river_live.mbtiles *.json

正确渲染结果如下图: 
这里写图片描述

猜你喜欢

转载自my.oschina.net/u/1464512/blog/1631972
今日推荐