Overpass API: Unterschied zwischen den Versionen

Aus Geoinformation HSR
Wechseln zu: Navigation, Suche
K
K
 
(21 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
'''Overpass API - Selektiver Download von Daten aus der Hauptdatenbank von [[OpenStreetMap]].'''
+
'''Overpass API - Gefilterter Download von [[OpenStreetMap]]-Daten in der nativen Struktur Nodes, Ways, Relations.'''
  
Siehe auch
+
Overpass Turbo Services (interaktive Webapp):
* [[OSM-Technisches]]
+
* '''>> Weltweit: http://overpass-turbo.eu/ <<'''
* [[HowTo OpenStreetMap]]
+
* Deutschland: http://overpass-api.de/  
 
+
* Schweiz: http://overpass-turbo.osm.ch/
Einstieg:
 
* Neues Handbuch??? https://dev.overpass-api.de/overpass-doc/de/
 
* Learn Overpass: http://osmlab.github.io/learnoverpass//en/
 
 
 
Language documentation:  
 
* http://wiki.osm.org/wiki/Overpass_API
 
* https://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_QL
 
* https://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide
 
  
Web-Apps / API:  
+
Overpass QL Query Beispiele:  
* Overpass Turbo (interaktive Webapp): http://overpass-turbo.eu/ (Weltweit)
+
* >> '''[[Overpass QL Beispiele]]''' <<
* http://overpass-turbo.osm.ch/ (Schweiz)
+
* Overpass API by Example: https://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_API_by_Example
* http://overpass-api.de/
+
* https://osm-queries.ldodds.com/for-local-mappers/index.html
  
Support:
+
Prompt für '''>> ChatGPT & Co. <<''' (Copy & Paste und ersetze "user query"):
* Mailing list: http://listes.openstreetmap.fr/wws/info/overpass
 
* https://help.openstreetmap.org/questions/20479/using-osm-data-with-javascript-via-api-in-my-code-display-data-as-text
 
 
 
== Beispiele ==
 
 
 
'''>>> Man bevorzuge 'Overpass QL' (versus'Overpass XML')! <<<'''
 
 
 
Umgang mit bbox:
 
* node[...]({bbox}) -- nur innerhalb Overpass (und ggf. uMap)
 
* node[...](47.213,8.704,47.336,8.905)
 
* Als Konstante... tbd.
 
 
 
=== POI Query ===
 
 
 
  /*
 
  Alle Castles...
 
  FYI: historic hat eigentlich die Werte
 
  "castle|yes|tower|ruins|archaeological_site"
 
  */
 
  [out:json][timeout:25];
 
  (
 
    node["historic"="castle"];
 
    way["historic"="castle"];
 
  );
 
  out geom tags;
 
  (
 
    relation["historic"="castle"];
 
  );
 
  out center tags;
 
 
 
Das liefert nicht ganz dasselbe:
 
  [out:json][timeout:25];
 
  (
 
    node["historic"="castle"];
 
    way["historic"="castle"];
 
    relation["historic"="castle"];
 
  );
 
  out center tags;
 
 
 
=== Filter by Admin Boundary ===
 
 
 
Given area relation name... Beispiel: Alle Fussgängerstreifen innerhalb der Stadt Zürich:
 
  /*
 
  Crossings/crosswalks
 
  */
 
  [out:json][timeout:25];
 
  area[name="Zürich"][admin_level=8] -> .tmp;
 
  (
 
    node["highway"="crossing"](area.tmp);
 
  );
 
  out body;
 
  >;
 
  out skel qt;
 
 
 
Given area relation (or way) OSM id...
 
 
 
<div style="border:solid 1px grey; padding:1em 1em 1em 1em;">
 
{|
 
| [[Bild:Hinweis.jpg|20px]]
 
|NOTE: Search by area using relation or way id. First lookup OSM id in osm.org. The area id needs to be OSM id + 2400000000 for ways and OSM id + 3600000000 for relations. See  https://wiki.osm.org/wiki/Overpass_API/Overpass_QL#By_area_.28area.29 
 
|}
 
</div>
 
 
 
Beispiel: Alle Brunnen innerhalb der Stadt Zürich (relation id=1682248):
 
  /*
 
  Fountains OR drinking_water within relation 1682248 (= 360 + 1682248)
 
  */
 
  [out:json];
 
  area(id:3601682248);
 
  nwr[amenity~"fountain|drinking_water"](area);
 
  out center;
 
 
 
=== Fuzzy Match ===
 
 
 
Alle Namen beginnend mit "St." innerhalb (within) Zürich (Ausgabeproblem bei overpass-turbo.osm.ch ?):
 
 
 
  [out:json][timeout:60];
 
  area["name"="Zürich"];
 
  (
 
    nwr["name"~"^St\\."](area);
 
  );
 
  out center;
 
 
 
---
 
 
 
Fuzzy-Match mit "~", z.B. Kastanienbäume:
 
  (
 
  node({{bbox}})
 
    ["natural"~"tree"]
 
    ["species"~"hippocastanum"];
 
  node["name"~"[Bb]aum|[Tt]ree"]
 
  );
 
  out;
 
 
 
  {{style:
 
  node[natural=tree]
 
  { color:red; fill-color:red; }
 
  }}
 
 
 
Centroide der Flächen ausgeben mit "out center":
 
  [timeout:25]
 
  ;
 
  (
 
    node["tourism"="zoo"];
 
    way ["tourism"="zoo"];
 
  );
 
  out meta center qt;
 
 
 
=== Verschiedenes ===
 
 
 
;"Rollstuhlparkplätze der Schweiz": siehe [[Nutzen von OpenStreetMap-Daten in GIS]].
 
 
 
;Pizzerias der Schweiz:
 
PostGIS-Terminal (Schweiz):
 
  SELECT ST_AsText(way) geom, name AS label
 
  FROM osm_poia
 
  WHERE tags @> hstore('cuisine', 'pizza')
 
 
 
Overpass Query aller Pizzerias der Schweiz (mit BBox):
 
  <osm-script>
 
    <union>
 
      <query type="node">
 
        <has-kv k="cuisine" v="pizza"/>
 
        <bbox-query e="10.5194091796875" n="47.92738566360356" s="45.45627757127799" w="5.69091796875"/>
 
      </query>
 
      <query type="way">
 
        <has-kv k="cuisine" v="pizza"/>
 
        <bbox-query e="10.5194091796875" n="47.92738566360356" s="45.45627757127799" w="5.69091796875"/>
 
      </query>
 
      <recurse type="way-node"/>
 
    </union>
 
    <print/>
 
  </osm-script>
 
 
 
Overpass Query aller Pizzerias der Schweiz (mit effektiven Grenzen):
 
  <osm-script output="json">
 
    <union>
 
      <!-- define restricted search area switzerland -->
 
      <query type="area">
 
        <has-kv k="boundary" v="administrative"/>
 
        <has-kv k="admin_level" v="2"/> <!-- alternative: admin_level=4 for canton -->
 
        <has-kv k="name:en" v="Switzerland"/>
 
      </query>
 
      <!-- get nodes tagged as Pizzerias -->
 
      <query type="node">
 
        <area-query/> <!-- apply area restriction defined before -->
 
        <has-kv k="cuisine" v="pizza"/>
 
      </query>
 
      <!-- define again restricted search area switzerland -->
 
      <query type="area">
 
        <has-kv k="boundary" v="administrative"/>
 
        <has-kv k="admin_level" v="2"/> <!-- alternative: admin_level=4 for canton -->
 
        <has-kv k="name:en" v="Switzerland"/>
 
      </query>
 
      <!-- get ways/areas tagged as Pizzerias -->
 
      <query type="way">
 
        <area-query/> <!-- apply area restriction -->
 
        <has-kv k="cuisine" v="pizza"/>
 
      </query>
 
      <recurse type="way-node"/>
 
    </union>
 
    <print/>
 
  </osm-script>
 
 
 
POIs:
 
 
<pre>
 
<pre>
  <!--
+
Convert the following natural language query into valid Overpass QL syntax for use on [Overpass Turbo](https://overpass-turbo.eu/):
  This query looks for nodes, ways and relations
 
  with the given key/value combination.
 
  Choose your region and hit the Run button above!
 
  -->
 
  {{key=payment:bitcoin}}
 
  {{value=yes}}
 
  <osm-script output="json">
 
    <union>
 
      <query type="node">
 
        <has-kv k="{{key}}" v="{{value}}"/>
 
        <has-kv k="shop" modv="not" regv="."/>
 
        <has-kv k="amenity" modv="not" regv="."/>
 
        <has-kv k="office" modv="not" regv="."/>
 
        <bbox-query {{bbox}}/>
 
      </query>
 
      <query type="way">
 
        <has-kv k="{{key}}" v="{{value}}"/>
 
        <has-kv k="shop" modv="not" regv="."/>
 
        <has-kv k="amenity" modv="not" regv="."/>
 
        <has-kv k="office" modv="not" regv="."/>
 
        <bbox-query {{bbox}}/>
 
      </query>
 
      <query type="relation">
 
        <has-kv k="{{key}}" v="{{value}}"/>
 
        <has-kv k="shop" modv="not" regv="."/>
 
        <has-kv k="amenity" modv="not" regv="."/>
 
        <has-kv k="office" modv="not" regv="."/>
 
        <bbox-query {{bbox}}/>
 
      </query>
 
    </union>
 
    <print mode="body"/>
 
    <recurse type="down"/>
 
    <print mode="skeleton"/>
 
  </osm-script>
 
</pre>
 
 
 
=== Beispiel Eisenbahnschienen ===
 
 
 
Eisenbahnschienen (inkl. Schmalspur) in der Gegend von Rapperswil.
 
  
Diskussion:
+
**Natural Language Query:**
* Die BBox-Werte für Osten müssen größer sein als für Westen und genauso die Werte für Norden größer als für Süden. Also: s<n und w<e, sortiert als s,w,n,e (Rapperswil: 47.213,8.704,47.336,8.905).
+
<<Insert user query here>>.
* '<nowiki>{{bbox}}</nowiki>' ist eine Variable im Overpass Turbo (funktioniert im Overpass Service natürlich nicht).
 
* 'recurse' löst Referenzen von Ways zu Nodes auf (warum auch immer 'recurse' (und nicht z.B. dereference) da dies ja nur einstufig ist?)
 
* 'union' fasst Ergebnismengen von Anfragen zusammen - auch wenn's im Falle von recurse nur eine ist (...?) (OR-Verknüpfung)
 
* 'print' gibt die Sache aus (warum auch immer print gewählt wurde, da dies ja nichts mit der Konsole zu tun hat... Warum nicht 'format='?)
 
  
XML:
+
**Requirements:**
  <osm-script timeout="900">
+
- The output must adhere to this [EBNF specification](https://gist.githubusercontent.com/sfkeller/43800cd64014d17c55e40fa5e877fd6a/raw/3410bd8817e47c86e5695388a96ba1db5c0630a7/Overpass_QL_EBNF_v0.2_ChatGPT_generated.txt).
    <union>
+
- Use appropriate tags (with key and value) from the [OpenStreetMap Wiki](https://wiki.openstreetmap.org/wiki/DE:How_to_map_a) and from the [JOSM Presets](https://josm.openstreetmap.de/browser/trunk/resources/data/defaultpresets.xml).
      <query type="way">
+
- Prefer geocoding with `geocodeArea` over `area["name"=...]`.
        <has-kv k="railway" v="rail"/>
+
- Prefer `nwr` (nodes, ways, relations).
        <bbox-query <nowiki>{{bbox}}</nowiki>/>
+
- Choose JSON output with center coordinates (`out center`).
      </query>
 
      <recurse type="way-node"/>
 
      <query type="way">
 
        <has-kv k="railway" v="narrow_gauge"/>
 
        <bbox-query <nowiki>{{bbox}}</nowiki>/>
 
      </query>
 
      <recurse type="way-node"/>
 
    </union>
 
    <print/>
 
  </osm-script>
 
 
 
QL:
 
  [timeout:900];
 
  (
 
    way
 
      ["railway"="rail"]
 
      (47.213,8.704,47.336,8.905);
 
    node(w);
 
  );
 
  out;
 
 
 
Kompakte QL:
 
* <nowiki>http://www.overpass-api.de/api/interpreter?data=(way["railway"="rail"](47.213,8.704,47.336,8.905);node(w););out;</nowiki>
 
* http://www.overpass-api.de/api/interpreter?data=(way%5B%22railway%22%3D%22rail%22%5D(47.213%2C8.704%2C47.336%2C8.905)%3Bnode(w)%3B)%3Bout%3B%0A
 
 
 
Alternative: XML mit Regex:
 
 
 
  <osm-script timeout="900">
 
    <union>
 
      <query type="way">
 
        <has-kv k="railway" regv="^(rail|narrow_gauge)$"/>
 
        <bbox-query s="47.213" w="8.704" n="47.336" e="8.905"/>
 
      </query>
 
      <recurse type="way-node"/>
 
    </union>
 
    <print/>
 
  </osm-script>
 
 
 
XML mit Negation und Regex:
 
 
 
  <osm-script timeout="900">
 
    <union>
 
      <query type="way">
 
        <has-kv k="railway" modv="not" regv="rail|narrow_gauge"/>
 
        <bbox-query s="47.213" w="8.704" n="47.336" e="8.905"/>
 
      </query>
 
      <recurse type="way-node"/>
 
    </union>
 
    <print/>
 
  </osm-script>
 
 
 
=== Flaechen ===
 
 
 
Get by OSM id:
 
* ''Step 1'': First, goto www.osm.org and look for the id and/or the exact name the area (= multipolygon).
 
* ''Step 2'': Do this query given a relaton with the given id 2135966. It works also type=node, type=way. Choose your region and hit the Run button above!
 
 
 
  http://overpass-turbo.eu/?template=type-id&type=relation&id=2135966&R
 
 
 
<pre>
 
  {{type=relation}}
 
  {{id=2135966}}
 
  <osm-script output="json">
 
    <id-query type="{{type}}" ref="{{id}}"/>
 
    <print mode="meta"/><!-- fixed by auto repair -->
 
    <recurse type="down"/>
 
    <print mode="meta"/><!-- fixed by auto repair -->
 
  </osm-script>
 
 
</pre>
 
</pre>
  
''Step 3'': Given the GeoJSON output (see output="json"), convert it e.g. with an [[Online Geoconverter]] like OSM2KML.
+
* Beispiele für erfolgreiche "user queries":
 +
** All restaurants including fast_food in Rapperswil-Jona
 +
** All bars around 3000 meters from the adress "Bahnhof Rapperswil-Jona"
 +
** All restaurants within 3000 meters of a public stop in of Switzerland
 +
** All restaurants in Kanton Glarus within 200 meters of public transport stops (station, bus_stop, tram_stop, funicular)
  
Get boundary by OSM name:  
+
* Beispiele für nicht erfolgreiche "user queries": Mountain passes of Switzerland which are navigable with a lake nearby" - siehe [[Overpass QL Beispiele]].
* Use this Overpass API command to get boundaries given a name:
 
  http://overpass-api.de/api/interpreter?data=area[name="Nordrhein-Westfalen"];rel(area)[admin_level=6];(._;>;);out;
 
  
=== öV-Routen inkl. aller Koordinaten ===
+
Einstieg in Overpass QL und API:
Mit folgender QL können alle öV-Routen (welche als Relation vorhanden sind) im Raum Bülach/Winterthur geladen werden:
+
* >> '''"Offizielles" Handbuch zur Overpass API: https://dev.overpass-api.de/overpass-doc/de/ <<'''
<pre>
+
* "Learn Overpass": http://osmlab.github.io/learnoverpass//en/
[out:json][bbox:47.46407,8.44228,47.56407,8.64228];
+
* Getting Started with Overpass Turbo (2020): https://nixintel.info/osint-tools/getting-started-with-overpass-turbo-part-1/
(relation[network=ZVV];);
+
* Tutorial: https://osm-queries.ldodds.com/tutorial/index.html
(._;>;);
+
* Blog: https://dev.overpass-api.de/blog/
out;
 
</pre>
 
  
Siehe auch: http://overpass-turbo.eu/s/5Ie
+
Overpass QL Language Documentation:  
 +
* http://wiki.osm.org/wiki/Overpass_API
 +
* https://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_QL
 +
* https://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide
 +
* [[Overpass API EBNF]] (experimental)
  
==== Schwierigkeiten und Stolpersteine ====
+
Support:
 +
* Mailing list: http://listes.openstreetmap.fr/wws/info/overpass
 +
* https://help.openstreetmap.org/questions/20479/using-osm-data-with-javascript-via-api-in-my-code-display-data-as-text
  
Eine Schwierigkeit war die Weiterverarbeitung der Daten, da die Relation auf mehreren Ways und diese wiederum auf Nodes aufbauen. Das obige Query liefert nur die Koordinaten der Nodes zurück. Eine weitere Variante ist die Ausgabe mit '''out geom;''' (siehe auch http://overpass-turbo.eu/s/5If) welche auch die Geometrien für die Ways ausgibt. Es wird jedoch immer noch ein Post-Processing benötigt, da die Relationen zwar mit jedem Way die Geometrien besitzt, jedoch sind diese weiterhin verschachtelt und müssen geparst werden. ''Gibt es eine Möglichkeit die Geometrien einer Relation ready-to-use mit dem Overpass-API abzufragen?''
+
Siehe auch:
 
+
* [[OSM-Technisches]]
=== Querying data with history
+
* [[HowTo OpenStreetMap]]
 
+
* [https://stackoverflow.com/questions/tagged/overpass-api Stackoverflow 'overpass-api']
Query example with history, (plus history data 'attic'), e.g. using https://overpass-turbo.eu/ :
 
 
 
  [adiff:"2018-09-01T00:00:00Z"];
 
  (
 
  node({{bbox}})(changed);
 
  way({{bbox}})(changed);
 
  );
 
  out;
 
  //out meta geom({{bbox}});
 
  //out count;
 
 
 
 
 
=== Download und Aufbereitung von Nodes mit PostgreSQL/PostGIS  ===
 
Für dieses Beispiel wird folgender QL verwendet:
 
<pre>
 
[out:json][bbox:47.46407,8.44228,47.56407,8.64228];
 
(node[highway=bus_stop];);
 
(._;>;);
 
out;
 
</pre>
 
 
 
Folgendes Skript lädt mit COPY FROM PROGRAM die Daten (im JSON Format) in eine temporäre Tabelle, anschliessend wird das JSON geparst und in eine Tabelle abgefüllt.
 
 
 
<pre>
 
BEGIN TRANSACTION;
 
 
 
CREATE TEMP TABLE network (
 
  data json
 
)
 
ON COMMIT DROP;
 
 
 
COPY network
 
FROM PROGRAM 'json=$(wget -q -O - "$@" "http://overpass-api.de/api/interpreter?data=[out:json][bbox:47.46407,8.44228,47.56407,8.64228]; (node[highway=bus_stop];);(._;>;);out body;");echo $json | sed "s/\n//g";';
 
 
 
CREATE TABLE station (
 
  id BIGINT PRIMARY KEY,
 
  name TEXT,
 
  geom GEOMETRY
 
);
 
 
 
WITH elements AS (
 
    SELECT json_array_elements((data -> 'elements') :: JSON) AS s
 
    FROM network
 
)
 
INSERT INTO station
 
  SELECT
 
    (s -> 'id')::TEXT::BIGINT,
 
    (s -> 'tags' ->> 'uic_name')::TEXT,
 
    ST_SetSRID(ST_MakePoint((s -> 'lon')::TEXT::DECIMAL,
 
    (s -> 'lat')::TEXT::DECIMAL), 4326)
 
  FROM elements
 
  WHERE (s ->> 'type')::TEXT LIKE 'node';
 
 
 
COMMIT;
 
</pre>
 
  
 
[[Kategorie:Howto]]  
 
[[Kategorie:Howto]]  
 
[[Kategorie:OpenStreetMap]]
 
[[Kategorie:OpenStreetMap]]
 +
[[Kategorie:Overpass]]

Aktuelle Version vom 16. September 2024, 09:56 Uhr

Overpass API - Gefilterter Download von OpenStreetMap-Daten in der nativen Struktur Nodes, Ways, Relations.

Overpass Turbo Services (interaktive Webapp):

Overpass QL Query Beispiele:

Prompt für >> ChatGPT & Co. << (Copy & Paste und ersetze "user query"):

Convert the following natural language query into valid Overpass QL syntax for use on [Overpass Turbo](https://overpass-turbo.eu/):

**Natural Language Query:**
<<Insert user query here>>.

**Requirements:**
- The output must adhere to this [EBNF specification](https://gist.githubusercontent.com/sfkeller/43800cd64014d17c55e40fa5e877fd6a/raw/3410bd8817e47c86e5695388a96ba1db5c0630a7/Overpass_QL_EBNF_v0.2_ChatGPT_generated.txt).
- Use appropriate tags (with key and value) from the [OpenStreetMap Wiki](https://wiki.openstreetmap.org/wiki/DE:How_to_map_a) and from the [JOSM Presets](https://josm.openstreetmap.de/browser/trunk/resources/data/defaultpresets.xml).
- Prefer geocoding with `geocodeArea` over `area["name"=...]`.
- Prefer `nwr` (nodes, ways, relations).
- Choose JSON output with center coordinates (`out center`).
  • Beispiele für erfolgreiche "user queries":
    • All restaurants including fast_food in Rapperswil-Jona
    • All bars around 3000 meters from the adress "Bahnhof Rapperswil-Jona"
    • All restaurants within 3000 meters of a public stop in of Switzerland
    • All restaurants in Kanton Glarus within 200 meters of public transport stops (station, bus_stop, tram_stop, funicular)
  • Beispiele für nicht erfolgreiche "user queries": Mountain passes of Switzerland which are navigable with a lake nearby" - siehe Overpass QL Beispiele.

Einstieg in Overpass QL und API:

Overpass QL Language Documentation:

Support:

Siehe auch: