Search code examples
pythonarraysnumpymeanarcpy

Mean center of polygon feature class using numpy, arcpy


I'm tasked with finding the mean center of a feature class using a numpy array. I have created a numpy array from the feature class using

import arcpy
import numpy
fc = "polygons.shp"
a = arcpy.da.FeatureClassToNumPyArray(fc, ["SHAPE@X", "SHAPE@Y"])

the array, a, is then:

array([( 3107178.29076947,  10151024.31186805),
       ( 3107961.30479125,  10139810.52458512),
       ( 3109603.8882401 ,  10119654.26424824),
       ( 2992362.40598316,  10049723.50515586),
       ....
       ( 3114517.82381449,  10071634.68261757)],
       dtype=[('SHAPE@X', '<f8'), ('SHAPE@Y', '<f8')])

which is the centroid (X,Y) of each record in fc. How do I get the meanX and meanY of these so the output would be ([(mean.X, mean.Y)])? I've tried using the following, as described here:

numpy.mean(a, axis=0)

but I get the mean of just the X values. Is there some additional step with changing dtype after the arcpy.da function to successfully get both the mean.X, mean.Y values? I have to do this using the numpy mean function. Thanks!


Solution

  • meanxy=[np.mean(y) for y in zip(*a)]
    

    * collects all the positional arguments in a tuple