arcpy.na.MakeRouteLayer(in_network_dataset, out_network_analysis_layer, impedance_attribute, {find_best_order}, {ordering_type}, {time_windows}, {accumulate_attribute_name}, {UTurn_policy}, {restriction_attribute_name}, {hierarchy}, {hierarchy_settings}, {output_path_shape}, {start_date_time})
|
名称
|
说明
|
数据类型
|
|
in_network_dataset
|
将在其中执行路径分析的网络数据集。
|
Network Dataset Layer
|
|
out_network_analysis_layer
|
要创建的路径网络分析图层的名称。
|
String
|
|
impedance_attribute
|
分析过程中将用作阻抗的成本属性。
|
String
|
|
find_best_order
(可选)
|
指定在计算最佳路径时是否必须以特定顺序访问输入停靠点。 此选项将路径分析由最短路径问题变为流动推销员问题 (TSP)。
|
Boolean
|
|
ordering_type
(可选)
|
当使用 FIND_BEST_ORDER 时,将指定停靠点的顺序。
PRESERVE_BOTH—按输入停靠点的第一个和最后一个停靠点将保留为路径中的第一个和最后一个停靠点。
PRESERVE_FIRST—按输入停靠点的第一个停靠点将保留为路径中的第一个停靠点,但可以对最后一个停靠点重新排序。
PRESERVE_LAST—按输入停靠点的最后一个停靠点将保留为路径中的最后一个停靠点,但可以对第一个停靠点重新排序。
PRESERVE_NONE—不会保留第一个和最后一个停靠点,但是可以重新排序。
|
String
|
|
time_windows
(可选)
|
指定是否在停靠点处使用时间窗。
|
Boolean
|
|
accumulate_attribute_name
[accumulate_attribute_name,...]
(可选)
|
分析过程中要累积的成本属性的列表。 这些累积属性仅供参考;求解程序仅使用阻抗属性参数所指定的成本属性来计算路径。
对于每个累积的成本属性,均会向求解程序所输出的路径中添加一个 Total_ [阻抗] 属性。
|
String
|
|
UTurn_policy
(可选)
|
指定将在交汇点处使用的 U 形转弯策略。 允许 U 形转弯表示求解程序可以在交汇点处转向并沿同一街道往回行驶。 考虑到交汇点表示街道交叉路口和死角,不同的车辆可以在某些交汇点转弯,而在其他交汇点则不行 - 这取决于交汇点是交叉路口还是死角。 为适应此情况,U 形转弯策略参数由与交汇点连通的边数隐性指定,这称为交汇点原子价。 此参数可接受的值如下所列;每个值的后面是根据交汇点价对其含义的描述。
如果您需要定义更加精确的 U 形转弯策略,可以考虑在网络成本属性中添加一个通用转弯延迟赋值器,或者如果存在的话,调整其设置,并特别注意反向转弯的配置。 还可以设置网络位置的 CurbApproach 属性。
ALLOW_UTURNS—无论在交汇点处有几条连接的边,均允许 U 形转弯。 这是默认值。
NO_UTURNS—在所有交汇点处均禁止 U 形转弯,不管交汇点原子价如何。 但是,即使已指定该选项,在网络位置仍允许 U 形转弯;但是也可以通过设置个别网络位置的 CurbApproach 属性来禁止 U 形转弯。
ALLOW_DEAD_ENDS_ONLY—除仅有一条相邻边的交汇点(死角)外,其他交汇点均禁止 U 形转弯。
ALLOW_DEAD_ENDS_AND_INTERSECTIONS_ONLY—在恰好有两条相邻边相遇的交汇点处禁止 U 形转弯,但是交叉点(三条或三条以上相邻边的交汇点)和死角(仅有一条相邻边的交汇点)处允许。 通常,网络在路段中间有多余的交汇点。 此选项用于阻止车辆在这些位置进行 U 形转弯。
|
String
|
|
restriction_attribute_name
[restriction_attribute_name,...]
(可选)
|
分析期间要应用的限制属性列表。
|
String
|
|
hierarchy
(可选)
|
如果未在用于执行分析的网络数据集中定义等级属性,该参数将不可用。
|
Boolean
|
|
hierarchy_settings
(可选)
|
旧版本:
在版本 10 之前,可使用此参数将网络数据集中建立的默认等级范围更改为其他范围以用于分析。 而版本 10 中不再支持此参数,并且应将其指定为空字符串。 要更改分析的等级范围,更新网络数据集中的默认等级范围。
|
Network Analyst Hierarchy Settings
|
|
output_path_shape
(可选)
|
指定将用于分析所输出的路径要素的形状类型。
无论指定的输出形状类型为何,最佳路径始终由网络阻抗(而非欧氏距离)决定。 这表示只是路径形状不同,而对网络进行的基础遍历则相同。
TRUE_LINES_WITH_MEASURES—输出路径将具有基础网络源的精确形状。 输出包含路径测量值以用于线性参考。 测量值从第一个停靠点增加并将记录到达指定位置的累积阻抗。
TRUE_LINES_WITHOUT_MEASURES—输出路径将具有基础网络源的精确形状。
STRAIGHT_LINES—输出路径形状为两个停靠点之间的一条直线。
NO_LINES—将不会为输出路径生成任何形状。
|
String
|
|
start_date_time
(可选)
|
路径的开始日期和时间。 路径开始时间通常用于查找阻抗属性随当日时间变化的路径。 例如,开始时间早上 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
例如,要指定行程从星期二 5:00 p.m. 开始,则请将该参数值指定为 1/2/1900 5:00 PM。
求解结束后,在输出路径中填充路径的开始时间与结束时间。 也会在生成方向时使用这些开始时间和结束时间。
了解如何在网络分析中使用和解释日期和时间的详细信息
|
Date
|
派生输出
|
名称
|
说明
|
数据类型
|
|
output_layer
|
新创建的网络分析图层。
|
Network Analyst Layer
|
代码示例
MakeRouteLayer 示例 1(Python 窗口)
仅使用必需参数运行该工具。
network = "C:/Data/SanFrancisco.gdb/Transportation/Streets_ND"
arcpy.na.MakeRouteLayer(network, "WorkRoute", "TravelTime")
MakeRouteLayer 示例 2(Python 窗口)
使用所有参数运行该工具。
network = "C:/Data/SanFrancisco.gdb/Transportation/Streets_ND"
arcpy.na.MakeRouteLayer(network, "InspectionRoute", "TravelTime",
"FIND_BEST_ORDER", "PRESERVE_BOTH", "USE_TIMEWINDOWS",
["Meters", "TravelTime"],
"ALLOW_DEAD_ENDS_AND_INTERSECTIONS_ONLY", ["Oneway"],
"USE_HIERARCHY", "", "TRUE_LINES_WITH_MEASURES",
"1/1/1900 9:00 AM")
以下独立 Python 脚本演示了如何使用 MakeRouteLayer 函数查找访问地理编码停靠点位置的最佳路径。
# Name: MakeRouteLayer_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"
impedance = "TravelTime"
address_locator = os.path.join(input_gdb, "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.MakeRouteLayer(network, layer_name, impedance)
#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, "",
exclude_restricted_elements="EXCLUDE")
#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))
本例在单次求解中创建多条路径,常用于计算起始点-目的地对间的距离或行驶时间。
# Name: MakeRouteLayer_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"
impedance = "TravelTime"
#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 TravelTime, but compute the
#distance traveled by accumulating the Meters attribute.
result_object = arcpy.na.MakeRouteLayer(network, layer_name, impedance,
accumulate_attribute_name=["Meters"],
hierarchy="NO_HIERARCHY",
start_date_time=start_time)
#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, "",
exclude_restricted_elements = "EXCLUDE")
arcpy.na.AddLocations(layer_object, stops_layer_name, stops_work,
field_mappings, "", append="APPEND",
exclude_restricted_elements = "EXCLUDE")
#Solve the route layer.
arcpy.na.Solve(layer_object)
# Get the output Routes sublayer and save it to a feature class
routes_sublayer = layer_object.listLayers(routes_layer_name)[0]
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))