测试环境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 createuser _renderd createdb -E UTF8 -O _renderd gis 创建数据库 psql \c gis CREATE EXTENSION postgis; CREATE EXTENSION hstore; ALTER TABLE geometry_columns OWNER TO _renderd; ALTER TABLE spatial_ref_sys OWNER TO _renderd; \q exit
Mapnik测试安装状态
1 2 3 python3 >>> import mapnik >>> quit()
样式表配置1 2 3 4 5 6 7 mkdir ~/src cd ~/src git clone https://github.com/gravitystorm/openstreetmap-cartocd openstreetmap-cartosudo npm install -g carto carto -v 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 ~/datacd ~/data wget https://download.geofabrik.de/asia/azerbaijan-latest.osm.pbf 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
下载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 datasudo chown _renderd datasudo -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-reloadsudo systemctl restart renderdsudo 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
查看世界地图localhost/hot/0/0/0.png
地图1 2 3 cd /var/www/htmlsudo wget https://raw.githubusercontent.com/SomeoneElseOSM/mod_tile/switch2osm/extra/sample_leaflet.htmlsudo 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