📜 ⬆️ ⬇️

Installing OpenStreetMap Nominatim to find latitude and longitude at the address entered

image

I would like to tell my story about installing the Nominatim geocoder on a dedicated server. Initially it was assumed that this task would take me about 5-7 hours, but it was not there ... Therefore, it was decided to write an article c describing how Nominatim was deployed to the server until the site was fully operational. But first things first.

Introduction


There are many services with geocoders, I will not list them all here, I note that there are good articles on Habré on this subject: "Geocoding with PHP and the Google Maps API" , and also "Yandex Maps: Search for arbitrary objects" .

For those who do not know: Geocoding is the process of finding the latitude and longitude at the entered address.
My choice fell on Nominatim, since it can be deployed on my server and not limited to the number of requests, and I also already had experience with OSM maps and I would like to use it.
')

Documentation


A site with an already working map can be found at the link: OpenStreetMap Nominatim
And here is a link to sample requests: Wiki nominatim
There is also a link to the installation of Nominatim Installation . But this documentation is a bit outdated, for installation it is for this article that you need to dance a bit with a tambourine. Everything described below will refer to this article on installation, only without the participation of a tambourine.
And there is also a docker container nominatim docker . For some reason, most likely due to outdated installation packages, this container did not start with me.

Step 1: Create a Virtual Machine


The machine was based on a standart D2 Microsoft Azure cloud with features: 2 cores, 7 GB RAM, 100 GB SSD. It is on this machine that Nominatim will be installed. 100GB is actually more than necessary for my task, since the map of Ukraine is not so big. After installing all the necessary components left free 70%.

Step 2: Install the necessary packages


sudo apt-get update sudo apt-get -y install wget sudo apt-get -y install build-essential automake sudo apt-get -y install libxml2-dev sudo apt-get -y install libgeos-dev sudo apt-get -y install libpq-dev sudo apt-get -y install libbz2-dev sudo apt-get -y install libtool libproj-dev sudo apt-get -y install libgeos++-dev sudo apt-get -y install gcc proj-bin libgeos-c1 git osmosis sudo apt-get -y install php5 php-pear php5-pgsql php5-json sudo apt-get -y install bc sudo apt-get -y install postgresql-9.4 postgresql-9.4-postgis-2.1 postgresql-contrib-9.4 postgresql-server-dev-9.4 sudo apt-get -y install libboost-chrono1.55-dev libboost-thread1.55-dev libboost-filesystem1.55-dev sudo apt-get -y install python-software-properties && add-apt-repository -y ppa:kakrueger/openstreetmap && apt-get update && apt-get --no-install-recommends install -y osm2pgsql 

Step 3: Setup


 sudo -i pear install DB sudo -u username sudo mkdir -p /app/nominatim sudo -i useradd -m -p password1234 -d /app/nominatim nominatim chown nominatim: /app/nominatim cd /app/nominatim wget http://www.nominatim.org/release/Nominatim-2.4.0.tar.bz2 tar xvf Nominatim-2.4.0.tar.bz2 rm Nominatim-2.4.0.tar.bz2 mv Nominatim-2.4.0/* . rm Nominatim-2.4.0/ sudo -u nominatim ./autogen.sh ./configure && make 

I think it’s not difficult to figure out these command lines. In the event that an error occurs in obtaining access rights, we will set read and write access rights for all users and chmod -R 755 / app groups. I would like to note that sometimes there was a problem when executing the make command . if you have this problem, use sudo make clean , and then ./configure && make .

Step 4: PostgreSQL


Initially, postgres is not configured for a combat server, so you need to configure it here from the nominatim documentation wiki:
Ubuntu location /etc/postgresql/9.x/main/postgresql.conf
CentOS location /var/lib/pgsql/data/postgresql.conf

shared_buffers (4GB)
maintenance_work_mem (10GB)
work_mem (50MB)
effective_cache_size (24GB)
synchronous_commit = off
checkpoint_segments = 100
checkpoint_timeout = 10min
checkpoint_completion_target = 0.9
For 32GB RAM machine

 sudo passwd postgres sudo usermod -a -G sudo postgres service postgresql start && pg_dropcluster --stop 9.4 main service postgresql start && pg_createcluster --start -e UTF-8 9.4 main service postgresql start && sudo -u postgres psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='nominatim'" | grep -q 1 || sudo -u postgres createuser -s nominatim && sudo -u postgres psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='www-data'" | grep -q 1 || sudo -u postgres createuser -SDR www-data && sudo -u postgres psql postgres -c "DROP DATABASE IF EXISTS nominatim" 

This is the easiest part of the installation work. And now hardcore begins, because further actions are not always performed correctly. And after such an incorrect end, unfortunately, sometimes you have to perform actions anew on a new virtual machine.

Step 5: Download and install osm cards


At the beginning, you need to download the map from the site in the * .pbf format: OpenStreetMap Data Extracts . As an example, I load the map of Ukraine and rename it to data.pbf.

 wget --output-document=data.pbf http://download.geofabrik.de/europe/ukraine-latest.osm.pbf chown nominatim: data.pbf touch local.php /app/nominatim/settings/local.php nano /app/nominatim/settings/local.php 

The last command opens the nominatim installation configuration file. In the file local.php you need to enter the code which is described below.

 <?php // Paths @define('CONST_Postgresql_Version', '9.4'); @define('CONST_Postgis_Version', '2.1'); // Website settings @define('CONST_Website_BaseURL', 'http://geocoder.cloudapp.net/'); ?> 

As BaseURL, you must enter the address of the site from which nominatim will be launched.

Now run the installation command nominatim. This operation takes quite a long time, for example, with a map of Ukraine, the installation lasted about 7 hours. Due to the fact that I executed all the commands via ssh, and am prone to alternate disconnection of the Internet, the installation command is executed in the screenshot .

 screen service postgresql start && sudo -u nominatim -- ./utils/setup.php --osm-file /app/nominatim/data.pbf --all --threads 2 2>&1; sudo -u nominatim -- ./utils/setup.php --index --create-search-indices 

To exit the screen you need to type the combination Ctrl + A then Ctrl + D. The screen -r command returns the screen.

Now we will launch the website in a quick way:

 ./utils/setup.php --create-website /var/www/html rm /var/www/html/index.html /etc/init.d/apache2 restart 

I did without apache settings or working with nginx, since you can easily read about this in other articles on Habré. Yes, and without setting the site works great.

Testing the site


As a result, we should see something similar, as in my screenshot:

image

Check how api works by making a request:
http://geocoder.cloudapp.net/?format=json&addressdetails=1&q=Odessa&format=json&limit=1
Response:

 [{"place_id":"1145869","licence":"Data © OpenStreetMap contributors, ODbL 1.0. http:\/\/www.openstreetmap.org\/copyright","osm_type":"relation","osm_id":"1413934","boundingbox":["46.342707","46.6291187","30.6114013","30.8313753"],"lat":"46.4858883","lon":"30.68365101101","display_name":",  , ","class":"place","type":"city","importance":0.45,"icon":"http:\/\/geocoder.cloudapp.net\/images\/mapicons\/poi_place_city.p.20.png","address":{"city":"","county":"","state":" ","country":"","country_code":"ua"}}] 

findings


In the end, I would like to note that to download a map of the Earth, you need a server that is more powerful than the D2 series, if you don’t want to wait * a week for the download. This problem can also be solved by temporarily scaling the server to the D14 series (16 cores, 112 GB of memory). But the time to fulfill a search query is very good: on average, a search in Ukraine takes only 300 ms.

I hope this article will help other developers spend less time deploying Nominatim and see if you need it. Maybe you should take it ready and use?

If someone from the Hub community will need a working Docker container with Nominatim, or other ideas - write, always happy to talk.

Source: https://habr.com/ru/post/259667/


All Articles