Installation and configuration of the generating map tiles based on OSM data under Windows

Disclaimer


Recently, I had the task of generating map tiles based on OSM data. I did some research, read more articles, but everywhere featured *nix systems, and I was in the presence of a server running Windows. In the end, more or less distinct tutorial was found, but it is slightly outdated and may not always be detailed, therefore, had to Tinker. After successful completion thought my experience will be useful.

Anyone interested, I ask under kat.

the

Introduction


The program is installed on a clean Windows 7 and 8.1 without any software. You will need the following:
the
    the
  • PostgreSQL (used 9.4.4) is a database for storing geographic information from the OSM dump.
  • the
  • PostGIS (used 2.1), the extension of the database to work with geo-information.
  • the
  • Osm2pgsql is a utility to import a dump of OSM data in the database.
  • the
  • Mapnik (2.2.0 used) — utility to generate map tiles.

You will also need additional tools, without which, however, nothing happens:
the
    the
  • Patch to GiST indexes in PostGIS
  • the
  • Python 2.x (used 2.7.10) — it is written the scripts generate tiles.
  • the
  • PROJ.4 — library of map projections.
  • the
  • the SVN client (we use TortoiseSVN) for pumping the scripts generate map tiles from OSM, see below.
  • the
  • Scripts generating from OSM (used revision 31519)
  • the
  • OSM information: the coastline and all that.

the

1. Installing and configuring PostgreSQL


The distribution can take the offside. The installation is rather trivial, however, there are a couple of points.

First, you may want to change the directory where is stored the database (may need up to a terabyte, in case of using OSM data for the entire planet). By default (for version 9.4) is C:\Program Files\PostgreSQL\9.4\data

Second, you may want to change the locale of the cluster. In my case, the Russian in Russia. During the installation it will also ask a password for the PostgreSQL server, we need it later, remember it.

After installing the PostgreSQL StackBuilder starts, the installation wizard extensions for the database. You need to choose two extensions: Spatial Extensions -> PostGIS Database Drivers -> JDBC. All installation settings by default. In the process of installing PostGIS will be prompted to create a system variable and a few more questions, agree. After that you need to restart the PostgreSQL service.

Next, add bin directory in the PostgreSQL PATH. After adding you need to log out and log in again (logoff-login) for the settings to take effect.

Check the correctness of the installation, you will need to remember the password that you entered during the installation phase. Find programs pgAdmin III and run. In the servers list will be the only localhost:5432, connect. If it fails, most likely the service is not running.

Creating a template for the geodatabase:
the
CREATE DATABASE template_postgis_20
WITH ENCODING='UTF8'
OWNER=postgres
CONNECTION LIMIT=-1
LC_COLLATE = 'Russian_Russia.1251'
LC_CTYPE = 'Russian_Russia.1251'
TABLESPACE=pg_default;

Add two extensions to the template: postgis and postgis_topology. Disconnect from the database.

Create database gis template-based template_postgis_20:
the
CREATE DATABASE gis
WITH OWNER = postgres
ENCODING = 'UTF8'
TEMPLATE = template_postgis_20
TABLESPACE = pg_default
LC_COLLATE = 'Russian_Russia.1251'
LC_CTYPE = 'Russian_Russia.1251'
CONNECTION LIMIT = -1;

Take the patch for PostGIS GiST here. Patchin from the command line:
the
C:\Users\red>psql -U postgres -d gis -f legacy-postgis-gist.sql
CREATE OPERATOR CLASS

The database is ready.

the

2. Install and configure Osm2pgsql


You can get the latest release github and collect yourself, and you can take the artifact here.

Download, unpack, rename for convenience, osm2pgsql. Add this directory to the variable PATH. Logoff-login. Check that everything works:

Should show the help. Take the last default.style from the repository on github.

Everything is ready to import OSM data.

The map of the planet in a compressed form weighs ~40 GB. For example, you can take a map of Moscow, for example, here. Downloaded.

For import runs in the command prompt:
the
C:\Users\red>osm2pgsql -d gis -U postgres -W-H localhost -P 5432 -s-s C:\osm2pgsql\default.style RU-MOW.osm.pbf

Option -s, i.e. slim mode, in which temporary data is stored in the database. This slows down the process, increases disk utilization, but reduces use of RAM.

If everything is correct, will request the password and start the import of the dump into the database. In the first lines will be all sorts of warnings, do not worry, all right. After the import is complete, you will receive something like:
the
Osm2pgsql took 160s overall

GEODATA in the database.

the

3. Install Python 2.x


Download the distribution package x86 (because the Mapnik Windows binaries are x86 only) here. Put settings on default, except for one thing: it is worth noting the "Add python.exe in the PATH", or do it by the handles after installation. Logoff-login.

Check for proper installation:
the
C:\Users\red>python
2.7.10 Python (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Python is ready.

the

4. Install and configure Mapnik'


Again, there are two options: either to build my own from source from github, or get ready binaries here. Looking for Windows 32 bit Package is downloaded, unpack it into C:\mapnik-v2.2.0.

They have a pretty detailed manual installation, in the wiki. In short: add lib and bin directory of mapnik in PATH, it is time. To create a new system variable PYTHONPATH and assign it the path to the C:\mapnik-v2.2.0\python\2.7\site-packages, is two. To put this vcredist, that's three. Logoff-login.

Check for proper installation:
the
C:\Users\red>python
2.7.10 Python (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>import mapnik
>>>
Possible errors parsed in the wiki. But in addition, it is worth to try to move path to lib and bin in PATH.

And generik test tiles:
the
C:\Users\red>cd C:\mapnik-v2.2.0\demo\python
C:\mapnik-v2.2.0\demo\python>python rundemo.py

Pycairo not available... will render Cairo formats using alternative method

12 maps have been rendered in the current directory:
- demo.png
- demo256.png
- demo64_binary_transparency.png
- demo128_colors_hextree_no_alpha.png
- demo_high.jpg
- demo_low.jpg
- demo.tif
- demo.pdf
- demo.ps
- demo.svg
- demo_cairo_rgb.png
- demo_cairo_argb.png

Have a look!

Almost all.

Go to github, find the library PROJ.4, find the binaries: Prebuilt Win32 executables, DLL including NAD27 grid shift files. Download, unpack (c:\proj). In PATH add the path to the bin, a new variable PROJ_LIB path to nad. Logoff-login.

Some more.

Downloadable scripts for generating map tiles from the official svn repository, put in c:\mapnik.

Next, the script needs the shoreline and stuff. This is the script get-coastlines.sh, however it is written in bash under Windows will not work. But it is quite simple and short, so just repeat the steps handles.

Download:
the Unpack all the archives into the directory c:\mapnik\world_boundaries.

Last preparations. Go to mapnik\inc, settings.xml.inc.template is renamed in settings.xml.inc.

The file fontset-settings.xml.inc.template in fontset-settings.xml.inc.
The file datasource-settings.xml.inc.template in datasource-settings.xml.inc and edit it:
the
<Parameter name="type">postgis</Parameter>
<Parameter name="password">password</Parameter>
<Parameter name="host">localhost</Parameter>
<Parameter name="port">5432</Parameter>
<Parameter name="user">postgres</Parameter>
<Parameter name="dbname">gis</Parameter>
<Parameter name="estimate_extent">false</Parameter>

A little fix that the script generate_tiles.py:
Block at line 193:
the
if __name__ == "__main__":
home = os.environ['HOME']
try:
mapfile = os.environ['MAPNIK_MAP_FILE']
except KeyError:
mapfile = home + "/svn.openstreetmap.org/applications/rendering/mapnik/osm-local.xml"
try:
tile_dir = os.environ['MAPNIK_TILE_DIR']
except KeyError:
tile_dir = home + "/osm/tiles/"

if not tile_dir.endswith('/'):
tile_dir = tile_dir + '/'
model
the
if __name__ == "__main__":
try:
mapfile = os.environ['MAPNIK_MAP_FILE']
except KeyError:
print('MAPNIK_MAP_FILE env var not found! Use default.')
mapfile = 'C:/mapnik/osm.xml' #the default
try:
tile_dir = os.environ['MAPNIK_TILE_DIR']
except KeyError:
print('MAPNIK_TILE_DIR env var not found! Use default.')
tile_dir = 'C:/tiles/' #default

if not tile_dir.endswith('/'):
tile_dir = tile_dir + '/'

Specify the boundaries of which will generate tiles (the geographical coordinates of the rectangle):

At line 213 before
the
 bbox = (-180.0,-90.0, 180.0,90.0)
render_tiles(bbox, mapfile, tile_dir, 0, 5, "World")
write (in our case)
the
 bbox = (37.32, 55.57, 37.88, 55.92)
render_tiles(bbox, mapfile, tile_dir, 17, 17, "Moscow")
exit() #to ensure that he did not go to generate a map of the world

Tips&Tricks
the coordinates of the borders taken from the osm file from which data are taken (they will be in the beginning). Due to the fact that the data file can be very large, a plain text editor it will not open, I advise you to use editors like EmEditor (paid) or put a grep.

Specify the path and run the generation of the map tiles:
the
C:\mapnik>generate_tiles.py

The process should go.
Will take from several minutes to weeks (in the case of the whole world and not the most powerful machine).

the

Links


0. softwaresimian.com/2012/12/02/openstreetmap-osm-install-on-windows-part-i-the-database
1. switch2osm.org/serving-tiles/manually-building-a-tile-server
2. wiki.openstreetmap.org/wiki/PostGIS/Installation
3. wiki.openstreetmap.org/wiki/Osm2pgsql
4. wiki.openstreetmap.org/wiki/Creating_your_own_tiles
5. habrahabr.ru/post/144675
6. habrahabr.ru/post/203212
7. Russification data: habrahabr.ru/post/259141
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

A Bunch Of MODx Revolution + LiveStreet

Monitoring PostgreSQL with Zabbix

PostgreSQL load testing using JMeter, Yandex.Tank and Overload