python list in memory to arcpy Table?

6138
2
Jump to solution
07-18-2012 01:40 AM
DanSlayback
New Contributor II
I'm wondering if there is a way to convert a list in python (or it could be an array, I think) to a Table object, that I'll then do further geoprocessing on. Currently, the only way I can get this to work is to write the list to a csv file, and then use arcpy.TableToGeodatabase_conversion to bring that csv file into a geodatabase. I dont want the csv file, so would prefer to simply pass the list object in memory to the geodatabase.

And actually, I'd like to not even put it in the geodatabase, but just keep it as a Table object in memory. I'll then run  arcpy.MakeXYEventLayer_management, and then run the IDW tool on the resulting layer to get the desired output raster. I'd rather not keep around (or have to bother deleting) the intermediate files.

Seems like this should be trivially easy, but I cant find any hints on how to do this. I have tried to pass the python list object directly to MakeXYEventLayer, but it just hangs/takes forever. Presumably eventually it would return an error.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinGasior
Occasional Contributor III
Why not create temporary in_memory point feature class right away.
Then populate fields with values from list using InsertCursor.

Here's an example script:
import arcpy,  os arcpy.env.overwriteOutput = True  #list of lists with values [X, Y, InterpolationValue] valLst = [  [20.0, 50.0, 345],             [20.0, 51.0, 346],             [20.5, 50.5, 347] ]  #set up spatial reference and names prjFile = os.path.join(arcpy.GetInstallInfo()["InstallDir"],             r"Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj") spatialRef = arcpy.SpatialReference(prjFile)  tempWorkspace = "in_memory" outXYfc = "XY_FeatureClass"  #create in_memory empty feature class arcpy.CreateFeatureclass_management(tempWorkspace, outXYfc , "POINT", "","","", spatialRef)  #build temp FC path and add new field for values to interpolate tempFC = os.path.join(tempWorkspace, outXYfc) arcpy.AddField_management(tempFC, "InterpVal", "DOUBLE")  #create insert cursor inCur = arcpy.InsertCursor(tempFC) #loop through main list, add values to cursor row and insert row for element in valLst:     pnt = arcpy.Point(element[0], element[1])     row = inCur.newRow()     row.Shape = pnt     row.InterpVal = element[2]     inCur.insertRow(row)  del inCur, row  #FOR TESTING: test if temporary feature class is OK #arcpy.CopyFeatures_management(tempFC, r"C:\tmp\Test.gdb\XYpoints")


Here's a result:
[ATTACH=CONFIG]16188[/ATTACH]

View solution in original post

0 Kudos
2 Replies
MarcinGasior
Occasional Contributor III
Why not create temporary in_memory point feature class right away.
Then populate fields with values from list using InsertCursor.

Here's an example script:
import arcpy,  os arcpy.env.overwriteOutput = True  #list of lists with values [X, Y, InterpolationValue] valLst = [  [20.0, 50.0, 345],             [20.0, 51.0, 346],             [20.5, 50.5, 347] ]  #set up spatial reference and names prjFile = os.path.join(arcpy.GetInstallInfo()["InstallDir"],             r"Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj") spatialRef = arcpy.SpatialReference(prjFile)  tempWorkspace = "in_memory" outXYfc = "XY_FeatureClass"  #create in_memory empty feature class arcpy.CreateFeatureclass_management(tempWorkspace, outXYfc , "POINT", "","","", spatialRef)  #build temp FC path and add new field for values to interpolate tempFC = os.path.join(tempWorkspace, outXYfc) arcpy.AddField_management(tempFC, "InterpVal", "DOUBLE")  #create insert cursor inCur = arcpy.InsertCursor(tempFC) #loop through main list, add values to cursor row and insert row for element in valLst:     pnt = arcpy.Point(element[0], element[1])     row = inCur.newRow()     row.Shape = pnt     row.InterpVal = element[2]     inCur.insertRow(row)  del inCur, row  #FOR TESTING: test if temporary feature class is OK #arcpy.CopyFeatures_management(tempFC, r"C:\tmp\Test.gdb\XYpoints")


Here's a result:
[ATTACH=CONFIG]16188[/ATTACH]
0 Kudos
DanSlayback
New Contributor II
Thanks much for the detailed example - this will certainly help! I figured using cursors was one way to do it, but was hoping there might be a simple way to directly transfer an in-memory list/array to a table.
0 Kudos