Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I want to convert all the geometries returned by search cursor to a list.

import math
import arcpy
from arcpy import env

env.workspace = r"C:\GIS Data\GIS data_for Maeenul vai"

desc = arcpy.Describe("River.shp")
shapefieldname = desc.ShapeFieldName

rows = arcpy.SearchCursor("River.shp")

featureList = []

for row in rows:

    feat = row.getValue(shapefieldname)
    featureList.append(feat)

    print "%i %i" % (feat.firstPoint.X, feat.firstPoint.Y)
    print
    print "%i %i" % (feat.lastPoint.X, feat.lastPoint.Y)
    print
    print

del row, rows
print "---------------------------------------------------------------"

for feat in featureList:
    print "%i %i" % (feat.firstPoint.X, feat.firstPoint.Y)
    print
    print "%i %i" % (feat.lastPoint.X, feat.lastPoint.Y)
    print
    print
    print

Output is

3610930 2135882 3611593 2134453

3611806 2134981 3611593 2134453

3614160 2136164 3617432 2131734

3611593 2134453 3617432 2131734

3617432 2131734 3620568 2127591

3620568 2127591 3620785 2127423

3617980 2126657 3620568 2127591

3616768 2129454 3617948 2126649

3617948 2126649 3617980 2126657

3615102 2128889 3617587 2126510

3617587 2126510 3617948 2126649

3617624 2126416 3617980 2126657

3613129 2128176 3615155 2125617

3615155 2125617 3617587 2126510

3615086 2125515 3615155 2125617


List Data

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

So basically the append methods of list only copies the last element returned by cursor to the whole list. How can I resolve this?

share|improve this question
add comment

3 Answers 3

try to use:

....
arr = arcpy.Array()
firstPoint = arcpy.Point()
lastPoint = arcpy.Point()

for row in rows:
    feat = row.getValue(shapefieldname)
    firstPoint.X = feat.firstPoint.X
    firstPoint.Y = feat.firstPoint.Y

    lastPoint.X = feat.lastPoint.X
    lastPoint.Y = feat.lastPoint.Y

    arr.add(lastPoint)
    arr.add(lastPoint)

more information here.

i hope it helps you.

share|improve this answer
    
Thanks! After posting on SO, I also got the same idea. I wonder why ArcGIS is doing such weird thing? –  user May 30 '12 at 11:59
add comment

That append should work, from Python help:

list.append(x) Add an item to the end of the list; equivalent to a[len(a):] = [x].

Try deleting the variable in the loop and see if that clears up the problem. If not, try the long hand method of:

featurelist[len(featurelist):] = [feat]

share|improve this answer
    
I've also tried this. Nothing's working!!! –  user May 30 '12 at 11:25
    
The only reason I said that was because you repeated the feat var, but that shouldn't really be an issue... –  Roy May 30 '12 at 11:27
add comment

What you have should work fine for a field that is not a geometry field. Try another field that is in the table to test. Example for a point shapefile:

for row in rows:
# Create the geometry object 'feat'
#
feat = row.getValue(shapefieldname)
pnt = feat.getPart()

# Print x,y coordinates of current point
#
print pnt.X, pnt.Y
featureList.append(pnt)

Notice you get the Value and then the part. More info here: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Reading_geometries/002z0000001t000000/

You will see that the example for line or polygon steps through the parts:

# Step through each part of the feature
#
for part in feat:
    # Print the part number
    #
    print "Part %i:" % partnum

    # Step through each vertex in the feature
    #
    for pnt in feat.getPart(partnum):
        if pnt:
            # Print x,y coordinates of current point
            #
            print pnt.X, pnt.Y
        else:
            # If pnt is None, this represents an interior ring
            #
            print "Interior Ring:"
    partnum += 1     

The Value you return from getValue is that of a geometry object, which returns them in an array of point objects.<- (also from the help) Word up.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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