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

I want to create a tricky overpass query, but im not sure how to do it and im hoping someone knows the answer

I want to

  1. Find all counties in Norway (admin_level=4)
  2. Find all municipalities in these counties (admin_level=7)
  3. Find all place=farm in these municipalities
  4. Output a list of some sorts where its possible to separate the municipalities

I did try to create something in overpass-turbo, but it quickly became a mess and didn't output what I wanted

Help!

asked 08 Apr '18, 19:30

FredrikLindseth's gravatar image

FredrikLindseth
815132435
accept rate: 13%

edited 08 Apr '18, 19:45


It's currently not possible to restrict an area query with an area, so it is necessary to use map_to_area calls to work down through the areas.

You can see at the top I've commented out the code to fetch all the admin_level 4 entities in Norway and instead just fetch 1 of them, because the script times out with more than 1. Anyway, this script emits the tags for each admin_level=4 and then emits the tags for each admin_level=7 and then any place=farm found within that admin_level=7. So processed as a stream, each farm will belong to the last seen county and municipality.

[out:json][timeout:150];
//area[admin_level=2]//["name:en"=Norway]->.country;
//(way(area.country)[admin_level=4];
// rel(area.country)[admin_level=4];
//)->.counties;
( rel[admin_level=4][name=Nordland];
  //rel[admin_level=4][name="Trøndelag"];
)->.counties;
foreach.counties(
  out tags;
  map_to_area->.countya;
  (way(area.countya)[admin_level=7];
   rel(area.countya)[admin_level=7];
  )->.munis;
  foreach.munis(
    out tags;
    map_to_area->.munia;
    (node(area.munia)[place=farm];
     way(area.munia)[place=farm];
     rel(area.munia)[place=farm];
     );
    out center;
  );
);

You can also look at https://github.com/drolbr/Overpass-API/issues/187 for some discussion of how to fetch points with containing areas.

permanent link

answered 09 Apr '18, 19:32

maxerickson's gravatar image

maxerickson
12.7k1083176
accept rate: 32%

That worked very well! I changed the output to out number too, since that was what I really was after.

(12 Apr '18, 00:42) FredrikLindseth

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
×65

question asked: 08 Apr '18, 19:30

question was seen: 3,232 times

last updated: 12 Apr '18, 00:42

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