0

Using Overpass I'm trying to get the admin_level 2, 4, and 6 parents of wikidata Q16557 (Dallas, Texas, but I want to be able to do this for any US city).

This query works fine for admin_level = 6 (returning all five counties that the city of Dallas is in):

[out:json];
area[wikidata=Q16557]->.a;
(
   relation[boundary=administrative][wikidata][wikipedia][admin_level~"6"](area.a);
);
out tags;

But if I change it to admin_level 4, I'm expecting to get Texas, but I get no results:

[out:json];
area[wikidata=Q16557]->.a;
(
   relation[boundary=administrative][wikidata][wikipedia][admin_level~"4"](area.a);
);
out tags;

And if I change it to admin_level 2, I'm expecting to get the United States, but I still get no results:

[out:json];
area[wikidata=Q16557]->.a;
(
   relation[boundary=administrative][wikidata][wikipedia][admin_level~"2"](area.a);
);
out tags;

Is my original query correct? It always seems to work with finding the county(ies) a city is in.

Is there any reason it would work for admin_level 6 but not 4 or 2?

2 Answers 2

0

You have to use recursive up relations statement like:

[out:json];
area[wikidata=Q16557]->.a;
relation[boundary=administrative][wikidata]
[wikipedia][admin_level~"6"](area.a) -> .rel;
    
// below line just copy .rel to .a
.rel -> .a; 

// this one finds all upper level relations where 
// .a are part(s) of them and removes (-) .rel
(.a <<; - rel.a;) -> .b;

(rel.b[admin_level="4"];);
    
out geom tags;

https://overpass-turbo.eu/s/1Ugs

enter image description here

Edited:

If you wish go back one level, to children relations, use >> (recursive down) statement like:

[...]
rel.b[admin_level="4"] -> .c;

.c >> -> .d;

(rel.d["boundary"="administrative"][wikidata];);
out geom tags;

enter image description here

https://overpass-turbo.eu/s/1Uld

3
  • 1
    Thanks, Grzegorz -- I'll have to study. I was hoping for something that didn't require recursion to get both the parents and children admin levels of any wikidata id. This works for the children, but not the parents: [out:json];area[wikidata=<wikidata>]->.a; relation[admin_level~"<admin_level(s)>"](area.a); out tags; and it's relatively quick to retrieve them. I.e., all counties in Georgia: [out:json];area[wikidata=Q1428]->.a; relation[admin_level~"6"](area.a); out tags; Commented Nov 18 at 16:48
  • Don't know your specific case but I would rather download the USA .pbf, extract the boundaries/places (towns/cities/etc) and play with it spatially (st_within(), st_covered_by() kinds of function) than mess with overpass. :) Commented Nov 18 at 16:58
  • I'm not familiar with that but I'll look at it. Commented Nov 18 at 16:59
0

I wanted to get away from recursion since I suspect it's slower and I also wonder if it's necessary, but this is the solution I ended up with:

[out:json];
area[wikidata=<wikidata id>]->.a;
(relation[wikidata][wikipedia](area.a); <<;)->.p;
relation.p[admin_level~"<admin_level(s)>"];
out tags;

Many thanks @Grzegorz Sapijaszko for pointing me in this direction and providing the correct answer to my initial question, which was poorly worded for my actual requirements.

I didn't end up using his solution in my application because the solution here is more succinct and it returns all the admin_levels, which is what I wanted although that admittedly strays from the question I had originally asked.

Your Answer

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

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