【问题标题】:Geocoding using Geopy and Python使用 Geopy 和 Python 进行地理编码
【发布时间】:2015-09-24 00:20:09
【问题描述】:

我正在尝试对包含位置名称和解析出的地址的 CSV 文件进行地理编码,其中包括地址编号、街道名称、城市、邮政编码、国家/地区。我想通过 Geopy 使用 GEOPY 和 ArcGIS Geocodes。我想创建一个循环遍历 5000 多个条目的 csv 的代码,并在我的 CSV 的单独列中给出纬度和经度。我想通过 Geopy 使用 ArcGIS 地理编码服务。谁能给我一个代码来开始?谢谢!

这是我的脚本:

import csv
from geopy.geocoders import ArcGIS


geolocator = ArcGIS()     # here some parameters are needed

with open('C:/Users/v-albaut/Desktop/Test_Geo.csv', 'rb') as csvinput:
    with open('output.csv', 'w') as csvoutput:
        output_fieldnames = ['Name','Address', 'Latitude', 'Longitude']
        writer = csv.DictWriter(csvoutput, delimiter=',', fieldnames=output_fieldnames)
        reader = csv.DictReader(csvinput)

        for row in reader:
            # here you have to replace the dict item by your csv column names
            query = ','.join(str(x) for x in (row['Name'], row['Address']))
            Address, (latitude, longitude) = geolocator.geocode(query)

            # here is the writing section
            output_row = {}
            output_row['Name'] = Name
            output_row['Address'] = Address
            output_row['Latitude'] = Latitude
            output_row['Longitude'] =Longitude
            writer.writerow(output_row)

【问题讨论】:

    标签: python csv arcgis geopy


    【解决方案1】:

    这只是一个开始,告诉我这是否有帮助。它不会写入 csv,但如果您也需要该部分,我稍后会编辑我的答案

    import csv
    from geopy.geocoders import ArcGIS
    
    geolocator = ArcGIS() #here some parameters are needed
    
    with open('C:/Users/v-albaut/Desktop/Test_Geo.csv', 'rb') as csvinput:
        with open('output.csv', 'w') as csvoutput:
           output_fieldnames = ['Name','Address', 'Latitude', 'Longitude']
           writer = csv.DictWriter(csvoutput, delimiter=',', fieldnames=output_fieldnames)
           reader = csv.DictReader(csvinput)
           for row in reader:
                #here you have to replace the dict item by your csv column names
                query = ','.join(str(x) for x in (row['Name'], row['Address']))
    
                try:
                    address, (latitude, longitude) = geolocator.geocode(query)
                except:
                    latitude = 'N/A'
                    longitude = 'N/A'
    
                #here is the writing section
                output_row = {}
                output_row['Name'] = row['Name']
                output_row['Address'] = row['Address']
                output_row['Latitude'] = latitude
                output_row['Longitude'] = longitude
                writer.writerow(output_row)
    

    文档:

    【讨论】:

    • 嗨,如果你也能提供写作部分那就太好了!
    • 我已经添加了写作部分,请您测试并告诉我它是否有效。请注意,您必须为 ArcGIS 地理编码器对象提供正确的参数才能使其正常工作
    • 当我在脚本上放置了正确的列名时出现了一个关键错误。我该怎么办?
    • 哪里出错了?确保csv的第一行包含列名并且字母大小写正确
    • 可能地理编码器无法对该地址进行地理编码,因此请使用 try/except 将此地址的 lat/lon 标记为“N/A”或其他值
    【解决方案2】:

    我一直在使用此脚本从 .csv 进行一些批量地理编码。它要求一列包含您希望进行地理编码的完整文本地址,并且一列的标题为“UniqueID”,它对 .csv 中的每个项目都有一个唯一标识符。它还将打印出任何未能进行地理编码的地址列表。它还会快速检查邮政编码是否可能不正确/放弃地理编码:

    def main(path, filename):
    # path to where your .csv lives, and the name of the csv.
        import geopy
        from geopy.geocoders import ArcGIS
        import pandas as pd
    
        Target_Addresses = pd.read_csv(path+'\\'+filename)
        Target_Addresses['Lat'] = np.nan
        Target_Addresses['Long'] = np.nan
        Indexed_Targets = Target_Addresses.set_index('UniqueID')
    
        geolocator = ArcGIS() #some parameters here
        Fails = []
        for index, row in Indexed_Targets.iterrows():
            Address = row['Address']
            Result = geolocator.geocode(Address)
            if Result == None:
                Result = geolocator.geocode(Address[:-7])
                if Result == None:
                    Fails.append[Address]
                else:
                    Indexed_Targets.set_value(index, 'Lat', Result.latitude)
                    Indexed_Targets.set_value(index, 'Long', Result.longitude)
            else:
                Indexed_Targets.set_value(index, 'Lat', Result.latitude)
                Indexed_Targets.set_value(index, 'Long', Result.longitude)
        for address in Fails:
            print address
        Indexed_Targets.to_csv(filename[:-4]+"_RESULTS.csv")
    
    if __name__ == '__main__':
        main(path, filename) # whatever these are for you...
    

    这将输出一个带有“_RESULTS”的新 csv(例如,输入“addresses.csv”将输出“addresses_RESULTS.csv”),其中包含“Lat”和“Long”两个新列。

    【讨论】:

    • 我尝试使用此代码。它只是打印(地址)第一行地址,并且没有创建文件“_RESULTS.csv”。请帮忙谢谢
    • @SourabhChoudhary 你能提供更多关于你的输入地址是如何组织的信息吗?它会产生什么样的错误?
    猜你喜欢
    • 1970-01-01
    • 2020-02-16
    • 2021-01-26
    • 1970-01-01
    • 2015-10-07
    • 2018-09-13
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    相关资源
    最近更新 更多