This is a static archive of our old OpenStreetMap Help Site. Please post any new questions and answers at community.osm.org.

Overpass get output structured (with hierarchy, not flat ?)

1

I have the following script working so that in the resulting file I get a list of all districts and their ways and their nodes and the nodes themselves (but in the file it's in reverse order and with a flat hierarchy)

<osm-script>
  <query type="relation">
    <has-kv k="admin_level" v="9"/>
    <bbox-query e="1" n="2" s="3" w="4"/>
  </query>
  <union>
    <item />
    <recurse type="relation-way"/>
  </union>
  <union>
    <item />
    <recurse type="way-node"/>
  </union>
  <print />
</osm-script>

Now I can't figure out how to get this structured, so that in the end it's not flat but so that the relation nodes have their member nodes (type="way"), and so that the member nodes themselves have long-lat nodes (all structured)

asked 15 Apr '13, 20:45

SeWo's gravatar image

SeWo
31224
accept rate: 0%

edited 15 Apr '13, 21:02


One Answer:

1

I suggest

<osm-script>
  <query type="relation">
    <has-kv k="admin_level" v="9"/>
    <bbox-query e="1" n="2" s="3" w="4"/>
  </query>
  <foreach>
    <print />
    <recurse type="relation-way"/>
    <union>
      <item />
      <recurse type="way-node"/>
    </union>
    <print />
  </foreach>
</osm-script>

This prints for each relation first the relation and then the ways and nodes belonging to this relation. Ways and nodes that belong to more than one relation are repeated to appear after each relation where they belong to.

You can even do the same thing with the ways:

<osm-script>
  <query type="relation">
    <has-kv k="admin_level" v="9"/>
    <bbox-query e="1" n="2" s="3" w="4"/>
  </query>
  <foreach>
    <print />
    <recurse type="relation-way"/>
    <foreach>
      <print />
      <recurse type="way-node"/>
      <print />
    </foreach>
    <print />
  </foreach>
</osm-script>

Or let the ways and relations appear after their members:

<osm-script>
  <query type="relation">
    <has-kv k="admin_level" v="9"/>
    <bbox-query e="1" n="2" s="3" w="4"/>
  </query>
  <foreach>
    <recurse type="relation-way" into="ways"/>
    <foreach from="ways" into="way">
      <recurse type="way-node" from="way" into="nodes"/>
      <print from="nodes"/>
      <print from="way"/>
    </foreach>
    <print from="ways"/>
    <print />
  </foreach>
</osm-script>

answered 16 Apr '13, 06:20

Roland%20Olbricht's gravatar image

Roland Olbricht
6.7k36489
accept rate: 36%

1

Again, note that as a way can belong to multiple relations and nodes can belong to multiple ways and relations, the resulting file will likely be bigger (and take longer to generate) than the flat version.

The hierarchy you're looking for doesn't exist in that form in OSM; depending on what you do, it might be less error-prone to embrace the OSM data structure directly.

(16 Apr '13, 09:23) Vincent de P... ♦

well it really builds some structures, but it's not what I wanted... I guess I'll build a script that parses all the data, builds a data-structure itself and then writes my kml file which is what I need in the end

(16 Apr '13, 17:14) SeWo

thanks anyway ;)

(16 Apr '13, 17:14) SeWo

Source code available on GitHub .