'Overpass API: query for counting amenity of specified type around set of lat lons

I'm trying to query data from the OSM Overpass API. Specifically I'm trying to determine the count of amenities of a given type around a point (using the 'around' syntax). When running this for many locations (lat, lons) I'm running into a TooManyRequests error.

I have tried to work around by setting sleep time pauses and playing with the timeout header and retry time, but I'm running into the same issue. I'm trying to find a way to adapt the query so that it just returns the count of amenities (of specified type) around each point, rather than the full json of nodes which is more data intensive. My current script is as follows;

# Running Overpass query for each point

results = {}

for n in range(0, 200):
    name = df.loc[n]['city']
    state = df.loc[n]['state_name']
    rad = df.loc[n]['radius_m']
    lat = df.loc[n]['lat']
    lon = df.loc[n]['lng']

    # Overpass query for amenities
    start_time = time.time()
    api = overpy.Overpass(max_retry_count=None, retry_timeout=2)
    r = api.query(f"""
    [out:json][timeout:180];
    (node["amenity"="charging_station"](around:{rad}, {lat}, {lon});
    );
    out;
    """)

    print("query time for "+str(name)+", number "+str(n)+" = "+str(time.time() - start_time))

    results[name] = len(r.nodes)

    time.sleep(2)

Any help is much appreciated from other Overpass users!

Thanks



Solution 1:[1]

In general, you can run out count; to return a count from an overpass API query.

It's hard to say without knowing how your data is specifically structured, but you might have better luck using area to look at specific cities, or regions.

Here is an example that returns the count of all nodes tagged as charging station in Portland, Oregon:

/* charging stations in portland */
area[name="Oregon"]->.state;
area[name="Portland"]->.city;
(
  node["amenity"="charging_station"](area.state)(area.city);
);
out count;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adrian Mole