TableToNumPyArray Exclude Geometry Column By Default

534
3
07-21-2018 02:32 PM
Status: Open
Labels (1)
JoshuaBixby
MVP Esteemed Contributor

One behavior of Table To Table—Conversion toolbox | ArcGIS Desktop, Copy Rows—Data Management toolbox | ArcGIS Desktop, and most other tools that are designed for tables and table views, they ignore/exclude/drop the geometry column when run against a feature class or feature layer.  I find the behavior quite intuitive and handy since it creates an easy way of dumping all of the attribute information from a feature class into a non-spatial data format. 

Unfortunately, TableToNumPyArray—Data Access module | ArcGIS Desktop treats a feature class the exact same way FeatureClassToNumPyArray—Data Access module | ArcGIS Desktop treats a feature class when passing the all-fields wildcard, "*", which is it exports an (X,Y) tuple of the geometry column.  Not only is this behavior inconsistent with most other tools that operate on tables, it is inconsistent with the documentation of the tool:

Geometry, raster, and BLOB fields are not supported.
3 Comments
DanPatterson_Retired

I won't downvote but... don't make me have to add it in because the geometry has been removed by some default.

Also, exporting a table that just has OIDNAME and SHAPE and you want shape removed isn't going to leave much.

Here the workaround

# ---- workaround
import arcpy

a = arcpy.da.TableToNumPyArray(t, "*")

a[:3]  # ---- yes the geometry is there as a list of X, Y

array([(1, [ 304932.4471, 5029991.8871], 304932.4471    , 5029991.8871    , 795.11785302, 1),
       (2, [ 304362.8167, 5031187.6541], 304362.81671667, 5031187.65406667, 556.98787116, 2),
       (3, [ 306405.3811, 5030756.3248], 306405.38107037, 5030756.32477778, 789.63652656, 3)],
      dtype=[('OBJECTID', '<i4'), ('Shape', '<f8', (2,)), ('CenterX', '<f8'), ('CenterY', '<f8'), ('StdDist', '<f8'), ('ID_poly', '<i4')])


# ---- maybe put in a toggle to 'exclude' some field types,
# ---- NOTE, you can provide a list of excluded fields this way, rather than
# ---- the default 'inclusion' fields

flds = [i.name for i in arcpy.ListFields(t) if i.type not in ['Geometry', 'BLOB']]

flds
Out[8]: ['OBJECTID', 'CenterX', 'CenterY', 'StdDist', 'ID_poly']‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

So the result will look like this now... sans the list of coordinates

b = arcpy.da.TableToNumPyArray(t, flds)

b

array([(1, 304932.4471    , 5029991.8871    , 795.11785302, 1),
       (2, 304362.81671667, 5031187.65406667, 556.98787116, 2),
       (3, 306405.38107037, 5030756.32477778, 789.63652656, 3),
       (4, 306341.206525  , 5029173.844525  , 569.44952193, 4),
       (5, 304253.46515333, 5029007.51995333, 596.71339983, 5)],
      dtype=[('OBJECTID', '<i4'), ('CenterX', '<f8'), ('CenterY', '<f8'), ('StdDist', '<f8'), ('ID_poly', '<i4')])
JoshuaBixby

By that same logic, should Table to Table and Copy Rows also include geometry information?  If one wants all the fields in a feature class by default, simply use FeatureClassToNumPyArray.  Personally, I would go with not allowing any type of geometry information exported from TableToNumPyArray since there is an equivalent tool that is designed to work with geometry information, but I can live with changing the wildcard default behavior.

DanPatterson_Retired

I basically like the exclusion principle rather than the inclusion principle as shown in my workaround and not the current approach.. or maybe an include/exclude option