在Centos7上安装Nominatim

0.环境介绍

系统:CentOS 7.2

1.准备工作

:tw-26a0: Never, ever run the installation as a root user.
通过root用户将一个普通用户joe加入sudoer,解决 xxx is not in the sudoers file。命令:

visudo

加入红框中的内容

Note: these installation instructions are also available in executable form for use with vagrant under vagrant/Install-on-Centos-7.sh
https://github.com/openstreetmap/Nominatim/blob/master/vagrant/Install-on-Centos-7.sh

Hardware

A minimum of 2GB of RAM is required or installation will fail. For a full planet import 32GB of RAM or more strongly are recommended.

For a full planet install you will need about 500GB of hard disk space (as of June 2016, take into account that the OSM database is growing fast). SSD disks will help considerably to speed up import and queries.

On a 6-core machine with 32GB RAM and SSDs the import of a full planet takes a bit more than 2 days. Without SSDs 7-8 days are more realistic.

2.安装必要的软件

#更新软件包
sudo yum update -y
#The standard CentOS repositories don't contain all the required packages,Enable the EPEL repository
sudo yum install -y epel-release
#安装软件
sudo yum install postgresql-server postgresql-contrib postgresql-devel postgis postgis-utils git cmake make gcc gcc-c++ libtool policycoreutils-python php-pgsql php php-pear php-pear-DB php-intl libpqxx-devel proj-epsg bzip2-devel proj-devel geos-devel libxml2-devel boost-devel expat-devel zlib-devel

For compiling:

For running Nominatim:

  • PostgreSQL (9.1 or later)
  • PostGIS (2.0 or later)
  • PHP (5.4 or later)
  • PHP-pgsql
  • PHP-intl (bundled with PHP)
  • PEAR::DB
  • a webserver (apache or nginx are recommended)

:tw-26a0: Note that you must install php5, php7 does not work.

For running continuous updates:

查看软件版本

#查看PHP版本
php -v
#查看PostgreSQL版本
psql --version
#查看PostGIS版本
rpm -qa | grep postgis

3.系统设置和配置

Nominatim will run as a global service on your machine. It is therefore best to install it under its own separate user account. In the following we assume this user is called nominatim and the installation will be in /srv/nominatim. To create the user and directory

#创建账号并设置目录
sudo useradd -d /srv/nominatim -s /bin/bash -m nominatim
export USERNAME=nominatim
export USERHOME=/srv/nominatim
#Make sure that system servers can read from the home directory:
chmod a+x $USERHOME

4.设置PostgreSQL数据库

#初始化
sudo postgresql-setup initdb
sudo systemctl enable postgresql
#修改PostgreSQL默认配置
vim /var/lib/pgsql/data/postgresql.conf

添加或修改如下内容:

fsync = off
full_page_writes = off
shared_buffers (2GB)
maintenance_work_mem (10GB)
work_mem (50MB)
effective_cache_size (24GB)
synchronous_commit = off
checkpoint_segments = 100 # only for postgresql <= 9.4
checkpoint_timeout = 10min
checkpoint_completion_target = 0.9

:tw-26a0: Don't forget to reenable fsync full_page_writesafter the initial import or you risk database corruption. Autovacuum must not be switched off because it ensures that the tables are frequently analysed.

#重启PostgreSQL数据库
sudo systemctl restart postgresql

创建三个PostgreSQL数据库用户:
one for the user that does the import;
another for the webserver which should access the database only for reading:
another for the website user www-data as a PostgreSQL database role

sudo -u postgres createuser -s $USERNAME
sudo -u postgres createuser apache
sudo -u postgres createuser -SDR www-data

5.设置Apache Webserver

You need to create an alias to the website directory in your apache configuration. Add a separate nominatim configuration to your webserver:

sudo tee /etc/httpd/conf.d/nominatim.conf << EOFAPACHECONF
<Directory "$USERHOME/Nominatim/build/website">
  Options FollowSymLinks MultiViews
  AddType text/html   .php
  DirectoryIndex search.php
  Require all granted
</Directory>

Alias /nominatim $USERHOME/Nominatim/build/website
EOFAPACHECONF
#重启Apache服务器
sudo systemctl restart httpd

6.Adding SELinux Security Settings

It is a good idea to leave SELinux enabled and enforcing, particularly with a web server accessible from the Internet. At a minimum the following SELinux labeling should be done for Nominatim:

sudo semanage fcontext -a -t httpd_sys_content_t "$USERHOME/Nominatim/(website|lib|settings)(/.*)?"
sudo semanage fcontext -a -t lib_t "$USERHOME/Nominatim/module/nominatim.so"
sudo restorecon -R -v $USERHOME/Nominatim

7.安装Nominatim

#进入/srv/nominatim目录,获取源码
cd $USERHOME
git clone --recursive git://github.com/openstreetmap/Nominatim.git
cd Nominatim
#When installing the latest source from github, you also need to download the country grid:
wget -O data/country_osm_grid.sql.gz http://www.nominatim.org/data/country_grid.sql.gz
#The code must be built in a separate directory.
mkdir build
cd build
cmake $USERHOME/Nominatim
make

create a minimal configuration file that tells nominatim the name of your webserver user and the URL of the website:

tee settings/local.php << EOF
<?php
 @define('CONST_Database_Web_User', 'apache');
 @define('CONST_Website_BaseURL', '/nominatim/');
EOF

8.导入 OSM data

If you plan to import a large dataset (e.g. Europe, North America, planet), you should also enable flatnode storage of node locations. With this setting enabled, node coordinates are stored in a simple file instead of the database.Add to your /srv/nominatim/Nominatim/build/settings/local.php

#因为我导入的地图数据较小,未设置该项
@define('CONST_Osm2pgsql_Flatnode_File', '/srv/nominatim/Nominatim/data/flatnode.file');

:tw-26a0: Replace the second part with a suitable path on your system and make sure the directory exists. There should be at least 35GB of free space.

8.1下载辅助数据(备选项)

8.1.1 Wikipedia rankings

Wikipedia can be used as an optional auxiliary data source to help indicate the importance of osm features. Nominatim will work without this information but it will improve the quality of the results if this is installed.

cd /srv/nominatim/Nominatim/data
wget http://www.nominatim.org/data/wikipedia_article.sql.bin
wget http://www.nominatim.org/data/wikipedia_redirect.sql.bin

Combined the 2 files are around 1.5GB and add around 30GB to the install size of nominatim. They also increase the install time by an hour or so.

8.1.2 UK postcodes

Nominatim can use postcodes from an external source to improve searches that involve a UK postcode.

cd /srv/nominatim/Nominatim/data
wget http://www.nominatim.org/data/gb_postcode_data.sql.gz

8.2导入地图数据

Important: first try the import with a small excerpt, for example from Geofabrik.
数据可以从Geofabrik下载,建议下载.osm.pbf格式。

#下载中国的数据
cd /srv/nominatim/Nominatim/data
wget http://download.geofabrik.de/asia/china-latest.osm.pbf
#导入数据
cd /srv/nominatim/Nominatim/build
./utils/setup.php --osm-file ../data/china-latest.osm.pbf --all 2>&1 | tee setup.log
# 

:tw-26a0: The --osm2pgsql-cache parameter is optional but strongly recommended for planet imports. It sets the node cache size for the osm2pgsql import part (see -C parameter in osm2pgsql help). 28GB are recommended for a full planet import, for excerpts you can use less. Adapt to your available RAM to avoid swapping, never give more than 2/3 of RAM to osm2pgsql.当导入的数据十分大时(比如超过1GB)建议使用以下命令:

./utils/setup.php --osm-file ../data/china-latest.osm.pbf --all [--osm2pgsql-cache 28000] 2>&1 | tee setup.log

完成导入数据大概需要1个小时!!!

8.3导入额外数据Loading additional datasets

The following commands will create additional entries for POI searches:

cd /srv/nominatim/Nominatim/build
./utils/specialphrases.php --wiki-import > specialphrases.sql
psql -d nominatim -f specialphrases.sql

9.保持数据是最新的Updates

There are many different possibilities to update your Nominatim database. The following section describes how to keep it up-to-date with Pyosmium. For a list of other methods see the output of

cd /srv/nominatim/Nominatim/build
./utils/update.php --help

9.1安装Pyosmium

It is recommended to install Pyosmium via pip:

pip install --user osmium

Nominatim needs a tool called pyosmium-get-updates that comes with Pyosmium. You need to tell Nominatim where to find it. Add the following line to your build/settings/local.php:

@define('CONST_Pyosmium_Binary', '/home/user/.local/bin/pyosmium-get-changes');

The path above is fine if you used the --user parameter with pip. Replace user with your user name.

9.2设置更新时间

Next the update needs to be initialised. By default Nominatim is configured to update using the global minutely diffs.
If you want a different update source you will need to add some settings to build/settings/local.php. For example, to use the daily country extracts diffs for Ireland from geofabrik add the following:

// base URL of the replication service
@define('CONST_Replication_Url', 'http://download.geofabrik.de/europe/ireland-and-northern-ireland-updates');
// How often upstream publishes diffs
@define('CONST_Replication_Update_Interval', '86400');
// How long to sleep if no update found yet
@define('CONST_Replication_Recheck_Interval', '900');

To set up the update process now run the following command:

cd /srv/nominatim/Nominatim/build
./utils/update --init-updates

It outputs the date where updates will start. Recheck that this date is what you expect.
The --init-updates command needs to be rerun whenever the replication service is changed.

10.更新Nominatim

The following command will keep your database constantly up to date:

cd /srv/nominatim/Nominatim/build
./utils/update.php --import-osmosis-all

If you have imported multiple country extracts and want to keep them up-to-date, have a look at the script in issue #60.

11.参考资料

1.https://github.com/openstreetmap/Nominatim/blob/master/docs/Install-on-Centos-7.md
2.https://github.com/openstreetmap/Nominatim/blob/master/docs/Import-and-Update.md
3.https://github.com/openstreetmap/Nominatim/blob/master/docs/Installation.md

12.错误解决Troubleshooting

1.https://github.com/openstreetmap/Nominatim/blob/master/docs/Faq.md
2.http://wiki.openstreetmap.org/wiki/Nominatim/Installation/Troubleshooting
3.ERROR: permission denied for relation

参考

  1. https://stackoverflow.com/questions/13497352/error-permission-denied-for-relation-tablename-on-postgres-while-trying-a-selec
  2. https://stackoverflow.com/questions/15520361/permission-denied-for-relation/37389690
    我的解决方案:
#切换用户
su - nominatim
进入PostgreSQL
psql
#使用nominatim数据库
c nominatim
#给apache用户赋予权限
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO apache;

比较好的解决方案:(最后的操作:把用户readonly改为apache,或者修改build/settings/local.phpCONST_Database_Web_User为readonly)

CREATE USER readonly  WITH ENCRYPTED PASSWORD 'readonly';
GRANT USAGE ON SCHEMA public to readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly;
#在每个数据库上重复以下代码,以nominatim数据库为例:
GRANT CONNECT ON DATABASE nominatim to readonly;
c nominatim
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO readonly;
#this grants privileges on new tables generated in new database "nominatim"
GRANT USAGE ON SCHEMA public to readonly;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;

13.访问Nominatim主页

1.http://localhost/nominatim
2.逆地理编码查询
http://localhost/nominatim/reverse?format=xml&lat=31&lon=121&zoom=18&addressdetails=1

原文地址:https://www.cnblogs.com/joejiafeng/p/7117330.html