文章目录
本次瓦片服务器我们主要基于OpenStreetMap-Tile-Server来做,以下是这个项目的介绍和具体操作流程
一、OpenStreetMap-Tile-Server
项目地址:https://gitcode.com/Overv/openstreetmap-tile-server
该容器基于最新的Ubuntu 18.04 LTS指南由switch2osm.org提供,允许您轻松地使用.osm.pbf文件设置OpenStreetMap PNG瓦片服务器,并采用默认的OpenStreetMap样式。
涉及的技术:Docker、OpenStreetMap、Osm2pgsql、PostgreSQL 数据库
二、OpenStreetMap地图资源下载
https://download.geofabrik.de/asia/china.html(国内可访问,本次使用国内地图,也可下载全球其他地区)
三、加载地图资源
OpenStreetMap-Tile-Server运行以及加载OpenStreetMap资源到项目中,这里是通过解析pdf文件到PostgreSql中实现,命令如下:
docker run --name osm-server-china
-v D:\docker\osm\china-latest.osm.pbf:/data/region.osm.pbf
-v osm-data-china:/data/database/
overv/openstreetmap-tile-server import
该命令时间会长一点,底层的主要操作如下:
①移动本地osm.pbf文件到docker容器中
②解析osm.pbf文件
Node数据(约154754k),Way数据(约11620k),Relation数据(约150050B)
③创建Postgre账号:renderer,表结构创建,地图数据导入数据库
④下载地图公共资源,这里有点坑,这里详细说一下
到这一步时,OpenStreetMap-Tile-Server会自动下载一些地图公共资源的图层包,通过进入容器,查看启动脚本知道,项目中有一个external-data.yml的文件(/data/style/),内容如下:
settings:
temp_schema: loading
schema: public
data_dir: data
database: gis
metadata_table: external_data
sources:
simplified_water_polygons:
# The type of file this source is
type: shp
# Where to get it
url: https://osmdata.openstreetmap.de/download/simplified-water-polygons-split-3857.zip
# The location within the archive
file: simplified-water-polygons-split-3857/simplified_water_polygons.shp
archive:
format: zip
# Files to extract from the archive
files:
- simplified-water-polygons-split-3857/simplified_water_polygons.cpg
- simplified-water-polygons-split-3857/simplified_water_polygons.dbf
- simplified-water-polygons-split-3857/simplified_water_polygons.prj
- simplified-water-polygons-split-3857/simplified_water_polygons.shp
- simplified-water-polygons-split-3857/simplified_water_polygons.shx
water_polygons:
type: shp
url: https://osmdata.openstreetmap.de/download/water-polygons-split-3857.zip
file: water-polygons-split-3857/water_polygons.shp
archive:
format: zip
files:
- water-polygons-split-3857/water_polygons.cpg
- water-polygons-split-3857/water_polygons.dbf
- water-polygons-split-3857/water_polygons.prj
- water-polygons-split-3857/water_polygons.shp
- water-polygons-split-3857/water_polygons.shx
icesheet_polygons:
type: shp
url: https://osmdata.openstreetmap.de/download/antarctica-icesheet-polygons-3857.zip
file: antarctica-icesheet-polygons-3857/icesheet_polygons.shp
archive:
format: zip
files:
- antarctica-icesheet-polygons-3857/icesheet_polygons.cpg
- antarctica-icesheet-polygons-3857/icesheet_polygons.dbf
- antarctica-icesheet-polygons-3857/icesheet_polygons.prj
- antarctica-icesheet-polygons-3857/icesheet_polygons.shp
- antarctica-icesheet-polygons-3857/icesheet_polygons.shx
icesheet_outlines:
type: shp
url: https://osmdata.openstreetmap.de/download/antarctica-icesheet-outlines-3857.zip
file: antarctica-icesheet-outlines-3857/icesheet_outlines.shp
ogropts:
- "-explodecollections"
archive:
format: zip
files:
- antarctica-icesheet-outlines-3857/icesheet_outlines.cpg
- antarctica-icesheet-outlines-3857/icesheet_outlines.dbf
- antarctica-icesheet-outlines-3857/icesheet_outlines.prj
- antarctica-icesheet-outlines-3857/icesheet_outlines.shp
- antarctica-icesheet-outlines-3857/icesheet_outlines.shx
ne_110m_admin_0_boundary_lines_land:
type: shp
url: https://naturalearth.s3.amazonaws.com/110m_cultural/ne_110m_admin_0_boundary_lines_land.zip
file: ne_110m_admin_0_boundary_lines_land.shp
ogropts: &ne_opts
- "--config"
- "SHAPE_ENCODING"
- "WINDOWS-1252"
- "-explodecollections"
# needs reprojecting
- '-t_srs'
- 'EPSG:3857'
archive:
format: zip
files:
- ne_110m_admin_0_boundary_lines_land.dbf
- ne_110m_admin_0_boundary_lines_land.prj
- ne_110m_admin_0_boundary_lines_land.shp
- ne_110m_admin_0_boundary_lines_land.shx
上面代码中,sources的内容即为地图公共资源包,可能会出现本地docker环境中,链接无法访问的情况,可以通过如下两种操作处理:
1、把文件下载下来,然后本地部署web容器,通过本机ip访问,同时,需要替换url为 你的容器地址/资源名
2、本地git clone我的码云项目:https://gitee.com/1390893380/osm-tile-source.git,打包运行即可(water-polygons-split-3857.zip资源八百多兆,无法上传码云,需要自己本地下载后,放在项目的pubilc目录下,如果嫌慢的话可以用我百度云链接下载:链接:https://pan.baidu.com/s/1I6xumrlnUfsJuJReeQ0D8A 提取码:nb4k),启动成功后,替换上面yml的url为http://你的ip/资源名,如下为我替换后的样例
settings:
temp_schema: loading
schema: public
data_dir: data
database: gis
metadata_table: external_data
sources:
simplified_water_polygons:
# The type of file this source is
type: shp
# Where to get it
url: http://172.16.18.36:80/simplified-water-polygons-split-3857.zip
# The location within the archive
file: simplified-water-polygons-split-3857/simplified_water_polygons.shp
archive:
format: zip
# Files to extract from the archive
files:
- simplified-water-polygons-split-3857/simplified_water_polygons.cpg
- simplified-water-polygons-split-3857/simplified_water_polygons.dbf
- simplified-water-polygons-split-3857/simplified_water_polygons.prj
- simplified-water-polygons-split-3857/simplified_water_polygons.shp
- simplified-water-polygons-split-3857/simplified_water_polygons.shx
water_polygons:
type: shp
url: http://172.16.18.36:80/water-polygons-split-3857.zip
file: water-polygons-split-3857/water_polygons.shp
archive:
format: zip
files:
- water-polygons-split-3857/water_polygons.cpg
- water-polygons-split-3857/water_polygons.dbf
- water-polygons-split-3857/water_polygons.prj
- water-polygons-split-3857/water_polygons.shp
- water-polygons-split-3857/water_polygons.shx
icesheet_polygons:
type: shp
url: http://172.16.18.36:80/antarctica-icesheet-polygons-3857.zip
file: antarctica-icesheet-polygons-3857/icesheet_polygons.shp
archive:
format: zip
files:
- antarctica-icesheet-polygons-3857/icesheet_polygons.cpg
- antarctica-icesheet-polygons-3857/icesheet_polygons.dbf
- antarctica-icesheet-polygons-3857/icesheet_polygons.prj
- antarctica-icesheet-polygons-3857/icesheet_polygons.shp
- antarctica-icesheet-polygons-3857/icesheet_polygons.shx
icesheet_outlines:
type: shp
url: http://172.16.18.36:80/antarctica-icesheet-outlines-3857.zip
file: antarctica-icesheet-outlines-3857/icesheet_outlines.shp
ogropts:
- "-explodecollections"
archive:
format: zip
files:
- antarctica-icesheet-outlines-3857/icesheet_outlines.cpg
- antarctica-icesheet-outlines-3857/icesheet_outlines.dbf
- antarctica-icesheet-outlines-3857/icesheet_outlines.prj
- antarctica-icesheet-outlines-3857/icesheet_outlines.shp
- antarctica-icesheet-outlines-3857/icesheet_outlines.shx
ne_110m_admin_0_boundary_lines_land:
type: shp
url: http://172.16.18.36:80/ne_110m_admin_0_boundary_lines_land.zip
file: ne_110m_admin_0_boundary_lines_land.shp
ogropts: &ne_opts
- "--config"
- "SHAPE_ENCODING"
- "WINDOWS-1252"
- "-explodecollections"
# needs reprojecting
- '-t_srs'
- 'EPSG:3857'
archive:
format: zip
files:
- ne_110m_admin_0_boundary_lines_land.dbf
- ne_110m_admin_0_boundary_lines_land.prj
- ne_110m_admin_0_boundary_lines_land.shp
- ne_110m_admin_0_boundary_lines_land.shx
注意:上述操作后,拿到的__external-data.yml内容,需要在步骤④之前替换掉原始的external-data.yml内容
⑤结束,中国地图1G左右,加载完成后,docker中volume为osm-data-china的磁盘空间占用大概在32G
四、启动瓦片服务器
docker run osm-server-china-tile -p 8080:80
-v osm-data-china:/data/database -d overv/openstreetmap-tile-server run
五、访问瓦片服务器
http://localhost:8080