'Cursor' object has no attribute 'getValue'

5442
6
Jump to solution
05-18-2012 02:04 PM
DerekBannon
New Contributor
I am trying to select an attribute value from one of my tables. Its does not seam to be working. It keeps giving me this error.

Runtime error <type 'exceptions.AttributeError'>: 'Cursor' object has no attribute 'getValue'

>>> mxd = arcpy.mapping.MapDocument("CURRENT") #defines the variable mxd to the current map>>>
... #sets the current author of the mxd to Derek Bannon
... mxd.author = "Derek Bannon"
... #defines the variable df to the one and only dataframe
... df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
... textnumb = 0 #defines varaible textnumb to equal 0
... indexnumb = 295
... #defines the variable PDFdoc to save a blank pdf in memory for the appending pdf files
... PDFdoc = arcpy.mapping.PDFDocumentCreate(r"\\swg.ca\files\Jobs\35000\35628 ED41 Kinder Morgan Canada Inc\500CAD\GIS\MapOutput\PDFOutput\TMX-Parcels-Mapbook.pdf")
... #opens a loop that runs and assigns i to a new value in a range that counts from 3 to 233 #but not including 233 in increments of 3
... for i in range (296, 356,1):
... #defines an if statement where if i is greater than 10 then the operation will break from #the loop and execute a PDF document save and close
... if i > 297:
... #breaks the loop if i is equal to greater than 10
... break
... #saves and close the PDF document created before the loop
... PDFdoc.saveAndClose()
... #this is the else statement that operates when the break statement has not yet been achieved
... else:
... #defines variable j as equaling to i minus 3. Defines the kp range that the map will zoom to
... j=i-3
... #defines the variable textnumb which assigns each map through the loop an incremented number #starting at 1 and counting up
... textnumb=textnumb + 1
... indexnumb=indexnumb + 1
... #defines the variable where_clause for the Select Layer by Attribute tool
... where_clause = "\"PageNumber\" = " + str(i)
... #runs the select layer by attribute tool selecting from the layer 'Kilometer Posts', a new #selection and using the where_clause variable for the selection criteria
... arcpy.SelectLayerByAttribute_management("TMPL_BC_IndexFile","NEW_SELECTION",where_clause)
... #defines the variable where_clause_add for the Select Layer by Attribute tool
... #where_clause_add = "\"KP\" = " + str(i)
... #runs the select layer by attribute tool selecting from the layer 'Kilometer Posts', a new #selection and using the where_clause_add variable for the selection criteria
... #arcpy.SelectLayerByAttribute_management("Kilometer Posts","ADD_TO_SELECTION",where_clause_add)
... #refreshes the Table of Contents
... arcpy.RefreshTOC
... #refreshes the active view
... arcpy.RefreshActiveView
... cursor = arcpy.SearchCursor("TMPL_BC_IndexFile", where_clause) <<<<< something to do with this code here I think.
... rotation = cursor.getValue("Angle")
...
...
...
... df.rotation(rotation)
... #zooms the dataframe to the selected features
... df.zoomToSelectedFeatures()
...
... arcpy.SelectLayerByAttribute_management("TMPL_BC_IndexFile","CLEAR_SELECTION",where_clause)
... dfAsFeature = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft, df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]),df.spatialReference)
...
... #arcpy.SelectLayerByLocation_management("Annotations\Sectiontest4","COMPLETELY_WITHIN", dfAsFeature, "","NEW_SELECTION")
... #arcpy.SelectLayerByLocation_management("Annotations\Sectiontest3","COMPLETELY_WITHIN", dfAsFeature, "","ADD_TO_SELECTION")
... #arcpy.SelectLayerByLocation_management("Annotations\Sectiontest2","COMPLETELY_WITHIN", dfAsFeature, "","ADD_TO_SELECTION")
... arcpy.SelectLayerByLocation_management("Annotations\BCParcelsAnno4","COMPLETELY_WITHIN", dfAsFeature, "","ADD_TO_SELECTION")
... #arcpy.CalculateField_management("Annotations\Sectiontest4","Status",0,"VB")
... #arcpy.CalculateField_management("Annotations\Sectiontest3","Status",0,"VB")
... #arcpy.CalculateField_management("Annotations\Sectiontest2","Status",0,"VB")
... arcpy.CalculateField_management("Annotations\BCParcelsAnno4","Status",0,"VB")
... arcpy.SelectLayerByAttribute_management("Annotations\BCParcelsAnno4","CLEAR_SELECTION","")
...
... #arcpy.SelectLayerByAttribute_management("Annotations\Sectiontest2","CLEAR_SELECTION","")
... #arcpy.SelectLayerByAttribute_management("Annotations\Sectiontest3","CLEAR_SELECTION","")
... #arcpy.SelectLayerByAttribute_management("Annotations\Sectiontest4","CLEAR_SELECTION","")
... #arcpy.SelectLayerByLocation_management("Annotations\Sectiontest4","CROSSED_BY_THE_OUTLINE_OF", dfAsFeature, "","NEW_SELECTION")
... #arcpy.SelectLayerByLocation_management("Annotations\Sectiontest3","CROSSED_BY_THE_OUTLINE_OF", dfAsFeature, "","ADD_TO_SELECTION")
... #arcpy.SelectLayerByLocation_management("Annotations\Sectiontest2","CROSSED_BY_THE_OUTLINE_OF", dfAsFeature, "","ADD_TO_SELECTION")
... arcpy.SelectLayerByLocation_management("Annotations\BCParcelsAnno4","CROSSED_BY_THE_OUTLINE_OF", dfAsFeature, "","ADD_TO_SELECTION")
... arcpy.CalculateField_management("Annotations\BCParcelsAnno4","Status",1,"VB")
... #arcpy.CalculateField_management("Annotations\Sectiontest2","Status",1,"VB")
... #arcpy.CalculateField_management("Annotations\Sectiontest3","Status",1,"VB")
... #arcpy.CalculateField_management("Annotations\Sectiontest4","Status",1,"VB")
... arcpy.SelectLayerByAttribute_management("Annotations\BCParcelsAnno4","CLEAR_SELECTION","")
... #arcpy.SelectLayerByAttribute_management("Annotations\Sectiontest2","CLEAR_SELECTION","")
... #arcpy.SelectLayerByAttribute_management("Annotations\Sectiontest3","CLEAR_SELECTION","")
... #arcpy.SelectLayerByAttribute_management("Annotations\Sectiontest4","CLEAR_SELECTION","")
... #clears the current selection for pdf print
... arcpy.SelectLayerByAttribute_management("Kilometer Posts","CLEAR_SELECTION",where_clause_add)
... #refreshes the active view
... arcpy.RefreshActiveView
... #defines the variable textelement to a piece of text within the layout, which us the map #number
... #textElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","mapnumber")[0]
... #changes the text from 00 to 00 plus the variable textnumb
... #textElement.text = "00"+str(textnumb)
... #defines the variable textElementkpfrom which is for the kp from label in the layout
... #textElementkpfrom = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","backkp")[0]
... #changes the text in the kp from element to equal to the variable j
... #textElementkpfrom.text = j
... #defines the variable textElementkpto which is the kp to text piece in the layout
... #textElementkpto = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","frontkp")[0]
... #changes the kp to text element to equal the variable i
... #textElementkpto.text = i
... #defines the variable mxdaltname
... #mxdaltname = "TMX-Parcels-Map"+str(i)+".mxd"
... #saves a the current map to an mxd referencing the variable mxdaltname as the filename
... #mxd.saveACopy(r"\\swg.ca\files\Jobs\35000\35628 ED41 Kinder Morgan Canada Inc\500CAD\GIS\MapOutput\\"+mxdaltname)
... #defines the variable pdfname
... pdfname = "TMX-Parcels-Map"+str(i)+".pdf"
... #export the current map to pdf format using pdfname variable as the filename
... arcpy.mapping.ExportToPDF(mxd,r"\\swg.ca\files\Jobs\35000\35628 ED41 Kinder Morgan Canada Inc\500CAD\GIS\MapOutput\PDFOutput\\"+pdfname)
... #appends the created pdf pages to the PDFdoc that was assigned before the loop
... PDFdoc.appendPages(r"\\swg.ca\files\Jobs\35000\35628 ED41 Kinder Morgan Canada Inc\500CAD\GIS\MapOutput\PDFOutput\\"+pdfname)
...
...


Any help much appreciated.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
BruceNielsen
Occasional Contributor III
You have only set up a cursor at this point. To use it you need to loop through the features, like this:
for row in cursor:     rotation = row.getValue("Angle") #You could also use row.Angle here     ...


Also: Please review the sticky topic on posting Python code.

View solution in original post

0 Kudos
6 Replies
BruceNielsen
Occasional Contributor III
You have only set up a cursor at this point. To use it you need to loop through the features, like this:
for row in cursor:     rotation = row.getValue("Angle") #You could also use row.Angle here     ...


Also: Please review the sticky topic on posting Python code.
0 Kudos
DerekBannon
New Contributor
Thanks a TON!!! Bruce.

This helped me out.

for i in range (296, 356,1):
...       for row in cursor:
...           rotation = row.getValue("Angle")

Inserted the for loop for the variable row in cursor and assigned the rotation value and it ran great.

I still have yet to read the sticky on inserting python code into the forums.

Next time for sure.

Thanks Again!
0 Kudos
BruceNielsen
Occasional Contributor III
Don't forget to help future users by marking the question as answered.
0 Kudos
NobbirAhmed
Esri Regular Contributor
for i in range (296, 356,1):
    for row in cursor:
        rotation = row.getValue("Angle") 


Could you elaborate little more why you are using two for loops?

Follow the screenshot to insert code block. Click on the hash symbol and paste your code within CODE tags copying them from a Python editor (such as IDLE).

[ATTACH=CONFIG]14647[/ATTACH]
0 Kudos
DerekBannon
New Contributor
I needed the attribute value for features I was selecting based off of the range I was running in the first for loop. If that makes sense.
0 Kudos
NobbirAhmed
Esri Regular Contributor
Once your cursor is iterated over the first time, it becomes empty. So, even if you run the inner loop 5 or 10 times, it does not do anything. If you insert a print statement as the following code, you'll see that row values are not printed after the first iteraton.

for i in range (296, 356,1):
    print "Iteration", i
    for row in cursor:
        rotation = row.getValue("Angle")
        print rotation


Here, "Iteration n" will print 60 times but rotation will be printed only once (for the first iteration). That's why I wanted to know what is the purpose of the outer for loop. I'm sure you have something else there as per your need 🙂 I was just wondering what that could be.
0 Kudos