arcpy.na.MakeRouteAnalysisLayer(network_data_source, {layer_name}, {travel_mode}, {sequence}, {time_of_day}, {time_zone}, {line_shape}, {accumulate_attributes}, {generate_directions_on_solve}, {time_zone_for_time_fields}, {ignore_invalid_locations}, {analysis_geodatabase}, {analysis_feature_dataset_name})
|
名称
|
说明
|
数据类型
|
|
network_data_source
|
将对其执行网络分析的网络数据集或服务。
可以使用以下值之一来指定该参数:
要使用门户 URL,您必须使用具有路径选择权限的账户登录门户。
了解如何在 Python 中登录门户
了解有关使用路径服务的网络分析详细信息
当使用 ArcGIS Online 或使用 ArcGIS Online 配置路径服务的 ArcGIS Enterprise 门户作为输入网络时,解决分析将消耗配额并受到某些限制,例如允许的输入数量。
了解关于网络分析中配额消耗和分析限制的详细信息
|
Network Dataset Layer; String
|
|
layer_name
(可选)
|
将创建的网络分析图层的名称。
|
String
|
|
travel_mode
(可选)
|
分析中使用的出行模式名称。 出行模式 为一组网络设置(例如行驶限制和 U 形转弯),用于确定行人、车辆、卡车或其他交通媒介在网络中的移动方式。 出行模式在网络数据源中进行定义。
arcpy.na.TravelMode 对象和包含出行模式有效 JSON 表示的字符串也可用作参数的输入。
|
String
|
|
sequence
(可选)
|
指定在计算最佳路径时是否必须以特定顺序访问输入停靠点。 此选项将路径分析由最短路径问题变为流动推销员问题 (TSP)。
USE_CURRENT_ORDER—将按照输入顺序访问停靠点。 这是默认设置。
FIND_BEST_ORDER—将重新排序停靠点以查找最佳路径。 此选项将路径分析由最短路径问题变为流动推销员问题 (TSP)。
PRESERVE_BOTH—将按输入顺序保留第一个和最后一个停靠点。 将重新排序其余的停靠点以查找最佳路径。
PRESERVE_FIRST—将按输入顺序保留第一个停靠点。 将重新排序其余的停靠点以查找最佳路径。
PRESERVE_LAST—将按输入顺序保留最后一个停靠点。 将重新排序其余的停靠点以查找最佳路径。
|
String
|
|
time_of_day
(可选)
|
路径的开始日期和时间。 路径开始时间通常用于查找阻抗属性随当日时间变化的路径。 例如,开始时间早上 7:00 可用于查找被认为是高峰时段流量的路径。 此参数的默认值为 8:00 a.m.。 日期和时间可以指定为 10/21/05 10:30 AM。如果路径跨越多天并且仅指定开始时间,则将使用当前日期。
求解结束后,在输出路径中填充路径的开始时间与结束时间。 也会在生成方向时使用这些开始时间和结束时间。
将分析配置为使用以下特殊日期之一以针对一周中的某天或当前日期建模(而非使用特定静态日期):
Today—12/30/1899
Sunday—12/31/1899
Monday—1/1/1900
Tuesday—1/2/1900
Wednesday—1/3/1900
Thursday—1/4/1900
Friday—1/5/1900
Saturday—1/6/1900
了解如何在网络分析中使用和解释日期和时间的详细信息
|
Date
|
|
time_zone
(可选)
|
指定 time_of_day 参数的时区。
LOCAL_TIME_AT_LOCATIONS—time_of_day 参数采用路径的第一个停靠点所处的时区。 这是默认设置。如果生成开始于多个时区的多个路径,则开始时间会采用协调世界时间 (UTC) 交错。 例如,time_of_day 值为上午 10:00, 1 月 2 日表示始于东部时区的路径的开始时间为东部标准时间上午 10:00 (3:00 p.m. UTC),始于中部时区的路径的开始时间为中部标准时间上午 10:00 (4:00 p.m. UTC)。 开始时间偏差一小时(UTC 时间)。输出停靠点要素类中记录的到达与离开的时间和日期将采用每个路径第一个停靠点的本地时区。
UTC—time_of_day 参数采用协调世界时间 (UTC)。 如果您想要在特定的时间(如现在)生成路径,但不确定第一个停靠点所在的时区,请选择此选项。如果您生成跨越多个时区的多个路径,以 UTC 表示的开始时间将发生同步。 例如,time_of_day 值为上午 10:00, 则表示始于东部时区的路径的开始时间为东部标准时间上午 5:00 (10:00 a.m. UTC),始于中部时区的路径的开始时间为中部标准时间上午 4:00 (10:00 a.m. UTC)。 这两个路径均于 UTC 时间 10:00 a.m. 开始。输出停靠点要素类中记录的到达与离开的时间和日期将采用 UTC 时间。
|
String
|
|
line_shape
(可选)
|
指定将用于分析所输出的路径要素的形状类型。
无论指定的输出形状类型为何,最佳路径始终由网络阻抗(而非欧氏距离)决定。 这表示只是路径形状不同,而对网络进行的基础遍历则相同。
|
String
|
|
accumulate_attributes
[accumulate_attributes,...]
(可选)
|
分析过程中要累积的成本属性的列表。 这些累积属性仅供参考;求解程序仅使用求解分析时指定的出行模式所使用的成本属性。
对于每个累积的成本属性,会在网络分析输出要素中填充 Total_ [阻抗] 属性。
如果网络数据源为 ArcGIS Online 服务,或如果网络数据源是不支持累积的 Portal for ArcGIS 版本上的服务,则此参数不可用。
|
String
|
|
generate_directions_on_solve
(可选)
|
指定运行分析时是否生成方向。
对于不需要生成转向指示的分析,关闭方向生成可以减少求解分析的时间。
|
Boolean
|
|
time_zone_for_time_fields
(可选)
|
指定将用于解释输入表中包含的时间字段(例如用于时间窗的字段)的时区。
|
String
|
|
ignore_invalid_locations
(可选)
|
指定是否忽略无效的输入位置。 通常,如果无法在网络上定位,则位置无效。 当无效位置被忽略时,求解程序将跳过它们并尝试使用剩余位置执行分析。
|
Boolean
|
|
analysis_geodatabase
(可选)
|
存储新网络分析图层的子图层的文件地理数据库工作空间。 当未指定此参数时,将在当前工作空间地理处理环境(通常为工程地理数据库)中创建子图层的数据。
了解有关网络分析图层的分析数据源的详细信息
|
Workspace
|
|
analysis_feature_dataset_name
(可选)
|
用于命名该工具将在分析地理数据库中创建的新要素数据集的名称,该数据集将用于存储网络分析图层的子图层数据。 如未指定,要素数据集将以随机唯一名称创建。
了解有关网络分析图层的分析数据源的详细信息
|
String
|
派生输出
|
名称
|
说明
|
数据类型
|
|
out_network_analysis_layer
|
输出网络分析图层。
|
Network Analyst Layer
|
代码示例
MakeRouteAnalysisLayer 示例 1(Python 窗口)
仅使用必需参数运行该工具。
network = "C:/Data/SanFrancisco.gdb/Transportation/Streets_ND"
arcpy.na.MakeRouteAnalysisLayer(network, "WorkRoute")
MakeRouteAnalysisLayer 示例 2(Python 窗口)
使用所有参数运行该工具。
network = "C:/Data/SanFrancisco.gdb/Transportation/Streets_ND"
arcpy.na.MakeRouteAnalysisLayer(network, "InspectionRoute", "Driving Time",
"FIND_BEST_ORDER", "1/1/1900 9:00 AM", "UTC",
"ALONG_NETWORK", ["Meters", "TravelTime"])
MakeRouteAnalysisLayer 示例 3(工作流)
以下独立 Python 脚本演示了如何使用 MakeRouteAnalysisLayer 函数查找访问地理编码停靠点位置的最佳路径。
# Name: MakeRouteAnalysisLayer_Workflow.py
# Description: Find a best route to visit the stop locations and save the
# route to a layer file. The stop locations are geocoded from a
# text file containing the addresses.
# Requirements: Network Analyst extension
#Import system modules
import arcpy
from arcpy import env
import os
try:
#Set environment settings
output_dir = "C:/Data"
#The NA layer's data will be saved to the workspace specified here
env.workspace = os.path.join(output_dir, "Output.gdb")
env.overwriteOutput = True
#Set local variables
input_gdb = "C:/Data/SanFrancisco.gdb"
network = os.path.join(input_gdb, "Transportation", "Streets_ND")
layer_name = "BestRoute"
travel_mode = "Driving Time"
address_locator = "C:/Data/SanFranciscoLocator"
address_table = "C:/Data/StopAddresses.csv"
address_fields = "Street Address;City City;State State;ZIP <None>"
out_stops = "GeocodedStops"
output_layer_file = os.path.join(output_dir, layer_name + ".lyrx")
#Create a new Route layer. For this scenario, the default values for all the
#remaining parameters statisfy the analysis requirements
result_object = arcpy.na.MakeRouteAnalysisLayer(network, layer_name,
travel_mode)
#Get the layer object from the result object. The route layer can now be
#referenced using the layer object.
layer_object = result_object.getOutput(0)
#Get the names of all the sublayers within the route layer.
sublayer_names = arcpy.na.GetNAClassNames(layer_object)
#Stores the layer names that we will use later
stops_layer_name = sublayer_names["Stops"]
#Geocode the stop locations from a csv file containing the addresses.
#The Geocode Addresses tool can use a text or csv file as input table
#as long as the first line in the file contains the field names.
arcpy.geocoding.GeocodeAddresses(address_table, address_locator,
address_fields, out_stops)
#Load the geocoded address locations as stops mapping the address field from
#geocoded stop features as Name property using field mappings.
field_mappings = arcpy.na.NAClassFieldMappings(layer_object,
stops_layer_name)
field_mappings["Name"].mappedFieldName = "Address"
arcpy.na.AddLocations(layer_object, stops_layer_name, out_stops,
field_mappings, "")
#Solve the route layer, ignoring any invalid locations such as those that
#cannot be geocoded
arcpy.na.Solve(layer_object, "SKIP")
#Save the solved route layer as a layer file on disk
layer_object.saveACopy(output_layer_file)
print("Script completed successfully")
except Exception as e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
print("An error occurred on line %i" % tb.tb_lineno)
print(str(e))
MakeRouteAnalysisLayer 示例 4(工作流)
本示例展示如何在单次求解中创建多条路径,这种方法可用于计算起始点-目的地对间的距离或行驶时间。
旧版本:
GetNASublayer 函数用于调用网络分析图层的子图层。 它在 ArcGIS Pro 2.7 中引入。 在以前的软件版本中,用于检索网络分析图层的子图层对象的最佳方法是使用网络分析 Layer 对象的 listLayers 方法,该方法将子图层名称用作通配符。
# Name: MakeRouteAnalysisLayer_MultiRouteWorkflow.py
# Description: Calculate the home-work commutes for a set of people and save
# the output to a feature class
# Requirements: Network Analyst extension
#Import system modules
import arcpy
from arcpy import env
import datetime
import os
try:
#Set environment settings
output_dir = "C:/Data"
#The NA layer's data will be saved to the workspace specified here
env.workspace = os.path.join(output_dir, "Output.gdb")
env.overwriteOutput = True
#Set local variables
input_gdb = "C:/Data/SanFrancisco.gdb"
network = os.path.join(input_gdb, "Transportation", "Streets_ND")
stops_home = os.path.join(input_gdb, "Analysis", "Commuters_Home")
stops_work = os.path.join(input_gdb, "Analysis", "Commuters_Work")
layer_name = "Commuters"
out_routes_featureclass = "Commuter_Routes"
travel_mode = "Driving Time"
#Set the time of day for the analysis to 8AM on a generic Monday.
start_time = datetime.datetime(1900, 1, 1, 8, 0, 0)
#Create a new Route layer. Optimize on driving time, but compute the
#distance traveled by accumulating the Meters attribute.
result_object = arcpy.na.MakeRouteAnalysisLayer(network, layer_name,
travel_mode, time_of_day=start_time,
accumulate_attributes=["Meters"])
#Get the layer object from the result object. The route layer can now be
#referenced using the layer object.
layer_object = result_object.getOutput(0)
#Get the names of all the sublayers within the route layer.
sublayer_names = arcpy.na.GetNAClassNames(layer_object)
#Stores the layer names that we will use later
stops_layer_name = sublayer_names["Stops"]
routes_layer_name = sublayer_names["Routes"]
#Before loading the commuters' home and work locations as route stops, set
#up field mapping. Map the "Commuter_Name" field from the input data to
#the RouteName property in the Stops sublayer, which ensures that each
#unique Commuter_Name will be placed in a separate route. Matching
#Commuter_Names from stops_home and stops_work will end up in the same
#route.
field_mappings = arcpy.na.NAClassFieldMappings(layer_object, stops_layer_name)
field_mappings["RouteName"].mappedFieldName = "Commuter_Name"
#Add the commuters' home and work locations as Stops. The same field mapping
#works for both input feature classes because they both have a field called
#"Commuter_Name"
arcpy.na.AddLocations(layer_object, stops_layer_name, stops_home,
field_mappings, "")
arcpy.na.AddLocations(layer_object, stops_layer_name, stops_work,
field_mappings, "", append="APPEND")
#Solve the route layer.
arcpy.na.Solve(layer_object)
# Get the output Routes sublayer and save it to a feature class
routes_sublayer = arcpy.na.GetNASublayer(layer_object, "Routes")
arcpy.management.CopyFeatures(routes_sublayer, out_routes_featureclass)
print("Script completed successfully")
except Exception as e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
print("An error occurred on line %i" % tb.tb_lineno)
print(str(e))