简体   繁体   English

使用Geopy获取多个位置的纬度和经度

[英]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 time import sleep 从时间导入睡眠

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.

相关问题 使用Geopy将英里转换为纬度和经度 - Conversion of miles to latitude and longitude degrees using geopy 使用 GeoPy 计算 pandas dataframe 上的纬度/经度距离 - Calculating distance of latitude/longitude on pandas dataframe using GeoPy 我可以在Python中使用geopy获得高度吗? (经度/纬度) - Can I get the altitude with geopy in Python? (with Longitude/Latitude) Python:从纬度和经度值获取高程 - Python: Obtaining elevation from latitude and longitude values 将物理地址转换为地理位置纬度和经度 - Convert physical addresses to Geographic locations Latitude and Longitude 抓取从 Mapbox 获取的经纬度位置 - Scrape latitude and longitude locations obtained from Mapbox 如何使用geopy获取dataframe中地址列的纬度和纬度? - How to get latitude and latitude for an address column in a dataframe using geopy? python:使用纬度和经度在提供位置的半径内查找位置的优雅方法 - python: elegant way to find the locations inside the radius of provided location using Latitude and Longitude 使用纬度和经度计算多个来源和目的地之间的距离 - distance calculation between multiple sources and destinations using latitude and longitude 是否有一个 Geopy Python function 将 dataframe 列(纬度/经度)从“度分秒”转换为“度十进制” - Is there a Geopy Python function that converts dataframe column (Latitude/Longitude) from “degree minute second” to “degree decimal”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM