2

I'm trying to understand Overpass API, I'm experimenting with it, but cannot get what I expect. Overpass API doesn't return me neither polygon shapes, nor WKT, nor GEOJson, nor even a highlight of the area I'm querying for.

My goal is to get either WKT or GEOJson of current OSM relations (exactly polygons (a set of lng/lat points)) for countries and cities. For example, I want to get a polygon for France. I make these queries in http://overpass-turbo.eu/:

relation
    ["name"="France"]
    ["coverage"="polygon"]
    ["type"="boundary:administrative"];
out body;

The response I get, no any polygon lat/lng data:

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API">
<note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>
<meta osm_base="2013-11-08T11:43:01Z"/>


</osm>

The second version of my request, I provide only a name:

relation
    ["name"="France"];
out body;

Now I get dilated information about the country, but no polygons either:

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API">
<note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>
<meta osm_base="2013-11-08T11:37:02Z"/>

  <relation id="11980">
    <member type="node" ref="17807753" role="admin_centre"/>
    <member type="relation" ref="1362232" role="outer"/>
    <member type="relation" ref="1263863" role="outer"/>
    <member type="relation" ref="1263868" role="outer"/>
    <member type="relation" ref="1628087" role="outer"/>
    <tag k="ISO3166-1" v="FR"/>
    <tag k="TMC:cid_58:tabcd_1:Class" v="Area"/>
    <tag k="TMC:cid_58:tabcd_1:LCLversion" v="8.00"/>
    <tag k="TMC:cid_58:tabcd_1:LocationCode" v="2"/>
    <tag k="admin_level" v="2"/>
    <tag k="currency" v="EUR"/>
    <tag k="flag" v="http://upload.wikimedia.org/wikipedia/commons/c/c3/Flag_of_France.svg"/>
    <tag k="land_area" v="administrative"/>

...... and so on ......

How to get a set of lng/lat (WKT or GEOJson) of the territory (countries and cities only) by their name?

3 Answers 3

3

I would use the area feature of the Overpass API in order to achieve this goal.

[out:json];
area
   ["name"="France"]
   ["boundary"="administrative"]
   ["admin_level"="2"];
(._;(pivot:);>;);
out body qt;

Lines 2-5 are selecting the correct area.

Line 6 creates an union consisting of:

  • the area itself
  • the pivot element of this area (the relationship/way/... from which this area was generated)
  • the ways/nodes referenced by the pivot element

Line 7 just prints out the results. The qt parameter is just a performance optimization and not necessary (http://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide#order_attribute)

By setting admin_level to 2 you can make sure, that you will only get national borders. You will need to adapt this value in order to get different types of boundaries. A list of the meaning of admin_level's values can be found at http://wiki.openstreetmap.org/wiki/Admin_level#admin_level.

By the way: Areas are created by the Overpass-API server and are not part of the original OpenStreetMap database. Internally, the Overpass server is doing exactly what scai wrote: It is gathering together the data from relationships. (see wiki.openstreetmap.org/wiki/Overpass_API/Areas)

1
  • I'm getting ERROR 400: Bad Request with this query, looks like (._;(pivot:);>;); doesn't work.
    – kontextify
    Nov 17, 2015 at 12:47
2

Where did you read about this coverage=polygon tag you specified in your first query? There is no such tag and therefore the query returns no results. You are mainly interested in the boundary=administrative and admin_level=2 tags.

Your second query returns relation 11980. As you can see in your XML output it contains other members with the role outer, for example relation 1362232. This is the relation for France métropolitaine and also contains other members with the role outer, for example relation 90162. This relation now finally defines a part of the country border at the English channel. It contains several ways as member which in turn are just a list of nodes (and possibly some tags). Every node has a coordinate and thus all nodes together define the polygon for France.

If you are not yet familiar with relations, ways and nodes then start reading about OSM's elements. Relations are not actually some type of polygons but contain other elements such as other relations, ways or nodes which in turn can define a polygon, or something else depending on the tags.

As a consequence you may have to run further queries for your relation members, depending on the output (note that not every country relation looks the same, OSM is very flexible here). Overpass API is also capable of recursive queries which you can use to retrieve all relation members, see the Overpass API language guide for further information.

2
relation
    //Name of country with native language
    // Use ["name:en"="France"] for english
    ["name"="France"]
    [boundary=administrative]
    [admin_level=2];

(._;>;);

out body qt;

It will return you Polygons, Lines and Points, so filter features by type, when import to GIS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.