[GIS] Overpass API: Get coordinates of postal boundary

openstreetmapoverpass-api

How can I use the overpass-API to recursively get all nodes and their coordinates which belong to the ways of an relation? I need this to draw bounding polygons of all the postal areas inside a city.

I have the following query:

<osm-script>
<area-query ref="3600062649" />
  <recurse type="up" />
  <query type="relation">
     <item />
     <has-kv k="boundary" v="postal_code" />
  </query>
<print />
</osm-script>

This returns all relations with postal boundary inside the queried area and all the ways of these relations, however it does not return the nodes/coordinates of the points which make up these ways:

<relation id="1300371">
   <member type="way" ref="51702433" role="outer"/>
   ...
   <tag k="note" v="04451 Borsdorf"/>
   ...
</relation>
<relation>
....
</relation>

I would like to get a list of postal areas with their nodes, like this:

<relation id="1300371">
   <member type="way" ref="51702433" role="outer">
      <node id="323735839" lat="51.3751873" lon="12.5689796" />
      .... 
   </member>
   <tag k="postal_code" v="04451"/>
   ...
</relation>

The tag with the postal code should still be included so I can distinguish all received postal areas.

Best Answer

I would recommend the following approach: it returns coordinates for ways' nodes 'in place', like you described in your question.

The only thing, which is missing in the output is the technical OSM node-id (ref). I assume that for use case of drawing some boundary area on a map, this is most likely not needed. If you take a look at the screenshot below, overpass turbo is drawing all postal code areas on the map without any issue.

area(3600062649);
rel(area)["boundary"="postal_code"];
out geom;

http://overpass-turbo.eu/s/7In

enter image description here

Example output:

  <relation id="1301656">
    <bounds minlat="51.3145882" minlon="12.2366519" maxlat="51.3391521" maxlon="12.3034696"/>
    <member type="way" ref="87537618" role="outer">
      <nd lat="51.3179538" lon="12.2426125"/>
      <nd lat="51.3178723" lon="12.2414464"/>
      <nd lat="51.3176008" lon="12.2399198"/>
    [...]
    </member>
    [...]
    <tag k="boundary" v="postal_code"/>
    <tag k="note" v="04205 Leipzig"/>
    <tag k="postal_code" v="04205"/>
    <tag k="postal_code_level" v="8"/>
    <tag k="type" v="boundary"/>
  </relation>
Related Question