问题描述
我有一个非常大的 1000 多个十字路口的 excel 文件,我需要找到经度和纬度,然后将该信息写入文件/列表以供其他程序使用。
我所坚持的是如何使用多线程/多处理构建更高效的脚本,我已经浏览了其他问题/帖子,但我觉得这一切都有些令人困惑。下面的代码大约需要 10+ 分钟。任何帮助都会很棒。
from geopy.geocoders import ArcGIS
import pandas
from datetime import datetime
start_time = datetime.Now()
def my_LatLong(address):
n = ArcGIS().geocode(address)
if n != None:
return n
else:
return [address,"None"]
df = pandas.read_excel("street_sample.xlsx",sheet_name=0)
count = 0
street_list = df.loc[:,"Description"]
new_list =[]
for i in list(street_list):
location = my_LatLong(f"{i.split('-')[0]},Vancouver,Canada")
if location != None:
new_list.append([f"{len(street_list)},{list(location)}"])
print(f"{count}/{len(street_list)} - {i} = Completed\t\t\t\t",end='\r')
else:
print(f"{count}/{len(street_list)} - {i} == None value \t\t\t",end='\r')
count += 1
# doing something with new_list
endtime_time = datetime.Now()
print (f"Program ran for: {endtime_time -start_time}")
street_sample.xlsx
ID | 描述 |
---|---|
12501x | 1900 W Georgia - ONAT_STREET: |
12501x | 第 4/6 分流和第 6 步 - ONAT_STREET: |
12501x | 第 4/6 次改道和第 6 次半 - ONAT_STREET: |
12501x | 雅培和科尔多瓦 - ONAT_STREET: |
12501x | Abbott & Expo - ONAT_STREET: |
12501x | 雅培和黑斯廷斯 - ONAT_STREET: |
12501x | Abbott & Keefer - ONAT_STREET: |
12501x | Aberdeen & Kingsway - ONAT_STREET: |
12501x | 艾伯塔省和第 49 区 - ONAT_STREET:70175 |
12501x | Alder & 12th - ONAT_STREET: |
12501x | Alder & 6th - ONAT_STREET: |
12501x | Alder & broadway - ONAT_STREET: |
12501x | 亚历山德拉和爱德华国王 - ONAT_STREET: |
12501x | Alma & 10th - ONAT_STREET: |
12501x | Alma & 4th - ONAT_STREET: |
12501x | Alma & 6th - ONAT_STREET: |
12501x | Alma & broadway - ONAT_STREET: |
12501x | Alma & Point Grey Road - ONAT_STREET: |
12501x | Anderson & 2nd / Lamey's Mill - ONAT_STREET: |
12501x | Anderson (Granville) & 4th - ONAT_STREET: |
12501x | Angus & 41st - ONAT_STREET: |
12501x | Angus & Marine - ONAT_STREET: |
12501x | Arbutus & 10th - ONAT_STREET: |
12501x | Arbutus & 11th - ONAT_STREET: |
12501x | Arbutus & 12th - ONAT_STREET: |
12501x | Arbutus & 16th - ONAT_STREET: |
12501x | Arbutus & 20th - ONAT_STREET: |
12501x | Arbutus & 33rd - ONAT_STREET: |
12501x | Arbutus & 4th - ONAT_STREET: |
12501x | Arbutus & 8th - ONAT_STREET: |
12501x | 杨梅和百老汇 - ONAT_STREET: |
12501x | 杨梅和康沃尔 - ONAT_STREET: |
12501x | 杨梅和爱德华国王 - ONAT_STREET: |
12501x | 杨梅 & Lahb - ONAT_STREET: |
解决方法
问题不是来自 Pandas,而是来自 ArcGIS().geocode(address)
,它非常慢。事实上,在我的机器上,这条线需要 400 毫秒/请求。每个请求都会向在线 ArcGIS API 发送一个慢速网络查询。使用多处理不会有太大帮助,因为您会很快达到额外的限制(API 请求率有限,网站饱和)。您需要发送批量请求。不幸的是,这似乎不受 geopy
包的支持。如果绑定到 ArcGIS,则需要使用他们自己的 API。您可以找到有关如何做到这一点的更多信息on the ArcGIS documentation。