FeatureClassToNumPyArray (arcpy.da)

摘要

将要素类转换为 NumPy 结构数组。

讨论

NumPy 是 Python 中用于进行科学计算的基础包,其包括支持功能强大的 N 维数组对象。有关详细信息,请参阅在 ArcGIS 中使用 NumPy

要将表转换成 NumPy 数组,请改为使用 TableToNumPyArray 函数。

语法

FeatureClassToNumPyArray (in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {skip_nulls}, {null_value})
参数说明数据类型
in_table

要素类、图层、表或表视图。

String
field_names
[field_names,...]

字段名称列表(或组)。对于单个字段,可以使用一个字符串,而不使用字符串列表。

如果要访问输入表中的所有字段(栅格和 BLOB 字段除外),可以使用星号 (*) 代替字段列表。但是,为了获得较快的性能和可靠的字段顺序,建议您将字段列表限制在实际需要的字段。

不支持日期、几何、栅格和 BLOB 字段。

以令牌(如 OID@)取代字段名称可访问更多的信息:

  • SHAPE@XY一组要素的质心 x,y 坐标。
  • SHAPE@TRUECENTROID一组要素的真正质心 x,y 坐标。
  • SHAPE@X要素的双精度 x 坐标。
  • SHAPE@Y要素的双精度 y 坐标。
  • SHAPE@Z要素的双精度 z 坐标。
  • SHAPE@M要素的双精度 m 值。
  • SHAPE@AREA要素的双精度面积。
  • SHAPE@LENGTH要素的双精度长度。
  • OID@ObjectID 字段的值。

Export a feature class to a NumPy array. The output array will include a field for the Object ID and a field containing a tuple of the feature's centroid's x,y coordinates.

import arcpy
array = arcpy.da.FeatureClassToNumPyArray(fc, ["OID@", "SHAPE@XY"])

# The first row would look similar to the following:
#  (1, [-147.82339477539062, 64.86953735351562])
print(array[0])

SHAPE@M and SHAPE@Z tokens will only return values if the in_table contains point features and is m-aware (or z-aware). If the in_table contains polygon, polyline, or multipart features, SHAPE@M and SHAPE@Z will return a nan. Any feature class that is not m-aware or z-aware will not support SHAPE@M and SHAPE@Z tokens.

(默认值为 *)

String
where_clause

用于限制所返回的记录的可选表达式。有关 WHERE 子句和 SQL 语句的详细信息,请参阅构建查询表达式使用 Python 指定查询

(默认值为 "")

String
spatial_reference

要素类的空间参考。可以使用 SpatialReference 对象或等效字符串来指定。

Use the spatial_reference argument to return coordinates in a different spatial reference. Here, a second feature class is described to access a spatial reference object.

import arcpy
SR = arcpy.Describe(fc2).spatialReference
arcpy.da.FeatureClassToNumPyArray(fc,
                                  ["OID@", "SHAPE@XY", "EDUCATION"], 
                                  spatial_reference=SR)

(默认值为 None)

SpatialReference
explode_to_points

将要素解构为单个点或折点。如果将 explode_to_points 设置为 True,则一个包含五个点的多点要素将表示为五行。

(默认值为 False)

Boolean
skip_nulls

控制是否跳过使用空值的记录。可以是布尔值(TrueFalse)、Python 函数或 lambda 表达式。

设置为 True 时,只要记录的属性是空值(包括几何图形),就跳过该记录。如果设置为 Falseskip_nulls 将尝试使用所有记录,而不考虑空值。在 NumPy 数组中,对于浮点数值,空值表示为 nan(not a number,不是数字),但不适用于整数。

Skip all records that include a null.

import arcpy
array = arcpy.da.FeatureClassToNumPyArray(fc, fields, skip_nulls=True)

使用 Python 函数或 lambda 表达式可进行更精细的控制,包括对含有空值的所有记录的 OID 值进行记录。在以下两个示例中,使用 lambda 表达式或函数来识别含有空记录的 OID。

Use a function to capture all records that are skipped because of nulls.

import arcpy
def getnull(oid):
    nullRows.append(oid)
    return True
nullRows = list()
array = arcpy.da.FeatureClassToNumPyArray(table, fields, skip_nulls=getnull)
print(nullRows)

Use a lambda expression to capture all records that are skipped because of nulls.

import arcpy
nullRows = list()
array = arcpy.da.FeatureClassToNumPyArray(fc, fields, 
                                    skip_nulls=lambda oid: nullRows.append(oid))
print(nullRows)
注注:

在 NumPy 数组中,空值以浮点型(如 nan)和文本类型(如 None)表示。整型不支持空值概念。

(默认值为 False)

Variant
null_value

将输入的空值替换为新值。

在计算 skip_nulls 之前,替换 null_value

Mask all None's in integer fields with a -9999.

import arcpy
fields = ['field1', 'field2']
arcpy.da.FeatureClassToNumPyArray(fc, fields, null_value=-9999)

Mask None's in integer fields with different values using a dictionary.

import arcpy
fields = ['field1', 'field2']
nullDict = {'field1':-999999, 'field2':-9999}
arcpy.da.FeatureClassToNumPyArray(fc, fields, null_value=nullDict)
警告警告:

对于掩膜(例如 -9999)允许将包含空值的整型字段导出到 NumPy 数组的情况,使用这些值进行分析时应格外小心。分析结果可能因引入的值而意外偏斜。

(默认值为 None)

Integer
返回值
数据类型说明
NumPyArray

NumPy 结构化数组。

代码实例

将表转化为 numpy 数组,并对 numpy 运算某些基本统计数据。

import arcpy
import numpy

input = "c:/data/usa.gdb/USA/counties"
arr = arcpy.da.FeatureClassToNumPyArray(input, ('STATE_NAME', 'POP1990', 'POP2000'))

# Sum the total population for 1990 and 2000
#
print(arr["POP1990"].sum())
print(arr["POP2000"].sum())

# Sum the population for the state of Minnesota
#
print(arr[arr['STATE_NAME'] == "Minnesota"]['POP2000'].sum())

使用 TableToNumPyArray 确定两个字段的相关系数。

import arcpy
import numpy

input = "c:/data/usa.gdb/USA/counties"
field1 = "INCOME"
field2 = "EDUCATION"

arr = arcpy.da.FeatureClassToNumPyArray(input, (field1, field2))

# Print correlation coefficients for comparison of 2 field values
#               
print(numpy.corrcoef((arr[field1], arr[field2])))

相关主题

5/10/2014