How do I retain the values and field names in the resulting shapefile's attribute table after using NumPyArrayToFeatureClass?

I'm starting out with a 2d array named npGrid and I'm making it into a structured array with column names in order to run NumPyArrayToFeatureClass. I read the following post to guide me this far: arcpy.NumPyArrayToFeatureClass exception: "TypeError: narray.fields require"

My Code:

import arcpy, os
import numpy as np

out_fc = os.path.join(os.getcwd(), "test.shp")
sr = arcpy.SpatialReference(4326)

npGrid = np.array([[40.10, 35.3, .001], [40.2, 35.25, .002], [40.8, 34.9, .003], [40.75, 35.75, .004]])

x = npGrid[:,0]
y = npGrid[:,1]
tprobs = npGrid[:,2]

struct = np.rec.fromarrays([x,y,tprobs], formats=['f8','f8','f8'], names=["X", "Y", "TPROBS"])

feature = arcpy.da.NumPyArrayToFeatureClass(struct, out_fc, ['X', 'Y', 'TPROBS'], sr)

print([f.name for f in arcpy.ListFields(out_fc)])

The resulting shapefile is missing all of the field names and values:

Output: [u'FID', u'Shape', u'Id']

edit after changing the code to the following, the attribute table in the output shapefile was populated with the tprob value:

feature = arcpy.da.NumPyArrayToFeatureClass(struct, out_fc, ['X', 'Y'], sr)
  • What is the function of the tprobs variable? Is it a spatial coordinate? – LMB Jan 10 '18 at 11:39
NumPyArrayToFeatureClass (in_array, out_table, shape_fields, {spatial_reference})

The shape_fields argument is used to create the geometry. If you use X and Y as input here, they will be used to create the location of the points in the shapefile. They will NOT be fields in the resulting attribute table of that shapefile.

If you wish that the resulting attribute table contains the field "TPROBS", you should not call it in the function. If you wish that the X and Y field are also in the attribute table you should add them afterwards via the add xy-coordinates tool.

  • This was the tip I needed. I took out the tprob variable, which was a non-spatial value, from the shape_fields in NumPyArrayToFeatureClass and it appeared in the attribute table. – silverSuns Jan 10 '18 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.