Tuesday, 3 April 2012

TileMill coast

While the OSM DB server is being upgraded and the data cannot be updated I thought I'd look at rendering. I've used Mapnik to do some rendering in the past. It is powerful, fast and uses the Anti-grain Geometry C++ templates which produce lovely sub-pixel rendering for clear thin lines, good colour contrasts and good quality curves and diagonals. The downside of Mapnik is its style sheets, which I don't think too many people get to grips with.

Recently I heard about TileMill so I thought I'd take a look. It installed very easily on my laptop running Ubuntu. A quick look at the results shows it is running node.js on my machine to provide the user interface through my browser. That works well and gives a slick and responsive interface. It also installed Mapnik, which is what renders the maps. TileMill can use various data sources such as shapefiles, GeoJSON, GeoTIFF and PostGIS. I already run PostGIS with some data imports for the existing rendering, so that was a place to start. This is data imported into tables in a way that makes rendering easier. I have just copied the data in the way the Standard map view on the OSM site uses it. This does not include all data from OSM for a selected area, only the data that the Mapnik style is set up to render.

When I created a new TileMill project it started by creating a layer to show the outline of the landmasses. I quickly zoomed in to see my local area. The coastline showed up the Humber Estuary and, as it should, it stopped abruptly at about the place the Humber bridge crosses. Beyond that it is not officially coastline but river. I have not yet imported any other data to show up the rest of the river system.

The first thing I tried to add were a few roads. Copying an example I created a layer that loaded some data from planet_osm_line, the dataset in the PostGIS db that holds an extract of linear things like roads. TileMill uses a language they call Carto to describe the selection and style of the objects to be displayed on the map. It is a lot like CSS. I copied a small piece of Carto code and on saving the code the map quickly redrew with some thin grey lines to show some roads. That was very satisfying. A few more lines of code and more road types appeared in various colours and thicknesses. Then I noticed a problem.

The coastline was rather crude and some roads strayed into the sea. I realised that what I needed was the OSM coastline to exactly match my OSM data. I looked around and found the processed_p shapefile which is a detailed OSM coastline. It was nearly 500MB uncompressed, but as I only want to work with GB data at the moment I loaded it into Quantum GIS and extracted the GB part and saved that separately as a shapefile. Once that shapefile was loaded as a layer in TileMill the detail jumped out and none of the roads were submerged any more. If you are interested in Birmingham none of this would matter. There may yet turn out to be a better way of using processed_p or some similar coastline.

I have a lot to look at yet but it is an interesting start. I want to be able to render an area as tiles to use in my own way with my own style and this looks very promising.

2 comments:

Kevin Donnelly said...

Hi Chris

"I loaded it into Quantum GIS and extracted the GB part and saved that separately as a shapefile."

Can you point me to some info on how to do that? I'm trying to get a map of Wales, and I can get all sorts of internal stuff, but not the coastline or boundary with England. I've got the UK data from http://osmdata.thinkgeo.com/openstreetmap-data/europe, and I was hoping to extract just the Wales outline, but I can't figure out how to do that. :-(

Thanks
Kevin

Chris Hill said...

I started by downloading the full processed_p shape files in a compressed form:

wget http://tile.openstreetmap.org/processed_p.tar.bz2

I uncompressed the downloaded file with:

tar -jxf processed_p.tar.bz2

This creates a (large) shapefile processed_p.shp (with its .shx & .dbf files). These can be loaded into Quantum GIS. Handling this large file is slow, you need to be patient and deliberate. To change the file you need to enable editing. From the menu select:

Layer -> Toggle Editing

The shapefile contains lots of rectangles or parts of rectangles that make up the land masses of the world. Delete all of the areas you do not need. I kept the British Isles, you might choose to leave out Ireland and the various islands, or even just all of the rectangles that are not part of Wales.

Save the new shapefile with a new name. Load this into Tilemill instead of the processed_p shapefile to make dealing with the shapefile much quicker.

The Wales / England boundary is in the OSM data. I'm not sure of the best way to use a land boundary as a cut off for rendering. If you download the data from Geofabrik (http://download.geofabrik.de/europe/great-britain/wales-latest.osm.pbf) for Wales any ways that cross into England will still be included, so roads, railways, rivers etc. will all cross into England and would need to be trimmed, perhaps by hand.

I have made a copy of my British Isles shapefile here http://handy.raggedred.net/

I hope this helps.