I have a collection of longitudes and latitudes, and I want to be able to extract the district of each of these coordinates using Python.
As of right now, I have developed the following function using the geopy
library,
from geopy.geocoders import Nominatim
from geopy.point import Point
MAX_RETRIES = 5
def get_district(lat, longi):
geolocator = Nominatim(user_agent="http")
point = Point(lat, longi)
retries = 0
while retries < MAX_RETRIES:
retries += 1
try:
location = geolocator.reverse(point)
district = location.raw['address']['state_district']
return district
except:
print('Request failed.')
print('Retrying..')
time.sleep(2)
print('Max retries exceeded.')
return None
This works fine for a single point, but I have a number of them (approximately 10,000) and this only works for one coordinate at a time. There is no option to make bulk requests for several points.
Furthermore, this API becomes quite unreliable when making multiple such requests.
Is there a better way to achieve this using Python? I am open to any approach. Even if there is a file of sorts that I can find with a mapping of the coordinates against the districts, it works for me.
Note: At the moment, I am looking at coordinates in Sri Lanka.