NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

[out:csv(::type, ::id, name, admin_level)]; 
area[name="Portugal"];
rel(area)[admin_level=4][name];
out;

I have this but I also wanted a column with the id of the relation to which each relation listed immediately belongs (parent). A search on OSM has in the result "...part of relation x", but how can I get that kind of information here through overpass-turbo?

A parent of a relation x is the relation at the admin_level above which has that relation x as a child.

I wanted something like: ::type, ::id, name, admin_level, ::parent id

asked 28 Jul '18, 15:17

andrefrsilva's gravatar image

andrefrsilva
11335
accept rate: 0%


You can use the convert statement to modify each element with the parent relation id:

[out:csv(::type, ::id, name, admin_level,parent)]; 
rel[name="Portugal"][admin_level=2]->.c;
.c map_to_area;
rel(area)[admin_level=4][name];
convert rel ::id = id(), ::=::,parent=c.set(id());
out;

It is necessary to save the country into a result set, so that the set can be named in the convert statement.

An alternate strategy is to make a second script where you recurse up relations and then post-process the json to get the parent information. This is ultimately the strategy the OSM website is using, the canonical information about relation membership is the list of children in each relation. It might be caching the reverse information somewhere, but that would be rebuilt each time a relation is edited.

(edited to move answer up from comment)

permanent link

answered 28 Jul '18, 16:50

maxerickson's gravatar image

maxerickson
12.7k1083176
accept rate: 32%

edited 28 Jul '18, 18:14

I also thought about using something like this:

[out:csv(::type, ::id, name, admin_level, ::parent_id)]; area["name"="Portugal"][admin_level=2]->.parent; //somehow saving the id of this parent to parent_id//

then:
rel admin_level = 4 ; out;

but how?

(28 Jul '18, 17:15) andrefrsilva

Oh, I didn't realize that the parent would be available. Here's a step in the right direction, http://overpass-turbo.eu/s/ACK just need to figure out how to pull the Portugal id into it instead of writing "blah".

(Which I'm not sure is currently possible)

(28 Jul '18, 17:36) maxerickson

And here you go: http://overpass-turbo.eu/s/ACT

I found the answer in https://dev.overpass-api.de/blog/final_0_7_54.html, the blog there is I think still the best place for information about the recently released features.

(28 Jul '18, 18:00) maxerickson

Thanks a lot maxerickson! However, this way it just ends up as some "manual" input, and not the actual "parent" id. If I use admin_level=5 it continues to give out as "parent" what is now "grandparent".
The goal would be to use this with several levels (in my initial case, using admin_level bigger and then <<; to have layers of family before).
::id, name, admin_level, ::parent_id


id 1, name 1, level 1, parent 1 ... id 2, name 2, level 2, parent 2 (its actual parent) ...

(28 Jul '18, 18:35) andrefrsilva

The technique should work using other flow control, you'd just have to use lots of named sets and foreach loops to keep track of everything.

I'm pretty sure I'd do it in Python using json, where it'd be easy to build up mapping between children and parents.

(28 Jul '18, 18:42) maxerickson

Ok. And do you know if it is possible to use id instead of "name", since there might be different relations with the same "name" and ending up causing mistakes?

(28 Jul '18, 18:48) andrefrsilva

rel(id) will fetch the relation with that id.

(28 Jul '18, 18:55) maxerickson

Instead of rel[name="Portugal"][admin_level=2]->.c; .c map_to_area; rel(area)[admin_level=6][name]; what should I use? (Being id=295480)

(28 Jul '18, 19:03) andrefrsilva

You'd replace rel[name="Portugal"][admin_level=2]->.c; with rel(295480)->.c; and keep the rest of it.

(28 Jul '18, 20:24) maxerickson

Ok, thanks. This is what's lacking: https://overpass-turbo.eu/s/AFo

(30 Jul '18, 16:00) andrefrsilva
showing 5 of 10 show 5 more comments

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×483
×228

question asked: 28 Jul '18, 15:17

question was seen: 3,581 times

last updated: 30 Jul '18, 16:00

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum