What you will need:
- PostgreSQL – Database used to store line OSM line data.
- PostGIS – Database plug-in that enables spatial indexes and spatial queries
- Mapnik - Tile renderer
- Python 2.7 - scripts to call the mapnik libraries to render the tiles.
I will be using the Directory C:\RhinoTile as my working directory which is where I will install my tools and download my data. In the end our tiles will be at C:\RhinoTile\osm\tiles\x\y.png
PostgreSQL and PostGIS
First thing we need to do is download and install the PostgreSQL database. Be sure to install the correct version for your architecture.
Once that is downloaded run the installer. The
installer will ask you for a password for the superuser(postgres). For the sake of this write-up I used the password postgres. It will ask for the port you would like the server to listen on, I left it on it’s default 5432. It will ask you if you want to run StackBuilder on exit which is not necessary, so uncheck that box close the installer.
The version of PostgreSQL I installed was version 9.4. Currently PostGIS version 2.1.7 supports PostgreSQL 9.4 so that is the version of PostGIS I will use. If you are reading this at a later date, make sure that whatever version of PostgreSQL you install has a supported version of PostGIS
Download PostGIS, for your correct architecture and run the installer (setup.exe). When you run the installer you will be
prompted by multiple pop up boxes, answer yes to all of them.
Now we need to create our database. Open the start menu and launch pgAdmin III. Connect to the local
host server (localhost:5432). Expand the PostgresSQL
server, right click on Databases and click New Database..
Name your database whatever you would like, I named mine gis so that is what I will use for the rest of this tutorial. Set the owner to postgres if it's not already set. Click Ok.
Open command prompt and run the command:
psql -d gis -U postgres -f "C:\Program Files\PostgreSQL\9.4\share\extension\postgis".sql
It should ask for your password (postgres) and then you should see a bunch of SQL activity. This adds some spatial_ref_systems and a bunch of other magic. If you're curious about what exactly this is doing check out PostGIS.
Now expand your gis database and right click on Extensions then click New Extension, name it hstore then click ok.
Now our database is ready however we are going to alter some config settings in order to handle large data more efficiently. In a text editor open the file:
Find and change and enable the following settings:
shared_buffers = 128MB
checkpoint_segments = 20
work_mem = 256MB
maintenance\_work\_mem = 256MB
autovacuum = off
Adding Map Data
I will be using OSM map data provided by Geofabrik. In order to actually add OSM data to our database we will need a tool called osm2pgsql. Download it here. Extract the zipfile contents to your working directory. For me that would be C:\RhinoTile\osm2pgsql. Navigate into that folder and then into the x64 or x86 folder depending on your architecture. Once there download and save this file there make sure its saved as default.style.
Getting your data extract
No you need to go to Geofabrik and download your data extract. I used the state of Arkansas for this tutorial, you could use whatever you want. Beware, the entire planet is huge and could take about a week to actually render tiles for. Here is the extract I used. Download the arkansas-latest.osm.pbf file and save it in your working directory.
Open command prompt as administrator and
cd to the directory where you installed osm2pgsql. Then run the following command replacing `postgres' with the password you entered upon install of postgresql:
now run the command:
osm2pgsql.exe -d gis -S C:\RhinoTile\osm2pgsql\x64\default.style -C 4096 --hstore -U postgres C:\RhinoTile\arkansas-latest.osm.pbf
Naturally replace the path to default.style with the path to the default style file that you downloaded and
replace path to .osm.pbf with the path to your data extract.
Doesn't really deserve it's own header but anyways... Download and install the 32-bit version of Python 2.7
Here is a great tutorial on installing mapnik onto a windows box. Follow that tutorial exactly and then come back here. If you get stuck hit me up and I'll try to help.
...and we're back.
Hopefully that went good and you were able to successfully
import mapnik into python.
Now you need some mapnik utilities in order to get off the ground quicker. These utilities will communicate with your database and render your map tiles according to the style that you have selected. You're welcome to download my mapnik-fork which has the default osm aswell as the mapquest theme. If you do not wish to use my fork you could use these utilites from OSM. I will be assuming that you used my fork in this write-up if you did not, it shouldn't be much different and you should be able to follow along without problems.
Navigate to C:\RhinoTile and run:
git clone https://github.com/zachatrocity/mapnik-fork.git
Now you need the world boundary and shore files so that mapnik can properly render your tiles. You can find those here:
I will say, they're rather big. It may take a few minutes. Once they finish you will need to extract them with your favorite extraction tool (I used 7zip) into the
mapquest_world_boundaries folder. If any folders are created during the extraction then move their contents up a director and delete the folder. Everything needs to be in the
if you are are not using the mapquest theme then you should just name the directory world_boundaries rather than mapquest_world_boundaries
Generate an image!
Navigate back to the
C:\RhinoTile\mapnik-fork\ and run the
generate_image.py file. If all went well, congratulations, you should see an image of the state of Arkansas. (If you did a different state then you should edit that python script and change the bounds variable to be the correct extent for your data extract.
generate_tiles.py in my mapnik-fork has an example of generating tiles for a specific county in Arkansas. If you wish to create entire tiles for your data extract you will just need to change the
to the extent of your data in your data base. If you do not know the extent of your data you can find it by running this on your database:
SELECT ST_Extent(the_geom) as table_extent FROM your_table;
I have the
generate_tiles.py in my mapnik-fork putting all the tiles in the directory:
Which you will have to create.
The most common use for a tile renderer is to create a tile server for use in a slippymap. I use my tile renderer to create .sqlite caches for use with a slippymap application that is required to function without an internet connection. If there is interest in that process I'll post on it.
Creating a tile server from where we are now is a cake walk. It just depends on what you want to use for your server. I would recommend using Node.js with something like node-tileserver. If you are forced to use something like IIS its really just as simple as routing all requests to
C:\RhinoTile\osm\tiles\zoom\x\y.png or where ever you put your tiles.
Follow me on twitter!