📜 ⬆️ ⬇️

Install and configure the generation of tiles based on OSM data under Windows

Disclaimer


Recently, the task of generating tiles based on OSM data arose. I looked for, read a few articles , but * nix-systems appeared everywhere, and I had a server running Windows. In the end, a more or less intelligible tutorial was found, but it is slightly outdated and not always detailed, therefore, I had to tinker. After successful completion, I thought that my experience would be useful.

Who are interested, please under the cat.

Introductory


Installation was made on clean Windows 7 and 8.1, without any software. It will take the following:

You will also need additional tools, without which, however, nothing will come of it:

1. Install and Configure PostgreSQL


Distribution can be taken from offsite . Installation is quite trivial, however, there are a couple of moments.

First, it may be necessary to change the directory where the database is stored (it may take up to a terabyte if OSM data is used for the entire planet). The default (for version 9.4) is C:\Program Files\PostgreSQL\9.4\data
')
Secondly, it is possible that the cluster locale should be changed. In my case in Russian, Russia . When installing, you will also be asked for the password for the PostgreSQL server, you will need it, remember it.

After installing PostgreSQL itself, it will start StackBuilder, the installation wizard for database extensions. You need to choose two extensions: Spatial Extensions -> PostGIS and Database Drivers -> JDBC. All settings at the default setting. During the installation of PostGIS there will be a request to create a system variable and a couple more questions, agree. After that, you need to restart the PostgreSQL service.

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

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

Create a template for the geodatabase:
 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 this database.

Create a gis database based on the template_postgis_20 template_postgis_20 :
 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 from here . Command line patch:
 C:\Users\red>psql -U postgres -d gis -f legacy-postgis-gist.sql CREATE OPERATOR CLASS 

The database is ready.

2. Install and configure Osm2pgsql


You can take the latest release from the githaba and collect it yourself, or you can take a ready-made artifact from here .

Download, unpack, rename for convenience in osm2pgsql . Add this directory to the PATH variable. Logoff-login. We check that everything works:
 C:\Users\red>osm2pgsql -h 

Help should be displayed. Take the last default.style from the github repository.

Everything is ready to import OSM data.

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

For import, execute on the command line:
 C:\Users\red>osm2pgsql -d gis -U postgres -W -H localhost -P 5432 -s -SC:\osm2pgsql\default.style RU-MOW.osm.pbf 

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

If everything is correct, there will be a password request and the dump will be imported into the database. The first lines will be all sorts of warnings, do not worry, everything is in order. After the import is complete, something like this will be displayed:
 Osm2pgsql took 160s overall 

Geodata in the database.

3. Installing Python 2.x


Download the x86 distribution (because Mapnik binaries for Windows are only x86) from here . We put, the default settings, except for one: It is worth noting "Add python.exe to PATH", or make it handles after installation. Logoff-login.

Check the installation is correct:
 C:\Users\red>python Python 2.7.10 (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.

4. Install and configure Mapnik


Again, there are two options: either compile from the github source itself , or take ready-made binaries here . We are looking for Windows 32 bit Package , download, unpack in C:\mapnik-v2.2.0 .

They have a pretty detailed installation guide on the wiki . In short: add lib and bin directories to the map in PATH , this time. Create a new system variable PYTHONPATH and assign it the path to C:\mapnik-v2.2.0\python\2.7\site-packages , these are two. Put this vcredist , it's three. Logoff-login.

Check the installation is correct:
 C:\Users\red>python Python 2.7.10 (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 are parsed on the wiki. But besides that, it is worth trying to move the paths to lib and bin to the top of the PATH .

And gene test tiles:
 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 the githab, find the PROJ.4 library, find the binaries: Prebuilt Win32 executables, DLL including NAD27 grid shift files . Download, unpack ( c:\proj ). In PATH add the path to bin , to the new variable PROJ_LIB path to nad . Logoff-login.

A bit more.

Download scripts to generate tiles from the official svn repository , put in c:\mapnik .

Further, for the operation of the script we need coastlines and all that. There is a get-coastlines.sh script for get-coastlines.sh , but it is written in bash and under Windows will not work. But it is quite simple and short, so just repeat all the actions with handles.

Download:
We unpack all archives in a directory in c:\mapnik\world_boundaries .

Last preparations. Go to mapnik\inc , rename the settings.xml.inc.template file to settings.xml.inc .

The fontset-settings.xml.inc.template file in fontset-settings.xml.inc .

The datasource-settings.xml.inc.template file is in datasource-settings.xml.inc and edit it:
 <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> 

Fix the generate_tiles.py script a bit:
Block on line 193:
 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 + '/' 
replace with
 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' # - try: tile_dir = os.environ['MAPNIK_TILE_DIR'] except KeyError: print('MAPNIK_TILE_DIR env var not found! Use default.') tile_dir = 'C:/tiles/' # - if not tile_dir.endswith('/'): tile_dir = tile_dir + '/' 

Specify the boundaries in which tiles will be generated (geographic coordinates of the rectangle):

On line 213 before
  bbox = (-180.0,-90.0, 180.0,90.0) render_tiles(bbox, mapfile, tile_dir, 0, 5, "World") 
write (in our case Moscow)
  bbox = (37.32, 55.57, 37.88, 55.92) render_tiles(bbox, mapfile, tile_dir, 17, 17, "Moscow") exit() # ,        

Tips & Tricks
Border coordinates are taken from the osm file from which the data is taken (they will be at the very beginning). Due to the fact that the data file can be very large, you can’t open it with a regular text editor, I advise you to use editors like EmEditor (paid) or set yourself grep.

Specify the path and start generating tiles:
 C:\mapnik>generate_tiles.py 

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

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. Data Russification: habrahabr.ru/post/259141

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


All Articles