OSM osm2pgsql extract by country

I hope this is the right forum to post this question. I have used the
osm2pgsql utility to import the planet-XXXXXX,osm.bz2 file into postgres /
postgis. I need now to extract features to GML using ogr2ogr, in
some cases I would like to exclude areas based on country, is this
possible? I don’t see any table / fields that make reference to a
country name or some other identifier



  • If your only need a few countries, you might want to import extracts of the OSM base like http://download.geofabrik.de/osm/ (one by one, or with different prefixes for tables)
    It saves you the full import, the complexe queries
    But it might be a bit larger than the country you are interested in, which means you will get data from a few km away from the actual boundary

  • Let the magic of PostGIS do the work :

A function like this one might be usefull :

  • if you want high boundaries accuracy, you can try to use those of OSM, however, they are always moving and updated and are not allways forming closed polygons, wich means you’ll have to check that before using them and might need to repair them before use.
    Those boundaries, if valid, can be found in the planet_osm_polygon table, with admin_level=2 tags