摘要
创建起始-目的地 (OD) 成本矩阵网络分析图层并设置其分析属性。OD 成本矩阵分析图层对于描述从一组起始位置到一组目的地位置的成本矩阵十分有用。
用法
语法
MakeODCostMatrixLayer_na (in_network_dataset, out_network_analysis_layer, impedance_attribute, {default_cutoff}, {default_number_destinations_to_find}, {accumulate_attribute_name}, {UTurn_policy}, {restriction_attribute_name}, {hierarchy}, {hierarchy_settings}, {output_path_shape}, {time_of_day})
参数 | 说明 | 数据类型 |
in_network_dataset | 要执行 OD 成本矩阵分析的网络数据集。 | Network Dataset Layer |
out_network_analysis_layer | 要创建的 OD 成本矩阵网络分析图层的名称。 | String |
impedance_attribute | 分析过程中用作阻抗的成本属性。 | String |
default_cutoff (可选) | 中断为指定起始点搜索目的地时对应的默认阻抗值。如果累积的阻抗大于中断值,则停止遍历。可通过指定起始点的中断值来覆盖此默认值。 | Double |
default_number_destinations_to_find (可选) | 要为每个起始点查找的默认目的地数。可通过为起始点的 TargetDestinationCount 属性指定一个值来覆盖默认值。 | Long |
accumulate_attribute_name [accumulate_attribute_name,...] (可选) | 分析过程中要累积的成本属性的列表。这些累积属性仅供参考;求解程序仅使用阻抗属性参数所指定的成本属性来计算路径。 对于每个累积的成本属性,均会向求解程序所输出的路径中添加一个 Total_[阻抗] 属性。 | String |
UTurn_policy (可选) | 交汇点的 U 形转弯策略。允许 U 形转弯表示求解程序可以在交汇点处转向并沿同一街道往回行驶。考虑到交汇点表示街道交叉路口和死角,不同的车辆可以在某些交汇点转弯,而在其他交汇点则不行 - 这取决于交汇点是交叉路口还是死角。为适应此情况,U 形转弯策略参数由连接到交汇点的边数隐性指定,这称为交汇点价。此参数可接受的值如下所列;每个值的后面是根据交汇点价对其含义的描述。
如果您需要定义更加精确的 U 形转弯策略,可以考虑在网络成本属性中添加一个通用转弯延迟赋值器,或者如果存在的话,调整其设置,并特别注意反向转弯的配置。还要考虑设置网络位置的 CurbApproach 属性。 | String |
restriction_attribute_name [restriction_attribute_name,...] (可选) | 分析过程中要应用的限制属性的列表。 | String |
hierarchy (可选) |
如果未在用于执行分析的网络数据集中定义等级属性,该参数将不可用。在这种情况下,使用 "#" 作为参数值。 | Boolean |
hierarchy_settings (可选) | Network Analyst Hierarchy Settings | |
output_path_shape (可选) |
无论选择何种输出形状类型,最佳路径始终由网络阻抗(而非欧氏距离)决定。只是对路径形状的表现不同,而对网络进行的基础遍历则相同。 | String |
time_of_day (可选) | 指示从起始点出发的时间。 如果您已经选择了基于流量的阻抗属性,将会根据特定的某天某时的动态交通状况来生成解决方案。可将日期和时间指定为 5/14/2012 10:30 AM。 可使用以下日期来指定一周中的每一天,而无需使用特定的日期:
| Date |
派生输出
名称 | 说明 | 数据类型 |
output_layer |
代码示例
MakeODCostMatrixLayer 示例 1(Python 窗口)
仅使用必需参数执行此工具。
network = "C:/Data/Paris.gdb/Transportation/ParisMultimodal_ND"
arcpy.na.MakeODCostMatrixLayer(network, "DrivetimeCosts", "DriveTime")
MakeODCostMatrixLayer 示例 2(Python 窗口)
使用所有参数执行此工具。
network = "C:/Data/Paris.gdb/Transportation/ParisMultimodal_ND"
arcpy.na.MakeODCostMatrixLayer(network, "DrivetimeCosts", "DriveTime", 10, 20,
["Meters", "DriveTime"], "NO_UTURNS",
["Oneway"], "USE_HIERARCHY", "", "NO_LINES")
MakeODCostMatrixLayer 示例 3(工作流)
以下独立 Python 脚本演示了如何使用 MakeODCostMatrixLayer 工具创建起始-目的地成本矩阵,用于将货物从仓库交付给距离仓库十分钟行程范围内的所有商店。
# Name: MakeODCostMatrixLayer_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
try:
#Check out the Network Analyst extension license
arcpy.CheckOutExtension("Network")
#Set environment settings
env.workspace = "C:/data/Paris.gdb"
env.overwriteOutput = True
#Set local variables
inNetworkDataset = "Transportation/ParisMultimodal_ND"
outNALayerName = "WarehouseToStoreDrivetimeMatrix"
impedanceAttribute = "Drivetime"
searchTolerance = "1000 Meters"
accumulateAttributeName = ["Meters"]
inOrgins = "Analysis/Warehouses"
inDestinations = "Analysis/Stores"
outLayerFile = "C:/data/output" + "/" + outNALayerName + ".lyr"
#Create a new OD Cost matrix layer. We wish to find all stores within a 10
#minute cutoff. Apart from finding the drive time to the stores, we also
#want to find the total distance. So we will accumulate the "Meters"
#impedance attribute.
outNALayer = arcpy.na.MakeODCostMatrixLayer(inNetworkDataset, outNALayerName,
impedanceAttribute, 10, "",
accumulateAttributeName)
#Get the layer object from the result object. The OD cost matrix layer can
#now be referenced using the layer object.
outNALayer = outNALayer.getOutput(0)
#Get the names of all the sublayers within the OD cost matrix layer.
subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
#Stores the layer names that we will use later
originsLayerName = subLayerNames["Origins"]
destinationsLayerName = subLayerNames["Destinations"]
#Load the warehouse locations as origins using a default field mappings and
#a search tolerance of 1000 Meters.
arcpy.na.AddLocations(outNALayer, originsLayerName, inOrgins, "",
searchTolerance)
#Load the store locations as destinations and map the NOM field from stores
#features as Name property using field mappings
fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, destinationsLayerName)
fieldMappings["Name"].mappedFieldName = "NOM"
arcpy.na.AddLocations(outNALayer, destinationsLayerName, inDestinations,
fieldMappings, searchTolerance)
#Solve the OD cost matrix layer
arcpy.na.Solve(outNALayer)
#Save the solved OD cost matrix layer as a layer file on disk with relative
#paths
arcpy.management.SaveToLayerFile(outNALayer,outLayerFile,"RELATIVE")
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)
MakeODCostMatrixLayer 示例 4(工作流)
下面的独立 Python 脚本演示了如何访问子图层、如何连接输入和输出图层以及如何将输入源和目的地的字段值传输至输出线图层。
# Name: MakeODCostMatrixLayer_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 datetime
#Import system modules
import arcpy
from arcpy import env
try:
#Check out the Network Analyst extension license
arcpy.CheckOutExtension("Network")
#Set environment settings
env.workspace = "C:/Data/SanFrancisco.gdb"
env.overwriteOutput = True
#Set inputs and outputs
inNetworkDataset = "Transportation/Streets_ND"
inOrigins = "Analysis/TractCentroids"
inDestinations = "Analysis/Hospitals"
outNALayerName = "HospitalsOD"
outTracts_withOD = "Analysis/TractCentroids_withOD"
#Define some OD cost matrix analysis settings
#Optimize based on travel time
impedanceAttribute = "TravelTime"
#Calculate the total distance, even though the analysis is optimizing time
accumulate_attrs = ["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.
outODResultObject = arcpy.na.MakeODCostMatrixLayer(inNetworkDataset,
outNALayerName, impedanceAttribute,
default_number_destinations_to_find=num_hospitals_to_find,
accumulate_attribute_name=accumulate_attrs,
output_path_shape=out_lines, time_of_day=start_time)
#Get the layer object from the result object. The OD layer can
#now be referenced using the layer object.
outNALayer = outODResultObject.getOutput(0)
#Get the names of all the sublayers within the OD layer.
subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
#Store the layer names for later use
originsLayerName = subLayerNames["Origins"]
destinationsLayerName = subLayerNames["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(outNALayer, originsLayerName,
"Tract_ID", "TEXT")
fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, originsLayerName)
fieldMappings["Tract_ID"].mappedFieldName = "ID"
#Load the census tracts as origins.
arcpy.na.AddLocations(outNALayer, originsLayerName, inOrigins,
fieldMappings, "",
exclude_restricted_elements = "EXCLUDE")
#Map the input hospital NAME field to a new Hospital_Name field in
#Destinations
arcpy.na.AddFieldToAnalysisLayer(outNALayer, destinationsLayerName,
"Hospital_Name", "TEXT")
fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer,
destinationsLayerName)
fieldMappings["Hospital_Name"].mappedFieldName = "NAME"
#Load the hospitals as desinations.
arcpy.na.AddLocations(outNALayer, destinationsLayerName, inDestinations,
fieldMappings, "",
exclude_restricted_elements = "EXCLUDE")
#Solve the OD layer
arcpy.na.Solve(outNALayer)
#Get sublayers
#arcpy.mapping.ListLayers returns a list of layer objects containing the NA
#layer itself (item 0) and each of the sublayers. Put these in a dictionary
#with the sublayer names as the keys
subLayers = dict((lyr.datasetName, lyr) for lyr in arcpy.mapping.ListLayers(outNALayer)[1:])
OriginsSubLayer = subLayers["Origins"]
DestinationsSubLayer = subLayers["Destinations"]
LinesSubLayer = subLayers["ODLines"]
#Transfer the tract ID from the input Origins to the output Lines
arcpy.management.JoinField(LinesSubLayer, "OriginID",
OriginsSubLayer, "ObjectID", "Tract_ID")
#Transfer the hospital name from the input Destinations to the output Lines
arcpy.management.JoinField(LinesSubLayer, "DestinationID",
DestinationsSubLayer, "ObjectID", "Hospital_Name")
#Transfer fields of interest (hospital name, TravelTime cost, and other
#accumulated costs) from the output Lines to the input census tracts
#feature class using the Tract_ID field
output_impedance_fieldname = "Total_" + impedanceAttribute
fields_to_transfer = ["Hospital_Name", output_impedance_fieldname]
for field in accumulate_attrs:
fields_to_transfer.append("Total_" + field)
arcpy.management.CopyFeatures(inOrigins, outTracts_withOD)
arcpy.management.JoinField(outTracts_withOD, "ID",
LinesSubLayer, "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)
环境
许可信息
- ArcGIS Desktop Basic: 是
- ArcGIS Desktop Standard: 是
- ArcGIS Desktop Advanced: 是