添加位置 (Network Analyst)

许可等级:BasicStandardAdvanced

摘要

向网络分析图层添加网络分析对象。向特定子图层(如“停靠点”图层和“障碍”图层)添加对象。对象将作为要素或记录输入。

用法

语法

AddLocations_na (in_network_analysis_layer, sub_layer, in_table, field_mappings, search_tolerance, {sort_field}, {search_criteria}, {match_type}, {append}, {snap_to_position_along_network}, {snap_offset}, {exclude_restricted_elements}, {search_query})
参数说明数据类型
in_network_analysis_layer

要添加网络分析对象的网络分析图层。

Network Analyst Layer
sub_layer

要添加网络分析对象的网络分析图层的子图层。

String
in_table

充当新建网络分析对象的源的要素类或表。

Table View
field_mappings

设置网络分析对象的属性值。属性可设置为常量或映射到输入要素类或表中的字段。

NAClassFieldMappings 类中获得的 NAClassFieldMappings 对象用于指定参数值。NAClassFieldMappings 对象是 NAClassFieldMap 对象的集合,允许您指定默认值或从网络分析对象属性的输入要素中映射字段名。如果正在加载的数据含有用于分析的网络数据集的网络位置或位置范围,请将网络位置字段从输入要素映射到网络位置属性。在字段映射中指定网络位置字段与使用工具对话框中的使用网络位置字段代替几何参数相类似。

警告警告:

如果为其中的任意一个网络位置属性指定字段映射,那么您需要为其余的网络位置属性指定字段映射,以避免工具执行错误。

Network Analyst Class FieldMap
search_tolerance

在网络上定位输入要素所需的搜索容差。搜索容差以外的要素将保持未定位状态。该参数包括容差的值和单位。

将位置添加到线障碍或面障碍子图层时,不使用该参数。在这种情况下,使用 "#" 作为参数值。

Linear unit
sort_field
(可选)

当网络分析对象被添加到网络分析图层时用于排序网络分析对象的字段。默认设置为输入要素类或表中的 ObjectID 字段。

Field
search_criteria
[[Source, SnapType],...]
(可选)

指定查找位置时将搜索网络数据集中的哪些源以及将使用几何的哪个部分(也称为捕捉类型)。

该参数值被指定为具有嵌套列表的列表。嵌套列表由两个值组成,分别表示每个网络源的名称和捕捉类型。捕捉类型通过关键字“SHAPE”、“MIDDLE”、“END”或“NONE”来指定。

  • SHAPE - 点将定位于此网络源中元素的最近点。
  • MIDDLE - 点将定位于此网络源中元素的最近的中点。
  • END - 点将定位于此网络源中元素的最近的端点。
  • NONE - 点将不会定位于此网络源中的元素。
例如,查找位置时,参数值 [["Streets","SHAPE"],["Streets_ND_Junctions","NONE"]] 指定搜索可定位于 Streets 源的形状,而不可定位于 Streets_ND_Junctions 源。

要为单一网络源指定多个捕捉类型,请使用捕捉类型关键字的组合形式,用下划线将关键字分开。例如,MIDDLE_END 指定位置可以捕捉到网络源的中部或末尾。

添加线网络位置或面网络位置时,即使已经指定了其他捕捉类型,也仅使用 Shape 捕捉类型。

未包含在本列表中的任何网络源将使用其默认捕捉类型。最可靠的做法是将所有网络源包含在列表中并为每个网络源明确地设置捕捉类型。

Value Table
match_type
(可选)
  • MATCH_TO_CLOSEST将新网络位置与所有已在搜索条件中指定捕捉类型的源中的最近的网络源匹配。这是默认设置。
  • PRIORITY将新网络位置与第一个在搜索条件中指定捕捉类型的网络源匹配。这些源按照优先级顺序进行搜索;当在搜索容差内找到位置时,搜索停止。

将位置添加到线障碍或面障碍子图层时,不使用该参数。在这种情况下,使用 "#" 作为参数值。

Boolean
append
(可选)

指定是否将新网络分析对象追加到现有的对象。

  • APPEND将新的网络分析对象追加到所选子图层中的现有对象集中。
  • CLEAR将删除现有的网络分析对象并使用新的对象来代替。
Boolean
snap_to_position_along_network
(可选)

指定想要沿着网络数据集或以离网络数据集的指定偏移量捕捉网络位置。

  • NO_SNAP 网络位置的几何将基于输入要素的几何。如果想使用要求清楚网络位置处于边的哪一侧的路边通道,则这一点将非常有用。
  • SNAP 如果有点要素,则点将被捕捉到网络,您将无法使用路边通道。如果您不关心车辆如何接近停靠点,则这一点将很有用。如果输入要素是线或面,请使用此参数值。

将位置添加到线障碍或面障碍子图层时,不使用该参数。在这种情况下,使用 "#" 作为参数值。

Boolean
snap_offset
(可选)

将点捕捉到网络时,您可以应用偏移距离。偏移距离为零表示点将与网络要素(通常是线)重合。要使点偏离网络要素,请输入偏移距离。偏移与起始点的位置有关;即,如果起始点在左侧,它的新位置就会向左偏移。如果起始点在右侧,它的新位置就会向右偏移。

将位置添加到线障碍或面障碍子图层时,不使用该参数。在这种情况下,使用 "#" 作为参数值。

Linear unit
exclude_restricted_elements
(可选)
  • EXCLUDE指定将网络位置仅放置在网络的可遍历部分。这样可防止将网络位置放在因限制或障碍而无法到达的元素上。使用该选项添加网络位置之前,请确保已经向输入网络分析图层添加了所有限制型障碍,以得到预期结果。添加障碍对象时,此参数不适用。在这种情况下,使用 "#" 作为参数值。
  • INCLUDE指定将网络位置放置在网络的所有元素上。如果通过该选项添加的网络位置被放置在受限元素上,那么在求解过程中,可能无法到达这些网络位置。
Boolean
search_query
[[Source, Expression],...]
(可选)

指定查询以将搜索范围限制在源要素类的要素子集内。这在不想查找可能不适合网络位置的要素时很有用。例如,如果正在加载面的质心,但不想定位在地方道路上,则可定义一个仅搜索主要道路的查询。

该参数值被指定为具有嵌套列表的列表。嵌套列表由两个值组成,分别表示所有网络源的名称和 SQL 表达式。对于不同的网络源要素类类型,SQL 表达式的语法略有不同。例如,如果要查询存储在文件或 ArcSDE 地理数据库中的源要素类、shapefile 或 SDC,需将字段名用双引号括起:"CFCC"。如果要查询存储在个人地理数据库中的源要素类,需将字段用方括号括起:[CFCC]

如果您不想指定特定源的查询,请使用 "#" 作为 SQL 表达式的值,或者将源名称和 SQL 表达式从参数值中排除。如果您不想指定所有网络源的查询,请使用 "#" 作为参数值。

例如,参数值 [["Streets","\"CFCC\" = 'A15'"], ["Streets_ND_Junctions",""]] 指定了 Streets 源要素类的 SQL 表达式,而没有指定 Streets_ND_Junctions 源要素类的表达式。注意:用于将字段名 CFCC 括起的双引号通过反斜杠字符进行转义,以避免 Python 解释程序出现分析错误。

Value Table

代码实例

添加位置 (AddLocations) 示例 1(Python 窗口)

仅使用必需参数执行此工具。

import arcpy
arcpy.env.workspace = "C:/ArcTutor/Network Analyst/Tutorial/SanFrancisco.gdb"
arcpy.na.AddLocations("Route","Stops","Analysis/Hospital","","")
添加位置 (AddLocations) 示例 2(Python 窗口)

使用所有参数执行此工具。

import arcpy
arcpy.env.workspace = "C:/ArcTutor/Network Analyst/Tutorial/SanFrancisco.gdb"
arcpy.na.AddLocations("Route","Stops","Analysis/Hospital",
                      "Name Name #;Attr_Minutes VisitTime 0;CurbApproach # 0",
                      "2 Miles","FID",
                      [["Streets","SHAPE"],["Streets_ND_Junctions","NONE"]],
                      "MATCH_TO_CLOSEST","CLEAR","SNAP","10 Feet","EXCLUDE",
                      [["Streets",'"FREEWAY" = 0'],["Streets_ND_Junctions",""]])
添加位置 (AddLocations) 示例 3(工作流)

下述独立 Python 脚本演示了如何使用添加位置 (AddLocations) 工具将起始点和目的地点加载到 OD 成本矩阵图层中。

# Name: AddLocations_Workflow.py
# Description: The scenario shows how to calculate a travel time matrix between 
#              store features. Use the Add Locations tool to load origins and 
#              destinations into an OD Cost Matrix layer. Since the origins and 
#              destinations are the same in this case, the origins are first loaded 
#              from the store features using their geometry and the destinations
#              are loaded from the origins using network location fields to 
#              speed up the load times. 
# 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/SanFrancisco.gdb"
    env.overwriteOutput = True
    
    #Set local variables
    inNetworkDataset = "Transportation/Streets_ND"
    outNALayerName = "StoreTravelTimeMatrix"
    impedanceAttribute = "TravelTime"
    inFeatures = "Analysis/Stores"
    searchTolerance = "500 Meters"
    searchQuery = [["Streets",'"FREEWAY" = 0'],["Streets_ND_Junctions",""]]
    odOrigins = "Origins"
    outLayerFile = "C:/data/output" + "/" + outNALayerName + ".lyr"
    
    #Create a new OD cost matrix analysis layer. For this scenario, the default 
    #value for all the remaining parameters statisfies the analysis requirements
    outNALayer = arcpy.na.MakeODCostMatrixLayer(inNetworkDataset, outNALayerName,
                                   impedanceAttribute)
    
    #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[odOrigins]
    destinationsLayerName = subLayerNames["Destinations"]
    
    #Get the origin layer object from within the OD cost matrix layer
    #The first layer returned by ListLayers is the OD cost matrix layer itself
    #which we don't want to use.
    for layer in arcpy.mapping.ListLayers(outNALayer)[1:]:
        if layer.datasetName == odOrigins:
            originsLayer = layer
            break
    else:
        raise Exception("Failed to get the origins layer object.")
    
    #Load store features as origins using the geometry of store features.
    #Ensure that the stores are not located on freeways by using a search query.
    #The OD cost matrix layer created previously is referred using its name. 
    arcpy.na.AddLocations(outNALayer,originsLayerName,inFeatures,"",
                          searchTolerance, exclude_restricted_elements = "EXCLUDE",
                          search_query = searchQuery)
    
    #Load origins as destinations using the network locations fields from 
    #origins. This is much faster than loading destinations from the store 
    #features using their geometry as their network locations have already been
    #calculated while loading them origins.
    #Create a field mappings object that supports network location fields from 
    #origins layer using the candidate fields from origins
    originsLayer = arcpy.management.MakeFeatureLayer(originsLayer,
                                                     outNALayerName+originsLayerName)
    candidateFields = arcpy.ListFields(originsLayer.getOutput(0))
    fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer,
                                                  destinationsLayerName, True,
                                                  candidateFields)
    arcpy.na.AddLocations(outNALayer,destinationsLayerName,originsLayer,
                          fieldMappings,"")
    
    #Solve the od cost matrix layer. Halt the execution if there is an 
    #invalid location 
    arcpy.na.Solve(outNALayer,"HALT")
    
    #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: 是
ArcGIS for Desktop Standard: 是
ArcGIS for Desktop Advanced: 是
5/10/2014