添加位置 (Network Analyst)
摘要
向网络分析图层添加网络分析对象。向特定子图层(如“停靠点”图层和“障碍”图层)添加对象。对象将作为要素或记录输入。
用法
- 
该工具可反复运行,以将多个网络分析对象追加到同一子图层中。例如,如果路径图层的停靠点来自于两个要素类,则该工具可通过 APPEND 选项调用两次。 
- 
如果想在加载新的网络分析对象前删除现有的网络分析对象,请使用 CLEAR 选项。 
- 
如果用作网络数据集中的网络源的要素类具有有效的最新的空间索引,那么该工具的运行速度将显著提高。 
- 添加移动点(例如从车辆上安装的接收器获得的 GPS 坐标)时,可使用方位角读数和方位角容差以更精确地在网络上定位点。 
语法
| 参数 | 说明 | 数据类型 | 
| 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”来指定。例如,查找位置时,参数值 [["Streets","SHAPE"],["Streets_ND_Junctions","NONE"]] 指定搜索可定位于 Streets 源的形状,而不可定位于 Streets_ND_Junctions 源。 要为单一网络源指定多个捕捉类型,请使用捕捉类型关键字的组合形式,用下划线将关键字分开。例如,MIDDLE_END 指定位置可以捕捉到网络源的中部或末尾。 添加线网络位置或面网络位置时,即使已经指定了其他捕捉类型,也仅使用 Shape 捕捉类型。 | Value Table | 
| match_type (可选) | 
 将位置添加到线障碍或面障碍子图层时,不使用该参数。在这种情况下,使用 "#" 作为参数值。 | Boolean | 
| append (可选) | 指定是否将新网络分析对象追加到现有的对象。 
 | Boolean | 
| snap_to_position_along_network (可选) | 指定想要沿着网络数据集或以离网络数据集的指定偏移量捕捉网络位置。 
 将位置添加到线障碍或面障碍子图层时,不使用该参数。在这种情况下,使用 "#" 作为参数值。 | Boolean | 
| snap_offset (可选) | 将点捕捉到网络时,您可以应用偏移距离。偏移距离为零表示点将与网络要素(通常是线)重合。要使点偏离网络要素,请输入偏移距离。偏移与起始点的位置有关;即,如果起始点在左侧,它的新位置就会向左偏移。如果起始点在右侧,它的新位置就会向右偏移。 将位置添加到线障碍或面障碍子图层时,不使用该参数。在这种情况下,使用 "#" 作为参数值。 | Linear unit | 
| exclude_restricted_elements (可选) | 
 | 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 | 
代码实例
仅使用必需参数执行此工具。
import arcpy
arcpy.env.workspace = "C:/ArcTutor/Network Analyst/Tutorial/SanFrancisco.gdb"
arcpy.na.AddLocations("Route","Stops","Analysis/Hospital","","")
使用所有参数执行此工具。
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",""]])
下述独立 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)