📜 ⬆️ ⬇️

Install and configure OSM-based tile generator in Ubuntu or Debian

Openstreetmap Most recently, the task of creating software for generating map tiles has arisen. As a basis, the choice fell on mapnik (there are few alternatives to it). As it turned out, there were a lot of difficulties and unexpected mistakes on the way, and more or less clear documentation on how to set up everything on a turnkey basis could not be found. Having fidgeted for some time, I managed to collect a lot of rakes that may arise and bring the matter to the bitter end. About this article.

Installation was done in Ubuntu and Debian. I will say right away that it is certainly better to use the latest versions of software products that are sometimes not in the repositories. They can be downloaded, if desired, manually from the official sites.

Apart from the necessary dependencies, in general we will need

')

Install PostgreSQL


First, check which version of PostgreSQL we have in the repositories:
$ apt-cache show postgresql 

If version 8.4 or more, then install the package.
 $ sudo apt-get install postgresql 

Otherwise, download the package from the official website www.postgresql.org/download/linux and install it. Next we need to set up our database. By default, its user is postgres and you can only log in from it using OC itself. However, we will act somewhat differently, first open the pg_hba.conf file. You can find out its location using the locate utility.

 $ sudo updatedb $ sudo locate pg_hba.conf 

If you have not installed this utility, then set
 $ sudo apt-get install findutils locate 

and repeat the commands above. For example, I have this file located at /etc/postgresql/8.4/main/pg_hba.conf. Open it and edit it.

 $ sudo vi "/etc/postgresql/8.4/main/pg_hba.conf" 

Replace:
 $ locate all all ident #    

On
 $ locate all all password #    

Save the file and restart PostgreSQL.
 $ sudo "/etc/init.d/postgresql-8.4 restart" 

Now we can create any user base and log in with a regular password without any encryption (we are not critical to super security). We also need a new database for our OSM data.
Go to the PostgreSQL management console.
 $ su postgres $ psql postgres=# CREATE ROLE osm WITH SUPERUSER PASSWORD 'my_password' LOGIN; CREATE ROLE postgres=# CREATE DATABASE osm; CREATE DATABASE postgres=# \q 

User and database created.
You can check the performance of a new user with the command
 $ psql -U osm -d osm -W 

If you enter the PostgreSQL console after entering the password, then everything went well.

PostGIS installation


We will need PostGIS. In my repository was version 1.4. Since, after its installation, for some reason I could not find the file postgis.sql, I demolished this version and downloaded version 1.5 from the official site. Therefore, in the article, we will do the same.
 $ wget "http://postgis.refractions.net/download/postgis-1.5.4.tar.gz" 

We unpack archive, we collect and we install.
 $ tar xvfz "./postgis-1.5.4.tar.gz" $ cd "./postgis-1.5.4" $ sudo ./configure $ sudo make install 

In the absence of the necessary libraries, install them.
Next, install a special language in the osm database.
 $ createlang plpgsql osm -U osm -W 

Now it is necessary to execute two SQL scripts in the osm database: postgis.sql and 900913.sql.
Using the locate utility, we find their location and execute them.
 $ psql -U osm -d osm -W -f "/usr/share/postgresql/8.4/contrib/postgis-1.5/postgis.sql" $ psql -U osm -d osm -W -f "/usr/share/osm2pgsql/900913.sql" 

Note that as practice has shown, this command must be executed exactly from the user with the privileges of the PostgreSQL superuser. If you have problems and errors in the ERROR plan : type "geometry" does not exist , then try the following before running the sql file:
 $ sudo ldconfig 

and repeat the command to run sql files.
Everything! With PostgreSQL setup, we are done. Go to the installation Mapnik.

Install Mapnik


 $ sudo add-apt-repository ppa:mapnik/nightly-trunk $ sudo apt-get update $ sudo apt-get install libmapnik mapnik-utils python-mapnik 

If the system shows that “add-apt-repository: command not found”, then
 $ sudo apt-get install python-software-properties 

And repeat the three commands above. Mapnik installed. I did not write about the need to install Python, since in most cases it is always worth it. Checking the work mapnik.
 $ python >>> import mapnik 

If there were no errors, then everything went well. Now we are going to install osm2pgsql

Installing Osm2pgsql


 $ sudo apt-get install osm2pgsql 

There is one important touch. The fact is that the default.style file supplied by osm2pgsql for exporting osm data to a database for some reason does not conform to the latest OSM format. (Maybe someone knows why?). Download the file in the correct format.
 $ wget "http://svn.openstreetmap.org/applications/utils/export/osm2pgsql/default.style" 

Next, replace the default one with osm2pgsql
 $ sudo cp "./default.style" "/usr/share/osm2pgsql/default.style" 


Installing scripts from OpenStreetMap to generate tiles


The OpenStreetMap repository svn.openstreetmap.org contains many scripts and utilities for cartographic topics. We need one of the applications written in python, which already contains all the necessary scripts for working with mapnik. To download it you need to install Subversion.
 $ sudo apt-get install subversion 

Now you can safely download OSM Application. Create a folder for its location. for example, I have this / home / osm / mapnik and run checkout.
 $ mkdir "/home/osm/mapnik" $ svn co "http://svn.openstreetmap.org/applications/rendering/mapnik" "/home/osm/mapnik" 

Now run the script in the application.
 $ bash /home/osm/mapnik/get-coastlines.sh 

It downloads us the necessary files with the shapes of the world.
Now you need to create an XML style file. This is done by the following command:
 $ python /home/osm/mapnik/generate_xml.py osm.xml my_osm.xml --dbname osm --user osm --password my_password --accept-none 

This will create a file my_osm.xml with the data to connect to PostgreSQL.

Attempt at writing


So the moment has come when you can download any OSM file and generate tiles on its basis. OSM files can be downloaded from sites presented on the wiki.openstreetmap.org/wiki/Planet.osm web page .
You can, of course, download the entire Planet.osm file, but do you need it? When unpacked, it weighs over 250 GB.

Suppose we want to generate Moscow tiles on the 17th scale.
Download the required OSM file.
 $ wget "http://download.bbbike.org/osm/bbbike/Moscow/Moscow.osm.gz" 

Now we can export it to the database.
 $ sudo osm2pgsql -U osm -d osm Moscow.osm.gz 

Everything! It remains only to run the script for generating tiles. But before that, we need to slightly edit it in order to indicate the desired scale and coordinates of which tiles we want to receive. Open the file /home/osm/mapnik/generate_tiles.py. Set the mapfile variable to point to our my_osm.xml.
 mapfile = "/home/osm/mapnik/my_osm.xml" 

Next, override the variable that indicates where you want to add tiles.
 tile_dir = "/home/osm/mapnik/all_tiles" 

The Moscow.osm file that we downloaded has the coordinates of the vector data
 xMin = 37.32000 yMin = 55.57000 xMax = 37.88000 yMax = 55.92000 

We find such lines in the script
 bbox = (-180.0,-90.0, 180.0,90.0) render_tiles(bbox, mapfile, tile_dir, 0, 5, "World") 

And before them (so as not to overwrite the existing code) we write:
 bbox = (37.32000, 55.57000, 37.88000, 55.92000) render_tiles(bbox, mapfile, tile_dir, 17, 17, "Moscow") exit() 

It is advisable to write exit (), so that the script after generating the Moscow tiles does not violate the generation of the whole world.
4 and 5, the parameters of the render_tiles function set the scale with which for which we will generate tiles. In this case, we chose only 17 scale.

Save and run.
 $ python /home/osm/mapnik/generate_tiles.py 

Will the process of generating tiles.

[UPD] You can also find a great manual here. Switch2osm.org/serving-tiles/manually-building-a-tile-server

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


All Articles