Geocoding using Geopy and Python
我正在尝试对一个包含位置名称和已解析地址的csv文件进行地理编码,其中包括地址编号、街道名称、城市、邮政编码和国家。我想通过geopy使用geopy和arcgis地理编码。我想创建一个代码,循环通过我的csv 5000多个条目,并在csv中的单独列中给出纬度和经度。我想通过地缘学使用Arcgis地理编码服务。有人能给我提供一个开始的代码吗?谢谢!
这是我的剧本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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中的每个项都具有唯一的标识符。它还将打印出一个没有地理编码的地址列表。它还可以快速检查邮政编码是否不正确/丢弃地理编码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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"(例如,输入"addresses.csv"将输出带有两个新列的"lat"和"long"的"addresses.csv")的新csv。
这只是一个乞讨,告诉我是否有帮助。它不会写入csv,但如果您还需要该部分,稍后我会编辑我的答案。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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) |
博士:
- http://geopy.readthedocs.org/en/latest/geopy.geocoders.arcgis
- https://developers.arcgis.com/rest/geocode/api-reference/overview-world-geocoding-service.htm网站
- https://docs.python.org/2/library/csv.html网站