Friday, 29 April 2011

Installation of Carto Mapnik Style File Builder on Ubuntu

I find mapnik style files a bit clumsy to edit - lots of repetition that makes them hard to follow.
There are (at least) two utilities available that may make style file generation simpler.  These are Cascadenik and Carto.
It appears that Carto is being more actively developed at the moment, so I thought I would look at that first. Carto also reports to be much faster than Cascadenink.
The problem is that Carto is new-fangled and uses a javascript based language called node.js.   Despite my dislike of javascript, I thought I would persevere.  This is how I got it working on my Ubuntu 10.10 system:

  • Downloaded node.js version 0.4.7 from http://nodejs.org, and extracted it into /usr/local/node-v0.4.7.  Built with ./configure; make; make install.
  • Installed the node package manager npm using: curl http://npmjs.org/install.sh | sh
  • Installed carto dependencies:
    • npm install get     [used to be node-get, but the module has changed name]
    • npm install step
    • npm install srs
    • npm install underscore
    • npm install zipfile (actually did this twice because one of the document build actions failed on the first attempt...).
  • Downloaded carto using:  git clone https://github.com/mapbox/carto.git
  • No compilation necessary (it is javascript).  Executing cd carto; ./bin/carto gave no nasty errors, just "no input files", which looks promising.
I just need to work out how to use it now.

I have had a quick look at TileMill, which is a web based editor for carto stylesheets - it is very impressive - you can change the stylesheet and preview the map in real time.   The only problem that I have found with tilemill is that it does not seem to work with a postgresql database as the data source, which is where my OSM data is stored.  But at least I should get the idea about the structure of the files using that tool.

Monday, 25 April 2011

Kefalonia Map

We are going to Kefalonia on our holidays this year, and I usually like to do a bit of OSM mapping when I go somewhere new, and may do some 'armchair mapping' before I go to get a head start, so I thought I would have a look at how well mapped it is.
The answer is not very - there are the main roads, and a few minor roads and footpaths, but not much else - this is a shame because I like to know where things like supermarkets and banks are.  Unfortunately there is not a lot of information available for remote mapping - very few GPS traces, and Bing Imagery does not cover the area I am interested in.
So instead I decided to make myself a map of the island showing what is there (in OSM anyway).  Because there is not a lot of detail, I want to have contours so I can tell where the mountains are, and also highlight what points of interest there are at lower zoom levels than the standard OSM style.   These are my notes on how I produced the map, so I can remember next time I need to do it.


  • The map bounding box was found by looking at the (lon,lat)  readouts on http://www.informationfreeway.org to check the bottom left and top right of the island.  They are (20.33, 38.03) and (20.82,38.50).
  • I downloaded the OSM data from the mapquest XAPI server using: 
wget http://open.mapquestapi.com/xapi/api/0.6/map?bbox=20.33,38.03,20.82,38.50 -O kefalonia.osm

  • Imported the data into postgresql database using:
osm2pgsql -s -S default.style -d kefalonia -m kefalonia.osm

  • Downloaded the SRTM elevation data covering the island from http://dds.cr.usgs.gov/srtm/version2_1/SRTM3/Eurasia/ - only needed one file (N38E020.hgt.zip), because it is a small island.
  • Generated the contours and imported them into my postgresql kefalonia database as described in http://wiki.openstreetmap.org/wiki/Contours
  • Modified the standard osm mapnik style file as described in http://wiki.openstreetmap.org/wiki/Contours, using the 'PostGIS' method.
  • Generated the map image using the generate_image.py file from the OSM mapnik archive.  I initially had some trouble with no roads appearing, but solved this by re-compiling osm2pgsql from the latest sources - I do not know what the problem was.
  • Having done that, I get this map image:



Kefalonia Map - Version 1 (25 April 2011)
Next I wanted to add a 1km grid to the map to make it easier to judge scale..."are we nearly there yet?" etc.
I did this by adopting the generate_graticule script from mapnik-utils to produce a grid on the google spherical mercator projection (=SRS 900913).  I must admit to not really knowing how this works, but my updated version (generate_grid.py) is in my SVN repository.   I added this to the map by creating an extra xml include file to define the grid layer and style (inc/grid.xml.inc), adding this to layers.xml.inc, and finally defining the layer in osm.xml.

The result (Version 2) is shown below:
Kefalonia Map Version 2 (26 April 2011)
I created another include file for holiday based points of interests (holiday_pois.xml.inc), added it to layers.xml.inc and again added the actual layer definition to osm.xml.
This gave the following map:
Kefalonia Map Version 3 (26 April 2011)

The final thing I needed was the English spelling adding to the Greek text labels.   I did this by adding name:en to the default.style file used by osm2pgsql and re-importing the OSM data into the database.
The challenge was updating the mapnik style file to use the name:en column - the OSM style files are very complicated with lots of SQL SELECT statements to extract the data out of the database.  I was tempted to do a global replace of 'name' with 'name:en'...but 'name' appears all over the place.
I decided the main thing I wanted with both writing styles were place names and I found the code for them in inc/layer-placenames.xml.inc.
I modified the SELECT statement from a simple 'name' to ,'"name" || ' (' || "name:en" || ')' as name'.  This gives me the greek spelling followed by the english spelling in brackets using the postgresql '||' string concatenation operator.  The result is here:
Kefalonia Map Version 4 (27 April 2011)
I think that is about the finished product - I will have to try printing it to see how it looks, then may need to adjust the image size to suit printing....then do some mapping to add some more POIs - Sacla in the south east of the island should be a lot better by the middle of June....

Sunday, 17 April 2011

Problems with GoogleCode SVN

I use GoogleCode (http://code.google.com) for my little open source projects, and it usually works quite nicely.
I was just trying to set up one of my repositories in writable mode on a new computer, and "svn ci" (checkin) gave 500-Internal Server errors, which was not very helpful.
Eventually I realised that although I had done the "svn checkout" with a --username=", when I was doing the checking it was using my computer username, rather than my google one...so presumably the authentication failed.
I had to do "svn ci --username=", then it worked ok.   I don't know why!

Sunday, 3 April 2011

Trouble with mysql, apache, php5 and codeIgniter

I am working on a new web interface to townguide based on php so it can be used on a low cost web hosting service (rendering will be done by a separate computer).
I am going to use the codeIgniter web framework because it seems simple, and is simiar to django, which I was just starting to get used to.....
Alas getting database access working on my laptop for development was difficult.
I have apache, php5 and mysql all working, but whenever I tried to load the database module of codeIgniter I got a 500 Internal Server error, but no errors in either the apache or codeigniter error logs.   This made debugging difficult.
After much searching on the internet I realised it was because I needed to install libapache2-mod-auth-mysql as well as php5-mysql.
Doing sudo apt-get install libapache2-mod-auth-mysql and re-starting apache made the following trivial mysql db connection work:

$conn = mysql_connect('localhost', 'www', '1234');
if($conn) {
  echo 'Finally connected!!!';
 } else {
  die('Still cannot connect' . mysql_error());
 }
?> 
A bit late now, but I think this will also have solved the problem connecting to the database using codeigniter....