Search code examples
pythonarraysnumpyarcpy

Remove sub-arrays from a numpy array


I'm trying to get a 2D array from a feature class using numpy and arcpy...

import arcpy
import numpy
locxyarray = arcpy.da.FeatureClassToNumPyArray("Points", ['SHAPE@XY', 'PrimeKey'])

The result:

array([([506173.7478, 5455684.263900001], 1),
       ([506175.22869999986, 5455648.723099999], 2),
       ([506229.03359999973, 5455661.5572999995], 3),
       ([506250.25939999986, 5455614.169500001], 4),
       ([506305.54509999976, 5455579.122300001], 5),
       ([506331.70710000023, 5455688.2129], 6)], 
      dtype=[('SHAPE@X', '<f8', (2,)), ('PrimeKey', '<i4')])

What I want:

array([(506173.7478, 5455684.263900001, 1),
       (506175.22869999986, 5455648.723099999, 2),
       (506229.03359999973, 5455661.5572999995, 3),
       (506250.25939999986, 5455614.169500001, 4),
       (506305.54509999976, 5455579.122300001, 5),
       (506331.70710000023, 5455688.2129, 6)], 
      dtype=[('SHAPE@XY', '<f8', (2,)), ('PrimeKey', '<i4')])

I want the above so I can sort by either the 0 or the 1 column, and return the 2 column in that order... Also, I need to be able to calculate the mean of X and the mean of Y, and pull all the Prime Keys with values above or below the mean.

EDIT - now able to make the array "look" correct;

locxyarray = arcpy.da.FeatureClassToNumPyArray("Points", ['SHAPE@X', 'SHAPE@Y', 'PrimeKey'])

>>array([(506173.7478, 5455684.263900001, 1),
       (506175.22869999986, 5455648.723099999, 2),
       (506229.03359999973, 5455661.5572999995, 3),
       (506250.25939999986, 5455614.169500001, 4),
       (506305.54509999976, 5455579.122300001, 5),
       (506331.70710000023, 5455688.2129, 6)], 
      dtype=[('SHAPE@X', '<f8'), ('SHAPE@Y', '<f8'), ('PrimeKey', '<i4')])

But I can't calculate the mean on the X or Y columns... (IndexError: too many indices)

SOLUTION (see comments below in solution, this is a summary):

import arcpy
import numpy as np
locxyarray = arcpy.da.FeatureClassToNumPyArray("Points", ['SHAPE@X','SHAPE@Y','PrimeKey'])
LSideLocs = np.where(locxyarray['SHAPE@X']<=np.mean(locxyarray['SHAPE@X']))
RSideLocs = np.where(locxyarray['SHAPE@X']>=np.mean(locxyarray['SHAPE@X']))


Solution

  • You can play around with the name of field.

    r=[(x['SHAPE@XY'][i][0],x['SHAPE@XY'][i][1],x['PrimeKey'][i]) for i in range(x.shape[0])]
    x=np.rec.fromrecords(r, formats = 'f8,f8,i4', names = 'SHAPE@X,SHAPE@Y,PrimeKey')
    rec.array([(506173.7478, 5455684.263900001, 1),
           (506175.22869999986, 5455648.723099999, 2),
           (506229.03359999973, 5455661.5572999995, 3),
           (506250.25939999986, 5455614.169500001, 4),
           (506305.54509999976, 5455579.122300001, 5),
           (506331.70710000023, 5455688.2129, 6)], 
          dtype=[('SHAPE@X', '<f8'), ('SHAPE@Y', '<f8'), ('PrimeKey', '<i4')])