ClosestFacility

摘要

用于执行最近设施点分析的 ArcPy 类。

说明

最近设施点分析允许您找到每个输入事件点的最近设施点,并返回网络中两者之间的最短路径。了解有关最近设施点分析的详细信息。

语法

ClosestFacility (in_network)
参数说明数据类型
in_network

将用于网络分析的网络数据集或服务。可以使用网络数据集的目录路径、网络数据集图层对象、网络数据集图层的字符串名称或网络分析服务的门户 URL 来指定参数。网络必须包含至少一个出行模式

要使用门户 URL,您必须使用具有路径选择权限的帐户登录门户。

当对象使用 ArcGIS Online 作为 in_network 时,求解分析将消耗配额。有关详细信息,请参阅服务配额概述

String

属性

属性说明数据类型
accumulateAttributeNames
(可读写)

分析过程中要累积的成本属性的列表。 这些累积属性仅供参考;求解程序仅使用求解分析时指定的出行模式所使用的成本属性。

默认值为空列表。

String
allowSaveLayerFile
(可读写)

指定是否允许在结果对象上使用 saveAsLayerFile 方法将分析结果另存为图层文件。 值为 True 表示可以保存图层文件。 值为 False 则表示无法保存图层文件。 如果分析引用网络数据集,则默认设置为 True;如果其引用门户服务,则为 False。 如果分析引用门户服务,则允许保存图层文件可能会降低分析速度。

Boolean
allowSaveRouteData
(可读写)

指定是否允许在结果对象上使用 saveRouteData 方法将分析结果保存为 ZIP 文件。 值为 True 表示可以保存路径数据。 值为 False 则表示无法保存路径数据。 如果分析引用网络数据集,则默认设置为 True;如果其引用门户服务,则为 False。 如果分析引用门户服务,则允许保存路径数据可能会降低分析速度。

Boolean
defaultImpedanceCutoff
(可读写)

停止从指定事件点搜索设施点时所对应的阻抗值。

如果分析中所使用的出行模式使用基于时间的阻抗属性,则 defaultImpedanceCutoff 将以 timeUnits 属性中指定的单位进行解释。如果分析中所使用的出行模式使用基于距离的阻抗属性,则 defaultImpedanceCutoff 将以 distanceUnits 属性中指定的单位进行解释。如果出行模式的阻抗属性既不是基于时间,也不是基于距离,则 defaultImpedanceCutoff 值将以阻抗属性的单位进行解释。默认值为 None,这意味着不使用中断。

travelDirection 属性设置为 TravelDirection.FromFacility 时,defaultImpedanceCutoff 可以通过输入设施点中的 Cutoff 字段覆盖每个设施点。当 travelDirection 属性设置为 TravelDirection.ToFacility 时,defaultImpedanceCutoff 可以通过输入设施点中的 Cutoff 字段覆盖每个事件点。

Double
defaultTargetFacilityCount
(可读写)

要按事件点查找的最近设施点数。默认值为 1。

在此属性中设置的值可以通过输入事件点中的 TargetFacilityCount 字段覆盖每个事件点。

Integer
directionsDistanceUnits
(可读写)

报告输出转弯方向的行驶距离时使用的单位。 将返回该属性并将其设置为 DistanceUnits 枚举的成员,且仅在 returnDirections 属性为 True 时可用。 默认值是分析时使用的出行模式中距离属性的单位。

Object
directionsLanguage
(可读写)

将显示输出转弯方向文本的语言。 返回该属性并将表示受支持方向语言的两个或五个字符的语言代码之一设置为字符串。 可从 arcpy.nax.ListDirectionsLanguages 函数中获得可用的方向语言列表。 默认值为 en(英语)或当前激活的语言包的语言。 此属性仅在 returnDirections 属性为 True 时可用。

String
directionsStyle
(可读写)

用于输出转弯方向文本的样式。 将返回该属性并将其设置为 DirectionsStyle 枚举的成员,且仅在 returnDirections 属性为 True 时可用。

默认值为 Desktop

Object
distanceUnits
(可读写)

报告分析输出中的行驶距离时使用的单位。 无论网络数据集中成本属性的单位如何,输出都将转换为此属性中设置的单位并以该单位进行报告。 将返回该属性并将其设置为 DistanceUnits 枚举的成员。 默认为 DistanceUnits.Kilometers

Object
ignoreInvalidLocations
(可读写)

指定是否忽略无效的输入位置。 值为 True 表示将忽略无效的输入位置,仅使用有效位置即可成功进行分析。 值为 False 表示不会忽略无效位置,从而导致分析失败。

旧版本:

基于运行 10.9 之前版本 ArcGIS Enterprise 的门户的路径服务将始终忽略无效的输入位置。 如果将 ignoreInvalidLocations 设置为 False 并且该服务不支持此选项,则此方法将返回 ValueError 异常。

Boolean
networkDataSource
(只读)

分析时使用的网络数据集的完整目录路径。

String
overrides
(可读写)

求解网络分析问题时,指定可影响求解程序行为的其他设置。

必须在 JavaScript 对象表示法 (JSON) 中指定此参数的值。 例如,有效值的格式如下:{"overrideSetting1" : "value1", "overrideSetting2" : "value2"}。 覆盖设置名称始终以双引号括起。 该值可以是数字、布尔值或字符串。

此参数的默认值为无值,表示不覆盖任何求解程序设置。

覆盖是高级设置,应仅在谨慎分析应用设置前后得到的结果之后使用。 要获取每个求解程序支持的覆盖设置及其可接受值的列表,请联系 Esri 技术支持。

String
returnDirections
(可读写)

指定是否将生成转弯方向。 值为 True 表示在求解分析时将生成转弯方向。 值为 False 则表示不会生成转弯方向。 生成方向可能会降低分析速度。 默认值为 False

Boolean
routeShapeType
(可读写)

要生成的表示输出路径的形状类型。 路径始终沿网络计算;但是,您可以选择使用不反映网络路径的形状来表示它们。 将返回该属性并将其设置为 RouteShapeType 枚举的成员。 默认为 RouteShapeType.TrueShapeWithMeasures

Object
searchQuery
(可读写)

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

了解有关在网络中定位输入的详细信息

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

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

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

默认情况下不使用查询。

List
searchTolerance
(可读写)

在网络上定位输入要素所需的最大搜索距离。 返回该属性并将其设置为双精度,然后通过 searchToleranceUnits 属性访问该值的单位。 默认值为 5000。

了解有关在网络中定位输入的详细信息

Double
searchToleranceUnits
(可读写)

searchTolerance 属性的单位。 将返回该属性并将其设置为 DistanceUnits 枚举的成员。 默认为 DistanceUnits.Meters

Object
timeOfDay
(可读写)

要用于分析的时间。 默认值为 None,表示分析将为时间中立。

DateTime
timeOfDayUsage
(可读写)

指示 timeOfDay 值表示路径的到达时间还是离开时间。该属性将返回并被设置为 TimeOfDayUsage 枚举的成员。默认设置为 TimeOfDayUsage.DepartureTime

Object
timeUnits
(可读写)

报告分析输出中的行驶时间时使用的单位。 无论网络数据集中成本属性的单位如何,输出都将转换为使用在此属性中设置的单位并以该单位进行报告。 将返回该属性并将其设置为 TimeUnits 枚举的成员。 默认为 TimeUnits.Minutes

Object
timeZone
(可读写)

指示 timeOfDay 属性中所指定的时间是否解释为输入位置的本地时间或协调世界时间 (UTC)。 将返回该属性并将其设置为 TimeZoneUsage 枚举的成员,且仅在 timeOfDay 属性不为 None 时可用。 默认为 TimeZoneUsage.LocalTimeAtLocations

Object
travelDirection
(可读写)

设施点与事件点之间的行驶方向。该属性将返回并被设置为 TravelDirection 枚举的成员。默认设置为 TravelDirection.ToFacility

Object
travelMode
(可读写)

要用于分析的出行模式。

将返回该值并将其设置为 arcpy.nax.TravelMode 对象,但也可以使用出行模式的字符串名称或包含出行模式的有效 JSON 表示的字符串进行设置。 默认为用于分析的网络数据集上定义的默认出行模式。

了解有关出行模式的详细信息

Object

方法概述

方法说明
addFields (input_type, field_description)

将自定义字段添加到指定的输入类。 这些字段将包含在通过 fieldMappings 方法创建的字段映射字典中,也可与 insertCursor 方法配合使用。

count (input_type)

返回为输入类型添加的行数。

fieldMappings (input_type, {use_location_fields}, {list_candidate_fields})

生成将输入类型名称字段映射到 arcpy.nax.NAClassFieldMap 对象的 NAClassFieldMappings 字典,以允许您将字段从输入数据映射到求解程序属性。 字典可以用作 load 方法 field_mappings 参数的输入。

fieldNames (input_type, {use_location_fields})

获取指定输入类型支持的字段名称列表。

insertCursor (input_type, field_names)

在指定的输入类型上建立写入光标。此游标可用于将行直接添加到输入。

load (input_type, features, {field_mappings}, {append}, {max_features})

设置用于分析的输入要素。

solve ()

使用在 ClosestFacility 对象上设置的属性和已加载的输入执行最近设施点分析。

方法

addFields (input_type, field_description)
参数说明数据类型
input_type

The type of input to which the fields should be added.

应使用 ClosestFacilityInputDataType 枚举设置此参数。

Object
field_description
[field_description,...]

The fields and their properties that will be added to the input class. The value should be constructed as a list of lists with each row containing the following items:

  • Field name—The name of the field that will be added to the input class.
  • Field type—The type of the new field.
  • Field alias—The alternate display name given to the field name.
  • Field length—The length of the field being added. This sets the maximum number of allowable characters for each record of the field. This option is only applicable to fields of type text; the default length is 255.
  • Default value—The default value of the field.
  • Field domain—The geodatabase domain that will be assigned to the field.

Available field types are as follows:

  • TEXT—Any string of characters.
  • FLOAT—Fractional numbers between -3.4E38 and 1.2E38.
  • DOUBLE—Fractional numbers between -2.2E308 and 1.8E308.
  • SHORT—Whole numbers between -32,768 and 32,767.
  • LONG—Whole numbers between -2,147,483,648 and 2,147,483,647.
  • DATE—Date or time.

The method will return an error if the field already exists in the table or if any of the field properties are invalid.

List
count (input_type)
参数说明数据类型
input_type

用于计数的输入要素类型。

应使用 ClosestFacilityInputDataType 枚举设置此参数。

Object
返回值
数据类型说明
Integer

行数。

fieldMappings (input_type, {use_location_fields}, {list_candidate_fields})
参数说明数据类型
input_type

The type of input for which the field mappings are returned.

应使用 ClosestFacilityInputDataType 枚举设置此参数。

请参阅每种输入类型的可用字段的描述

Object
use_location_fields

Indicates whether network location fields should be included in the returned field mappings dictionary. Network location fields describe the point on the network where an object is located. You can use network location fields to more precisely control how your analysis inputs locate on the network and to save time when calling the solve method because the solver will not have to calculate the location fields from the geometry of the inputs. You can calculate location fields for a feature class using the Calculate Locations tool.

Learn more about network location fields and how inputs are located on the network

When this argument is set to True, the returned field mappings dictionary will contain network location fields. The default is False; the field mapping dictionary will not include network location fields.

(默认值为 False)

Boolean
list_candidate_fields
[list_candidate_fields,...]

Use this parameter to map additional, non-default fields from your input data into your analysis inputs. For example, if your input feature class contains a field named MyField, and you want this field to be included in your analysis inputs, pass the MyField field object to the list_candidate_fields parameter. MyField will be included in the returned field mapping dictionary and automatically mapped. When you call the load method using these field mappings, MyField will be included in the analysis inputs along with all the default fields. In many cases, these extra fields will be passed to the analysis output as well.

The parameter should be specified as a list of arcpy.Field objects, which can be obtained from a given feature class or table using the arcpy.ListFields function.

Learn more about best practices for setting up analysis inputs

(默认值为 None)

Field
返回值
数据类型说明
Dictionary

如果 NAClassFieldMappings 字典中的键为字段名称和值,则其为 arcpy.nax.NAClassFieldMap 对象。

fieldNames (input_type, {use_location_fields})
参数说明数据类型
input_type

返回其支持的字段名称的输入类型。

应使用 ClosestFacilityInputDataType 枚举设置此参数。

请参阅每种输入类型的可用字段的描述

Object
use_location_fields

指示网络位置字段是否包含在返回的字段名称列表中。网络位置字段可描述网络中对象所在的点。您可以使用网络位置字段更精确地控制分析输入在网络上的定位方式,并且由于求解程序不必从输入的几何计算位置字段,因此在调用 solve 方法时可以节省时间。您可以使用计算位置工具计算要素类的位置字段。

了解有关网络位置字段以及如何在网络中定位输入的详细信息

当此参数设置为 True 时,返回的字段名称列表将包含网络位置字段。默认为 False;字段名称列表不包括网络位置字段。

(默认值为 False)

Boolean
返回值
数据类型说明
String

指定输入类型支持的字段名称列表。

insertCursor (input_type, field_names)
参数说明数据类型
input_type

游标可用于插入行的输入类型。

应使用 ClosestFacilityInputDataType 枚举设置此参数。

Object
field_names
[field_names,...]

当使用游标插入行时要设置其值的输入类型的字段名称列表。您可以使用 fieldNames 方法获取输入类型支持的字段名称。

请参阅每种输入类型的可用字段的描述

除常规字段外,还可以使用以下几何令牌之一设置输入的几何:

  • SHAPE@XY一组要素的质心 x,y 坐标。
  • SHAPE@XYZ一组要素的质心 x,y,z 坐标。
  • SHAPE@JSON 表示几何的 Esri JSON 字符串。
  • SHAPE@WKBOGC 几何的熟知二进制 (WKB) 制图表达。该存储类型将几何值表示为不间断的字节流形式。
  • SHAPE@WKTOGC 几何的熟知文本 (WKT) 制图表达。其将几何值表示为文本字符串。
  • SHAPE@要素的几何对象。

基于点的输入类型仅支持 SHAPE@XYSHAPE@XYZ 令牌。在使用 SHAPE@XYSHAPE@XYZ 令牌时,应在分析中所使用的网络数据源的空间参考中指定 x、y 和 z 值。

String
返回值
数据类型说明
Object

可用于写入要素的 SolverInsertCursor 对象。

load (input_type, features, {field_mappings}, {append}, {max_features})
参数说明数据类型
input_type

The type of input feature to load.

应使用 ClosestFacilityInputDataType 枚举设置此参数。

Object
features

The input features to load. This parameter accepts the following input types:

  • Catalog path to a feature class or a table
  • Layer object
  • String representing the name of a layer
  • FeatureSet or RecordSet object

For layer inputs, only selected features will be loaded. If a layer has a definition query, only the subset of features visible with the definition query will be loaded. The method also honors the Extent geoprocessing environment; only features in the specified extent will be loaded.

String
field_mappings

An NAClassFieldMappings dictionary that maps the field names of the input type to arcpy.nax.NAClassFieldMap objects representing the mapping of fields from the input features. Valid input for this parameter can be constructed using the fieldMappings method.

If field mappings are not specified, all fields from the input features that have the same name as the supported fields for the input type will be mapped.

(默认值为 None)

Dictionary
append

Indicates whether the features being loaded should be appended to the existing set of features for the input type. A value of True indicates that the new features should be appended; the existing features will be preserved. This is useful if you want to load inputs from multiple feature classes or tables to use in a single analysis. This is the default. A value of False indicates that any existing features for the input type should be deleted and replaced with the features currently being loaded.

(默认值为 True)

Boolean
max_features

The maximum number of features that can be loaded into the input type. This is useful if you are creating a tool or service and want an error returned if the size of the input exceeds the available resources. The load method will return an arcpy.nax.LimitError if the number of input features exceeds the max_features limit.

If a value is not specified, no limit is enforced for the count of the input features.

(默认值为 None)

Integer
solve ()
返回值
数据类型说明
Object

arcpy.nax.ClosestFacilityResult 对象可用于访问输出和求解程序消息。

代码示例

ClosestFacility 示例

使用输入停靠点的要素类执行最近设施点分析。

# An example showing how to perform closest facility analysis using inputs from feature classes.
import arcpy
arcpy.CheckOutExtension("network")

nds = "C:/data/NorthAmerica.gdb/Routing/Routing_ND"
nd_layer_name = "Routing_ND"
input_facilities = "C:/data/io.gdb/Incidents"
input_incidents = "C:/data/io.gdb/Facilities"
output_routes = "C:/data/io.gdb/ClosestRoutes"

# Create a network dataset layer and get the desired travel mode for analysis
arcpy.nax.MakeNetworkDatasetLayer(nds, nd_layer_name)
nd_travel_modes = arcpy.nax.GetTravelModes(nd_layer_name)
travel_mode = nd_travel_modes["Driving Time"]

# Instantiate a ClosestFacility solver object
closest_facility = arcpy.nax.ClosestFacility(nds)
# Set properties
closest_facility.travelMode = travel_mode
closest_facility.timeUnits = arcpy.nax.TimeUnits.Minutes
closest_facility.defaultImpedanceCutoff = 15
closest_facility.defaultTargetFacilityCount = 2
closest_facility.routeShapeType = arcpy.nax.RouteShapeType.TrueShapeWithMeasures
# Load inputs
closest_facility.load(arcpy.nax.ClosestFacilityInputDataType.Facilities, input_facilities)
closest_facility.load(arcpy.nax.ClosestFacilityInputDataType.Incidents, input_incidents)
# Solve the analysis
result = closest_facility.solve()

# Export the results to a feature class
if result.solveSucceeded:
    result.export(arcpy.nax.ClosestFacilityOutputDataType.Routes, output_routes)
else:
    print("Solve failed")
    print(result.solverMessages(arcpy.nax.MessageSeverity.All))