# define the input file shapefile = '_myfile.shp' # import arcpy import arcpy arcpy.overwriteOutput = True # set the cursor shpName = arcpy.Describe(shapefile).shapeFieldName cursor = arcpy.SearchCursor(shapefile) # get the features BEFORE doing processing # WHERE THINGS GET FUNKY features = [r.getValue(shpName) for r in cursor] cursor = None r = None # to illustrate these are all the same, save them results = [] for i in xrange(len(features)): fName = '_myfeat%i.shp' % i results.append(arcpy.CopyFeatures_management(features, fName)
Solved! Go to Solution.
features = [r.Shape for r in cursor]
I think I too have seen this before and it isn't so intuitive from the online samples - and I may not explain it well, but I'll give it a shot.
The cursor row object is a reference, a 'pointer' if you will...so when you go to the next row, it's actually referencing the next geometry. (At least that's how I understand it.)
Methods have changed for 10.1 and I'm still at 10, so I cannot test this - however, I think it's a little more straightforward at 10.1 so I think it's easier to show that 1st. I think you need the object reference like this for the list you seem to be after (this should only store a list of the geometry objects):
features = [r[0] for r in arcpy.da.SearchCursor(shapefile, ("SHAPE@"))]
The 10.1 documentation actually states a geometry object is returned using the '@' token. Also notice the use of the da (data access module), supposedly a performance enhancement, although I am not sure it is efficient to load the geometry as you wish into a list - better test it out.
For 10.0, which I am a little more familiar with, but haven't tested this at length enough to say it'll work, I think you have to 'load' the geometry into a separate object you create, as in the script excerpt shown (this is not what I wrote, but a webhelp sample):
########################################
# Create an empty Geometry object
#
g = arcpy.Geometry()
# Run the CopyFeatures tool, setting the output to the geometry object. GeometryList
# is returned as a list of geometry objects.
#
geometryList = arcpy.CopyFeatures_management("c:/temp/outlines.shp", g)
########################################
I didn't know CopyFeatures could be used directly - that a cursor is not necessary to do exactly what you want (although I suppose you'll be opening a cursor to then write further output). This short script in its entirety can be found here (at the bottom of the page):
Using geometry objects with geoprocessing tools
Resource Center » Professional Library » Geoprocessing » Geoprocessing with Python » Accessing geographic data in Python
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Using_geometry_objects_with_geoprocess...
Try this:features = [r.Shape for r in cursor]
I tried this and it works just fine. However, I am using 10.1 (but I didn't use da.searchcursor). I suspect it will work on 10.0.
Also, you can use the in_memory workspace to make a temporary featureclass instead of writing out a shapefile.
Mike