引言

Nominatim是一种通过名称和地址(地理编码)搜索OpenStreetMap数据以及生成OSM点的合成地址(反向地理编码)的工具。

写本篇文章的原因是在通过Docker配置Nominatim时,遇到了一些困难,无法使用,所以就只好自己配置一遍/(ㄒoㄒ)/~~

写在前面:是由于官方提供的docker镜像启动时连接外网下载数据,下载的数据是外网的,所以下载不下来,导致容器无法使用,如何修改直接往后看=》

参考链接

https://blog.csdn.net/xff1994/article/details/79707720

https://blog.csdn.net/vatrenoludilo/article/details/125003804

https://nominatim.org/release-docs/latest/appendix/Install-on-Ubuntu-20/

pbf数据下载地址:http://download.openstreetmap.fr/extracts/asia

基于Ubuntu

1.ubuntu换源

https://zhuanlan.zhihu.com/p/142014944

Ubuntu系统中,软件源文件地址为:/etc/apt/sources.list

1.备份原来的源,将以前的源备份一下,以防以后可以用的。

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

2.打开/etc/apt/sources.list文件,删除所有 ggdG ,并保存。

sudo vim /etc/apt/sources.list(可将vim更换为自己熟悉的编辑器)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#添加阿里源
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
#添加清华源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse multiverse

3.更新

更新源

1
sudo apt-get update

如出现依赖问题,解决方式如下:

1
sudo apt-get -f install

更新软件:

1
sudo apt-get upgrade

2.解决中文乱码

https://www.jianshu.com/p/5fc84838f816

查看当前系统语言

1
$ echo $LANG

查看系统安装的语言包

1
2
3
4
5
6
$ locale -a 
C
C.UTF-8
POSIX

$ sudo dpkg -l | grep language-pack-zh-hans

如果没有中文语言包,需要安装

1
$ sudo apt-get install language-pack-zh-hans

安装成功后,确认是否安装成功

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ locale -a 
C
C.UTF-8
POSIX
zh_CN.utf8
zh_SG.utf8

$ sudo dpkg -l | grep language-pack-zh-hans
ii  language-pack-zh-hans           1:20.04+20210802                    all          translation updates for language Simplified Chinese
ii  language-pack-zh-hans-base      1:20.04+20210802                    all          translations for language Simplified Chinese

设置系统语言环境

1
2
3
4
5
6
7
// export 的方式只对当前终端生效
$ export LANG="zh_CN.UTF-8"

//  /etc/profile 文件中添加export LANG="zh_CN.UTF-8" 对所有用户生效
$ vim /etc/profile
// 使配置生效
$ source /etc/profile

3.docker中无法输入中文

docker exec -it nominatim_docker env LANG=C.UTF-8 /bin/bash

永久在docker中输入中文

在Dockerfile中添加一行,如下所示:

ENV LANG C.UTF-8

4.安装必要软件

1
2
3
4
5
6
7
8
9
sudo apt install -y php-cgi
sudo apt install -y build-essential cmake g++ libboost-dev libboost-system-dev \
                    libboost-filesystem-dev libexpat1-dev zlib1g-dev \
                    libbz2-dev libpq-dev \
                    postgresql-12-postgis-3 \
                    postgresql-contrib-12 postgresql-12-postgis-3-scripts \
                    php-cli php-pgsql php-intl libicu-dev python3-dotenv \
                    python3-psycopg2 python3-psutil python3-jinja2 \
                    python3-icu python3-datrie python3-yaml

安装systemctl

apt-get install systemctl

5.创建Nominatim专用用户

1
2
3
4
5
6
7
8
9
// To create the user and directory run
sudo useradd -d /srv/nominatim -s /bin/bash -m nominatim

// To be able to copy and paste instructions from this manual, export user name and home directory now like this:
export USERNAME=nominatim
export USERHOME=/srv/nominatim

// Make sure that system servers can read from the home directory:
chmod a+x $USERHOME

6.设置PostgreSQL

修改配置文件的地方在 /etc/postgresql/12/main/postgresql.conf

对于初次导入,需要设置

1
2
fsync = off
full_page_writes = off

开启postgresql

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
root@d505aab0a27b:/var/run/postgresql# service postgresql status
12/main (port 5432): down
root@d505aab0a27b:/var/run/postgresql# systemctl postgresql status
ERROR:systemctl:Unknown operation postgresql.
root@d505aab0a27b:/var/run/postgresql# service postgresql start
 * Starting PostgreSQL 12 database server                                                                        [ OK ]
root@d505aab0a27b:/var/run/postgresql# service postgresql status
12/main (port 5432): online
root@d505aab0a27b:/var/run/postgresql# systemctl status postgresql
postgresql.service - PostgreSQL RDBMS
    Loaded: loaded (/usr/lib/systemd/system/postgresql.service, enabled)
    Active: active (running)

初次导入后记得重新打开。设置完成后重启postgresql:

1
sudo systemctl restart postgresql

创建两个postgresql用户(一个导入数据,一个用于webserver的只读用户):

1
2
3
4
5
6
root@d505aab0a27b:/var/run/postgresql# su postgres
postgres@d505aab0a27b:/var/run/postgresql$ createuser -s $USERNAME
postgres@d505aab0a27b:/var/run/postgresql$ createuser www-data
postgres@d505aab0a27b:/var/run/postgresql$ exit
exit
root@d505aab0a27b:/var/run/postgresql#

7.安装Nominatim

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
apt-get install -y wget
cd $USERHOME
wget https://nominatim.org/release/Nominatim-4.1.0.tar.bz2
tar xf Nominatim-4.1.0.tar.bz2
wget -O data/country_osm_grid.sql.gz https://www.nominatim.org/data/country_grid.sql.gz
mkdir $USERHOME/build
cd $USERHOME/build
cmake $USERHOME/Nominatim-4.1.0
make
sudo make install

8.导入数据

创建文件夹作为工作目录

1
2
3
mkdir $USERHOME/nominatim-project
mkdir $USERHOME/nominatim-project/website
export PROJECT_DIR=$USERHOME/nominatim-project

导入过程会使用该文件夹

PROJECT_DIR 导入数据

导入数据库前要给文件夹赋予权限

1
chmod -R a+x nominatim-project/
1
su $USERNAME -c "nominatim import --osm-file beijing.osm.pbf --offline 2>&1 | tee setup.log"

注意:需加 offline 参数,不加的话在导入的时候会请求外网链接导致数据导入失败

在将pbf数据导入到数据库时出现如下错误

2022-11-10 21:07:22: Post-process tables 2022-11-10 21:07:28: Create search index for default country names. 2022-11-10 21:07:29: Recompute word counts 2022-11-10 21:07:31: Setup website at /srv/nominatim/nominatim-project/data/website 2022-11-10 21:07:32: Failed to load URL: https://www.openstreetmap.org/api/0.6/node/10169283021/1 2022-11-10 21:07:32: Cannot determine date of database: <urlopen error EOF occurred in violation of protocol (_ssl.c:1131)>

image-20230727232637005

如果操作错误,一次没有导入成功,再次导入时记得先删除数据库再导入:

1
su $USERNAME -c "dropdb nominatim "

9.测试是否安装成功

1
2
nominatim admin --check-database
nominatim serve

测试

1
2
3
4
5
root@d505aab0a27b:/# curl http://localhost:8088/status.php
OK

root@d505aab0a27b:/# curl http://localhost:8088/search.php?q=tiananmen
[{"place_id":281751,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"relation","osm_id":941596,"boundingbox":["39.8988884","39.9058312","116.3896532","116.3932766"],"lat":"39.90272175","lon":"116.39144087676334","display_name":"天安门广场, 东城区, 北京市, 100010, 中国","place_rank":25,"category":"place","type":"square","importance":0.125},{"place_id":114335,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"node","osm_id":5211002739,"boundingbox":["39.9055272","39.9056272","116.3912549","116.3913549"],"lat":"39.9055772","lon":"116.3913049","display_name":"天安门广场旗杆, 西长安街, 西长安街街道, 东城区, 北京市, 100010, 中国","place_rank":30,"category":"man_made","type":"flagpole","importance":0.0001}]

10.设置web服务器

1
sudo apt install -y apache2 libapache2-mod-php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
sudo tee /etc/apache2/conf-available/nominatim.conf << EOFAPACHECONF
<Directory "$USERHOME/nominatim-project/website">
  Options FollowSymLinks MultiViews
  AddType text/html   .php
  DirectoryIndex search.php
  Require all granted
</Directory>

Alias /nominatim $USERHOME/nominatim-project/website
EOFAPACHECONF
1
2
3
sudo a2enconf nominatim

sudo systemctl restart apache2

设置完成之后便可请求 nominatim 的api http://localhost/nominatim/

基于Docker

1.nominatim-docker修改

默认的nominatim-docker在导入数据的时候需要请求外网,会导致导入数据失败

在将pbf数据导入到数据库时出现如下错误

2022-11-10 21:07:22: Post-process tables 2022-11-10 21:07:28: Create search index for default country names. 2022-11-10 21:07:29: Recompute word counts 2022-11-10 21:07:31: Setup website at /srv/nominatim/nominatim-project/data/website 2022-11-10 21:07:32: Failed to load URL: https://www.openstreetmap.org/api/0.6/node/10169283021/1 2022-11-10 21:07:32: Cannot determine date of database: <urlopen error EOF occurred in violation of protocol (_ssl.c:1131)>

因此需将docker构建文件中的init.sh在下述位置加上 --offline 重新 build 生成新的 nominatim 镜像即可

image-20230727232839715

2.容器之间的通信

启用默认的bridge模式

使用nominatim-docker启动项目

1
docker run -itd -e PBF_PATH=/nominatim/data/beijing-latest.osm.pbf  -p 8080:8080 -p 5432:5432 -v /e/Projects/DockerWorkspace/nominatim/data:/nominatim/data --name nominatim_docker2 nominatim:1.0

请求的地址为 http://localhost:8080/search.php?q=beijing

其他容器访问该容器的方式:

1
curl 172.17.0.4:8080/search.php?q=beijing

172.17.0.4 为nominatim-docker分配的ip

3.nominatim查询api

https://nominatim.org/release-docs/latest/api/Search/