Need help with a particular Overpass query

Hello,

Just a simple request to build a particular overpass query.

The desired result is to find all elements of a particular type, for example amenity=school.

a) if the school is an area (area amenity=school), the outer area (or areas) is wanted (way or relation).
b) if there are school buildings (building=school, amenity=school + building=*) OUTSIDE an amenity=school area, the area of those buildings are wanted.
c) if there are school nodes OUTSIDE any of the above areas those nodes are wanted.

Pls halp :sob:

I’m going to try this puzzle :sweat_smile:, without knowing much about it and with “some” :rofl: trial and error. Does this make an approximation of what you are asking for?

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

[out:xml][timeout:60][bbox:{{bbox}}];
// Part A: School Areas (ways or relations with amenity=school)
(
  wr["amenity"="school"][!building];
)->.schoolAreasWr;
.schoolAreasWr map_to_area -> .schoolAreas;

// Part B: School Buildings Outside School Areas
(
  way["building"="school"]["amenity"!="school"];
  way["amenity"="school"]["building"];
) -> .schoolBuildingsWays;
(
  way(area.schoolAreas)["building"="school"]["amenity"!="school"];
  way(area.schoolAreas)["amenity"="school"]["building"];
) -> .schoolBuildingsWaysInAreas;
(.schoolBuildingsWays; - .schoolBuildingsWaysInAreas;)-> .schoolBuildingsWaysOutsideAreas;

// Part C: School Building Nodes Outside School Areas and Buildings
.schoolBuildingsWaysOutsideAreas map_to_area -> .schoolBuildingsWaysOutsideAreasArea;
node(area.schoolAreas)["amenity"="school"]-> .schoolNodesInAreas;
node(area.schoolBuildingsWaysOutsideAreasArea)["amenity"="school"]-> .schoolNodesInBuildingsWays;
node["amenity"="school"]-> .schoolNodesAll;
((.schoolNodesAll; - .schoolNodesInAreas;); - .schoolNodesInBuildingsWays;)-> .schoolNodesOutsideAll;

// Outputs
.schoolAreas out geom;
.schoolBuildingsWaysOutsideAreas out geom;
.schoolNodesOutsideAll out;

{{style:
  way[amenity] {
    color:orange;
    fill-color:orange;
  }
  relation[amenity] {
    color:orange;
    fill-color:orange;
  }
  way[building] {
    color:magenta;
    fill-color:magenta;
  }
  node[building] {
    color:red;
    fill-color:red;
  }
  node[amenity] {
    color:red;
    fill-color:red;
  }
}}

true :sob:, in my area there were no relations, try this one please :crossed_fingers:

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

[out:xml][timeout:60][bbox:{{bbox}}];
// Part A: School Areas (ways or relations with amenity=school)
(
  wr["amenity"="school"][!building];
)->.schoolAreasWr;
(.schoolAreasWr;>;)->.schoolAreasComplete;
.schoolAreasComplete map_to_area -> .sa;

// Part B: School Buildings Outside School Areas
(
  wr["building"="school"]["amenity"!="school"];
  wr["amenity"="school"]["building"];
) -> .schoolBuildingsWays;
(
  wr(area.sa)["building"="school"]["amenity"!="school"];
  wr(area.sa)["amenity"="school"]["building"];
) -> .schoolBuildingsWaysInAreas;
(.schoolBuildingsWays; - .schoolBuildingsWaysInAreas;)-> .schoolBuildingsWaysOutsideAreas;

// Part C: School Building Nodes Outside School Areas and Buildings
.schoolBuildingsWaysOutsideAreas map_to_area -> .schoolBuildingsWaysOutsideAreasArea;
node(area.schoolAreasComplete)["amenity"="school"]-> .schoolNodesInAreas;
node(area.schoolBuildingsWaysOutsideAreasArea)["amenity"="school"]-> .schoolNodesInBuildingsWays;
node["amenity"="school"]-> .schoolNodesAll;
((.schoolNodesAll; - .schoolNodesInAreas;); - .schoolNodesInBuildingsWays;)-> .schoolNodesOutsideAll;

// Outputs
.schoolAreasComplete out geom;
.schoolBuildingsWaysOutsideAreas out geom;
.schoolNodesOutsideAll out;

{{style:
  way[amenity] {
    color:orange;
    fill-color:orange;
  }
  relation[amenity] {
    color:orange;
    fill-color:orange;
  }
  way[building] {
    color:magenta;
    fill-color:magenta;
  }
  node[building] {
    color:red;
    fill-color:red;
  }
  node[amenity] {
    color:red;
    fill-color:red;
  }
}}
1 Like

Excellent!! That seems like it’s almost perfect! However, school areas that are relations seem to be left out (multiple outers). Example: Relation: ‪Bønes skole‬ (‪5912282‬) | OpenStreetMap

I think you nailed it. If only ChatGPT was this good. I’ll have some looks and I’ll come back if there is anything that I haven’t seen spotted yet. Stellar work though, thank you so much!

1 Like