NumPyArrayToFeatureClass (arcpy.da) in Python script

5613
9
08-29-2014 01:31 AM
AlastairOram1
New Contributor

Hi

I'm using arcmap 10.2 and have been trying in vain to get the
numpyArrayToFeatureClass tool to work in a python script. It generates a shapefile
but the output I get only ever has an index column - no other columns are
output.



When I use the code example here ArcGIS Help (10.2, 10.2.1, and 10.2.2)  I get exactly the same problem. Can you please
give me any indication where I am going wrong?

Thanks

Alastair


0 Kudos
9 Replies
DanPatterson_Retired
MVP Emeritus

Can you post your code

0 Kudos
AlastairOram1
New Contributor

Hi

Note that I’m starting with a pandas data frame (df) and converting it using “to_records” (although I have tried several methods like 'df.as_matrix()';

turbinesNPArray = df.to_records()

dt = turbinesNPArray.dtype

#(Note this returns:

#[('index', '<i8'), ('TurbineID', '<f8'), ('X', '<f8'), ('Y', '<f8'), ('Z', '<f8'), ('HubH', '<f8'), ('RotorD', '<f8'), ('WGS84Lat', '<f8'), ('WGS84Long', '<f8'), ('OFFSETA', '<f8'), ('OFFSETB', '<f8')]

#type turbinesNPArray:<class 'numpy.core.records.recarray'>

outShapeFile = r"%s\%s.shp" %(rootPath + "Outputs", "outLayout_temp")

arcpy.da.NumPyArrayToFeatureClass(turbinesNPArray, outShapeFile, ("TurbineID","X","Y","Z","HubH","RotorD","WGS84Lat","WGS84Long","OFFSETA", "OFFSETB"), self.spRef)

0 Kudos
JamesCrandall
MVP Frequent Contributor

I suspect that you are not correctly setting up the nparray:

turbinesNPArray = df.to_records()

Without knowing your fields info, I don't see you fully qualifying the fields in your conversion from Pandas DataFrame to NumPy Array.   You can try the following but I have no real way to test:

turbinesNPArray = numpy.array(df.to_records(), numpy.dtype([('index', '<i8'), ('TurbineID', '<f8'), ('X', '<f8'), ('Y', '<f8'), ('Z', '<f8'), ('HubH', '<f8'), ('RotorD', '<f8'), ('WGS84Lat', '<f8'), ('WGS84Long', '<f8'), ('OFFSETA', '<f8'), ('OFFSETB', '<f8')]))

0 Kudos
DanielGarcia
New Contributor III

Hi Alastair,

This code works fine for me. Please, use it and tell us what you get.

import arcpy

import numpy

outFC = "E:/temp/testShape.shp"

array = numpy.array([(1, 'testText1', (471316.3835861763, 5000448.782036674)),

                     (2, 'testText2', (470402.49348005146, 5000049.216449278))],

                    numpy.dtype([('idfield',numpy.int32),('textField','<U10'),('XY', '<f8', 2)]))

arcpy.da.NumPyArrayToFeatureClass(array, outFC, ['XY'])

Regards

Dani

0 Kudos
AlastairOram1
New Contributor

Hi

Well a shape file gets created but only has an index column - no other fields - see below.

 

idfield
1
2

Thanks

0 Kudos
DanPatterson_Retired
MVP Emeritus

Dani...line 7 is truncated

0 Kudos
AlastairOram1
New Contributor

Not sure what you mean about line 7?

0 Kudos
DanielGarcia
New Contributor III

I attach you py file and the result shapefile.

These work for me in ArcGIS 10.2.2.3552 version and Python 2.7.

0 Kudos
AlastairOram1
New Contributor

I get;

Traceback (most recent call last):

  File "<module1>", line 23, in <module>

RuntimeError: create table

0 Kudos