Geographos
Geographos

Reputation: 1496

Conditional-based geolocator in Python

I am working on the Nominatim geolocator in Python. Unfortunately, some addresses are missing, therefore I tried to make some condition-based workaround, which would allow executing something based at least on postcode, which works well in any case. Unfortunately, I failed for now. With the following code:

 import pandas as pd
 import folium
 import web-browser
 from geopy.geocoders import Nominatim

 geolocator = Nominatim(timeout=10, user_agent="Krukarius")

 def find_location(row):
  place = row['Address']
  place_data = newstr = place[-8:]
  location = geolocator.geocode(place)
  location_overall = geolocator.geocode(place_data)


  if location != None:
    return location.latitude, location.longitude
  else:
   #return 0,0
    return location_overall 

  points = pd.read_csv("Addresses4.csv")

  points[['Lat','Lng']] = points.apply(find_location, axis="columns", result_type="expand")
  print(points)

  points.to_csv('NewAddresses4.csv')

ValueError: Location should consist of two numerical values, but '' of type <class 'str'> is not convertible to float.

Upvotes: -3

Views: 332

Answers (1)

Geographos
Geographos

Reputation: 1496

I think I sorted it out on my own.

The code should exactly look like this:

def find_location(row):
place = row['Address']
place_data = newstr = place[-8:]
location = geolocator.geocode(place)
location_overall = geolocator.geocode(place_data)


if location != None:
    return location.latitude, location.longitude
else:
    return  location_overall.latitude, location_overall.longitude

where instead of the first condition, we should call another one.

Upvotes: -2

Related Questions