python-3.xdataframegoogle-geocodergoogle-geocoding-api

How can I get the proper location of a company using geocode for this particular case? (Python, Google API)


I want to get the latitude and longitude of the companies listed in a dataframe already cleaned but the only information that I have is the name of the company and the country (In this case just UK).

DataFrame

After trying different things I have got some of the lats and longs but not the ones located in UK in most of the cases. This is the code I tried:

base_url= "https://maps.googleapis.com/maps/api/geocode/json?"
AUTH_KEY = "AI**************QTk"
geolocator = GoogleV3(api_key = AUTH_KEY)

parameters = {"address": "Revolut, London",
             "key": AUTH_KEY}
print(f"{base_url}{urllib.parse.urlencode(parameters)}")
r = requests.get(f"{base_url}{urllib.parse.urlencode(parameters)}")
data = json.loads(r.content)
data.get("results")[0].get("geometry").get("location")   #That works for the first company

df["loc"] = df["Company name for communication"].apply(geolocator.geocode)
df["point"]= df["loc"].apply(lambda loc: tuple(loc.point) if loc else None)
df[['lat', 'lon', 'altitude']] = pd.DataFrame(df['point'].to_list(), index=df.index)

DataFrame with long and lat wrong

I would agree so much any help. Let me know if my explanation is not clear to provide more details. Thank you!


Solution

  • If you are only trying to get Geocoding API results in the UK, then you would want to make use of component filtering.

    The Geocoding API can return address results restricted to a specific area. You can specify the restriction using the components filter. For more information, see Component Filtering. Specifically, you would want to include the country.

    Note that the value should be a country name or a two letter ISO 3166-1 country code. The API follows the ISO standard for defining countries, and the filtering works best when using the corresponding ISO code of the country. For example

    Here is a sample Geocoding web request with country components filtering in the UK looks like:

    https://maps.googleapis.com/maps/api/geocode/json?address=high+st+hasting&components=country:gb&key=YOUR_API_KEY
    

    This will only return a result that is only located in the UK, and will return zero results if not available.

    You may also want to take a look at region biasing.

    Note that if you bias for the region, the returned result prefers results in the country, but doesn't restrict them to that country and will return a result for an address. Unlike component filtering, this takes a ccTLD (country code top-level domain) argument specifying the region bias. Most ccTLD codes are identical to ISO 3166-1 codes, with some notable exceptions. For example, the United Kingdom's ccTLD is "uk" (.co.uk) while its ISO 3166-1 code is "gb" (technically for the entity of "The United Kingdom of Great Britain and Northern Ireland").

    Please also take a look at the Geocoding API Best Practices