FeatureClassToNumPyArray (arcpy.da)

Zusammenfassung

Converts a feature class to NumPy structured array.

Diskussion

The FeatureClassToNumPyArray function is similar to the TableToNumPyArray function. The FeatureClassToNumPyArray function also provides additional arguments for dealing with feature data.

NumPy is a fundamental package for scientific computing in Python, including support for a powerful N-dimensional array object. For more information, see Working with NumPy in ArcGIS.

Syntax

FeatureClassToNumPyArray (in_table, field_names, {where_clause}, spatial_reference, {explode_to_points}, {skip_nulls}, {null_value})
ParameterErläuterungDatentyp
in_table

The feature class, layer, table, or table view.

String
field_names
[field_names,...]

A list (or tuple) of field names. For a single field, you can use a string instead of a list of strings.

Use an asterisk (*) instead of a list of fields if you want to access all fields from the input table (raster and BLOB fields are excluded). However, for faster performance and reliable field order, it is recommended that the list of fields be narrowed to only those that are actually needed.

Date, geometry, raster, and BLOB fields are not supported.

Additional information can be accessed using tokens (such as OID@) in place of field names:

  • SHAPE@XYA tuple of the feature's centroid x,y coordinates.
  • SHAPE@TRUECENTROIDA tuple of the feature's true centroid x,y coordinates.
  • SHAPE@XA double of the feature's x-coordinate.
  • SHAPE@YA double of the feature's y-coordinate.
  • SHAPE@ZA double of the feature's z-coordinate.
  • SHAPE@MA double of the feature's m-value.
  • SHAPE@AREAA double of the feature's area.
  • SHAPE@LENGTHA double of the feature's length.
  • OID@The value of the ObjectID field.

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.

(Der Standardwert ist *)

String
where_clause

An optional expression that limits the records returned. For more information on WHERE clauses and SQL statements, see Building a query expression and Specifying a query in Python.

(Der Standardwert ist "")

String
spatial_reference

The spatial reference of the feature class. It can be specified with either a SpatialReference object or string equivalent.

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)

(Der Standardwert ist None)

SpatialReference
explode_to_points

Deconstruct a feature into its individual points or vertices. If explode_to_points is set to True, a multipoint feature with five points, for example, is represented by five rows.

(Der Standardwert ist False)

Boolean
skip_nulls

Control whether records using nulls are skipped. It can be either a Boolean true or false or a Python function or lambda expression.

When set to True, if any of the record's attributes is null (including geometry), the record is skipped. With a False setting, skip_nulls attempts to use all records regardless of nulls (NumPy supports nan for floating-point numeric values, but not for integers).

Skip all records that include a null.

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

A Python function or lambda expression can be used to allow finer control, including logging the OID values of all records that include a null value. In both examples below, the lambda expression or function is used to identify OIDs that include null records.

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)
HinweisHinweis:

In NumPy arrays, nulls are represented in float types as nan and in text types such as None. Integer types do not support the concept of null values.

(Der Standardwert ist False)

Variant
null_value

Replaces null values from the input with a new value.

null_value is replaced before skip_null is evaluated.

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)
AchtungAchtung:

Providing a mask such as -9999 allows integer fields containing nulls to be exported to a NumPy array, but be cautious when using these values in any analysis. The results may be inadvertently skewed by the introduced value.

(Der Standardwert ist None)

Integer
Rückgabewert
DatentypErläuterung
NumPyArray

A NumPy structured array.

Codebeispiel

Convert a table to a numpy array and perform some basic statistics with 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())

Use TableToNumPyArray to determine correlation coefficients for two fields.

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])))

Verwandte Themen

9/11/2013