Extracting layers from open street map using osmosis and overpass API

Thanks to the good folk developing Open Street Map (OSM), we have a fantastic mapping resource that is completely free. Coverage is quickly expanding, as well as detail, and its these advancements that provide an opportunity to create gis layers that we would otherwise struggle to find in the public domain.

My first need of this was when I needed a shapefile of all the open/green spaces in london, and after many hours trying to locate this resource without paying a high fee or waiting several weeks, I decided to look into OSM.

OSM contributors provide a tag for every object they map, and this provides the basis for extracting objects of interest.

Firstly, we need some data! Probably the easiest place to collect this is from geofabrik. Here you can download maps at various different geographical scales in both .shp and .osm format. If you want to just extract general attributes e.g. landuse, leisure, natural etc. then downloading the shapefile can provide this straightaway. If you want to go deeper then you will need the .osm file. After the data was collected, I used two different methods to extract data.

Osmosis

Osmosis is a command line interface that allows extraction based on the key:value tag provided by the contributor e.g. landuse would be the main tag, with agriculture being a specific key under that tag. Osmosis can be easily installed on windows with instructions here.

There were two main commands I used to for extraction: Firstly to extract nodes (i.e. any object represented by a point; like a bus stop):

 osmosis --rbf layer_name.osm.pbf -–node-key-value keyValueList=”key.value” -–wx outputfile.osm

Secondly, the following was used for extracting ways (i.e. polygons):

osmosis --read-xml layer_name.osm –-way-key-value keyValueList = “key.value” –-used-node –-wx outputfile.osm

Note: Make sure you are in the directory where your map is stored.

Note 2: When extracting ways (i.e. polygons) you need to use a .osm file as the input. The tool osmconvert can easily convert from pbf to osm.

  1. –rbf locates the .osm layer you downloaded earlier (note, you must be in the directory where is file is saved).
  2. -node-key-value specifies you want capture a node with a specific key/value.
  3. keyValueList is a string to specify what you want to extract. Multiple nodes/ways can be selected using the format:
     keyValueList="leisure.park, landuse.recreation_ground"
  4. -wx is the output file

The output file (.xml) can be directly loaded into QGIS and then saved as a shapefile. If you are using ArcMap, then a ogr2ogr command will provide the transfer to shapefile.

Overpass API

This method takes queries on the Overpass website to extract data from the OSM API. Examples and formats for queries can be found here. I found this method slightly less intuitive to use than osmosis but I can still see the benefit if you could get to understand the query format well.

Example format for node, with brackets representing bounding box of area of interest:

(
   node
     ["highway"="bus_stop"]
     ["landuse"="forest"]
     (51.3, 0.33, 51.7, -0.51);
   body out;
)

Example format for way. Note, to display way’s in map you need to extract the nodes as well. This is achieved by the command >;

(
    way
      ["landuse"="woodland"]
      (51.3, 0.33, 51.7, -0.51);
   >;
);

out;

Finally, an example for getting everything connected to a tag:

(
  node
    ["amenity"="fire_station"]
    (50.6,7.0,50.8,7.3);
  way
    ["amenity"="fire_station"]
    (50.6,7.0,50.8,7.3);
  rel
    ["amenity"="fire_station"]
    (50.6,7.0,50.8,7.3);
);
(._;>;);
out;

These basic commands provide you with very strong tools for generating shapefile data from open street map for use in GIS, something I have found very useful!

If you would like to donate or contribute to the Open Street Map cause then visit here.

Advertisements

2 thoughts on “Extracting layers from open street map using osmosis and overpass API

  1. Pingback: What’s New In GIS And Biological Research: 26 May 2015 | GIS In Ecology

  2. Pingback: Open spaces shapefile for London | geospatial wandering

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s