1

I wonder could someone advise me on how to get node information out of a Overpass query (using the Python Overpy library) result in json format.

I started with a query looking simply for the number of buildings within a polygon and this worked fine with the following code:

''' import overpy

buildings_in_polygon = api.get(overpass_query_element) '''

In this case overpass_query_element was as follows:

way(poly:"53.360596 -6.305466 53.360596 -6.289673 53.359222 -6.285553 53.356476 -6.282806 53.353729 -6.274567 53.350983 -6.274567 53.339996 -6.285553 53.338623 -6.289673 53.338623 -6.295166 53.339996 -6.299286 53.342743 -6.302032 53.34549 -6.310272 53.349609 -6.311646 53.355103 -6.311646 53.359222 -6.310959 53.360596 -6.308899 53.360596 -6.305466")["building"~"residential|house|terrace|detached|apartments"]; out;

But now, I want to get details on the public parks (e.g. lat and lng and tags) in the same polygon, preferably in json format. Looking at Overpass turbo for some clues, I changed the query to be the following:

[out:json]; way(poly:"53.360596 -6.305466 53.360596 -6.289673 53.359222 -6.285553 53.356476 -6.282806 53.353729 -6.274567 53.350983 -6.274567 53.339996 -6.285553 53.338623 -6.289673 53.338623 -6.295166 53.339996 -6.299286 53.342743 -6.302032 53.34549 -6.310272 53.349609 -6.311646 53.355103 -6.311646 53.359222 -6.310959 53.360596 -6.308899 53.360596 -6.305466")["leisure"="park"]; out body;>;

I get a list of 18 ways with their constituent nodes but can someone tell me, how do I access the data for the nodes and how to put it in json, in the way the data for a postbox node is returned from overpass turbo as below:

{
  "type": "node",
  "id": 331566535,
  "lat": 51.6555684,
  "lon": 7.8635666,
  "tags": {
    "amenity": "post_box",
    "collection_times": "Mo-Fr 16:45, Sa 10:30",
    "operator": "Deutsche Post",
    "ref": "Dambergstraße 49, 59069 Hamm"
  }
},

Is the issue that the Overpy library can only do counts and cannot return json?

1 Answer 1

4

If you have a look at the overpass documentation, the out body on a set of ways just gives the IDs and tags of the ways, and the IDs of the nodes in the ways. But no geographical indication.

To get the lat and lng of the tags, you either have to ask for : out geom or get the nodes from your set of ways by adding the union of the set of ways and the recursion down to the nodes (>): (._;>;);. But this must be * before the out statement.

So you should ask for :

[out:json];
 way(poly:"53.360596 -6.305466 53.360596 -6.289673 53.359222 -6.285553 53.356476 -6.282806 53.353729 -6.274567 53.350983 -6.274567 53.339996 -6.285553 53.338623 -6.289673 53.338623 -6.295166 53.339996 -6.299286 53.342743 -6.302032 53.34549 -6.310272 53.349609 -6.311646 53.355103 -6.311646 53.359222 -6.310959 53.360596 -6.308899 53.360596 -6.305466")["leisure"="park"];
out geom;

or

[out:json];
 way(poly:"53.360596 -6.305466 53.360596 -6.289673 53.359222 -6.285553 53.356476 -6.282806 53.353729 -6.274567 53.350983 -6.274567 53.339996 -6.285553 53.338623 -6.289673 53.338623 -6.295166 53.339996 -6.299286 53.342743 -6.302032 53.34549 -6.310272 53.349609 -6.311646 53.355103 -6.311646 53.359222 -6.310959 53.360596 -6.308899 53.360596 -6.305466")["leisure"="park"];
 (._;>;);
out body;

The difference between using out geom on a set a ways is that the json output will be a list of ways, which each contains a list of nodes and a list of lat/lon of thoses nodes. Like this:

 "elements": [

{
  "type": "way",
  "id": xxx,
  "nodes": [
 //liste of nodes ID
  ],
  "geometry": [
    { "lat": 50.7410694, "lon": 7.2009669 },
  //  list of the lat lon of the nodes
... tags of the way and other data

But if you do a union of ways and their nodes, you'll get a list of nodes with their tags and lat/lon, and a list of ways with the IDs of the nodes.

So it depends on how your want to process your data.

Link to the overpy json parsing

1
  • This is great thank you. Feb 3, 2023 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.