Friday 29 June 2012

Leaflet vectors

Looking at a new-style speed limit made me realise that I might not have completed the speed limit tagging in Hull. I looked for a map showing speed limits as an overlay, struggled to find what I wanted and decided to put something together for myself. My last blog post about creating a map with overlays using leaflet seemed popular, so I thought I'd write up my experience here too.

I wanted to show a map with an overlay of roads that don't have a speed limit tag (maxspeed=*) so I can survey them and add the correct tag. I decided to extract all highway=* that are roads which could have speed limits, so not service roads or cycleways for example. The maxspeed tag (or absence of it) could drive an overlay to highlight missing ones and show the actual speed limits where there is a tag.

I thought I would extract the OSM data for an area, parse it to extract the relevant highways and store that in a database. Then create a page to display the map using AJAX and GeoJSON to deliver an overlay.

All of the scripts are on github, so you can download it from there. The README file includes some installation stuff to make it work for you. A working version is here, though the data will rapidly get out of date, so don't trust what you see - it is only an example.

First, get some data. I download the data for areas from Geofabrik's download server. Their service is excellent and by providing data that is less than a day old and cut into useful areas it is hard to beat. I downloaded the county data for East_Yorkshire_with_Hull. I usually download the data as .pbf a file. That can go straight into many utilities, but to use the data myself with Python I convert it to a .osm file first with osmosis.

I wrote some Python code to parse an OSM file long ago and I keep going back to it. It is included as python/ I wanted to find the ways tagged with highway. I wanted to narrow that down to a smaller group of highways: unclassified to motorways ignoring footways for example. I then wanted to load the ways into a database for use later. All of this is in the script called python/ My cheap and cheerful website host provides PHP and MySQL, so my database is MySQL. To draw the overlay on a map I need the list of nodes that make up the way, so I extracted them too and added them to a table too. The table layouts are in github folder database.

The script works through the ways that fit the requirements and extracts from the list of nodes that make up the way. From these it works out the most northerly, southerly, easterly and westerly of the nodes and stores this bounding box of the way. We want to show an overlay for the current part of the map being displayed and this bounding box for a way will allow that.

Loading the data for (38,000 ways and 350,000 nodes to work through) took a few minutes, loading 15,700 ways and 114,000 nodes.

Now to create a PHP script that gets called with a bounding box and returns the ways that fall, at least partially, into that area. This script will get called by an AJAX call so it needs to return data in the correct format for Leaflet to use. I use GeoJSON, which Leaflet handles well. Now we need to make a decision. Do we choose the things that get displayed and how it gets displayed in the PHP script that extracts the data or do we throw everything at the client and select there? I've decided to manage the process in the PHP. My interest is in finding which roads have no maxspeed tag, so I paint the overlay red with no tag and blue with tagged roads. The roads with a maxspeed tag get its text set for a popup, no maxspeed tag gets 'Not set'. The script is called getspeeds.php.

The HTML is simple, I create two columns side by side, managed by CSS. The map is displayed in a div, as always, this time called speedmap. The work is all done in the JavaScript showspeed.js, which has comments to guide you along. I have deliberately limited the minimum zoom to be 14 so not too much data is requested by AJAX.

I hope this will help anyone wondering about vector layers. If you have any questions, leave a comment and I'll try to answer it. If you have a better way of doing this, leave a comment linking to your solution and I'll ask you questions.

Monday 25 June 2012

New 40 Zone

Making maps has made me look at my surroundings much more carefully. I have found things close to home that I didn't know existed and it has focussed my attention for detail. A couple of weeks ago we saw a speed limit sign which displayed the usual circular 40 speed limit, except it had the word zone written below it. We saw some more the other day and it seemed unusual, so I investigated some more. Finally I got an email from someone in the Highways department at East Riding of Yorkshire council. In it she stated

"Draft guidance from the Department for Transport on setting speed limits supports the introduction of lower limits on routes in rural areas where high speeds are inappropriate and there are significant numbers of leisure users such as cyclists, horse riders and walkers."

She goes on to explain,

"The implementation of a 40 zone requires less signage than a conventional 40mph speed limit, therefore is less intrusive on the rural environment.  Special authorisation for the signs was sought from the Department for Transport.  A Traffic Regulation Order is in place for the 40 zone, which means it is enforceable by the Police.

The routes selected for the zones are pilot schemes and are being closely monitored."

I find this both interesting and a little puzzling. Firstly I'm in favour of reducing traffic speeds on some rural roads. Fatal accident rates for both vehicles and cyclists is higher on some types of rural roads and one way to try to reduce this is slow down traffic. Speed limits alone won't help much unless they are enforced which seems highly unlikely on quiet rural roads, but it may help a little. Today I was driving along one of the newly changed roads at 40 mph being tail-gated by an impatient driver from behind and almost forced off the road by a Mercedes-moron coming the opposite way doing much, much more than 40 mph.

The signs are subtly different. The initial 40 sign is the same size as normal but has the word ZONE below it. This is in upper case, which I think breaks the usual use of mixed case. 40 mph limits normally have small repeaters every few hundred metres to remind the drivers they are in a speed limit. This does not. It does have reminders every mile or so. To me this is too far apart. The sign carries a 40 mph circular sign at the same size as the beginning of the limit with 'ZONE reminder' below.

The road itself can have extra markings. Some parts have 40 roundels painted on the road occasionally and some parts have broken white lines painted down both sides of the road, but not at sharp corners.

The end of the zone can either be the national speed limit or a lower limit such as 30 mph when the road enters a village, say.  Where there used to be a simple 30 mph sign there now has to be a new sign with a plaque saying the 40 zone ends.

The only other zones I have come across are 20 mph zones such as ones outside schools. Some 20 mph areas are not zones, they are simply a 20 mph speed limit. I do wonder what kind of bureaucratic mind dreams up these subtleties in the Department for Transport and imposes the hapless motorist with these distractingly different signs? 

One other point is that the guidance from the DfT is draft, so what happens if the final guidance does not include this kind of road or if the signage is different?

Tuesday 12 June 2012

Open Data

I have been speaking to someone from a local council and they are keen to publish lots of data under an Open licence. Some of it is geo data and I'm interested in what benefit that might bring to OSM. I hope to have more to report soon.