Using FeatureClassToNumPyArray for a Point Feature Class

814
2
Jump to solution
10-17-2019 06:32 AM
RobW
by
New Contributor III

I am trying to get acquainted with numpy and point feature classes in ArcGIS and I started with what I thought was an simple example. 

I am reading in a single part point feature class (contains fields named: X,  Y and YR) via FeatureClassToNumPyArray. I am interested in finding the 'max' value within a given field using numpy. 

A snippet of my code is below: 

array = arcpy.da.FeatureClassToNumPyArray(inputFC, ["X", "Y", "YR"])
print array

It prints: 

[(2531050.0, 7431830.0, 2018.0) (2531050.0, 7431810.0, 2018.0)
 (2531050.0, 7431850.0, 2018.0) (2531050.0, 7431930.0, 2018.0)
 (2531050.0, 7431890.0, 2018.0) (2531050.0, 7431870.0, 2018.0)
 (2531050.0, 7431910.0, 2018.0) (2531050.0, 7431950.0, 2018.0)
 (2531050.0, 7431970.0, 2018.0) (2531050.0, 7432030.0, 2018.0)
 (2531070.0, 7428230.0, 2018.0)]

A slight change in the code attempts to get the max for each column by specifying axis = 0: 

array = arcpy.da.FeatureClassToNumPyArray(inputFC, ["X", "Y", "YR"])
 print numpy.amax(array, axis = 0)

However the script output is: 

File "work5_EFI_sum.py", line 102, in <module>
 doSummary()
 File "work5_EFI_sum.py", line 20, in doSummary
 print numpy.amax(array, axis = 0)
 File "C:\Python27\ArcGIS10.6\lib\site-packages\numpy\core\fromnumeric.py", line 2140, in amax
 out=out, keepdims=keepdims)
 File "C:\Python27\ArcGIS10.6\lib\site-packages\numpy\core\_methods.py", line 26, in _amax
 return umr_maximum(a, axis, None, out, keepdims)
TypeError: cannot perform reduce with flexible type

Any idea on what's going on?

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

Your dtype has the year as a float, was that by design?

Use numpy 1.16.4

xy
Out[6]: 
array([(300004.72, 5000004.73), (300017.5 , 5000007.  ),
       (300013.  , 5000012.67), (300015.  , 5000030.  ),
       (300002.4 , 5000027.4 ), (300022.  , 5000027.  ),
       (300014.  , 5000024.  ), (300020.96, 5000020.96),
       (300024.4 , 5000018.93), (300031.32, 5000017.85),
       (300029.88, 5000011.35), (300033.06, 5000025.98)],
      dtype={'names':['X_cent','Y_cent'], 'formats':['<f8','<f8'], 'offsets':[4,12], 'itemsize':76})

from numpy.lib.recfunctions import structured_to_unstructured as stu

stu(xy)
Out[8]: 
array([[ 300004.72, 5000004.73],
       [ 300017.5 , 5000007.  ],
       [ 300013.  , 5000012.67],
       [ 300015.  , 5000030.  ],
       [ 300002.4 , 5000027.4 ],
       [ 300022.  , 5000027.  ],
       [ 300014.  , 5000024.  ],
       [ 300020.96, 5000020.96],
       [ 300024.4 , 5000018.93],
       [ 300031.32, 5000017.85],
       [ 300029.88, 5000011.35],
       [ 300033.06, 5000025.98]])

np.max(stu(xy), axis=0)
Out[9]: array([ 300033.06, 5000030.  ])

Or make it easier on yourself if you don't have a more recent version of numpy installed

np.max(xy['X_cent'])
Out[17]: 300033.05638901744


np.max(xy['Y_cent'])
Out[19]: 5000030.0

View solution in original post

0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus

Your dtype has the year as a float, was that by design?

Use numpy 1.16.4

xy
Out[6]: 
array([(300004.72, 5000004.73), (300017.5 , 5000007.  ),
       (300013.  , 5000012.67), (300015.  , 5000030.  ),
       (300002.4 , 5000027.4 ), (300022.  , 5000027.  ),
       (300014.  , 5000024.  ), (300020.96, 5000020.96),
       (300024.4 , 5000018.93), (300031.32, 5000017.85),
       (300029.88, 5000011.35), (300033.06, 5000025.98)],
      dtype={'names':['X_cent','Y_cent'], 'formats':['<f8','<f8'], 'offsets':[4,12], 'itemsize':76})

from numpy.lib.recfunctions import structured_to_unstructured as stu

stu(xy)
Out[8]: 
array([[ 300004.72, 5000004.73],
       [ 300017.5 , 5000007.  ],
       [ 300013.  , 5000012.67],
       [ 300015.  , 5000030.  ],
       [ 300002.4 , 5000027.4 ],
       [ 300022.  , 5000027.  ],
       [ 300014.  , 5000024.  ],
       [ 300020.96, 5000020.96],
       [ 300024.4 , 5000018.93],
       [ 300031.32, 5000017.85],
       [ 300029.88, 5000011.35],
       [ 300033.06, 5000025.98]])

np.max(stu(xy), axis=0)
Out[9]: array([ 300033.06, 5000030.  ])

Or make it easier on yourself if you don't have a more recent version of numpy installed

np.max(xy['X_cent'])
Out[17]: 300033.05638901744


np.max(xy['Y_cent'])
Out[19]: 5000030.0
0 Kudos
RobW
by
New Contributor III

The feature class I am using is test/junk data, so no design thought was put in place for the YR field. 

Your 2nd suggestion worked. For the time being, I will stick with my current version of numpy.  

Thanks

0 Kudos