Wednesday, June 15, 2011

Create your own map with OpenStreetMap data on Ubuntu - Phase 1: setup

In this tutorial, we're going to use TileMill, an easy frontend to Mapnik, on Ubuntu. So first you have to install TileMill and Mapnik. There are good and up to date commands given on the TileMill site: http://tilemill.com/

Now you need to get your data ready. First of all, you need to download the data. If you only need data for one small city, you can use the export tab on the OpenStreetMap website. Choose to export to OpenStreetMap XML data.

If you need more than one city, the api is probably not going to accept such a big request, but than you can use the cloudmade OSM downloads. Here again, choose to download osm files, but these here are zipped. So you need to unpack them before you can use them. If you need the whole world (a whopping 220 GB of data, compressed to 16 GB for downloading), you can check out different ways to get the planet file: http://wiki.openstreetmap.org/wiki/Planet.osm. Reading the entire planet takes, even on strong servers, serveral days.

Now we have to set up a database and put our data in it. Postgres has already been installed if you followed the installation tutorial of the TileMill page, but we still need to configure it.

The instructions come from the OSM wiki and are confirmed to work for Ubuntu 11.04.

(note that long lines are split in two in blogger. if you have problems with this, use the OSM wiki instead.)
sudo -u postgres -i -H
createuser -SdR gisuser
createdb -E UTF8 -O gisuser gis
createlang plpgsql gis
psql -d gis -f /usr/share/postgresql/8.4/contrib/_int.sql
psql -d gis -f /usr/share/postgresql/8.4/contrib/postgis-1.5/postgis.sql
# to populate the table spatial_ref_sys (mandatory for use with osm2pgsql):
psql -d gis -f /usr/share/postgresql/8.4/contrib/postgis-1.5/spatial_ref_sys.sql
psql gis -c "ALTER TABLE geometry_columns OWNER TO gisuser"
psql gis -c "ALTER TABLE spatial_ref_sys OWNER TO gisuser"
psql gis -c "ALTER TABLE geography_columns OWNER TO gisuser"
exit
Now you need to edit a configuration file before that user can login to the database.
sudo gedit /etc/postgresql/8.4/main/pg_hba.conf
Change in the last lines that should look like
local all all ident
host all all 127.0.0.1/32 md5
the words ident and md5 to trust.

Now you need to reload the database and login to test it.
sudo /etc/init.d/postgresql reload 
psql gis gisuser
With the command \d, you can see the databases that are created and with \q you can exit.

Now you need to get the fresh OSM file into a pgsql database. This happens with the osm2pgsql program.
sudo apt-get install osm2pgsql
And finally put the data in the database
osm2pgsql -m -d gis -U gisuser  data.osm
 Where dat.osm is off coarse your file you downloaded. You don't need to unpack it. If it's a big file, this process can take a huge amount of RAM and a lot of time.

Now that you have imported the data, you can add it to a layer on your TileMill. If your TileMill server isn't running anymore, start it with the tilemill.js script in the directory where you installed TileMill.

Now go to http://localhost:8889 and add a new project. Open the project by clicking on it, you will see a world map with all boundaries and sees drawn. It will also open a stylesheet file for that worldmap.

Add a layer, choose for Postgre SQL and fill in the details.

The id is the name of the layer. The database name is gis (unless you changed it) and the user is gisuser. In the query, you put the table created by osm2pgsql. These are the following tables:

planet_osm_point
planet_osm_roads
planet_osm_polygon
planet_osm_line
So you cannot enter the lines and polygons in one layer. You need to add 4 different layers if you want all features.

I've called the layers point, roads, polygon and line. The names suggested by osm2pgsql.


Now your data is in the TileMill application. The next post will be easier and I will try to create some maps with color and icons.

No comments:

Post a Comment