Kev

kjs.me.uk

  • Increase font size
  • Default font size
  • Decrease font size
Home Open Street Map OSM & Mapnik Quickstart

OSM & Mapnik Quickstart

E-mail Print PDF

Mapnik Installation on Ubuntu 8.10

These instructions are a copy of my notes for installing Mapink and associated pre-requasits and rendering the first tile with Open Street Map data - they are in need of rewriting as there are a few small errors and the detail gets rather vague towards the end.

Prerequisites

We'll need to install some extra applications onto the base Ubuntu install to allow us to download and compile what we really care about so let's get them out the way before we do anything else

Firstly we need Subversion to allow us to download the latest versions of Mapnik and OSM2PGSQL

kev@machine$ sudo apt-get install subversion

We also need some development libraries to allow us to compile the latter

kev@machine$ sudo apt-get install build-essential libxml2-dev libgeos-dev libpq-dev libbz2-dev

Download regional planet files

kev@machine$ wget http://download.geofabrik.de/osm/europe/great_britain.osm.bz2
kev@machine$ wget http://download.geofabrik.de/osm/europe/ireland.osm.bz2

Download World Boundaries

kev@machine$ wget http://tile.openstreetmap.org/world_boundaries-spherical.tgz
kev@machine$ wget http://hypercube.telascience.org/~kleptog/processed_p.zip

Install Postgresql

sudo apt-get install postgresql-8.3-postgis

and create the needed tables

kev@machine$ sudo -u postgres -i
postgres@machine$ createuser kev
postgres@machine$ createdb -E UTF8 -O kev gis
postgres@machine$ createlang plpgsql gis

Install OSM2PGSQL

Revision 13641 appeared to be borked so I got a slightly older one, try dropping the revision tag...

kev@machine$ svn co --revision=13470 http://svn.openstreetmap.org/applications/utils/export/osm2pgsql/
kev@machine$ cd osm2pgsql
kev@machine$ make
kev@machine$ psql -d gis -f 900913.sql

Amend default.style

kev@machine$ gedit default.style

Then uncomment the lines after

# If you're interested in bicycle routes, you may want the following fields
# To make these work you need slim mode or the necessary data won't be remembered.

Finally add the lines

way       surface      text     linear
way operator text linear
way colour text linear
way maxspeed text linear

and save

Install Mapnik

kev@machine$ sudo apt-get install python-mapnik

Import the data

kev@machine$  osm2pgsql -m -d gis great_britain.osm.bz2
kev@machine$ osm2pgsql -m -d gis ireland.osm.bz2

Laptop appeared to lock up for at least 2 hours here on the great_britain one, and then failed. Tried again and it failed in 15 minutes.... Managed to get to 6,500k nodes out of 7,291k. No ways or relations....

Try a smaller bounding box using the following :-

kev@machine$ ./osm2pgsql --slim -m -d gis --append -b -1.345,52.85,-0.962,53.067  ../great_britain.osm.bz2

or including Derby

kev@machine$ ./osm2pgsql --slim -m -d gis -b -1.562,52.746,-1.048,53.055  ../great_britain.osm.bz2

The -a switch appends the data - useful when trying to add Ireland into the map, also worth trying smaller bounding boxes of the UK in sequence and see if that works

Extract Coastal Data

kev@machine$ tar xzf world_boundaries-spherical.tgz
kev@machine$ unzip processed_p.zip
kev@machine$ mv coastlines/* world_boundaries/
kev@machine$ rmdir coastlines
kev@machine$ mv world_boundaries ~/mapnik/

The above should leave you with all the shape files in the world_boundaries directory.

Rendering Maps

Checkout the Mapnik stuff from the OSM subversion repository:

kev@machine$ svn checkout http://svn.openstreetmap.org/applications/rendering/mapnik
kev@machine$ cd mapnik

Create a local copy of the "set-mapnik-env" script and edit it to fit your local file names,database name,tiles folder location, etc . All the settings are documented inside this script. - The basic changes needed are

set MAPNIK_DBNAME='gis'
set MAPNIK_DBPASS='password'


Create the osm.xml file from osm-template.xml by calling:

kev@machine$ source ./set-mapnik-env
kev@machine$ ./customize-mapnik-map >$MAPNIK_MAP_FILE

You can decide which areas get rendered by setting bounding boxes and zoom levels in generate_(tiles|image).py i.e. open the two files and amend the line that begins "ll = " to

* ll = (-1.35,52.85,-0.962,53.067)

Now generate the images

kev@machine$ ./generate_tiles.py

generate_tiles.py generates tiles which can be served by a suitable server. (Look inside the file to find out what area of the planet it will render by default and at what zoom. Look for bbox and min,max zoom. If you want to select a different area go to openstreetmap.org and click the "export" tab; then "manually select a different area". Draw the area you want, and the four text boxes will show values you should provide in a sequence [left,bottom,right,top]). If you have multi-core machine, to speed-up generation of tiles, you can divide an area for e.g. four squares and run few instances in the same time.

or

kev@machine$ ./generate_image.py

generate_image.py generates a single image for a given bounding box. That is, it essentially gets the tiles and stitches them into a single large image. This may be useful for people who want to just cover a limited area and thus want to save the hassle of handling tiles.

Customising the rendering

Now you have the standard stylesheet rendering working you can start customising your map.

Contours

It is possible to add contours to your map like the cyclemap has, the contours are based on the public domain SRTM (Shuttle Radar Topograhy Mission) from NASA. It is also possible to add hillshading and elevation colouring to your map based on the same data.

Stylesheet

The stylesheet is an XML document controlling most aspects of how features are rendered, such as color and widths of lines showing different types of roads. The format is not very compact or easy to understand, but it does give a lot of power/flexibility. The version we use on the live slippy map is probably the osm.xml file in the SVN head:applications/rendering/mapnik/osm.xml . See Mapnik Example for an example of a more simple stylesheet.

The stylesheet syntax is specified here: XMLConfigReference. MinScaleDenominator and MaxScaleDenominator specify the minimum and maximum scales at which each symbol/feature should be rendered.

You can play around with mapnik stylesheets online here: http://tile.openstreetmap.nl/~panman/styledit/

Hillshading a routes stuff... http://wiki.openstreetmap.org/index.php/HikingBikingMaps http://wiki.openstreetmap.org/wiki/HikingBikingMaps/RouteRendering

UK Projection

Update the database to support British National Grid

Firstly we need to ensure that the database supports the British national grid, to do this create a sql file

gedit ~/osm2pgsql/27700.sql

Which contains the following (one one line)

INSERT into spatial_ref_sys (srid, auth_name, auth_srid, proj4text, srtext) values ( 927700, 'epsg', 27700, '+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs ', 'PROJCS["OSGB 1936 / British National Grid",GEOGCS["OSGB 1936",DATUM["OSGB_1936",SPHEROID["Airy 1830",6377563.396,299.3249646,AUTHORITY["EPSG","7001"]],AUTHORITY["EPSG","6277"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4277"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",49],PARAMETER["central_meridian",-2],PARAMETER["scale_factor",0.9996012717],PARAMETER["false_easting",400000],PARAMETER["false_northing",-100000],AUTHORITY["EPSG","27700"],AXIS["Easting",EAST],AXIS["Northing",NORTH]]');

Now add it to the database

sudo -u postgres -i
psql -d gis -f /home/logobrand/osm2pgsql/27700.sql

Then we need to import the data into the database

./osm2pgsql -d gis --proj 27700 great_britain.osm.bz2
./osm2pgsql -d gis -a --proj 27700 ireland.osm.bz2

At this point I was getting some buffer overflow errors, so download the source code

In Makefile change

-g -O2 to -g -ggbd

then execute

make clean
make

Try again, worked for me

Mapnik

Once you have done this change the srs's for anything containing to

srs="+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.999601 +x_0=400000 +y_0=-100000 +ellps=airy  +units=m +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894 +units=m +nodefs"

i.e. tell Mapnik our database is in the UK projection.

At this point the roads will be okay but the landmass will be screwed, as will built up areas.

Copy the world_boundaries_m files and builtup_area files to a windows box.

Install http://www.mapwindow.org/download.php?show_details=1

Click the Green Plus, select Add Layer and select world_boundaries_m

Click the "Select" icon and draw a box round the British Isles (I included the Faroe Islands, northern France and Benelux to get a complete map.

Click GIS Tools, Click Vector, Click Export Selected Shapes to New Shape File

Give it a sensible name (e.g. british_isles_landmass)


Repeat for the builtup_area shape file.

Click the new button


Click "GIS Tools", "Vector", "Reproject a shape file"

Open british_isles_landmass.shp


In the dialogue that appears select

Category : Projected Coordinate Systems Group : National Grids Name : British National Grid

Click OK

This should take a few seconds, at which point a "Finished, Add to map?" dialogue will be shown - Click "NO" otherwise it screws up.

Repeat for british_isles_urban.shp


Copy back to your Ubuntu box


In the osm.xml file search for "shape" then change as follows

File : Your reprojected file name SRS : +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.999601 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs

Job Done :o)

Last Updated on Saturday, 17 October 2009 15:53  

Facebook Share

Share on facebook

Adverts Support This Site