使用 Geopy 和 Python 进行地理编码
Geocoding using Geopy and Python
我正在尝试对包含位置名称和解析地址的 CSV 文件进行地理编码,其中包括地址编号、街道名称、城市、邮政编码、国家/地区。我想通过 Geopy.I 使用 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)
这只是一个开始,请告诉我是否有帮助。它不会写入 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)
文档:
我一直在使用此脚本从 .csv 中执行一些 batch-geocoding。它要求一列包含您希望进行地理编码的完整文本地址,并且一列标题为 'UniqueID',它具有 .csv 中每个项目的唯一标识符。它还将打印出所有未能进行地理编码的地址的列表。它还会快速检查邮政编码是否可能 incorrect/throwing 偏离地理编码:
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' 的两个新列.
我正在尝试对包含位置名称和解析地址的 CSV 文件进行地理编码,其中包括地址编号、街道名称、城市、邮政编码、国家/地区。我想通过 Geopy.I 使用 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)
这只是一个开始,请告诉我是否有帮助。它不会写入 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)
文档:
我一直在使用此脚本从 .csv 中执行一些 batch-geocoding。它要求一列包含您希望进行地理编码的完整文本地址,并且一列标题为 'UniqueID',它具有 .csv 中每个项目的唯一标识符。它还将打印出所有未能进行地理编码的地址的列表。它还会快速检查邮政编码是否可能 incorrect/throwing 偏离地理编码:
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' 的两个新列.