2

I am trying to count the number of buildings within a series of polygons using the Overpass API in Python.

I have created a list of strings which contain the co-ordinates of the polygons and I iterate through them passing the string to a separate function to perform the count. In the called function, I am trying to create the query using the polygon string but I am not getting anything meaningful back. Eventually, I would like to get the co-ordinates of each building and the total number of buildings in the polygon but if I could even get the count that would be great.

An example of one of the polygon strings is

poly:"-6.199206 53.615774 -6.200352 53.615451 -6.200937 53.615362 -6.201668 53.616131 -6.201761 53.616235 -6.199634 53.616626 -6.199206 53.615774"

My code is as follows:

def count_buildings(poly):
    api = overpy.Overpass()
    query_string = 'node(' + poly +')["building"];(._;>;); out;'
    # print(query_string)
    result = api.query(query_string)
    print(result)
    return len(result.nodes)

1 Answer 1

1

Your example of polygon string is in the middle of the Indian Ocean, so there might not be that many buildings there...

enter image description here

Overpass documentation on polygons:

(poly:"latitude_1 longitude_1 latitude_2 longitude_2 latitude_3 longitude_3 …");

If you invert the latitudes and longitudes, you get to a suburb of Dublin, which has more buildings, but the buildings are defined by ways and not nodes, so you will also get zero matches.

on overpass turbo:

way(poly:"53.615774 -6.199206  53.615451 -6.200352  53.615362 -6.200937  53.616131 -6.201668  53.616235 -6.201761  53.616626 -6.199634  53.615774 -6.199206")["building"];

(._;>;);

out ;

enter image description here

and if you count:

<count id="0">
    <tag k="nodes" v="198"/>
    <tag k="ways" v="73"/>
    <tag k="relations" v="0"/>
    <tag k="total" v="271"/>
  </count>

Note that using (._;>;); will give you the union of your initial results (the ways) and the recurse down (the nodes of the ways). So you'll end up with 271. But you only want to count the ways, of which there are 73 in this example.

2
  • Thanks so much. I'm quite new to this and am getting the coordinates from a Irish census file in GeoJSON, I did not realise that the latitude and longitude were reversed in the Census multipolygon. If it's okay could I ask a related question? I wrote my own manual conversion from GeoJSON polygon to Overpass poly, is there a standard way to do this conversion? Thanks again. Jan 19 at 9:55
  • @DavidGraham the GeoJSON standard is to write Longitude, Latitude (like x,y in a graph), while the "common" usage is to use latitude first. I'm not aware of a standard ways of going from GeoJSON to Overpass poly, but if you've already writen a tool, then that should be fine.
    – XaC
    Jan 20 at 12:25

Your Answer

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

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