6

我正在尝试对包含位置名称和解析出的地址的 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)
4

2 回答 2

3

这只是一个开始,告诉我这是否有帮助。它不会写入 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)

文档:

于 2015-07-06T18:46:17.843 回答
3

我一直在使用这个脚本从 .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”两个新列。

于 2015-07-31T18:27:00.250 回答