Sunday, 12 December 2010

Townguide - progress at last!

You can't beat some really bad weather to help make progress with my nerdy jobs.
I have got Waldemar's django front end to townguide working (http://dtownguide.webhop.net), and have managed to make a few changes, including adding fancy tab things to the main map selection form.
There are still a few things to sort out:

  • The area selection on the map does not align properly with the mouse pointer - not sure why - must be something to do with OpenLayers projections...
  • Mapnik re-sizes the output map to match the bounding box, so townguide needs to check the actual map size to make sure the grid squares are right.
  • There is something funny about the output resolution - need to see what it is using - the output looks much higher resolution than I asked for, so I suspect there is a sum wrong somewhere!
  • Add the GPX track and waypoint plugins (they are just templates at the moment).
  • Add an option to suppress un-named ways - the 'None' in the street index can confuse people.
  • Reinstate other output formats - only 'poster' is working.

Saturday, 27 November 2010

Townguide - where next?

My townguide map rendering application is about a year old now, but hasn't changed much since last winter.
Waldemar did a lot of work on it for his Google Summer of Code project where he re-hashed the townguide renderer code to make it more modular and produced a django based web front end.
I have not done very well getting this working because I do not understand django well enough to sort out a few minor bugs in it.
Instead I have taken his re-hashed renderer code and developed it further to make it even more modular - as well as 'plugins' for different PDF output formats, there are plugins for the base map and for different map overlays (a grid, custom markers, gpx tracks etc.) [well nearly anyway].  It also uses mapnik2 so can now produce nice high resolution output to make it comparable to the much prettier maposmatic output.

I have been reading a book about django for a few days now (Python Web Development with Django by Forcier, Bissex and Chun) and feel that I should be able to understand both Waldemar's code and the maposmatic version, so I will have a go at the web front end now - there seems little point in developing my clanky php version with these two much slicker front ends available.

Sunday, 24 October 2010

IBM ThinkPad X60S Middle Mouse Button

Every time a new release of Ubuntu linux comes out, something breaks.   Fortunately over time the somethings are becoming less and less significant!
This time it was the middle mouse button on my IBM ThinkPad X60S.    I had got it working following the instructions here in version 10.04 - it just needed a configuration file in /usr/lib/X11/xorg.conf.d.
But it stopped working following the upgrade to Ubuntu 10.10.
It turns out that the xorg.conf.d location has changed to /usr/share/X11/xorg.conf.d.
Putting a file named 20-thinkpad.conf in that directory with the following contents got middle button scrolling working nicely again!
Section "InputClass"
Identifier "Trackpoint Wheel Emulation"
MatchProduct "TPPS/2 IBM TrackPoint|DualPoint Stick|Synaptics Inc. Composite 
TouchPad / TrackPoint"
MatchDevicePath "/dev/input/event*"
Option "EmulateWheel" "true"
Option "EmulateWheelButton" "2"
Option "Emulate3Buttons" "false"
Option "XAxisMapping" "6 7"
Option "YAxisMapping" "4 5"
EndSection
 There is talk that it should work by doing:
sudo apt-get install gpointing-device-settings

But it did not work for me (at least not just like that - tried re-starting xwindows, but still no scrolling - maybe there is some configuration needed, but I like the old way of dropping a file into a configuration directory!

Sunday, 3 October 2010

OS Vector Map District with PostGIS - Take 3

To recap:

  • The entire UK's VectorMapDistrict dataset has been loaded into my postgresql/postgis database using my vmd2pgsql script, which is based around shp2pgsql.
  • I created some stylesheets for mapnik rendering of the data based on the mapnik tutorial, but they came out with a transparent background and black lines and text, which is not what I wanted.
The rendering problem turned out to be quite easy - it is just that the mapnik tutorial is based on Mapnik 0.7, not mapnik2.  To make it work with mapnik2 you need to:
  1. Replace the bgcolor parameter of the map element with background-color.
  2. Remove all of the CSSParameter business in the style definitions with simple name="xyz" constructs.
It is a shame that mapnik did not complain about these issues, even in debug mode, but never mind!
Adding the different linear features works ok - you see something of an improvement from the black and white version to something resembling a map:
So now all I need to do is add the areas (water, woodland towns etc.) to make it look extra pretty, and sort out what to plot at different zoom levels so it is reasonable.

Here I hit a problem.   When I tried to add the settlement_area or naturalfeature_area items, mapnik crashed with an "invalid geometry" error, after taking quite a long time thinking about it!   It is something to do with polygons not being closed properly.   I suspect it is rounding errors, but am not too sure.  One possibility is that I should have used the shp2pgsql option to just use integers, which would have avoided rounding, or I could try to fix the database.  As it took 9 hours to import, and I don't know if integers will work, I'll try to fix it.

With much internet searching I discovered that you can check the validity of each geometric feature with the postgis st_isvalid() function.  Running a simple select statement on the database like:
select gid from naturalfeature_area where not st_isvalid(the_geom);
takes a long time (about an hour I think) and gives me a list of invalid geometries, after pages and pages of warnings about things crossing over themselves.
It seems that one trick that people use to force gemetries to be valid is to use the st_buffer() command with the buffer radius set to zero.   I checked it worked by doing:
select st_isvalid(st_buffer(the_geom,0.0)) from naturalfeature_area where gid=2342149;
(2342149 was the first entry in my list of invalid geometries) - Success - it returns 't' for true meaning the st_buffer trick fixes it.
Now to update the database.  To correct the error I had to do:
update naturalfeature_area set the_geom=st_multi(st_buffer(the_geom,0.0)) where gid=2342149;
Note that I do not really know what the st_multi thing does, but I got errors about geometry constraint violations without it - I think that st_buffer returns the simplest type of geometry it can, and st_multi forces it to be a multipolygon, but I could be wrong!.

Now all I need to do is remember enough about subqueries to write a bit of SQL that does the correction for every invalid geometry - back to that well known search engine....

Well, I have set it going using the following to try to repair the geometries:
update naturalfeature_area set the_geom=st_multi(st_buffer(the_geom,0.0)) where gid in (select gid from naturalfeature_area where not st_isvalid(the_geom));
I think I'll leave it for a few hours....

ThinkPad X60s Battery Trouble

I recently bought myself a second hand IBM Thinkpad X60s because I wanted a small laptop to use on the train, which has enough power to run postgresql/mapnik etc. so I can play with maps while I am living out of hotels (better on the body than too much food and beer, but not as pleasant!).
The problem I have is that at every charge cycle the gnome power manager in Ubuntu reports that the battery is less charged than before, and then it shuts down very quickly when running off battery because it thinks it is discharged.
I tried running it to completely empty by letting it suspend on low battery, then waiting for the battery to really die.  The next charge was higher than before, but the same problem has repeated.
I just used gconf-editor to set the action on low battery to 'nothing' so that it will run until the battery is properly dead.   The charge history is shown below:

Basically Ubuntu thinks the battery was dead nearly an hour ago, but it is still running!
I think this must be a problem with ACPI, but don't know how to sort it - a job for a very rainy afternoon.   For now I will just leave the auto-suspend switched off and try to guess when the battery is about to give up.

Saturday, 2 October 2010

Using VectorMapDistrict with PostGIS

Well, vmd2pgsql took about 9 hours to import the entire UK into my postgresql database...No too bad.
Now I need to do something with it, because the mapnik stylesheets that I set up to use the shapefiles will not work - need to change it to use the postgresql database instead.
The things that need changing are:

  1. Add the layers and datasources in XML (no need to do it in python now I don't have hundreds of separate datasources).
  2. Convert the styles to use lowercase letters for the fields ('featcode' rather than 'FEATCODE') - postgresql seems to be case sensitive, and they have gone into the database lower case, but were upper case in the shapefiles.
Sounds easy,  but I decided I don't want it to look like the OSM stylesheet, which is a bit garbled - I want a nice structure to make it easy to maintain.  I could have used XML entities, but that seems a bit crude - define an entity, then 'use' it to actually 'call' it.   Includes seem much more suitable....But to get includes working I need mapnik2.  I have put a basic structure at code.google.com/p/ntmisc/vmdmap, but still have a few issues:
  • I have had to define the database parameters manually because entities do not seem to be working in the include files, which is no good!
  • The output is transparent background with black lines (see picture below), which is not what I asked for - not sure if this is a problem with the style file, or my build of mpanik2 - I'll have to run some tests on mapnik2 to make sure it works right.
  • But it's too late to fix it now - job for tomorrow!

Friday, 1 October 2010

More Rendering of VectorMapDistrict Data

The nice people from Ordnance Survey have sent me a complete set of VectorMapDistrict data (which is on 6 DVDs!).   Surprisingly they didn't even charge me for the DVDs or postage.
Now I have all that data I thought I'd better do something with it!

First step was to get it onto a nice fast disk, so I copied it onto the hard disk of my server...which took a while...

Then I tried to use my vmdmap.py program to render it all, but mapnik bombed out with an error as it was adding the various shapefile layers.  I suspect that this is because every single shapefile is in its own layer, as it is a completely separate datasource, and I think I either ran out of memory or hit some mapnik internal limit.   This means I can't use vmdmap.py to render the whole country, which is a bit of a shame.

To get around this I think I need to merge them into a single datasource.  I don't know enough about manipulating shapefiles to do this, so instead am making use of shp2pgsql which allows you to import a shapefile into a postgresql database.   I have written another program, based on vmdmap.py called vmd2pgsql which will scan through a directory tree looking for the various shapefiles in the vectormap district dataset, and importing them into postgresql.   This gives a much lower number of tables - just one per shapefile name, but each one will have a lot of data.

It is importing now, so will see how long the import takes, then how well it renders.  I suppose it should render ok because I have the whole UK OSM dataset in a single database and that works, but we'll see over the weekend if it ever finishes!  The code is at my google code site.

Friday, 3 September 2010

Mapnik and OS_OpenData

I decided to compare the quality of OpenStreetMap mapping to that which has been released by Ordnance Survey OpenData.  
I started with VectorMapDistrict, which is a dataset provided as ESRI Shapefiles.   It is provided as a large number of shapefiles, so setting up a mapnik stylesheet manually would have been a pain.  Instead I separated the styles into an XML file, but added the layers using a python program which scans down the directory tree to add the various shapefiles to the map.  The python code is vmdmap.py and the stylesheet is styles.xml.

Then I tried Meridian2.  This is much simpler - just one shapefile for each sort of feature (A-Road, river etc.).  I kept the same structure with the styles defined in an XML file, and the layers in python, but it could have all been done in a single XML file this time.  The python code is md2map.py and the stylesheet is styles_md2.xml.

You can see the results below.

The most notable things are that Meridian2 is much simpler geometry, but it includes more road names than Vector Map District.  The OpenStreetMap rendering is fancier because I used the standard OSM style, rather than the home made ones I used for the OS Data.   I do like the rocks that appear in the VectorMap District rendering though - I will have to import it into OSM!.

VectorMapDistrict
Meridian2
OpenStreetMap


Sunday, 8 August 2010

Mapping - OSM and Mapnik

I have been playing with mapnik quite a bit lately - I decided I needed to learn how to use it better to improve my townguide program.
I have been very impressed with Lonvia's Hiking Map, which is an overlay showing hiking routes on top of a standard OpenStreetMap map, and thought I could do something similar.

My first effort was a map showing the locations of supermarkets (handy when we go on holiday so we know where to go to stock up on provisions) - It can be seen here.
Someone has imported a lot of powerline data from Ordnance Survey into my local area, so I tried a Power Station Map.  These maps all have an 'about' link showing how they work.

I have produced a similar one to show the use of Ordnance Survey OpenData in the OpenStreetMap map of the UK, which can be seen here.

More recently I have wanted to produce maps that include contours, and also allow me to plot GPX traces to show where we have been.   To do this I have imported the SRTM data for the UK into my postgresql database using the technique described here.   This allows me to render an Openstreetmap map with contours, which is useful for the countryside.
Adding a GPX trace over the top was a bit difficult - I wanted to do it in python to save hard coding the path to the GPX file in the mapnik style file, but I haven't managed to do that - I ended up adding a GPX layer to my mapnik style file using the mapnik ogr plugin.   The result can be seen here.

Sunday, 25 April 2010

The Irritating thing about Android

I have recently got an Android phone so that I can write software for it without the irritating security issues you get with J2ME on 'normal' phones (boxes popping up saying 'do you want to let this program do this?' etc.).

I was very surprised that some things that most phones do 'out of the box' are a real pain in Android, that makes me think this is still a platform for gadget enthusiasts rather than end users.

These include:

* Sharing Contacts: All of the phones I have seen with bluetooth let you send contacts from one phone to another over bluetooth. Not Android - I ended up adding my contacts from my old phone to Google Mail manually. This made them appear on the Android one.
* Using the phone as a modem: My Sony Ericsson W890i just works - plug the phone into a computer and it appears as an ethernet card - dead easy (I think I had to select this as an option in the phone menu, but it was just a simple menu action. The nearest thing I have got with Android is a program called 'Proxoid' to run on the phone. You then have to use the ADB program that comes with the Android Developers SDK to forward packets to the phone, and set up your browser to use a proxy. A long way from an end user experience. I'll do a separate post on this so I can remember how to do it next time I am stranded in Spain and my T-Mobile broadband dongle won't work!

Saturday, 3 April 2010

Reprojecting Ordnance Survey Raster Data

I have been looking at the Ordnance Survey StreetView data that was released to the public on 1st April.
This has got me thinking about a previous project that I failed to make work - I have some old maps of my home town and wanted to be able to display them on the web and switch between them so you can see what changes from year to year - I have maps from 1870 and 1914.  I want to use OpenStreetMap as the recent data.  Now StreetView is available I can use that too for comparison.

The problem is projecting them onto the same coordinate system - I failed miserably last time, but my two scanned old maps were on the same scale, so you can see them at (http://maps.webhop.net/oldmaps/openlayers.html).

The maps are in Ordnance Survey projection, which has the code EPSG:27700.   The downloaded OS data has the origin specified in metres northings and eastings.
You can translate this into the same projection as OpenStreetMap (EPSG:900113) using:
gdalwarp -s_srs EPSG:27700 -t_srs EPSG:900913 *.TIF nz.tif
Check the projection information with
gdalinfo nz.tif | more

Thursday, 1 April 2010

Ordnance Survey Data Released

Ordnance Survey have released quite a lot of data for free use (http://www.ordnancesurvey.co.uk/oswebsite/opendata/).
The most interesting sounding data (meridian) is a 2Gb shape file, so I chickened out of trying to do anything with that tonight.
Instead I downloaded the 50k Gazetteer and have put it into a postgresql database so it can be queried to look for things.
Work in progress is at http://www.maps2.webhop.net/openos.
It sort of works (just not very useful yet - you can do http://maps2.webhop.net/openos/gaz/www/doSearch.php, and it gives you a list of all of the items in the database containing the string 'hartlepool' - just need to transfer them back to the browser and plot them on a map now....

Sunday, 7 March 2010

Android User Interfaces

I am working on a colour processing program for android (more later once it works). A fundamental part of this is to create a custom view to use in the user interface - it will eventually display a colour wheel, but it is nowhere near that yet!
The problem was that I am creating a subClass of View, which draws something on the screen.
I then simply added it to my UI XML description and tried to use it.
I got an InflateException, which was a bit of a surprise.
It turns out that I made a mistake - I had overridden the simple View(Context) constructor, which would be fine if I was adding the view directly from a program, but to use it from XML you need to override another constructor, View(Context, AttributeSet).

Now I have done that it works. Sorry this makes no sense unless you are used to Android things, and if you are used to Android, it is probably obvious, but this will help me next time I forget! I found this out here.

GPX Route Editor

I was trying to plan a family cycling trip in a rush this morning and struggling to find a map to
use (opencyclemap is no good because I like to go to un-mapped places so I can add them to the map).
I finally remembered that ages ago I wrote a little javascript application to display an Ordnance Survey map, and let you put route markers on it which you can then put onto a Garmin GPS receiver to use for navigation - I used that to record where I intended to go, so I knew where there was supposed to be a bridleway...
Anyway I thought I should add that program to my repository in case anyone else is interested. There are a few things to do to it - see the README file in the repository directory - The source code is in my ntmisc code repository under gpxedit.
You can find the program working at http://www.maps.webhop.net.

I will do a bit more work on this when I get chance. I always intended to convert it to OpenLayers rather than multimap's proprietary API, and at the moment you have to copy and paste the GPX file into a text editor to get around the irritating web browser security - need a way of getting around that - the simplest would be to add an 'echo' server side application that would allow you to download the GPX file from the server, but that feels like brute force and ignorance!

Wednesday, 17 February 2010

Android Development on Ubuntu

I just got an LG GW620 android mobile phone. I want to use it for mapping (http://www.openstreetmap.org). There is an android editor called Vespucci available, but it needs some work to make the UI easier to use. I thought I would have a look at the source code to see if I can work out how to modify it.
The android developer web site says that it is best to use the Eclipse IDE, but when I tried I got all sorts of errors about un-satisfied dependencies.
Fortunately someone had posted some instructions to get it working on Ubuntu (http://ubuntuforums.org/showthread.php?p=8842292#post8842292).
Now I have followed this I have a working IDE to develop Android applications, and have got a basic "Hello World" application working. I have also downloaded and complied the Vespucci source code, and run it on the SDK emulator.
Just need to understand how Android UIs work now....

Sunday, 14 February 2010

Progress Mapping Hartlepool

Benjamin and I are making progress mapping Hartlepool for OpenStreetMap.
I have just run a query on my database to see how many kilometers we must have cycled to produce the map. I was really quite shocked that it was over 550 km! No wonder I am tired...
Progress with the map and the breakdown of the distances are shown on the Hartlepool OpenStreetMap page.

Tuesday, 2 February 2010

Townguide Web Service Working

Well, I think the townguide web service is working now (http://www.townguide.webhop.net).
There is a simple queue so it only renders one job at a time, and you can view the progress of your job through the queue.
The source code and early documentation is stored at http://code.google.com/p/townguide.
The main thing to fix now is the output resolution - if I increase the resolution (in dpi) of the output map too far, mapnik changes zoom level and reduces the size of the text, so it is no easier to read - I need to understand mapnik style sheets now....

Sunday, 3 January 2010

Town Guide Progress

The good thing about so much bad weather over Christmas was that I could do some nerdy work rather than going out walking or cycling.....
The town guide Python project has therefore made good progress. The program can now generate a PDF 'poster' that can include a street index, and labelled features from the map database (pubs, shops, tourist attractions etc.). A typical example is shown at http://www.townguide.webhop.net/example1_hartlepool.pdf.

I have set up a very rudimentary web service using a PHP script to call townguide.py to generate a town guide on demand at http://www.townguide.webhop.net. It is running on a very small computer, and there is no queue, so there is no saying what will happen if more than one person tries to use it at once - that is another job!

Tuesday, 15 December 2009

Animation

Laura and I have been experimenting with stop-motion animation - set up a camera on a tripod with a remote shutter release, then position a scene in front of it, and change it by a very small amount between shooting frames.
10 frames per second seems to work ok for what we are doing (and you get more video for your money, so it is a bit more rewarding!).
I am using mencoder to turn the jpeg images from the camera into a mpeg4 video using the following:

mencoder mf://*.JPG -mf fps=10 -o leaves.avi -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=800 -vf scale=640:480

This produces an mpeg4 encoded .avi file, scaled to 640x480 pixels.
You can then run mencoder again to add a mp3 audio stream.
The results can be seen at http://flickr.com/jones139.

Sunday, 29 November 2009

Town Guide (again)

Progress with the Town Guide program stalled a bit when I went on holiday, then was rather busy at work for a while. I am back to thinking about it now.
The current version has two output modes, HTML and PDF - both are quite crude, but work.
The PDF output uses LaTeX, which seemed like a good idea at the time, but I am struggling because LaTeX thinks it knows better than me what the output is supposed to look like - it keeps putting the tables etc. at the end, rather than where I ask for them.
This means I have two options - either do battle with LaTeX to sort it out, or abandon it and go for a more 'direct' PDF production.
I think that as the output I am producing is short and simple, I may as well go for 'direct' production. I am going to have a try with ReportLab's python library (http://www.reportlab.org/oss/rl-toolkit/guide/).
I'll go and read the instructions and see how it goes....