Search code examples
pythontypescriptopenstreetmapoverpass-api

overpass "around" to find addresses in certain radius from a coordinate


I would like to find the amount of registered addresses within a certain radius (lets say radius=30 (meters) to a coordinate I input (lat,lon).
The code I'm looking for can be in either Python or Typescript.
I do not care if these are residential or business addresses.

I read that the 'around' function in the overpass API should be able to do this. Also found this and this but did not really understand how to do it.

If there is a similar working solution using a different API (e.g. OSM) this would also work for me.

could you help please?


Solution

  • Yes I should have provided some code. Here is the solution in python:

    def search_nearby_features(latitude, longitude, radius):
        overpass_url = "https://overpass-api.de/api/interpreter"
        query = f"""
        [out:json];
        (
          node(around:{radius},{latitude},{longitude});
          way(around:{radius},{latitude},{longitude});
          relation(around:{radius},{latitude},{longitude});
        );
        out center;
        """
        params = {
            "data": query
        }
        response = requests.get(overpass_url, params=params)
        if response.status_code == 200:
            data = response.json()
            return data["elements"]
        else:
            print("Error occurred:", response.text)
            return []
        
    def get_node_points(nearby_features):
        nodes = []
        for point in nearby_features:
            if point['type'] == 'node':
                nodes.append(point)
        return nodes
    

    just have to pick a lat, long and r values and then run:

    nearby_features = search_nearby_features(latitude, longitude, radius)
    nodes = get_node_points(nearby_features)
    print(f"number of points\locations in a radius of {radius} meters from the given coordinates: {len(nodes)}")
    

    note that I look specifically for type=='node' because these are address locations as far as I understood. I dont care about roads etc.