使用Geopy和Python进行地理编码 [英] Geocoding using Geopy and Python

查看:135
本文介绍了使用Geopy和Python进行地理编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to Geocode a CSV file that contains the name of the location and a parsed out address which includes Address number, Street name, city, zip, country. I want to use GEOPY and ArcGIS Geocodes through Geopy.I wanted to create a code that loops through my csv of 5000+ entries and gives me the latitude and longitude in separate columns in my CSV. I want to use ArcGIS Geocoding service through Geopy. Can anyone provide me with a code to get started? Thanks!

这是我的剧本:

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)

推荐答案

我一直在使用此脚本从.csv中进行一些批处理地理编码.它要求一列包含您要地理编码的完整文本地址,并且一列标题为"UniqueID",该列具有.csv中每个项目的唯一标识符.它还会打印出无法进行地址解析的所有地址的列表.它还会进行快速检查,以查看邮政编码是否可能不正确/扔掉了地理编码:

I've been using this script to do some batch-geocoding from .csv. It requires that one column contain the complete text address that you wish to geocode, and that one column be titled 'UniqueID', which has a unique identifier for each item in the .csv. It will also print out a list of any addresses that it failed to geocode. It also does a quick check to see if the zip code might be incorrect/throwing off the geocoding:

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"添加两个新列.

This will output a new csv with "_RESULTS" (e.g., an input of 'addresses.csv' will output 'addresses_RESULTS.csv') with two new columns for 'Lat' and 'Long'.

这篇关于使用Geopy和Python进行地理编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆