复制遍历源要素 (Network Analyst)

许可等级:BasicStandardAdvanced

摘要

创建两个要素类和一个表,它们组合在一起以包含求解网络分析图层时所遍历的边、交汇点和转弯的信息。

了解“复制遍历源要素”的输出

用法

语法

CopyTraversedSourceFeatures_na (input_network_analysis_layer, output_location, edge_feature_class_name, junction_feature_class_name, turn_table_name)
参数说明数据类型
input_network_analysis_layer

将复制遍历源要素的网络分析图层。如果网络分析图层没有有效结果,则会通过求解图层以生成有效结果。

Network Analyst Layer
output_location

用来保存输出表和两个要素类的工作空间。

Workspace; Feature Dataset
edge_feature_class_name

将包含遍历边源要素相关信息的要素类的名称。如果求解的网络分析图层不遍历任何边要素,则会创建空要素类。

String
junction_feature_class_name

将包含遍历交汇点源要素(包括输入网络分析图层中的系统交汇点和相关点)相关信息的要素类的名称。如果求解的网络分析图层不遍历任何交汇点,则会创建空要素类。

String
turn_table_name

将包含基础边成本按比例增加的遍历通用转弯和转弯要素相关信息的表的名称。如果求解的网络分析图层不遍历任何转弯,则会创建空表。因为始终不会遍历受限制的转弯,因此它们始终不会包含在输出中。

String

代码实例

CopyTraversedSourceFeatures 示例 1(Python 窗口)

以下 Python 窗口脚本演示了如何使用 CopyTraversedSourceFeatures 工具在内存工作空间中将遍历的边、交汇点和转弯从“路径”网络分析图层写入到要素类和表中。

import arcpy
arcpy.na.CopyTraversedSourceFeatures("Route","in_memory",
                                     "TraversedEdges",
                                     "TraversedJunctions",
                                     "TraversedTurns")
CopyTraversedSourceFeatures 示例 2(工作流)

以下独立 Python 脚本演示了如何使用 CopyTraversedSourceFeatures 来查找从人口普查区域质心到最近消防站的路径的共有街道。这些结果有助于确定在紧急情况下使用最为频繁的街道。

# Name: CopyTraversedSourceFeatures_ex02.py
# Description: The scenario shows how to find the streets that are common to the
#              routes between the closest fire station and the census tract
#              centroids. These streets can be used to identify critical points
#              in case of an emergency. 
# Requirements: Network Analyst Extension 

#Import system modules
import os
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 local variables
    inNetworkDataset = "Transportation/Streets_ND"
    outNALayerName = "EmergencyRoutes"
    impedanceAttribute = "TravelTime"
    inFacilities = "Analysis/FireStations"
    inIncidents = "Analysis/TractCentroids"
    edgeFrequency = "in_memory/EdgeFrequency"
    outLayerFile = "C:/data/output" + "/" + outNALayerName + ".lyr"
    outFeatures = "CriticalStreets"
    
    #Create a new closest facility analysis layer. For this scenario, the default 
    #value for all the remaining parameters statisfies the analysis requirements
    outNALayer = arcpy.na.MakeClosestFacilityLayer(inNetworkDataset, outNALayerName,
                                                   impedanceAttribute, "TRAVEL_FROM")
    
    #Get the layer object from the result object. The closest facility layer can 
    #now be referenced using the layer object.
    outNALayer = outNALayer.getOutput(0)
    
    #Get the names of all the sublayers within the closest facility layer.
    subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
    #Stores the layer names that we will use later
    facilitiesLayerName = subLayerNames["Facilities"]
    incidentsLayerName = subLayerNames["Incidents"]
    
    #Load fire station features as facilities and ensure that they are not
    #located on restricted portions of the network. Use default field mappings
    #and search tolerance
    arcpy.na.AddLocations(outNALayer,facilitiesLayerName,inFacilities,"", "",
                          exclude_restricted_elements = "EXCLUDE")
    
    #Load tract centroids as incidents and ensure that they are not located on
    #restricted portions of the network. Map the ID field from Tract Centroids
    #as the name for incidents using field mappings
    fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, incidentsLayerName)
    fieldMappings['Name'].mappedFieldName = "ID"
    arcpy.na.AddLocations(outNALayer,incidentsLayerName, inIncidents,
                          fieldMappings,"", exclude_restricted_elements = "EXCLUDE")
    
    #Solve the closest facility layer and copy the travered source features to a
    #temporary in-memory workspace. Use default names for the output feature
    #classes and table. Get only the first output which are the edges traversed.
    traversedEdges = arcpy.na.CopyTraversedSourceFeatures(outNALayer,
                                                          "in_memory").getOutput(0)
    
    #Calculate the frequency of SourceOID in the traversed edges
    arcpy.analysis.Frequency(traversedEdges, edgeFrequency,
                             ["SourceOID", "SourceName"])
    
    #Get the full path to the streets feature class by describing the network
    #dataset referenced by the network analysis layer. 
    network = arcpy.Describe(outNALayer.dataSource)
    edgeSources = network.edgeSources
    for es in edgeSources:
        if es.name.lower() == "streets":
            streetsSource = os.path.join(os.path.dirname(network.catalogPath),
                                         es.name)
            break
    else:
        raise Exception("Failed to detrmine the path for the streets feature class")
    
    #Join the frequency field to the streets feature class. In order to speed up
    #the join select the streets that share a line segment with traversed streets.
    streetsLayer = "StreetsLayer"
    arcpy.management.MakeFeatureLayer(streetsSource,streetsLayer)
    arcpy.management.SelectLayerByLocation(streetsLayer, "SHARE_A_LINE_SEGMENT_WITH",
                                           traversedEdges)
    arcpy.management.JoinField(streetsLayer, "ObjectID", edgeFrequency,
                               "SourceOID", "FREQUENCY")
    
    #Copy the streets that have a frequency value to a new feature class.
    arcpy.management.SelectLayerByAttribute(streetsLayer, "SUBSET_SELECTION",
                                            "FREQUENCY IS NOT NULL")
    arcpy.management.CopyFeatures(streetsLayer,outFeatures)
    
    #Delete the Frequency field from the streets feature class
    arcpy.management.DeleteField(streetsLayer, "FREQUENCY")

    #Save the solved na 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 occured on line %i" % tb.tb_lineno
    print str(e)

环境

相关主题

许可信息

ArcGIS for Desktop Basic: 需要 Network Analyst
ArcGIS for Desktop Standard: 需要 Network Analyst
ArcGIS for Desktop Advanced: 需要 Network Analyst
5/10/2014