OSM地图服务器搭建(Apache)

上一篇:

介绍了OSM的数据结构,本文介绍Apache2+postgres+Ubuntu21.04搭建OSM服务器。

软件包

1
sudo apt install libmapnik-dev postgresql postgis osm2pgsql libapache2-mod-tile

mapnik 是一个开源地图渲染引擎,它可以把postgresql数据或者shape文件渲染为图片。
mod_tile 是连接mapnik和apache的中间件

在终端输入

1
mapnik-config -v

会输出版本号

数据库配置

修改/etc/postgres/13/main/postgres.conf文件

1
wal_level = replica

修改后需要重启服务端

添加数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
sudo -u postgres psql #以root的权限用postgres用户登录数据库
postgres=# alter user postgres with password 'postgres'; #修改登录密码
- ALTER ROLE
jackey@ubuntu:/etc/postgresql/13/main$ sudo passwd -d postgres #清空用户postgres的密码
- passwd: password expiry information changed.
jackey@ubuntu:~/Desktop$ sudo -u postgres passwd #修改用户密码,必须和上面设置的密码一样
New password:
Retype new password:
passwd: password updated successfully
jackey@ubuntu:~/Desktop$ su postgres #切换到postgres用户
Password:
postgres@ubuntu:/home/jackey/Desktop$
psql #只有postgres用户才能登录到数据库中
create user osm; #创建osm用户
create database gis; #创建数据库gis
grant all on database gis to osm;#赋予osm权限
alter database gis owner to osm;#修改gis所属者为osm
\c gis;#加入gis库,创建扩展
create extension hstore;
create extension postgis;
\q
useradd -m osm #创建Linux的OSM用户

进入osm用户,执行

1
osm2pgsql -d gis -C 8600 ./33.osm

d表示使用的数据库
C表示缓存大小,如果提示ERROR: Out of memory for dense node cache, reduce --cache size.就调小一点。

导入输出为:

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
$ osm2pgsql -d gis -C 1024 ./33.osm
2021-12-19 05:13:18 osm2pgsql version 1.4.1
2021-12-19 05:13:18 Database version: 13.5 (Ubuntu 13.5-0ubuntu0.21.10.1)
2021-12-19 05:13:18 PostGIS version: 3.1
2021-12-19 05:13:18 Node-cache: cache=1024MB, maxblocks=16384*65536, allocation method=3
2021-12-19 05:13:18 Setting up table 'planet_osm_point'
2021-12-19 05:13:18 Setting up table 'planet_osm_line'
2021-12-19 05:13:18 Setting up table 'planet_osm_polygon'
2021-12-19 05:13:18 Setting up table 'planet_osm_roads'
2021-12-19 05:13:18 Reading input files done in 0s.
2021-12-19 05:13:18 Processed 12278 nodes in 0s - 12k/s
2021-12-19 05:13:18 Processed 2144 ways in 0s - 2k/s
2021-12-19 05:13:18 Processed 18 relations in 0s - 18/s
2021-12-19 05:13:18 node cache: stored: 12278(100.00%), storage efficiency: 37.49% (dense blocks: 1, sparse nodes: 12277), hit rate: 100.00%
2021-12-19 05:13:18 Clustering table 'planet_osm_point' by geometry...
2021-12-19 05:13:18 Clustering table 'planet_osm_line' by geometry...
2021-12-19 05:13:18 Creating geometry index on table 'planet_osm_point'...
2021-12-19 05:13:18 Analyzing table 'planet_osm_point'...
2021-12-19 05:13:18 Creating geometry index on table 'planet_osm_line'...
2021-12-19 05:13:18 All postprocessing on table 'planet_osm_point' done in 0s.
2021-12-19 05:13:18 Clustering table 'planet_osm_polygon' by geometry...
2021-12-19 05:13:18 Analyzing table 'planet_osm_line'...
2021-12-19 05:13:18 All postprocessing on table 'planet_osm_line' done in 0s.
2021-12-19 05:13:18 Clustering table 'planet_osm_roads' by geometry...
2021-12-19 05:13:18 Creating geometry index on table 'planet_osm_roads'...
2021-12-19 05:13:18 Analyzing table 'planet_osm_roads'...
2021-12-19 05:13:18 All postprocessing on table 'planet_osm_roads' done in 0s.
2021-12-19 05:13:18 Creating geometry index on table 'planet_osm_polygon'...
2021-12-19 05:13:18 Analyzing table 'planet_osm_polygon'...
2021-12-19 05:13:18 All postprocessing on table 'planet_osm_polygon' done in 0s.
2021-12-19 05:13:18 osm2pgsql took 0s overall.