使用Geopy获取多个位置的经纬度
Obtaining latitude and longitude of multiple locations using Geopy
我一直在导入包含多个地址的 csv 数据集。我想获取这些地方的经纬度并将它们与原始地址一起写入新的 csv 文件。我一直在尝试使用 python 中的 Geopy 来实现这一点。以下是代码:
import csv
##from time import sleep
from geopy.geocoders import Nominatim
with open('D:/location_to_lat_lon/tolocate.csv', 'r') as fp:
with open('D:/location_to_lat_lon/places_located.csv', 'w',newline='') as op:
a = csv.writer(op)
a.writerow(["Town","District","State","Country","Address","Latitude","Longitude"])
for line in fp.readlines():
geolocator = Nominatim()
town_new = line.split(',')[0]
district_new = line.split(',')[1]
state_new = line.split(',')[2]
country_new = line.split(',')[3]
address_new = line.split(',')[4]
location = geolocator.geocode(address_new)
lat=location.latitude
lon=location.longitude
##time.sleep(3)
a.writerow([town_new,district_new,state_new,country_new,address_new,lat,lon])
但是,每次我 运行 这段代码都会出现以下错误
Traceback (most recent call last): File "", line 13, in
lat=location.latitude AttributeError: 'NoneType' object has no attribute 'latitude
谁能帮我解决这个问题?
'
由于各种原因,包括地理编码服务没有给定地址的地理空间数据,您有时会忘记位置 None。
简单做
location = geolocator.geocode(address_new)
if location:
lat=location.latitude
lon=location.longitude
else :
lat = None
long = None
你也可以try, except
有时实际位置(即纬度和经度)对于特定地址不可用。在那种情况下,您必须忽略此类地址。在你的代码中它应该是这样的 -
导入 csv
从时间导入睡眠
从 geopy.geocoders 导入 Nominatim
with open('D:/location_to_lat_lon/tolocate.csv', 'r') 作为 fp:
with open('D:/location_to_lat_lon/places_located.csv', 'w',newline='') as op:
a = csv.writer(op)
a.writerow(["Town","District","State","Country","Address","Latitude","Longitude"])
for line in fp.readlines():
geolocator = Nominatim()
town_new = line.split(',')[0]
district_new = line.split(',')[1]
state_new = line.split(',')[2]
country_new = line.split(',')[3]
address_new = line.split(',')[4]
location = geolocator.geocode(address_new)
''' This will check if your given address has any latitude or longitude and if true then lat and lon will be assigned otherwise, both lat and lon will be 0. '''
if location:
lat=location.latitude
lon=location.longitude
##time.sleep(3)
else:
lat = 0
lon = 0
a.writerow([town_new,district_new,state_new,country_new,address_new,lat,lon])`
我一直在导入包含多个地址的 csv 数据集。我想获取这些地方的经纬度并将它们与原始地址一起写入新的 csv 文件。我一直在尝试使用 python 中的 Geopy 来实现这一点。以下是代码:
import csv
##from time import sleep
from geopy.geocoders import Nominatim
with open('D:/location_to_lat_lon/tolocate.csv', 'r') as fp:
with open('D:/location_to_lat_lon/places_located.csv', 'w',newline='') as op:
a = csv.writer(op)
a.writerow(["Town","District","State","Country","Address","Latitude","Longitude"])
for line in fp.readlines():
geolocator = Nominatim()
town_new = line.split(',')[0]
district_new = line.split(',')[1]
state_new = line.split(',')[2]
country_new = line.split(',')[3]
address_new = line.split(',')[4]
location = geolocator.geocode(address_new)
lat=location.latitude
lon=location.longitude
##time.sleep(3)
a.writerow([town_new,district_new,state_new,country_new,address_new,lat,lon])
但是,每次我 运行 这段代码都会出现以下错误
Traceback (most recent call last): File "", line 13, in lat=location.latitude AttributeError: 'NoneType' object has no attribute 'latitude
谁能帮我解决这个问题?
'
由于各种原因,包括地理编码服务没有给定地址的地理空间数据,您有时会忘记位置 None。
简单做
location = geolocator.geocode(address_new)
if location:
lat=location.latitude
lon=location.longitude
else :
lat = None
long = None
你也可以try, except
有时实际位置(即纬度和经度)对于特定地址不可用。在那种情况下,您必须忽略此类地址。在你的代码中它应该是这样的 -
导入 csv
从时间导入睡眠
从 geopy.geocoders 导入 Nominatim
with open('D:/location_to_lat_lon/tolocate.csv', 'r') 作为 fp:
with open('D:/location_to_lat_lon/places_located.csv', 'w',newline='') as op:
a = csv.writer(op)
a.writerow(["Town","District","State","Country","Address","Latitude","Longitude"])
for line in fp.readlines():
geolocator = Nominatim()
town_new = line.split(',')[0]
district_new = line.split(',')[1]
state_new = line.split(',')[2]
country_new = line.split(',')[3]
address_new = line.split(',')[4]
location = geolocator.geocode(address_new)
''' This will check if your given address has any latitude or longitude and if true then lat and lon will be assigned otherwise, both lat and lon will be 0. '''
if location:
lat=location.latitude
lon=location.longitude
##time.sleep(3)
else:
lat = 0
lon = 0
a.writerow([town_new,district_new,state_new,country_new,address_new,lat,lon])`