搭建本地OSM地图服务器

测试环境

  • Ubuntu 22.04.3 LTS
  • Apache2 2.4.52

依赖

1
sudo apt install screen locate libapache2-mod-tile renderd git tar unzip wget bzip2 apache2 lua5.1 mapnik-utils python3-mapnik python3-psycopg2 python3-yaml gdal-bin npm fonts-noto-cjk fonts-noto-hinted fonts-noto-unhinted fonts-unifont fonts-hanazono postgresql postgresql-contrib postgis postgresql-14-postgis-3 postgresql-14-postgis-3-scripts osm2pgsql net-tools curl
  • postgres用于保存数据
  • renderd是渲染进程
  • apache2提供www服务
  • mod-tile基于apache2提供切片服务,决定什么时候该切片、把什么切片

创建数据库

1
2
3
4
5
6
7
8
9
10
11
sudo -u postgres -i #以root登陆psotgres
createuser _renderd #创建新用户
createdb -E UTF8 -O _renderd gis 创建数据库
psql #此时终端显示postgres=#
\c gis #此时提示You are now connected to database "gis" as user "postgres". 终端显示gis-#
CREATE EXTENSION postgis; #提示CREATE EXTENSION
CREATE EXTENSION hstore; #提示CREATE EXTENSION
ALTER TABLE geometry_columns OWNER TO _renderd; #提示ALTER TABLE
ALTER TABLE spatial_ref_sys OWNER TO _renderd; #提示ALTER TABLE
\q #退出psql
exit #退出root

Mapnik

测试安装状态

1
2
3
python3 #进入python环境
>>> import mapnik #导入库,没有保存就正常
>>> quit() #退出环境

样式表配置

1
2
3
4
5
6
7
mkdir ~/src #创建文件夹
cd ~/src
git clone https://github.com/gravitystorm/openstreetmap-carto
cd openstreetmap-carto
sudo npm install -g carto #安装编译器
carto -v #1.2.0
carto project.mml > mapnik.xml #有一些警告

加载数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
mkdir ~/data
cd ~/data
wget https://download.geofabrik.de/asia/azerbaijan-latest.osm.pbf # 35.6MB
chmod o+rx ~
sudo -u _renderd osm2pgsql -d gis --create --slim -G --hstore --tag-transform-script ~/src/openstreetmap-carto/openstreetmap-carto.lua -C 2500 --number-processes 1 -S ~/src/openstreetmap-carto/openstreetmap-carto.style ~/data/azerbaijan-latest.osm.pbf

#以下为我的输出
2024-01-03 11:07:52 osm2pgsql version 1.6.0
2024-01-03 11:07:52 Database version: 14.10 (Ubuntu 14.10-0ubuntu0.22.04.1)
2024-01-03 11:07:52 PostGIS version: 3.2
2024-01-03 11:07:52 Setting up table 'planet_osm_point'
2024-01-03 11:07:52 Setting up table 'planet_osm_line'
2024-01-03 11:07:52 Setting up table 'planet_osm_polygon'
2024-01-03 11:07:52 Setting up table 'planet_osm_roads'
2024-01-03 11:08:20 Reading input files done in 28s.
2024-01-03 11:08:20 Processed 5151545 nodes in 16s - 322k/s
2024-01-03 11:08:20 Processed 477473 ways in 11s - 43k/s
2024-01-03 11:08:20 Processed 2957 relations in 1s - 3k/s
2024-01-03 11:08:21 Clustering table 'planet_osm_point' by geometry...
2024-01-03 11:08:22 Creating geometry index on table 'planet_osm_point'...
2024-01-03 11:08:22 Creating osm_id index on table 'planet_osm_point'...
2024-01-03 11:08:22 Analyzing table 'planet_osm_point'...
2024-01-03 11:08:22 Clustering table 'planet_osm_line' by geometry...
2024-01-03 11:08:37 Creating geometry index on table 'planet_osm_line'...
2024-01-03 11:08:39 Creating osm_id index on table 'planet_osm_line'...
2024-01-03 11:08:39 Analyzing table 'planet_osm_line'...
2024-01-03 11:08:39 Clustering table 'planet_osm_polygon' by geometry...
2024-01-03 11:08:41 Creating geometry index on table 'planet_osm_polygon'...
2024-01-03 11:08:42 Creating osm_id index on table 'planet_osm_polygon'...
2024-01-03 11:08:42 Analyzing table 'planet_osm_polygon'...
2024-01-03 11:08:42 Clustering table 'planet_osm_roads' by geometry...
2024-01-03 11:08:42 Creating geometry index on table 'planet_osm_roads'...
2024-01-03 11:08:42 Creating osm_id index on table 'planet_osm_roads'...
2024-01-03 11:08:42 Analyzing table 'planet_osm_roads'...
2024-01-03 11:08:42 Done postprocessing on table 'planet_osm_nodes' in 0s
2024-01-03 11:08:42 Building index on table 'planet_osm_ways'
2024-01-03 11:08:56 Done postprocessing on table 'planet_osm_ways' in 13s
2024-01-03 11:08:56 Building index on table 'planet_osm_rels'
2024-01-03 11:08:56 Done postprocessing on table 'planet_osm_rels' in 0s
2024-01-03 11:08:56 All postprocessing on table 'planet_osm_point' done in 0s.
2024-01-03 11:08:56 All postprocessing on table 'planet_osm_line' done in 16s.
2024-01-03 11:08:56 All postprocessing on table 'planet_osm_polygon' done in 2s.
2024-01-03 11:08:56 All postprocessing on table 'planet_osm_roads' done in 0s.
2024-01-03 11:08:56 osm2pgsql took 64s (1m 4s) overall.
  • -d gis 使用数据库
  • –create 创建数据库
  • slim 渲染表
  • G 表布局
  • hstore 多折线如何处理
  • tag-transform-script
  • C 2500 为导入过程分配osm2pgsql 2.5Gb内存,可以修改
  • number-processes 使用的CPU核心数
  • 最后的参数为需要加载的文件

创建索引

1
2
cd ~/src/openstreetmap-carto/
sudo -u _renderd psql -d gis -f indexes.sql #输出CREATE INDEX 16次

下载shapefile

此文件提供国家边界

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
cd ~/src/openstreetmap-carto/
mkdir data
sudo chown _renderd data
sudo -u _renderd scripts/get-external-data.py #文件很大,你要忍一下
INFO:root:Starting load of external data into database
INFO:root:Checking table simplified_water_polygons
INFO:root: Download complete (23795202 bytes)
INFO:root: Decompressing file
INFO:root: Importing into database
INFO:root: Import complete
INFO:root:Checking table water_polygons
INFO:root: Download complete (847964073 bytes)
INFO:root: Decompressing file
INFO:root: Importing into database
INFO:root: Import complete
INFO:root:Checking table icesheet_polygons
INFO:root: Download complete (52393148 bytes)
INFO:root: Decompressing file
INFO:root: Importing into database
INFO:root: Import complete
INFO:root:Checking table icesheet_outlines
INFO:root: Download complete (53057255 bytes)
INFO:root: Decompressing file
INFO:root: Importing into database
INFO:root: Import complete
INFO:root:Checking table ne_110m_admin_0_boundary_lines_land
INFO:root: Download complete (57325 bytes)
INFO:root: Decompressing file
INFO:root: Importing into database
INFO:root: Import complete

设置服务器

1
2
3
cd ~/src/openstreetmap-carto/
scripts/get-fonts.sh #安装字体,安装不了也不影响使用。
vim /etc/renderd.conf

添加配置

1
2
3
4
5
6
[s2o]
URI=/hot/
XML=/home/accountname/src/openstreetmap-carto/mapnik.xml # 注意修改
HOST=localhost
TILESIZE=256
MAXZOOM=20

调试信息

1
sudo vim /usr/lib/systemd/system/renderd.service

在service字段添加

1
Environment=G_MESSAGES_DEBUG=all

重启

1
2
3
sudo systemctl daemon-reload
sudo systemctl restart renderd
sudo systemctl restart apache2

在/var/log/syslog中可以看到

1
2
3
Jan  3 11:39:03 hyper-vm apachectl[10696]: [Wed Jan 03 11:39:03.138282 2024] [tile:notice] [pid 10696:tid 140362100856704] Loading tile config s2o at /hot/ for zooms 0 - 20 from tile directory /var/cache/renderd/tiles with extension .png and mime type image/png
Jan 3 11:39:03 hyper-vm apachectl[10696]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Jan 3 11:39:03 hyper-vm systemd[1]: Started The Apache HTTP Server.

瓦片

用浏览器打开localhost

apache2主页

查看世界地图localhost/hot/0/0/0.png

地图瓦片

地图

1
2
3
cd /var/www/html
sudo wget https://raw.githubusercontent.com/SomeoneElseOSM/mod_tile/switch2osm/extra/sample_leaflet.html
sudo nano sample_leaflet.html

使用浏览器打开localhost/sample_leaflet.html

地图

同时在syslog中可以查看信息。

1
2
3
4
5
6
7
8
9
10
Jan  3 11:52:30 hyper-vm renderd[10265]: START TILE s2o 13 5936-5943 3240-3247, new metatile
Jan 3 11:52:30 hyper-vm renderd[10265]: START TILE s2o 13 5944-5951 3240-3247, new metatile
Jan 3 11:52:30 hyper-vm renderd[10265]: START TILE s2o 13 5936-5943 3232-3239, new metatile
Jan 3 11:52:30 hyper-vm renderd[10265]: START TILE s2o 13 5944-5951 3232-3239, new metatile
Jan 3 11:52:30 hyper-vm renderd[10265]: DONE TILE s2o 13 5936-5943 3240-3247 in 0.263 seconds
Jan 3 11:52:30 hyper-vm renderd[10265]: DONE TILE s2o 13 5936-5943 3232-3239 in 0.263 seconds
Jan 3 11:52:30 hyper-vm renderd[10265]: START TILE s2o 13 5936-5943 3232-3239, age 0.00 days
Jan 3 11:52:30 hyper-vm renderd[10265]: DONE TILE s2o 13 5944-5951 3240-3247 in 0.277 seconds
Jan 3 11:52:30 hyper-vm renderd[10265]: DONE TILE s2o 13 5944-5951 3232-3239 in 0.264 seconds
Jan 3 11:52:30 hyper-vm renderd[10265]: DONE TILE s2o 13 5936-5943 3232-3239 in 0.255 seconds

搭建本地OSM地图服务器
https://blog.jackeylea.com/osm/how-to-setup-offline-osm-map-server/
作者
JackeyLea
发布于
2024年1月4日
许可协议