【问题标题】:Obtaining latitude and longitude of multiple locations using Geopy使用 Geopy 获取多个位置的纬度和经度
【发布时间】:2016-06-03 07:47:50
【问题描述】:

我一直在导入一个包含多个地址的 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(最近一次调用最后一次):文件“”,第 13 行,in lat=location.latitude AttributeError: 'NoneType' 对象没有属性 'latitude

谁能帮我解决这个问题?

'

【问题讨论】:

  • 你没有考虑location = geolocator.geocode(address_new)返回None时的情况
  • 如何在我的代码中处理它?

标签: python csv geopy


【解决方案1】:

由于各种原因,包括地理编码服务没有给定地址的地理空间数据,您有时会忘记位置可能为无。

简单的做

location = geolocator.geocode(address_new)
if location:
    lat=location.latitude
    lon=location.longitude
else :
    lat = None
    long = None

try, except 也可以

【讨论】:

    【解决方案2】:

    有时实际位置(即纬度和经度)不适用于特定地址。在这种情况下,您必须忽略这种地址。在你的代码中应该是这样的 -

    导入 csv

    从时间导入睡眠

    从 geopy.geocoders 导入 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)
                ''' 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])`

    【讨论】:

      猜你喜欢
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多