I’m helping out with a political campaign, which requires getting volunteers to leaflet certain streets, and they have to coordinate their efforts so they don’t duplicate or run back on each other. I think the best way of doing this would be with an etherpad document, or a google spreadsheet, where the volunteers just tick off which streets are going to be leafleted and when. But before getting there I need a list of streets for any given city council ward. So here’s a method for getting that.

First, gather all the primary data you’re working with. In this example I’m going to do Abingdon Northcourt ward, which is in Oxfordshire. The OpenStreetMap data for Oxfordshire can be downloaded from Geofabrik, and the Northcourt ward boundary can be downloaded from MySociety. You’re going to want to get the ward boundary in GeoJSON format.

Next, download osmctools, there are packages for it in the Ubuntu & Debian repos.

Then, process the GeoJSON file and turn it into a .poly file. Here’s what our sample GeoJSON file typically looks like:

{ "type": "Polygon", "coordinates": [ [ [ -1.282529466386853, 51.676802917902897 ], [ -1.282601673673259, 51.676809652993249 ], [ -1.2822182526448, 51.676728185157565 ], [ -1.282529466386853, 51.676802917902897 ] ] ] }

and here’s what a .poly file looks like:

-1.282529466386853 51.676802917902897
-1.282601673673259 51.676809652993249
-1.282695771865959 51.676803933562887
-1.282766504639175 51.676812457855576
-1.283108443661912 51.67686489781606
-1.283397401403342 51.676883744884776
-1.283514368279363 51.676895248230487

The method of converting between the two is to cut out the text and extra square brackets at the beginning and end, then find ], and replace with \n then find [ and , and replace them with nothing, then add a 1 at the beginning of the file and an END at the end. There are a few plugins for gedit which give it macro functionality, but at this point it’s faster to just do the find/replace than look for a macro plugin.

Save the new file as northcourt.poly

Then open up a terminal to clip your oxfordshire-latest.osm.pbf file down to just the Northcourt ward:

osmconvert oxfordshire-latest.osm.pbf -B=northcourt.poly -o=northcourt.pbf

Convert .pbf to .osm

osmconvert northcourt.pbf > northcourt.osm

Here’s what that should render as:


Remove everything but the roads

osmfilter northcourt.osm --keep="highway=residential =primary =secondary =tertiaty =unclassified" > northcourt_streets.osm

Extract the street names and output to csv

osmconvert northcourt_streets.osm --csv="name" --csv-headline --csv-separator=, -o=northcourt_streets.csv

There are always some duplicates, so to finish off use the bash command

uniq sort northcourt_streets.csv | sort | uniq > northcourt_streets_unique.csv

And there you should have a list of the streets in Abingdon’s Northcourt ward, ready for volunteers to run through. If I have to do lots of these I’ll try to make a script to automate the process, but otherwise there’s no actual coding involved and it’s quite straightforward.