Search for rental properties with Streamlit — pt 3
Add geolocation to addresses
Hi everyone, I’m back with part 3 of this web application using Streamlit.
In part 1, I wrote about the project structure and the critical point of this project, click here to go to the article.
In part 2, I wrote about the ET part of the pipeline, the extraction, and the transformation, click here to go to the article.
And in this part, I will write about, how to add the latitude and longitude in the address and ingest this data into MongoDB.
The link to the streamlit app is below, I hope you enjoy it.
Add Latitude and longitude
For this task, I use the geopy library.
geopy is a Python client for several popular geocoding web services.
geopy makes it easy for Python developers to locate the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders and other data sources.
It’s effortless to use.
About geocoders
Each geolocation service you might use, such as Google Maps, Bing Maps, or Nominatim, has its own class in geopy.geocoders
abstracting the service’s API. Geocoders each define at least a geocode
method, for resolving a location from a string, and may define a reverse
method, which resolves a pair of coordinates to an address. Each Geocoder accepts any credentials or settings needed to interact with its service, e.g., an API key or locale, during its initialization.
An example using geocoders:
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="specify_your_app_name_here")
location = geolocator.geocode("175 5th Avenue NYC")
print(location.address)
Flatiron Building, 175, 5th Avenue, Flatiron, New York, NYC, New York, ...
print((location.latitude, location.longitude))
(40.7410861, -73.9896297241625)
print(location.raw)
{'place_id': '9167009604', 'type': 'attraction', ...}
There were some limitations, and we couldn’t find all the addresses from our database, but it helped a lot.
Back to our project, to add the latitude and longitude to the address, I wrote this code:
geocode = Nominatim(user_agent="myGeocoder")
df['location'] = df['Endereco'].progress_apply(geocode)
Applying the geocode for each address (endereco), it is possible to find the complete location.
The result is:
[Location(Rua Jasmim, Mansões Santo Antônio, Campinas,
Região Imediata de Campinas, Região Metropolitana de Campinas,
Região Geográfica Intermediária de Campinas, São Paulo, Região Sudeste,
13087–460, Brasil, (-22.8548518, -47.0513215, 0.0))]
With this result, we can add the latitude and the longitude.
df['point'] = df['location'].progress_apply(lambda loc: tuple(loc.point)
if loc else None)
#output
(-22.8548518, -47.0513215, 0.0)
So, now we can turn this ‘point’ column into 3 columns, latitude, longitude, and altitude.
df[['latitude', 'longitude', 'altitude']] = pd.DataFrame(df['point'].tolist(),
index=df.index)
The complete code for adding latitude and longitude is this:
And now we can add the data to MongoDB.
But that's all for today.
In the next article, I will share how to connect python with Mongo and how to add our data to it.
If this series of articles is making sense and helping you, share it with your friends.
See you next week.