Search code examples
pythonpython-3.xfolium

Can we use folium maps to get some kind of site type or site description?


I am playing around with some code to loop through records with longitude and latitude coordinates, and get some kind of site type, or site classification, or whatever you want to call it. This sample code below won't work, but I think it's kind of close.

import folium
import requests
from xml.etree import ElementTree
from folium import plugins


m = folium.Map(location=[40.7368436,-74.1524242], zoom_start=10)
for lat,lon in zip(df_cellgroups['latitude'], df_cellgroups['latitude']):
   marker = folium.Marker(location=[lat,lon], tooltip = tip, popup = name)
   marker.add_to(m)
m

Essentially I want to grab names like 'Red Bull Arena', 'Upstate University Hospital', 'San Francisco International Airport', etc., etc., etc. So, is it possible to get descriptions of sites, based on lat and lon coordinates, using folium maps? Maybe it's known as a tooltip or popup, not sure.

enter image description here

enter image description here

enter image description here


Solution

  • You can retrieve information about a location by using a reversed geocoding service/provider such as offered by OpenStreetMap, Google or Esri.

    (There is an overview here of all providers supported by the Python's geocoder package.)


    Below an example using the geocoder package and OpenStreetMap (Nominatim) as a provider:

    # pip install geocoder requests
    
    import time
    import requests
    import geocoder
    
    locations = (
        (40.7368436, -74.1524242),
        (44.6371650, -63.5917312),
        (47.2233913, 8.817269106),
    )
    
    with requests.Session() as session:
    
        for location in locations:
    
            g = geocoder.osm(
                location=location,
                method="reverse",
                lang_code="en",
                session=session,
                headers={
                    "User-Agent": "Stackoverflow Question 69578280"
                },
            )
    
            print(g.osm)  # or print(g.json)
    
            # slow down loop in order to comply with the Nominatim's Usage Policy:
            # https://operations.osmfoundation.org/policies/nominatim
            time.sleep(1)
    

    Alternatively, there are other Python libraries such as the ArcGIS for Python API or GeoPy. Here is an example using the geopy package using also OpenStreetMap (Nominatim) as a provider:

    # pip install geopy
    
    from geopy.geocoders import Nominatim
    from geopy.extra.rate_limiter import RateLimiter
    
    locations = (
        (40.7368436, -74.1524242),
        (44.6371650, -63.5917312),
        (47.2233913, 8.817269106),
    )
    
    locator = Nominatim(user_agent="Stackoverflow Question 69578280")
    
    # using RateLimiter to comply with Nominatim's Usage Policy
    reverse = RateLimiter(locator.reverse, min_delay_seconds=1)
    
    for location in locations:
        result = reverse(location, language="en")
        print(result.raw)
    

    Note: Make sure that you read the term of use of the service you are going to use. Nominatim's Usage Policy can be found here: https://operations.osmfoundation.org/policies/nominatim