OpenStreetMap

How to extract a smaller geographic region using polygon

Sometimes you want to extract a sub-region of OpenStreetMap region because:
(1) download.geofabrik.de does not have the region that your are interested in,
(2) you are only interested in a smaller region (e.g. hong-kong.map is 9 MB instead of china.map 305 MB).
(3) in specialized situation (such as disaster relief effort or special sports event), you want to minimize the size of the offline map to fit as many mobile devices as possible.

Technique: download a larger region, use "polygon" to extract the smaller region of interests. For example, Myanmar is not a sub-region extract of Asia in download.geofabrik.de but you can create your own.

Download asia-latest.osm.pbf from download.geofabrik.de, caution, this is a 4.3 GB download, using wget:

wget http://download.geofabrik.de/asia-latest.osm.pbf

Create a polygon that covers your region of interest   

On a PC
Download and invoke "JOSM - Java OpenStreetMap Editor".
File..New Layer
Imagery...OpenStreetmap...zoom in to the region Myanmar (press RIGHT mouse button to drag/pan)

(1) click "Draw Nodes" in the left-hand side tool bar,
(2) draw nodes that cover Myanmar, you don't need too many points, a rough polygon will do.
Make sure the last node "completes" the polygon, i.e, the last point coincides/overlaps the first point.
(3) Move mouse to the Layers windows/panel, click to select the "Data Layer" (deep blue).
(4) File...Save As....  myanmar.osm
(5) File...Exit...Exit Now (please do not upload your polygon to the OSM world wide database server)

polygon

Download a public domain Perl code osm2poly written by Frederik Ramm, save that file as osm2poly.pl

Use Filezilla to transfer osm2poly.pl and myanmar.osm to your Ubuntu VM

Invoke VirtualBox, start the VM ubuntu-osmosis, use Filezilla to transfer osm2poly.pl and myanmar.osm to your Ubuntu VM home directory
cd ~
perl osm2poly.pl myanmar.osm > myanmar.poly

Extract Myanmar from Asia using the polygon extraction
~/osmosis/bin/osmosis --rb file=asia-latest.oem.pbf --bp file=myanmar.poly --wb file=myanmar.pbf

Convert myanmar.pbf to myanmar.map

~/osmosis/bin/osmosis --rb file=myanmar.pbf --mw file=myanmar.map 

(the map will show in Burmese, the official language of Myanmar)

If you want the map to show in English whenever possible, add a preferred-languages directive:

~/osmosis/bin/osmosis --rb file=myanmar.pbf --mw file=myanmar.map preferred-languages=en 
(the map will show in English whenever possible)

For other preferred languages, use ISO 639.2 2-letter-code as the parameter in the preferred-languages directive.
See https://www.loc.gov/standards/iso639-2/php/code_list.php

Data Flow:

myanmar.osm  --> myanmar.poly   [conversion using Perl code osm2poly.pl]

asia-latest.osm.pbf -->  myanmar.pbf  [osmosis region extraction using polygon myanmar.poly]

myanmar.pbf --> myanmar.map  [osmosis conversion from pbf to map format]


© 2016 Nicholas Fong

nicholas.fong@gmail.com

Last updated: 2016-12-08

Disclaimer