Examples¶
Basic example¶
Lets start with an example from the Overpass API documentation.
Query String:
1node(50.745,7.17,50.75,7.18);
2out;
Use OverPy:
1>>> import overpy
2>>> api = overpy.Overpass()
3>>> result = api.query("node(50.745,7.17,50.75,7.18);out;")
4>>> len(result.nodes)
51984
6>>> len(result.ways)
70
8>>> len(result.relations)
90
10>>> node = result.nodes[2]
11>>> node.id
12100792806
13>>> node.tags
14{}
- Line 1:
Import the required Python module
- Line 2:
Create a new instance of the Overpass() class. This instance is used to query the Overpass API.
- Line 3:
Use the Query-String from above to query the Overpass API service.
- Line 4,5:
Get the number of nodes in the result set.
- Line 6-9:
Get the number of ways and relations available in the result set.
- Line 10-14:
Get the third node from the list. Display the ID and the tags of this node.
Use Overpass QL or Overpass XML¶
Queries are passed directly to the Overpass API service without any modification. So it is possible to use Overpass QL and Overpass XML.
Overpass QL¶
Query:
1node["name"="Gielgen"];
2out body;
Use OverPy:
1>>> import overpy
2>>> api = overpy.Overpass()
3>>> result = api.query("""node["name"="Gielgen"];out body;""")
4>>> len(result.nodes)
56
6>>> len(result.ways)
70
8>>> len(result.relations)
90
Overpass XML¶
Query:
1<osm-script>
2 <query type="node">
3 <has-kv k="name" v="Gielgen"/>
4 </query>
5 <print/>
6</osm-script>
Use OverPy:
1>>> import overpy
2>>> api = overpy.Overpass()
3>>> result = api.query("""<osm-script>
4... <query type="node">
5... <has-kv k="name" v="Gielgen"/>
6... </query>
7... <print/>
8... </osm-script>""")
9>>> len(result.nodes)
106
11>>> len(result.ways)
120
13>>> len(result.relations)
140
Parse JSON or XML responses¶
On a request OverPy detects the content type from the response.
JSON response¶
Query String:
1[out:json];
2node(50.745,7.17,50.75,7.18);
3out;
Use OverPy:
1>>> import overpy
2>>> api = overpy.Overpass()
3>>> result = api.query("[out:json];node(50.745,7.17,50.75,7.18);out;")
4>>> len(result.nodes)
51984
6>>> len(result.ways)
70
8>>> len(result.relations)
90
XML response¶
Query String:
1[out:xml];
2node(50.745,7.17,50.75,7.18);
3out;
Use OverPy:
1>>> import overpy
2>>> api = overpy.Overpass()
3>>> result = api.query("[out:xml];node(50.745,7.17,50.75,7.18);out;")
4>>> len(result.nodes)
51984
6>>> len(result.ways)
70
8>>> len(result.relations)
90
Ways¶
Get all nodes of a way¶
In this example the Overpass API will only return the Way elements with the name “Gielgenstraße”. But there will be no Node elements in the result set.
OverPy provides a way to resolve missing nodes.
Query String:
1way
2["name"="Gielgenstraße"]
3(50.7,7.1,50.8,7.25);
4out;
Use OverPy:
1>>> import overpy
2>>> api = overpy.Overpass()
3>>> result = api.query("""way["name"="Gielgenstraße"](50.7,7.1,50.8,7.25);out;""")
4>>> len(result.nodes)
50
6>>> len(result.ways)
74
8>>> way = result.ways[0]
9>>> way.nodes
10Traceback (most recent call last):
11 File "<stdin>", line 1, in <module>
12 [...]
13 raise exception.DataIncomplete("Resolve missing nodes is disabled")
14overpy.exception.DataIncomplete: ('Data incomplete try to improve the query to resolve the missing data', 'Resolve missing nodes is disabled')
15>>> way.get_nodes()
16Traceback (most recent call last):
17 File "<stdin>", line 1, in <module>
18 [...]
19 raise exception.DataIncomplete("Resolve missing nodes is disabled")
20overpy.exception.DataIncomplete: ('Data incomplete try to improve the query to resolve the missing data', 'Resolve missing nodes is disabled')
21>>> nodes = way.get_nodes(resolve_missing=True)
22>>> len(nodes)
2313
24>>> len(result.nodes)
2513
26>>> len(way.nodes)
2713
- Line 1-3:
Send a query to the Overpass API service.
- Line 4-6:
There are 4 Way elements and 0 Node elements in the result set.
- Line 7:
Get the first way.
- Line 8-19:
Use
overpy.Way.nodes
class attribute and theoverpy.Way.get_nodes()
function to get the nodes for the way. Both raise an exception because the nodes are not in the result set and auto resolving missing nodes is disabled.- Line 20-21:
Use the
overpy.Way.get_nodes()
function and let OverPy try to resolve the missing nodes. The function will return all Node elements connected with the Way element.- Line 22-25:
The resolved nodes have been added to the result set and are available to be used again later.