[英]Obtaining latitude and longitude of multiple locations using Geopy
I have been importing a csv dataset containing multiple addresses. 我一直在导入包含多个地址的csv数据集。 I want to get the latitude and longitude of these places and write those to a new csv file along with the original address.
我想得到这些地方的纬度和经度,并将它们与原始地址一起写入新的csv文件。 I have been trying to use Geopy from python to achieve this.
我一直在尝试使用python中的Geopy来实现这一目标。 Given below is the code:
以下是代码:
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])
However, every time I run this code I get the following error 但是,每次运行此代码时,我都会收到以下错误
Traceback (most recent call last): File "", line 13, in lat=location.latitude AttributeError: 'NoneType' object has no attribute 'latitude
回溯(最近一次调用最后一次):文件“”,第13行,在lat = location.latitude中AttributeError:'NoneType'对象没有属性'纬度
Can anyone please help me to resolve this? 有人可以帮我解决这个问题吗?
' “
You are forgetting that location can be None at times due to various reasons including the geocoding service not having geo spatial data for the given address. 由于各种原因,包括地理编码服务没有给定地址的地理空间数据,您忘记了该位置有时可能为无。
simpley do 这很简单
location = geolocator.geocode(address_new)
if location:
lat=location.latitude
lon=location.longitude
else :
lat = None
long = None
you could do try, except
as well 你也可以
try, except
Sometimes Actual location ie latitude and longitude are not available for a particular address. 有时,实际位置即纬度和经度不适用于特定地址。 In that case you have to ignore such kind of address.
在这种情况下,你必须忽略这种地址。 In you code it should be something like this -
在你的代码中它应该是这样的 -
import csv 导入csv
from geopy.geocoders import Nominatim 来自geopy.geocoders导入Nominatim
with open('D:/location_to_lat_lon/tolocate.csv', 'r') as fp: 打开('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])` a.writerow([town_new,district_new,state_new,country_new,address_new,LAT,LON])`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.