Python 如何使用arcGIS api获取两个地址之间的多个驱动器时间
我正在使用ArcGIS api(ArcMap-10.5.1),并试图获得两个地址之间的驱动器时间。我可以得到两点之间的驾驶时间,但我不知道如何迭代多个点。我有一百个地址。我一直在 AttributeError:“非类型”对象没有属性“\u tools” 这是我正在使用的熊猫数据帧。我有两个带索引的列。第1列是源地址,第2列是第二个地址。如果可能的话,我想在开车时间上再划一行Python 如何使用arcGIS api获取两个地址之间的多个驱动器时间,python,pandas,arcgis,Python,Pandas,Arcgis,我正在使用ArcGIS api(ArcMap-10.5.1),并试图获得两个地址之间的驱动器时间。我可以得到两点之间的驾驶时间,但我不知道如何迭代多个点。我有一百个地址。我一直在 AttributeError:“非类型”对象没有属性“\u tools” 这是我正在使用的熊猫数据帧。我有两个带索引的列。第1列是源地址,第2列是第二个地址。如果可能的话,我想在开车时间上再划一行 df2 Address_1
df2
Address_1 Address_2
0 1600 Pennsylvania Ave NW, Washington, DC 20500 2 15th St NW, Washington
1 400 Broad St, Seattle, WA 98109 325 5th Ave N, Seattle
这是我从中获取代码的链接
我试着破解这个密码。特别是下面的代码
def pairwise(iterable):
a, b = tee(iterable)
next(b, None)
return zip(a, b)
#empty list - will be used to store calculated distances
list = [0]
# Loop through each row in the data frame using pairwise
for (i1, row1), (i2, row2) in pairwise(df.iterrows()):
我查了一下non_type的意思,所以我试着打印出来,看看是否有任何东西可以打印出来,而且效果很好。我主要使用R,很少使用python
for (i,j) in pairwise(df2.iterrows()):
print(i)
print(j)
预期输出是一个驱动器时间列表,作为一个列表。我尝试将所有内容附加到数据帧,这将是理想的。因此理想的输出应该是3列—地址1、地址2和驱动时间。代码一次只处理一个地址(而不是i,j,它只是两个地址作为字符串,没有for语句)
例如:
Address_1 Address_2
0 1600 Pennsylvania Ave NW, Washington, DC 20500 2 15th St NW, Washington
1 400 Broad St, Seattle, WA 98109 325 5th Ave N, Seattle
drive_time
0 7 minutes
1 3 minutes
您不需要使用成对函数。只需将arcGIS代码封装在一个函数中,该函数将向您返回时间,这样您就可以将值映射为数据帧上的一个新列 另外,请确保导入时间库,该时间库未在arcGIS文档中注明,但需要运行该时间库 ` `
Address_1 Address_2
0 1600 Pennsylvania Ave NW, Washington, DC 20500 2 15th St NW, Washington
1 400 Broad St, Seattle, WA 98109 325 5th Ave N, Seattle
drive_time
0 7 minutes
1 3 minutes
def getTime(row):
try:
stop1_geocoded = geocoding.geocode(row.df_column_1)
stop2_geocoded = geocoding.geocode(row.df_column_2)
stops = '{0},{1}; {2},{3}'.format(stop1_geocoded[0]['attributes']['X'],
stop1_geocoded[0]['attributes']['Y'],
stop2_geocoded[0]['attributes']['X'],
stop2_geocoded[0]['attributes']['Y'])
route_layer = network.RouteLayer(route_service_url, gis=my_gis)
result = route_layer.solve(stops=stops, return_directions=False, return_routes=True,
output_lines='esriNAOutputLineNone', return_barriers=False,
return_polygon_barriers=False, return_polyline_barriers=False)
travel_time = result['routes']['features'][0]['attributes']['Total_TravelTime']
time = "Total travel time is {0:.2f} min".format(travel_time)
return time
except RuntimeError:
return
streets['travel_time'] = streets.apply(getTime, axis=1)