添加位置 (网络分析)
摘要
向网络分析图层添加网络分析对象。向特定子图层(如“停靠点”图层和“障碍”图层)添加对象。对象将作为要素或记录输入。
用法
-
该工具可反复运行将网络分析对象追加到同一子图层中。例如,如果路线图层的停靠点来自于两个要素类,则该工具可通过 APPEND 选项调用两次。
-
如果想在加载新的网络分析对象前删除现有的网络分析对象,请使用 CLEAR 选项。
-
如果用作网络数据集中的网络源的要素类具有有效且最新的空间索引,那么该工具的运行速度将显著提高。
语法
参数 | 说明 | 数据类型 |
in_network_analysis_layer |
要添加网络分析对象的网络分析图层。 | Network Analyst Layer |
sub_layer |
要添加网络分析对象的网络分析图层的子图层。 | String |
in_table |
充当新网络分析对象的源的要素类或表。 | Table View |
field_mappings |
Sets the values for the properties of the network analysis objects. Properties can be set to a constant or mapped to a field from the input feature class or table. 参数值被指定为一个由分号分开的,含有三个值且由属性名、字段名和默认值组成的列表。您应该指定属性的字段名或默认值。例如,参数值 "Name Address #; Attr_Minutes # 2" 指定出“名称”属性从“地址”字段映射且 Attr_Minutes 属性在添加输入要素时应该具有常量值 2。 如果某个特定属性没有映射到字段中或具有默认值或没有包含在参数值中,那么该属性将进行默认映射。如果想对所有属性使用默认映射,请将 # 用于参数值。 If both the Field and the Default Value are not specified for a property, the resulting network analysis objects will have null values for that property. 如果正在加载的数据含有基于用于分析的网络数据集的网络位置或位置范围,请将网络位置字段从输入要素映射到网络位置属性。对于点位置(如停靠点或事件点),映射 SourceID、SourceOID、PosAlong 和 SideOfEdge 属性;对于线位置或面位置(如面障碍),映射“位置”属性。使用网络位置字段来添加网络分析对象比按照几何加载网络分析对象快得多。在字段映射中指定网络位置字段与使用工具对话框中的使用网络位置字段代替几何参数相类似。 警告: 如果为其中的任意一个网络位置属性指定字段映射,您需要为其余的网络位置属性指定字段映射,以避免工具执行错误。 | Network Analyst Field Map |
search_tolerance |
The search tolerance for locating the input features on the network. Features that are outside the search tolerance are left unlocated. The parameter includes a value and units for the tolerance. 将位置添加到线障碍或面障碍子图层时,不使用该参数。在这种情况下,使用 "#" 作为参数值。 | Linear Unit |
sort_field (可选) |
当网络分析对象被添加到网络分析图层时存储网络分析对象的字段。默认设置为输入要素类或表中的 ObjectID 字段。 | Field |
search_criteria [[Source, SnapType],...] (可选) |
Specifies which sources in the network dataset will be searched when finding locations and what portions of geometry (also known as snap types) will be used. 该参数值被指定为具有嵌套列表的列表。嵌套列表由两个值组成,分别表示每个网络源的名称和捕捉类型。捕捉类型通过关键字“SHAPE”、“MIDDLE”、“END”或“NONE”来指定。例如,查找位置时,参数值 [["Streets","SHAPE"],["Streets_ND_Junctions","NONE"]] 指定搜索可定位于 Streets 源的形状,而不可定位于 Streets_ND_Junctions 源。 要为单一网络源指定多个捕捉类型,请使用捕捉类型关键字的组合形式,用下划线将关键字分开。例如,MIDDLE_END 指定位置可以捕捉到网络源的中部或末尾。 When adding line or polygon network locations, only the Shape snap type is used even if other snap types are specified. | Value Table |
match_type (可选) |
The parameter is not used when adding locations to the Line Barriers or Polygon Barriers sublayers. In such cases, use "#" as the parameter value. | Boolean |
append (可选) |
Specifies whether or not to append new network analysis objects to existing objects.
| Boolean |
snap_to_position_along_network (可选) |
Specifies that you want to snap the network locations along the network dataset or at some specified offset from the network dataset.
The parameter is not used when adding locations to the Line Barriers or Polygon Barriers sublayers. In such cases, use "#" as the parameter value. | Boolean |
snap_offset (可选) |
When snapping a point to the network, you can apply an offset distance. An offset distance of zero means the point will be coincident with the network feature (typically a line). To offset the point from the network feature, enter an offset distance. The offset is in relation to the original point location. That is, if the original point was on the left side, its new location will be offset to the left. If it was originally on the right side, its new location will be offset to the right. The parameter is not used when adding locations to the Line Barriers or Polygon Barriers sublayers. In such cases, use "#" as the parameter value. | Long |
exclude_restricted_elements (可选) |
| Boolean |
search_query [[Source, Expression],...] (可选) | Specifies a query to restrict the search to a subset of the features within a source feature class. This is useful if you don't want to find features that may be unsuited for a network location. For example, if you are loading centroids of polygons and don't want to locate on local roads, you can define a query that searches for major roads only. 该参数值被指定为具有嵌套列表的列表。嵌套列表由两个值组成,分别表示所有网络源的名称和 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.AddLocations_na("Route","Stops","Analysis/Hospital","","")
使用所有参数执行此工具
import arcpy arcpy.env.workspace = "C:/ArcTutor/Network Analyst/Tutorial/SanFrancisco.gdb" arcpy.AddLocations_na("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 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" outNALayer = "StoreTravelTimeMatrix" impedanceAttribute = "TravelTime" inFeatures = "Analysis/Stores" searchTolerance = "500 Meters" searchQuery = [["Streets",'"FREEWAY" = 0'],["Streets_ND_Junctions",""]] originsLayer = outNALayer + "/" + "Origins" fieldMappings = "Name Name #; SourceID SourceID #;SourceOID SourceOID #;" \ + "PosAlong PosAlong #;SideOfEdge SideOfEdge #" outLayer = "C:/data/output" + "/" + outNALayer + ".lyr" #Create a new OD cost matrix analysis layer. For this scenario, the default #value for all the remaining parameters statisfies the analysis requirements arcpy.MakeODCostMatrixLayer_na(inNetworkDataset, outNALayer, impedanceAttribute) #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.AddLocations_na(outNALayer,"Origins",inFeatures,"",searchTolerance, 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. arcpy.AddLocations_na(outNALayer,"Destinations",originsLayer,fieldMappings, "") #Solve the od cost matrix layer. Halt the execution if there is an #invalid location arcpy.Solve_na(outNALayer,"HALT") #Save the solved na layer as a layer file on disk with relative paths arcpy.SaveToLayerFile_management(outNALayer,outLayer,"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)