User:Kovposch/Overpass API/Overpass API by Example

From OpenStreetMap Wiki
Jump to navigation Jump to search

Lack of feature in areas

try it yourself in overpass-turbo
wr({{bbox}})[landuse=farmyard];
foreach
(
  wr(area)[building]->.inside;
  (._; .inside;); 
  wr._(if: count(wr) == 1); // For convenience, to output them together
  out geom meta;
);

3D rendering

try it yourself in overpass-turbo
wr({{bbox}})["building:part"][location!=underground]["building:levels"]["building:levels"!=0] -> .parts;  
.parts out geom;
(
  way.parts;
  way(r.parts); 
); 
node(w)->.partsp;
 
wr({{bbox}})[building][location!=underground];  
foreach
{ 
  ._ -> .each; 
  (.each; way(r.each);); 
  node(w); 
  node._.partsp ->.shared;
  wr(area.each)["building:part"] -> .inside;
  if (shared.count(nodes) == 0 && inside.count(wr) == 0) // Includes check for `building:part=` lying exactly on `building=` outline, which is not returned by `(area)` 
  {  
    .each out geom;
  }; 
};  

{{style:
area[building]
{ color:blue; fill-color:blue; }

area[building:part]
{ color:green; fill-color:green; }
}}

(To be refined)

Overlapping areas

try it yourself in overpass-turbo
(way({{bbox}})[natural](if: is_closed()); rel({{bbox}})[natural][type=multipolygon];);   
foreach
{
  ._ -> .this;
  (way(around.this:0)[natural](if: is_closed()); rel(around.this:0)[natural][type=multipolygon];); // Any touching areas
  (._; - .this;) ->.touching; // Removes itself 
  if (touching.count(wr) > 0)
  {
    way(r.this);
    (node(w); node(w.this);) -> .thispts;  
    foreach.touching
    {
      ._ -> .thistouching;
      way(r);
      (node(w); node(w.thistouching);) -> .thistouchingpts;  
      node.thispts.thistouchingpts -> .sharedpts;    
      if (sharedpts.count(nodes) == 0) // Overlapping areas without shared points 
      { 
        (.this; .thistouching;);
        out geom meta; 
      }; 
      if (sharedpts.count(nodes) > 0) 
      { 
        node(area.this);
        (._; - .thispts;) -> .ptsinsidethis; // `node(area)` finds points lying on the area. Need to remove them. 
        node.ptsinsidethis.thistouchingpts -> .crossptsinsidethis; 
        if (crossptsinsidethis.count(nodes) > 0) // Check for points inside to discard areas with exactly shared outlines
        { 
          (.this; .thistouching;);
          out geom meta; 
        }; 
      }; 
    }; 
  };  
};

Intersection streets

Overpass_API_by_Example#Search_for_street_intersections

try it yourself in overpass-turbo
[out:json][timeout:25];

// The public street network
way({{bbox}})["highway"~"^(trunk|primary|secondary|tertiary|unclassified|residential|living_street|pedestrian)$"]->.streets;

// Get nodes that connect between three or more street segments
node(way_link.streets:3-)->.connections;

// Get intersections between distinct streets
foreach .connections->.connection
{
  // Get adjacent streets
  way(bn.connection);
  // If the names don't all match, add the node to the set of intersections
  if (u(t["name"]) == "< multiple values found >") 
  {
     make node  
      ::id = connection.u(id()), 
      _streets = set(t["name"]);  
    (._; .intersectionstreets;) -> .intersectionstreets;
    (.intersections; .connection;) -> .intersections; 
  };
}; 
for.intersections->.this(id())
{
  foreach.intersectionstreets
  {
    if (this.val == u(id()))
    {
      ._ -> .thisintersectionstreets;
    };
  };
  node.intersections(if: id() == this.val) -> ._;
  convert node 
    ::id = id(),
    ::geom = geom(),
    :: = ::,
    _intersectingstreets = thisintersectionstreets.u(t["_streets"]);
  out geom;
};