There are times that you noway but getting data from a third part library and that library has rate limit on their endpoints. For example I have recently used
This library by default sends its requests to
Now read from cache in case it exists:
Make sure to put
#python #geopy #geo #latitude #longitude #Nominatim #redis #hset #geocoders
geopy
python library to get latitude and longitude by giving city name to the function:from geopy.geocoders import Nominatim
city_name = 'Tehran'
geolocator = Nominatim()
location = geolocator.geocode(city_name)
print location.latitude, location.longitude
This library by default sends its requests to
https://nominatim.openstreetmap.org/search
to get geo location data. It's rate limit is 1 request per second. To circumvent these problems and limitations use redis to cache results in your server and read cached result from your own system:self.redis.hset(city_name, 'lat', lat)
self.redis.hset(city_name, 'long', longitude)
Now read from cache in case it exists:
if self.redis.hexists(city_name, 'lat'):
location = self.redis.hgetall(city_name)
Make sure to put
sleep(1)
when reading from Nominatim
in order to by pass its limitation.NOTE:
instead of Nominatim
other 3rd parties can be used.#python #geopy #geo #latitude #longitude #Nominatim #redis #hset #geocoders