How to cut the city subset (any relation) from OSM data

Once I needed to get a clean city subset from raw OSM data (because it’s so convenient, compact and just beautiful). To my surprise, I did not find a ready-made recipe, which is why it took me a little sweat to complete this task.



In view of the foregoing, I consider it my duty to share my experience.



Chisinau OSM



Suppose I want to get a Chisinau subset (why? ... it's warm there, my house is there, my mother is there)



Of course, we are not interested in Planet.osm , but there is geofabrik , but there the planet is cut into rather large shmatka - http://download.geofabrik.de/europe/moldova.html



So, the city border is in the raw data .



It’s easy to get a landfill from the relationship, we do it either by hand or using any suitable utility, for example, this one .



III ... now osmosis comes into play .



We have the osm of all Moldova and the limiting range of Chisinau, this is quite enough to get the city’s site from a raw XML (OSM) file:



osmosis --read-xml file="moldova.osm" --bounding-polygon file="kishinev.poly" --write-xml file="kishinev.osm"
      
      





Similarly, you can dig out data from the PBF (Protocolbuffer Binary Format) file:



 osmosis --read-pbf file="moldova.osm.pbf" --bounding-polygon file="kishinev.poly" --write-xml file="kishinev.osm"
      
      





You can read a little more about osmosis here .



Well, the finished subset needs to be opened urgently in JOSM or imported into a personal project!



Chisinau JOSM







All Articles