摘要
返回指定网络分析图层的指定子图层或子表的 Layer 或 Table 对象。该对象可用作输入以进行进一步分析。
例如,可使用此方法检索“服务区”分析图层的“面”子图层。
注:
此 Python 函数类似于选择数据 ModelBuilder 工具。
旧版本:
此函数已在 ArcGIS Pro 2.7 中引入。在以前的软件版本中,用于检索网络分析图层的子图层对象的最佳方法是使用网络分析 Layer 对象的 listLayers 方法,该方法将子图层名称用作通配符。
语法
GetNASublayer (network_analyst_layer, naclass_name)
参数 | 说明 | 数据类型 | ||||||||||||||
network_analyst_layer | 要从中检索子图层的网络分析图层,引用为图层对象或在其图层名称中引用。 | Layer | ||||||||||||||
naclass_name | 要检索的子图层或子表的类名称。 下表列出了每种网络分析图层类型的类名称:
| String |
代码示例
检索名为 Tuesday deliveries 的车辆配送分析图层的“路径特殊要求”子表。
tuesday = datetime.datetime(1900, 1, 2)
layer_name = "Tuesday deliveries"
vrp_layer_object = arcpy.na.MakeVehicleRoutingProblemAnalysisLayer(
"Streets_ND", layer_name, "Driving Time",
default_date=tuesday).getOutput(0)
route_specialties_table = arcpy.na.GetNASublayer(layer_name, "RouteSpecialties")
下面的独立 Python 脚本演示了如何在 OD 成本矩阵分析工作流中访问子图层、如何连接输入和输出图层以及如何将输入源和目的地的字段值传输至输出线图层。
# Name: MakeODCostMatrixAnalysisLayer_Workflow2.py
# Description: Find the travel time to the closest hospital from each census
# tract and join the travel time and hospital name to the input
# tracts.
# Requirements: Network Analyst Extension
# Import system modules
import datetime
import os
import arcpy
try:
# Check out Network Analyst license if available. Fail if the Network Analyst license is not available.
if arcpy.CheckExtension("network") == "Available":
arcpy.CheckOutExtension("network")
else:
raise arcpy.ExecuteError("Network Analyst Extension license is not available.")
# Set environment settings
output_dir = "C:/Data"
#The NA layer's data will be saved to the workspace specified here
arcpy.env.workspace = os.path.join(output_dir, "Output.gdb")
arcpy.env.overwriteOutput = True
# Set inputs and outputs
input_gdb = "C:/Data/SanFrancisco.gdb"
network = os.path.join(input_gdb, "Transportation", "Streets_ND")
origins = os.path.join(input_gdb, "Analysis", "TractCentroids")
destinations = os.path.join(input_gdb, "Analysis", "Hospitals")
output_features = "TractCentroids_withOD"
# Define some OD cost matrix analysis settings
layer_name = "HospitalsOD"
# User settings for driving
travel_mode = "Driving Time"
# Calculate the total distance, even though the analysis is optimizing time
accumulate_attributes = ["Meters"]
# Find only the closest hospital
num_hospitals_to_find = 1
# Set the time of day for the analysis to 6PM on a generic Monday.
start_time = datetime.datetime(1900, 1, 1, 18, 0, 0)
# Don't output line shapes (output Lines will still list travel times)
out_lines = "NO_LINES"
# Create a new OD cost matrix layer.
result_object = arcpy.na.MakeODCostMatrixAnalysisLayer(network, layer_name,
travel_mode,
number_of_destinations_to_find=num_hospitals_to_find,
time_of_day=start_time, line_shape=out_lines,
accumulate_attributes=accumulate_attributes)
# Get the layer object from the result object. The OD layer can
# now be referenced using the layer object.
layer_object = result_object.getOutput(0)
# Get the names of all the sublayers within the OD layer.
sublayer_names = arcpy.na.GetNAClassNames(layer_object)
# Store the layer names for later use
origins_layer_name = sublayer_names["Origins"]
destinations_layer_name = sublayer_names["Destinations"]
# The input census tract data has a unique ID field that can be transferred
# to the analysis layer. Add the field, and then use field mapping to
# transfer the values.
arcpy.na.AddFieldToAnalysisLayer(layer_object, origins_layer_name,
"Tract_ID", "TEXT")
field_mappings = arcpy.na.NAClassFieldMappings(layer_object,
origins_layer_name)
field_mappings["Tract_ID"].mappedFieldName = "ID"
# Load the census tracts as origins.
arcpy.na.AddLocations(layer_object, origins_layer_name, origins,
field_mappings, "")
# Map the input hospital NAME field to a new Hospital_Name field in
# Destinations
arcpy.na.AddFieldToAnalysisLayer(layer_object, destinations_layer_name,
"Hospital_Name", "TEXT")
field_mappings = arcpy.na.NAClassFieldMappings(layer_object,
destinations_layer_name)
field_mappings["Hospital_Name"].mappedFieldName = "NAME"
# Load the hospitals as desinations.
arcpy.na.AddLocations(layer_object, destinations_layer_name, destinations,
field_mappings, "")
# Solve the OD layer
arcpy.na.Solve(layer_object)
# Get sublayers
origins_sublayer = arcpy.na.GetNASublayer(layer_object, "Origins")
destinations_sublayer = arcpy.na.GetNASublayer(layer_object, "Destinations")
lines_sublayer = arcpy.na.GetNASublayer(layer_object, "ODLines")
# Use the JoinField tool to transfer OD Cost Matrix information to the
# output feature class
# Transfer the tract ID from the input Origins to the output Lines
arcpy.management.JoinField(lines_sublayer, "OriginID",
origins_sublayer, "ObjectID", "Tract_ID")
# Transfer the hospital name from the input Destinations to the output Lines
arcpy.management.JoinField(lines_sublayer, "DestinationID",
destinations_sublayer, "ObjectID", "Hospital_Name")
# Transfer fields of interest (hospital name, impedance attribute, and other
# accumulated costs) from the output Lines to a copy of the input census
# tracts feature class using the Tract_ID field
# Determine the impedance attribute
solver_props = arcpy.na.GetSolverProperties(layer_object)
impedance = solver_props.impedance
output_impedance_fieldname = "Total_" + impedance
fields_to_transfer = ["Hospital_Name", output_impedance_fieldname]
for field in accumulate_attributes:
fields_to_transfer.append("Total_" + field)
arcpy.management.CopyFeatures(origins, output_features)
arcpy.management.JoinField(output_features, "ID",
lines_sublayer, "Tract_ID", fields_to_transfer)
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))