Background:
I've used the Near Analysis tool which has added the following fields:
I'd like to use the following fields from the output of using the Near Analysis tool to generate a Structured NumPy Array:
I've read through the ESRI online help documentation on:
, and I'm unable to figure out how to generate a Structured NumPy Array that I can use as input to NumPyArrayToFeatureClass. The Structured NumPy Array format that is required as input to NumPyArrayToFeatureClass is:
array = numpy.array([(1, (471316.3835861763, 5000448.782036674)), (2, (470402.49348005146, 5000049.216449278))], numpy.dtype([('idfield',numpy.int32),('XY', '<f8', 2)]))
From what I gathered the coordinates are stored as tuples within the Structured NumPy Array. I'm still learning to code so any assistance in how to covert my data into a Structured NumPy Array that I may use as input into NumPyArrayToFeatureClass will be appreciated.
Regards
PeterW
Solved! Go to Solution.
fc = #feature class with NEAR_X and NEAR_Y fields idfield = #an identifier field nearList = [] with arcpy.da.SearchCursor(fc, [idfield, "NEAR_X", "NEAR_Y"]) as cur: for id, x, y in cur: nearList.append((id, (x, y))) dtype = numpy.dtype([(idfield,numpy.int32),('XY', '<f8', 2)]) narr = numpy.array(nearList, dtype)
You can add extra fields, or drop the idfield, if you want.
fc = #feature class with NEAR_X and NEAR_Y fields idfield = #an identifier field nearList = [] with arcpy.da.SearchCursor(fc, [idfield, "NEAR_X", "NEAR_Y"]) as cur: for id, x, y in cur: nearList.append((id, (x, y))) dtype = numpy.dtype([(idfield,numpy.int32),('XY', '<f8', 2)]) narr = numpy.array(nearList, dtype)
You can add extra fields, or drop the idfield, if you want.
Hi Joshua
Thanks for the following, its truly appreciated, I had not idea it would be that easy to create the Structured NumPy Array. I'm going to be using the following a lot in the future.
Regards
Peter Wilson
Peter, you can use FeatureClassToNumPyArray as well...It just handles the messy job of checking for nulls etc in attribute fields and truncating fieldnames so that they are compliant with shapefiles or featureclass should the need to have them brought back to Arc* be required.
Also, check out my blog there are some useful tips there. I now work almost exclusively with numpy arrays since most of the work I do is with geometry. A post on recfunctions and structured arrays may be of particular interest peaking since it adds the capabilities of appending columns to arrays without the need for the ExtendTable function in arcpy's data access module. If you work with data that has null values/nodata in the columns, you should do some reading on masked arrays which is nothing more than a subclass of the ndarray category. There are also a few tips on maintaining the input array structure to enable you to maintain column references as well as their named counter parts, but I won't go into that here. I should also mention that if bringing shapefiles/featureclass data proves problematic in some circumstances, I have exported their contents out to delimited textfiles and brought them in using numpy's various array reading capabilities, which handle tons of capabilities to handle the most difficult of data maintainence issues. enjoy your evolution from Pythonista to NumPythonista
FeatureClassToNumpyArray, nah, that would be too easy. I got tunnel vision on showing the mechanics of building the numpy array, but FeatureClassToNumPyArray is the more ArcPy approach.
but it does show how two kids can play nice