This Page is outdated, have look at RenderingOsmDataUbuntu for more recent (ubuntu) instructions!
The osm2pgsql data structure is no longer usable with the osm mapserver-utils package. Instead, imposm is used to create a completely differen table layout - yay the two weeks i spent on running osm2pgsql \o/ see link above for more recent instructions.
Rendering OpenStreetMap Data with MapServer
The map displayed on the front page of the MapServer website is an example of MapServer's rendering capabilities.
OpenStreetMap data can be used freely under the terms of the Creative Commons Attribution-ShareAlike 2.0 license:
OpenStreetMap creates and provides free geographic data such as street maps to anyone who wants them. The project was started because most maps you think of as free actually have legal or technical restrictions on their use, holding back people from using them in creative, productive, or unexpected ways.
Which MapServer version to use
The mapfile presented here assumes a very recent version of MapServer is used, i.e. at least 5.4. You will have to include (at least) support for AGG rendering,postgis, and wms server.). Once you're done compiling, copy the mapserv binary to a cgi executable directory of your mapserver (/usr/lib/cgi-bin on debian)
Importing OpenStreetMap Data
This howto will take australian data as an example. Note: if you already have imported OSM data, see the section "Using an existing database" below.
- Download an osm excerpt, for example from http://downloads.cloudmade.com/oceania/australia/australia.osm.bz2, other countries can be found at http://downloads.cloudmade.com/
- Optionally, download and unzip the shapefiles containing the world coastlines from http://hypercube.telascience.org/~kleptog/processed_p.zip (this shapefile isn't included or rendered with the supplied mapfile)
- Download and install cygwin and make sure you add the following packages: "wget", "unzip", "gcc" and "make".
- Download and install PostgreSQL for windows and select to install PostGIS in PostgreSQL setup.
- If you don't have MapServer for windows - install it - just make sure you have the latest version of MapServer and not an old one...
Download the osm-mapserver utils
svn checkout http://mapserver-utils.googlecode.com/svn/trunk/ mapserver-utils-read-only
Import into Postgis
Preparing the postgis database
We'll assume that the data will be imported into a database named osm. We will also grant access to a www-data user.
On a debian-ish system
# sudo su postgres # createdb -E utf8 osm # createlang plpgsql osm # cat /usr/share/postgresql-8.3-postgis/lwpostgis.sql | psql -d osm # cat /usr/share/postgresql-8.3-postgis/spatial_ref_sys.sql | psql -d osm
Now log into the created database (as postgres user):
# psql -d osm
And run create the www-data user:
create user "www-data" with password 'www-data';
eventually add the 900913 projection to your epsg database if it isn't already present (usually in /usr/share/proj/epsg):
<900913> +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs
On a windows system (Option A: Using pgAdmin)
1. If you haven't installed it yet - install PostgreSQL for windows with PostGIS extension (you can select to add PostGIS during the PostgreSQL setup).
2. Start-->"Programs"-->"PostgreSQL 8.x"-->"pgAdmin III".
3. Connect to the server.
4. Go to the server's "Databases" tree node, right click, "New Database...", a window titled "New Database..." will be opened.
5. Enter "osm" in the name field.
6. Select the template "template_postgis" in the template field combo box.
7. Press the "OK" button on the new database window.
8. Select with the mouse the new osm database under the Databases subtree.
9. From the main menu select: Tools-->"Query tool".
10. Enter the following text in the "Query tool":
create user "www-data" with password 'www-data';
11. Press F5 to run the query.
Leave the "Query tool" on - you will need it later to run more queries.
On a windows system (Option B: Using Commandline)
1. Make sure the 'C:/Program Files/PostgreSQL/xx/bin/' directory is added to your PATH environment variable
2. These instructions assume that you already have a 'postgres' user
3. Open a command prompt and enter the following commands:
4. createdb -E utf8 -U postgres osm
5. psql -U postgres -d osm -f "C:\Program Files\PostgreSQL\8.4\share\contrib\postgis-1.4\postgis.sql"
6. psql -U postgres -d osm -f "C:\Program Files\PostgreSQL\8.4\share\contrib\postgis-1.4\spatial_ref_sys.sql"
7. createuser -U postgres -P www-data
enter 'www-data' for password for new user
8. try connecting as this new user to the 'osm' database:
psql -U www-data -d osm
(enter 'www-data' for password)
Importing the OSM data
- Install the Osm2pgsql helper program
- On windows make sure that PROJ_LIB environment variable is define to the location of your epsg file (In some case the path is: "C:\ms4w\proj\nad") - else you will get a "Projection code failed to initialise" error.
- Run the importation process (as the postgres user) (this will take some time, depending on your processing power and the quantity of data you want to import):
# osm2pgsql -l -d osm -p osm -s -C 1024 /path/to/australia.osm.bz2
Example on Windows (notice the extra path to the style):
osm2pgsql -U postgres -W -l -d osm -p osm -s -C 1024 -S "C\Program Files\osm2pgsql\default.style" colorado.osm.bz2
- Postprocess the imported data:
Optionally, edit mapserver-utils-read-only/postprocess.sql to comment out or remove the following two lines if you'd rather leave names unchanged:
-- optional, makes it easier to wrap place names update osm_point set name = replace(name,'-',' ');
Note that you can also use "WRAP [character] MAXLENGTH [integer]" wrap names on hyphen.
Then proceed with postprocessing:
On a debian-ish system
# cat mapserver-utils-read-only/postprocess.sql | psql -d osm
On windows systems system (OptionA: using pgAdmin)
1. Open the file named postprocess.sql in a text editor.
2. Copy the text to the "Query tool".
3. Press F5 to run the query.
On windows systems system (OptionB: using Commandline)
1. Open a command prompt
2. cd to the 'mapserver-utils-read-only' directory, and execute the command:
3. psql -U postgres -d osm -f postprocess.sql
Note: you might also want to execute the command 'vacuum analyze' on the osm database before moving on, to optimize queries:
psql -U postgres -d osm -c "vacuum analyze"
Using an existing database
If you already have a postgis database with OSM data imported, all there is left to do (besides compiling mapserver with postgis support) is editing generate_style.py. at approx. line 72 change
'db_connection': '"host=localhost dbname=osm user=osm password=osm port=5432"'
to reflect your database port, name, user name and user password.
Creating the Mapfile
Open the folder of "osm-mapserver utils" you downloaded earlier.
this is a work in progress mapfile, that compiles a template and a style file into a full-blown MapServer mapfile. The supplied Makefile supposes that the C preprocessor is installed on debian-ish systems (or cygwin on windows). The fonts.lst file contains the path to the two fonts used: adapt to reference similar fonts if the ones in the file don't exist on your system.
You may need to edit the Makefile to change the OSM_EXTENT parameter to suit your own data. To find the correct extent, run this command:
# su postgres # psql -d osm -c "SELECT extent(way) FROM osm_line"
You can also enter 'SELECT extent(way) FROM osm_line' (without the quotes) as a SQL command in the pgAdmin Query tool.
Return should be (for France) something like:
extent ------------------------------------------------- BOX(-572406.4375 5067315,1064254.875 6637725.5) (1 row)
- On windows use cygwin command prompt and you may also need to edit the "Makefile" file first line to be "CPP=cpp".
# cd mapserverosm (or the "osm-mapserver utils" path) # make
check that you now have a full mapfile, named osm-mapserver.map, and that all the paths and database accesses are ok by running :
# /path/to/mapserver/shp2img -m osm.map -o osm.png
If you get permission denied errors probably you will need to edit the file.
osm.png should be an image centered on sydney
Creating the OpenLayers page
copy/paste this block to a file accessible on your webserver. you will have to adapt at least the path to osm.map file, and the url of your mapserv binary. If necesary change also the maxExtent parameter with the extent found above:
- you can change the color scheme by editing the osm1.style file, and rerunning make
- feel free to edit this page to include any solutions to problems you might have had following these steps
- for more info: thomas.bonfort at gmail dot com