arcpy.na.MakeODCostMatrixAnalysisLayer(network_data_source, {layer_name}, {travel_mode}, {cutoff}, {number_of_destinations_to_find}, {time_of_day}, {time_zone}, {line_shape}, {accumulate_attributes}, {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
|
|
cutoff
(可选)
|
停止为指定起点搜索目的地时所对应的阻抗值。 该值将以所选出行模式使用的阻抗属性为单位。 无法找到超过此限制的目的地。 可通过在起始点子图层中指定单个中断值来逐个起始点覆盖中断值。 默认情况下分析不使用中断。
|
Double
|
|
number_of_destinations_to_find
(可选)
|
要为每个起始点查找的目的地数。 可通过为起始点子图层的 TargetDestinationCount 属性指定一个值来覆盖此默认值。 默认情况下无任何限制,可找到所有目的地。
|
Long
|
|
time_of_day
(可选)
|
从起始点出发的时间。
如果您选择基于流量的阻抗属性,将会根据特定的某天某时的动态交通状况来生成解决方案。 日期和时间可被指定为 5/14/2012 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
(可选)
|
时间参数的时区。
|
String
|
|
line_shape
(可选)
|
指定输出线形状。
无论指定的输出形状类型为何,最佳路径始终由网络阻抗(而非欧氏距离)决定。 这表示只是路径形状不同,而对网络进行的基础遍历则相同。
|
String
|
|
accumulate_attributes
[accumulate_attributes,...]
(可选)
|
分析过程中要累积的成本属性的列表。 这些累积属性仅供参考;求解程序仅使用求解分析时指定的出行模式所使用的成本属性。
对于每个累积的成本属性,会在网络分析输出要素中填充 Total_ [阻抗] 属性。
如果网络数据源为 ArcGIS Online 服务,或如果网络数据源是不支持累积的 Portal for ArcGIS 版本上的服务,则此参数不可用。
|
String
|
|
ignore_invalid_locations
(可选)
|
指定是否忽略无效的输入位置。 通常,如果无法 在网络上定位,则位置无效。 当无效位置被忽略时,求解程序将跳过它们并尝试使用剩余位置执行分析。
|
Boolean
|
|
analysis_geodatabase
(可选)
|
存储新网络分析图层的子图层的文件地理数据库工作空间。 当未指定此参数时,将在当前工作空间地理处理环境(通常为工程地理数据库)中创建子图层的数据。
了解有关网络分析图层的分析数据源的详细信息
|
Workspace
|
|
analysis_feature_dataset_name
(可选)
|
用于命名该工具将在分析地理数据库中创建的新要素数据集的名称,该数据集将用于存储网络分析图层的子图层数据。 如未指定,要素数据集将以随机唯一名称创建。
了解有关网络分析图层的分析数据源的详细信息
|
String
|
派生输出
|
名称
|
说明
|
数据类型
|
|
out_network_analysis_layer
|
新创建的网络分析图层。
|
Network Analyst Layer
|
代码示例
MakeODCostMatrixAnalysisLayer 示例 1(Python 窗口)
仅使用必需参数运行该工具。
network = "C:/Data/Paris.gdb/Transportation/ParisMultimodal_ND"
arcpy.na.MakeODCostMatrixAnalysisLayer(network, "DrivetimeCosts")
MakeODCostMatrixAnalysisLayer 示例 2(Python 窗口)
使用所有参数运行该工具。
network = "C:/Data/Paris.gdb/Transportation/ParisMultimodal_ND"
arcpy.na.MakeODCostMatrixAnalysisLayer(network, "DrivetimeCosts",
"Driving Time", 10, 20, "1/1/1900 9:00 AM",
"UTC", "NO_LINES", ["Meters", "TravelTime"])
MakeODCostMatrixAnalysisLayer 示例 3(工作流)
以下独立 Python 脚本演示了如何使用 MakeODCostMatrixAnalysisLayer 函数创建起始-目的地成本矩阵,用于将货物从仓库交付给距离仓库十分钟行程范围内的所有商店。
# Name: MakeODCostMatrixAnalysisLayer_Workflow.py
# Description: Create an origin-destination cost matrix for delivery of goods
# from the warehouses to all stores within a 10-minute drive time
# and save the results to a layer file on disk. Such a matrix can
# be used as an input for logistics, delivery and routing analyses.
# 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/Paris.gdb"
network = os.path.join(input_gdb, "Transportation", "ParisMultimodal_ND")
layer_name = "WarehouseToStoreDrivetimeMatrix"
travel_mode = "Driving Time"
search_tolerance = "1000 Meters"
origins = os.path.join(input_gdb, "Analysis", "Warehouses")
destinations = os.path.join(input_gdb, "Analysis", "Stores")
output_layer_file = os.path.join(output_dir, layer_name + ".lyrx")
#Create a new OD Cost matrix layer. We wish to find all stores within a 10
#minute cutoff.
result_object = arcpy.na.MakeODCostMatrixAnalysisLayer(network, layer_name,
travel_mode, 10)
#Get the layer object from the result object. The OD cost matrix 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 cost matrix layer.
sublayer_names = arcpy.na.GetNAClassNames(layer_object)
#Stores the layer names that we will use later
origins_layer_name = sublayer_names["Origins"]
destinations_layer_name = sublayer_names["Destinations"]
#Load the warehouse locations as origins using a default field mappings and
#a search tolerance of 1000 Meters.
arcpy.na.AddLocations(layer_object, origins_layer_name, origins, "",
search_tolerance)
#Load the store locations as destinations and map the NOM field from stores
#features as Name property using field mappings
field_mappings = arcpy.na.NAClassFieldMappings(layer_object,
destinations_layer_name)
field_mappings["Name"].mappedFieldName = "NOM"
arcpy.na.AddLocations(layer_object, destinations_layer_name, destinations,
field_mappings, search_tolerance)
#Solve the OD cost matrix layer
arcpy.na.Solve(layer_object)
#Save the solved OD cost matrix 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))
MakeODCostMatrixAnalysisLayer 示例 4(工作流)
下面的独立 Python 脚本演示了如何访问子图层、如何连接输入和输出图层以及如何将输入源和目的地的字段值传输至输出线图层。
旧版本:
GetNASublayer 函数用于调用网络分析图层的子图层。 它在 ArcGIS Pro 2.7 中引入。 在以前的软件版本中,用于检索网络分析图层的子图层对象的最佳方法是使用网络分析 Layer 对象的 listLayers 方法,该方法将子图层名称用作通配符。
# 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:
# 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))
MakeODCostMatrixAnalysisLayer 示例 5(独立脚本)
以下独立 Python 脚本演示了如何从网络数据集创建出行模式的修改版本,并在创建新的 OD 成本矩阵图层时使用此出行模式。
import json
network = r"C:/Data/SanFrancisco.gdb/Transportation/Streets_ND"
# Get all travel modes from the network dataset
travel_modes = arcpy.na.GetTravelModes(network)
# Get the Driving Distance travel mode
dd_travel_mode = travel_modes["Driving Distance"]
# Make a json representation of the travel mode
travel_mode_json = json.loads(str(dd_travel_mode))
# Modify the userHierarchy property to turn hierarchy off, and update the name
travel_mode_json["useHierarchy"] = False
travel_mode_json["name"] = "Driving Distance without Hierarchy"
# Create a new travel mode object from the modified json
new_travel_mode_object = arcpy.na.TravelMode(json.dumps(travel_mode_json))
# Use the new travel mode object to MakeODCostMatrixAnalysisLayer
# We could also pass in the json directly without first converting it to an object
arcpy.na.MakeODCostMatrixAnalysisLayer(network, "OD Without Hierarchy", new_travel_mode_object)