OpenStreetMap

How to create an OSM extract of your city.

Posted by skorasaurus on 8 November 2011 in English. Last updated on 19 April 2014.

Update:

Here's how to obtain all of the OSM data within your city's boundaries. This assumes you are a linux or Mac OS X user and have JOSM and [osmosis](http://wiki.openstreetmap.org/wiki/Osmosis) installed.

1. Download an extract of your state or country. Geofabrik posts extracts for each US state and Canadian Province on a [http://download.geofabrik.de/north-america.html](daily basis).

2. Obtain the OSM_id for the relation of your city's boundary. I do this by downloading a small area of my city and its border in JOSM, selecting part of your boundary, and then pressing cntrl + shift + i). This will open a new window in your web browser containing the tags for a portion of your boundary.
Now the way id that you brought up IS NOT the entire boundary for the city, it's only a part of it as ihighlighted in the map. You need the osm_id for the city's entire boundary. You can obtain this by going to the bottom of that webpage and find "Part of: Relation: Name of the city (THE_OSM_ID)" This is the_OSM_id of your city's entire boundary. For example, "Relation Cleveland (182130) (as outer)" Copy this You will need it for Step #2.

3. Obtain the .poly for your city's entire boundary. I prefer to use this tool by Jocelyn at http://osm102.openstreetmap.fr/~jocelyn/polygons/index.py
Paste in the THE_OSM_ID from step #1 into the uppermost text box and hit enter.
In the new window, right-click and save the .poly file and choose whatever you'd like.

This will new window will appear, copy its contents and choose its file name, name.poly

4. Use osmosis to filter out the administrative boundaries.

osmosis --read-pbf file=ohio-latest.osm.pbf --bounding-polygon file=cleveland.poly --used-node --write-pbf cleveland.osm.pbf

Customize the command above to the your file names. Make sure that the poly file is in the same directory as your PBF extract.

If you have a very small town and want to examine the data in JOSM first, you can
osmosis --read-pbf file=ohio.osm.pbf --bounding-polygon file=cleveland.poly --used-node --write-xml cleveland.osm

Voila, you have your extract :)

==========================================================================
Original post test, kept for posterity.

I have been rendering maps of my local city [Cleveland, Ohio] recently. Because of Cleveland's administrative borders, I had to extract the city of Cleveland using a large bounding box that included a lot of data outside of the city of Cleveland. As a result, I had to manually remove many ways and nodes (via josm) outside the city's borders before I later used this data within the city's boundaries for rendering in osmarender, and as a postgis db in tilemill and mapbik.

To automate this, I have been looking to create an extract of my city (and hopefully to be able to replicate this with other cities). This extract would only contain data within the city's administrative boundaries.

I found a similar project to mine,
https://github.com/migurski/Extractotron The Extractotron - which is a great project but its extracts are for cities including areas outside a city's administrative borders.

I have unsuccessfully tried the following:

1] Created an extract that contains the administrative borders of my city in its entirity from a larger extract (in my case, my state) from geofabrik

bzcat ohio.osm.bz2 | osmosis\
--read-xml enableDateParsing=no file=-\
--bounding-box top=41.606 left=-81.88 bottom=41.3903 right=-81.5323 clipIncompleteEntities=true --write-xml file=-\
| bzip2 > extracted.osm.bz2

2.] Filtered the extract again in osmosis to only contain the administrative boundaries [and highway motorway link as a reference for me]

Specifically I did:
osmosis --read-xml extracted.osm --way-key-value keyValueList="highway.motorway_link,boundary.administrative" --used-node --write-xml entireclevelandboundingbox.osm

4. Opened the file in josm, selected the multipolygon that consisted of the city's administrative boundary, and then right-clicked on it, Selected Members, and copied it to a new layer

5. In the new layer, Removed the tags from the city's boundaries and then combined all of them into one single way [in one direction] and saved it as a new osm file.

http://dl.dropbox.com/u/17003223/clevelandboundaryonly-dell.osm

6. Used osm2poly to create a polygon:
perl ./osm2poly.pl clevelandboundaryonly-dell.osm > clevelandboundaryonly-dell.poly

polygon can be found:

http://dl.dropbox.com/u/17003223/clevelandboundaryonly-dell.poly

7. Use the polygon as the boundary from the first large extract [from the state of ohio].

bzcat ohio.osm.bz2 | osmosis\
--read-xml enableDateParsing=no file=-\
--bounding-polygon file=clevelandboundaryonly-dell.poly clipIncompleteEntities=true --write-xml file=-\
| bzip2 > extracted.osm.bz2

http://dl.dropbox.com/u/17003223/extracted-dell.osm
This last osm file results in administrative boundaries are broken and incomplete.

An example of these broken boundaries can be can see in a screenshot of josm at http://i.imgur.com/Tw8Ba.png
In this screenshot, the underlying gray line is an inactive layer in josm and the same OSM file that I used to create the polygon in Step 5. The
administrative boundary, shown as orange dashed line in the screenshot, is from the extract in step 7 and should be over the gray line.

Any idea why the boundaries are broken in my new extract ?

Although we were unsuccessful, thank you to stereo in #osm for troubleshooting this with me yesterday

Location: Tremont, Cleveland, Cuyahoga County, Ohio, United States

Discussion

Comment from chriscf on 8 November 2011 at 21:08

If you're happy to have a map that's blank outside the city boundaries, find the relation or way that you need and stick it into the Overpass API. It has an option for doing something like this. I've used it to generate extracts of a single city district for testing. I think I managed to string together a query that would get everything within the bounding box and then complete any incomplete objects, though if I did they're now lost to history courtesy of a failed hard disk.

Comment from skorasaurus on 12 November 2011 at 17:26

Chriscf,
That's the map that I'm looking to make ! Unfortunately, I haven't figured out yet the correct rules to use for the Overpass API. :/

Through my searching, I found a couple suggestions in http://help.openstreetmap.org/questions/8273/how-do-i-extract-the-polygon-of-an-administrative-boundary

and I'll be testing those out in the next day or so to see if they will work.

Comment from skorasaurus on 13 November 2011 at 23:33

I tried nicolasdumoulin's approach
and it unfortunately left me with the same results that I had experienced earlier, missing segments of the administrative border. :[

Comment from chriscf on 14 November 2011 at 14:02

If this is what you're looking for, then start with the sample query under "Download an entire city" and change the number to 3600182130. If you've still got bits missing, then the relation either has the wrong ways in it or pieces are missing from it. I'd try it now but I'm at work so don't have my usual tools to hand.

Comment from skorasaurus on 14 November 2011 at 17:20

Thanks for the info, I'm anxiously waiting to try that query once more as each time I have tried [3 or 4 today], the server is overcrowded :(

After just 2 or 3 seconds of submitting the input I have received the message:
Error: runtime error: open64: 0 /osm3s_v0.6.95_areas Dispatcher_Client::request_read_and_idx::timeout. Probably the server is overcrowded.

Comment from skorasaurus on 25 February 2013 at 22:26

For anyone reading, I figured this out several months ago:

  1. Obtain the id for the relation of the boundary. (You can do this in josm by clicking on the boundary and then press cntrl + shift + i).

Now the way that you brought up IS NOT the boundary for the city, it’s only a part of it. Go down to the bottom of the new webpage and find “Part of: Relation: Name (THISID####)”

  1. Obtain the .poly for this file. There’s multiple ways to do this, I find overpass, although powerful, it was slammed/overcrowded.

Open this great tool by jocelyn. http://osm102.openstreetmap.fr/~jocelyn/polygons/index.py

new window will appear, copy its contents and choose its file name, name.poly

  1. Use osmosis to filter out the administrative boundaries.

osmosis –read-pbf file=ohio.osm.pbf –bounding-polygon file=summitcounty.poly –tf accept-ways boundary=administrative –used-node –write-xml summit.osm

Log in to leave a comment