I have a location that I want to find the coordinates of. But my function gmaps_geoencoder is interpreting the address wrongly as belonging to a different country. So, my logic is, keep removing words one by one from the address until the latitudes and longitudes returned are in the correct range(i.e., until it interprets the address correctly).
To illustrate the issue, here's the code:
place = "*LORENZO GOMEZ & CO CPA'S RM 202 DON RAFAEL CASTILLO BLDG LEGASPI ST DAVAO CITY DAVAO DEL SUR PHILIPPINES"
gmaps_geoencoder(place)
>>> "{'results': [{'address_components': [{'long_name': 'San Diego', `'short_name': 'San Diego', 'types': ['locality', 'political']}, {'long_name': 'California', 'short_name': 'CA', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'United States', 'short_name': 'US', 'types': ['country', 'political']}, {'long_name': '92123', 'short_name': '92123', 'types': ['postal_code']}], 'place_id': 'ChIJAy3bGzT-24ARmX-IpDjdadk', 'types': ['doctor', 'establishment', 'health', 'point_of_interest'], 'formatted_address': '4715 Viewridge Avenue Suite 230, San Diego, CA 92123, San Diego, CA 92123, United States', 'geometry': {'location': {'lat': 32.8238366, 'lng': -117.119949}, 'location_type': 'GEOMETRIC_CENTER', 'viewport': {'southwest': {'lat': 32.8224876197085, 'lng': -117.1212979802915}, 'northeast': {'lat': 32.8251855802915, 'lng': -117.1186000197085}}}}], 'status': 'OK'}"`
As we can see, its interpreting the address as belonging to the United States, when its actually in the Philippines.
When I remove the first few words from the address though, it interprets it correctly:
place = "RM 202 DON RAFAEL CASTILLO BLDG LEGASPI ST DAVAO CITY DAVAO DEL SUR PHILIPPINES"
gmaps_geoencoder(place)
>>> "{'results': [{'address_components': [{'long_name': 'Rafael Castillo', 'short_name': 'Rafael Castillo', 'types': ['administrative_area_level_5', 'political']}, {'long_name': 'Agdao', 'short_name': 'Agdao', 'types': ['political', 'sublocality', 'sublocality_level_1']}, {'long_name': 'Davao City', 'short_name': 'Davao City', 'types': ['locality', 'political']}, {'long_name': 'Davao del Sur', 'short_name': 'Davao del Sur', 'types': ['administrative_area_level_2', 'political']}, {'long_name': 'Davao Region', 'short_name': 'Davao Region', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'Philippines', 'short_name': 'PH', 'types': ['country', 'political']}], 'partial_match': True, 'types': ['administrative_area_level_5', 'political'], 'place_id': 'ChIJi4qcxTBs-TIRfnWnF4MGzDE', 'formatted_address': 'Rafael Castillo, Agdao, Davao City, Davao del Sur, Philippines', 'geometry': {'location': {'lat': 7.100270999999999, 'lng': 125.6382528}, 'bounds': {'southwest': {'lat': 7.095814, 'lng': 125.6311701}, 'northeast': {'lat': 7.1040211, 'lng': 125.644075}}, 'location_type': 'APPROXIMATE', 'viewport': {'southwest': {'lat': 7.095814, 'lng': 125.6311701}, 'northeast': {'lat': 7.1040211, 'lng': 125.644075}}}}, {'address_components': [{'long_name': 'Pelayo Street', 'short_name': 'Pelayo St', 'types': ['route']}, {'long_name': 'Poblacion District', 'short_name': 'Poblacion District', 'types': ['political', 'sublocality', 'sublocality_level_1']}, {'long_name': 'Davao City', 'short_name': 'Davao City', 'types': ['locality', 'political']}, {'long_name': 'Davao del Sur', 'short_name': 'Davao del Sur', 'types': ['administrative_area_level_2', 'political']}, {'long_name': 'Davao Region', 'short_name': 'Davao Region', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'Philippines', 'short_name': 'PH', 'types': ['country', 'political']}], 'partial_match': True, 'types': ['route'], 'place_id': 'ChIJJaE5cndt-TIRqYbFlmdcZP4', 'formatted_address': 'Pelayo St, Poblacion District, Davao City, Davao del Sur, Philippines', 'geometry': {'location': {'lat': 7.0685545, 'lng': 125.6068616}, 'bounds': {'southwest': {'lat': 7.0662803, 'lng': 125.6049452}, 'northeast': {'lat': 7.0718272, 'lng': 125.6078379}}, 'location_type': 'GEOMETRIC_CENTER', 'viewport': {'southwest': {'lat': 7.0662803, 'lng': 125.6049452}, 'northeast': {'lat': 7.0718272, 'lng': 125.6078379}}}}], 'status': 'OK'}")
So, to automate this process (of how many words to remove) for other such misidentified addresses, my code is the following:
place = "*LORENZO GOMEZ & CO CPA'S RM 202 DON RAFAEL CASTILLO BLDG LEGASPI ST DAVAO CITY DAVAO DEL SUR PHILIPPINES"
lat = 0
lon = 0
while ((lat not in range(4,21)) or (lon not in range(116,128))): # Philippines' latitude and longitude range
place = place.split(' ', 1)[1]
try:
lat, lon, res = gmaps_geoencoder(place)
except: # This is because sometimes the gmaps API does not return any result for a given address. In that case, remove the word at the beginning and search again.
place = place.split(' ', 1)[1]
lat, lon, res = gmaps_geoencoder(place)
But this keeps stripping words until the last word is stripped and returns a list index out of range error. Why does the while condition not stop when lat and lon are in the desired range as indicated to it in the code?
EDIT: Modifying the while condition to always keep running, and adding an if ... break to stop the while loop when the desired condition is reached, solves the issue:
while True:
place = place.split(' ', 1)[1]
try:
lat, lon, res = gmaps_geoencoder(place)
except:
place = place.split(' ', 1)[1]
lat, lon, res = gmaps_geoencoder(place)
if (4<lat<21 or 116<lon<128):
break
gmaps_geoencoder,latandlonare floats, andresis the string returned by the API I showed in the 2 examples.gmaps_geoencoderwith the sameplace.split(' ', 1)[1]with the same literal constantplaceto give you any different results from the first one?