Ibrahim Mohamed
Ibrahim Mohamed

Reputation: 45

Need to reverse latitude & longitude and get types of them with python

I have data in excel contain latitude and longitude. I need to Know what the types of lat and lon in column type. I used GeoPy to reverse lat and lon but I got just address. and need to get types also. for example

import pandas as pd 
from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter
df = pd.read_excel("MyExcel_file.xlsx")
df["Geo"] = df["lat"].astype(str)+ ',' + df["lon"].astype(str)
geolocator = Nominatim(user_agent="MyApp", timeout=10)
rgeocode = RateLimiter(geolocator.reverse, min_delay_seconds=0.001)

and apply my code in address column and its working fine but i can not find the types

df['address'] = df['Geo'].apply(rgeocode)
latitude longitude address types
49.789 6.06569

i need the output types for example hospital or house, Building, pharmacy etc..

latitude longitude address types
49.789 6.06569 Stralauer Allee, Fhain, Friedrichshain-Kreuzberg hospital

i hope every thing its clear. any help will be great. best regards

Upvotes: 0

Views: 326

Answers (1)

RJ Adriaansen
RJ Adriaansen

Reputation: 9619

You can access the type in the full json response that can be accessed with the key raw. To wrap it in a function that populates both columns:

import pandas as pd
df = pd.DataFrame([{'latitude': 49.789, 'longitude':6.06569}])
df["Geo"] = df["latitude"].astype(str)+ ',' + df["longitude"].astype(str)
geolocator = Nominatim(user_agent="MyApp", timeout=10)
rgeocode = RateLimiter(geolocator.reverse, min_delay_seconds=0.001)

def get_data(coordinates):
    location = rgeocode(coordinates)
    return pd.Series([location.address, location.raw['osm_type']])

df[["address", "types"]] = df['Geo'].apply(get_data)

Result:

latitude longitude Geo address types
0 49.789 6.06569 49.789,6.06569 Atelier communal, 9, Rue du Fossé, Bissen, Canton Mersch, 7772, Lëtzebuerg way

Upvotes: 1

Related Questions