Using OS Shapefiles

From OpenStreetMap Wiki
Jump to navigation Jump to search

exclamation mark

Imports and automated edits should only be carried out by those with experience and understanding of the way the OpenStreetMap community creates maps, and only with careful planning and consultation with the local community.
See Import/Guidelines and Automated Edits code of conduct for more information. Imports/automated edits which do not follow these guidelines might be reverted!

Using the Ordnance Survey shape files

There are various shape files that are part of the OS Opendata. They can all be used in a similar way. For more general shape files information see the Shapefiles page

Merkaartor

A simple way to use OS Shape files is to open them into Merkaartor. You can then use Merkaartor standard editing tools to edit the shape files. Then select the ways and choose the Force Upload option to ensure they are uploaded to OSM.

Re-projecting the shape file

Some software requires the OS shape files need to be re-projected to the WGS84 datum that OSM uses. One way to do this is is to use the ogr2ogr tool that is part of the GDAL toolset: http://www.gdal.org/. Unfortunately the .prj file supplied with the OS data set is not suitable for this. It is a simple matter to replace it with one that has the extra data in it.

You can find a suitably modified .prj file here: http://shapes.raggedred.net/ as supplied by SK53 (thanks).

A copy of the .prj file should be renamed to match the shape file.

I suggest re-projecting the original OS shape file to your own copy which you then use for everything else.

To re-project a shape file called NaturalFeature_Area.shp, using its .prj file (replaced as above) and saving the changed file as osmnf.shp use:

ogr2ogr -t_srs WGS84 -s_srs ESRI::NaturalFeature_Area.prj osmnf.shp NaturalFeature_Area.shp

or, just using GDAL's built-in OSGB datum:

ogr2ogr -t_srs WGS84 -s_srs 'EPSG:27700' osmnf.shp NaturalFeature_Area.shp

This will give a file accurate to within 10 metres. For sub-metre accuracy, obtain the gridshift files derived from OSTN02 (link), and use those:

ogr2ogr -t_srs WGS84 -s_srs '+init=epsg:27700 +nadgrids=scotland.gsb,england-wales.gsb' osmnf.shp NaturalFeature_Area.shp

Examine the shape file

To find out what is in a shape file, open it with a suitable tool, such as Quantum GIS (QGIS) from http://qgis.org/. Shape files can have various data type in them, the types that are most useful here tend to be polylines and polygons. We can convert polylines to an OSM way and a polygon to a closed OSM way, both with suitable tags. Most of the OS shape files have large amounts of data in them, so it is usually best to select some of the data and save it as a smaller shape file.

Extract OSM data

Shape files have a .dbf component to them. This gives them a way of adding data about each shape file object. You can look at these in the Attribute Table using QGIS. In addition each object has an index known as its id. This means that each shape can be extracted either by looking for an attribute or by its index. Alternatively all of the shapes can be extracted at once. The attributes used in the shape files vary across the types of OS data, so you need to look in the file for the one that suits you. Note that some of the attributes apply to more than one shape object in the shape file. In some shape files there are only two or three different attributes that cover hundreds of objects, so in this case saving a subset into a smaller shape file and then extracting all of these objects works well.

To extract the shapes there are a number of tools available, including some of my simple ones, described below. You can get these from here: http://shapes.raggedred.net/ The three scripts exallpoly.py, extractpoly.py and explylinen.py all need the routines in dbfUtils.py and shpUtils.py.

exallpoly.py

This is the simplest tool in that it extracts all of the polygon shapes in the shape file and converts them into a single OSM file suitable for loading into JOSM. The routine as supplied adds a tag natural=water onto every OSM closed way, but you can change that in the code to be any other tag.

extractpoly.py

This tool extracts a named polygon from the shape file and writes it as an OSM closed way in a file, using the polygon name as the default filename.

explylinen.py

This tool extracts the nth polyline from the shape file and writes it as an OSM way in a file, using the number as default filename.

All of these tools have extra command line arguments, use -h for help.

Examples

I will assume that you have a folder to work with. The scripts have been installed in subfolder called scripts. In Linux remember to change the permissions to allow execute. The shape files you want to work on have been re-projected and are in a subfolder called shp.

1. Extract a civil parish boundary from the BoundaryLine data set

The BoundaryLine shape file for civil parishs is called parish_region. It is big, so I selected an area I was interested in (a county) using QGIS and saved it as a smaller shape file. I then re-projected that and saved it into the shp subfolder as eroy.shp.

I used QGIS to find the name of the parish I wanted (Swanland CP).

I then ran the extractpoly.py script to extract the named polygon from the shapefile.

scripts/extractpoly.py shp/eroy.shp -p"Swanland CP"

I could have used the full python command:

python scripts/extractpoly.py shp/eroy.shp -p"Swanland CP"

If the shape file is found and the polygon within it is found the OSM way will be written as a file called Swanland CP.osm. The way will have negative node numbers and a way number, so it can be loaded into JOSM. You can override the starting node and way numbers if required with command line arguments. You can also override the output file name, see help for more details:

scripts/extractpoly.py -h

When you use the OSM way, it will overlap with surrounding civil parishes, districts, counties and regions. Please ensure that these ways and nodes are shared. Don't just load every civil parish, leaving all of the overlapping ways. The actual parish should be marked with a relation; the ways that are used should also be tagged boundary=administrative and use the lowest value of admin_level of the relations using that way. This tool does none of this leaving you with the task of tidying up manually, other tools may well do a better job. Note that many of the coastal boundaries extend beyond the coastline way to the low water mark, so they look as if they jut out into the sea.

2. Extract an area of water from the VectorMap District data set

The VectorMap District data is broken into 100km squares, based on the OS grid system. These are given a two-letter code and are available individually to download and expand as a folder. Within the data section of each there is a folder for a 10kmx10km square for each area. In here there is a shape file for water and woodland called NaturalFeature-Area. I re-projected this shape file and used QGIS to select the Water with the attributes table and save it. I then selected a smaller area and saved that as a small shape file called ponds in the shp subfolder.

I then ran the exallpoly.py script to extract all of the waterways in the shape file into multiple OSM ways in an osm file called ponds.osm.

scripts/exallpoly.py shp/ponds.shp -oponds.osm

The resulting file can be loaded into JOSM for editing before uploading. You can download the area too to help to manage the merging with any existing data. The OS polygons have gaps where anything crosses a waterway and all of these need adjusting, usually by closing the gaps. Rivers, streams and canals need the tags changing to waterway=riverbank and a suitable waterway way drawn through the middle to show the direction of flow and the name.

3. Extract a coastline way from the BoundaryLine data set

The BoundaryLine shape file for coastlines is called high_water_polyline. The high water mark is what OSM uses as coastline. It is big, so I selected an area I was interested in using QGIS and saved it as a smaller shape file. I then re-projected that and saved it into the shp subfolder as oshwey.shp.

I used QGIS to find the index of the part of the coast I wanted.

I ran the explylinen.py script to extract the polyline from the shape file and saved it into an OSM file called spurn.osm.

scripts/explylinen.py shp/oseyhw.shp -i3 -ospurn 

Before you upload the coastline make sure the direction is correct (land on the left) and that the end nodes of one way is merged to the end nodes of the adjoining way. You will need to deal with the existing coastline too. The script as it stands creates a way with the tag natural=coastline. If you use the script for any other purpose, such as extracting power lines from the VectorMap Settlement_Line data set, you can change the tag in the script.

Alternative: Downloadable BoundaryLine GPX files

If you want to skip all the shapefile manipulations and get straight to work, reprojected GPX files for regions, counties, unitary authorities and districts for the whole of the UK can be found here: https://csmale.dev.openstreetmap.org/os_boundaryline/ Civil Parishes are also being progressively added.

More information

Please use these data and scripts responsibly. Only load data for areas you know. Check it on the ground as much as you can. Some of the OS data is incomplete and out-of-date. Surveys are always useful.

Chillly 15:49, 11 May 2010 (UTC)