Select to view content in your preferred language

nesting UpdateCursor within SearchCursor and more...?

736
2
Jump to solution
12-17-2012 01:40 PM
deleted-user-iREoAvyfnLld
Deactivated User
Hello All,

I am new to python and I'm in the learning/testing phase.

From borrowing various snipits, I am calculating the azimuth of a line segment then attempting to write that value to the attribute table. Here's my code, then I'll get to the question. 

import arcpy from arcpy import env from math import atan2, pi env.workspace = "c:/my_data/test_data.gdb" fc = "polyline1" # polyline1 has pre-created "azimuth" field and is a single segment feature class   #angleList []  angleMath = arcpy.da.SearchCursor(fc, ["OID@", "SHAPE@"])   for row in angleMath:     startpt = row[1].firstPoint     x1 = startpt.X     y1 = startpt.Y     endpt = row[1].lastPoint     x2 = endpt.X     y2 = endpt.Y     deltax = x2 - x1     deltay = y2 - y1     angle_rad = atan2(deltay,deltax)     angle_deg = angle_rad*180.0/pi          #angleList.append      cursor = arcpy.UpdateCursor(fc)     for upRow in cursor:         upRow.azimuth = angle_deg         cursor.updateRow(upRow)          del cursor  del angleMath


I am able to get the angle_deg value and my commented angleList list works but I'm not sure where/how to use it. I know my UpdateCursor is in the wrong spot. It updates the first angle_deg value for all records. Is there a way to use the UpdateCursor to write the value of the angle_deg variable for each record?

Or would it be best to try to use the angleList list with an UpdateCursor and if so, how do you use a list to feed an UpdateCursor?

Thank you for any help you can provide.

Wesley
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
deleted-user-iREoAvyfnLld
Deactivated User
Thanks for the post Matt, it got me going in the right direction. With the path I was going down, I needed to use the arcpy.da.UpdateCursor to access the geometry "SHAPE@". However, this cursor list would not recognize the "azimuth" field for some reason. So, I scrapped the arcpy.da.UpdateCursor to access the geometry and went with another method mentioned in the ArcGIS 10.1 help. I also tweaked my math to get north azimuth. The code below worked!


import arcpy from arcpy import env from math import atan2, pi env.workspace = "c:/my_data/test_data.gdb" fc = "polyline1" # polyline1 has pre-created "azimuth" field and is a single segment feature class  desc = arcpy.Describe(fc) shapefieldname = desc.ShapeFieldName  angleMath = arcpy.UpdateCursor(fc)   for row in angleMath:      feat = row.getValue(shapefieldname)          startpt = feat.firstPoint     x1 = startpt.X     y1 = startpt.Y     endpt = feat.lastPoint     x2 = endpt.X     y2 = endpt.Y     deltax = x2 - x1     deltay = y2 - y1     angle_rad = atan2(deltay,deltax)     angle_deg_pre = (((angle_rad*180.0/pi) - 90)*-1)      if angle_deg_pre < 0:         angle_deg = (((angle_rad*180.0/pi) - 90)*-1)+360     else:         angle_deg = (((angle_rad*180.0/pi) - 90)*-1)              row.azimuth = angle_deg     angleMath.updateRow(row)        del row, angleMath

View solution in original post

0 Kudos
2 Replies
MattSayler
Frequent Contributor
I think you can get away with using just an Update cursor, as it has the same methods as a Search cursor in addition to the ones for writing. You also won't need to store the values in a list since you can write within the same for loop.

import arcpy
from arcpy import env
from math import atan2, pi
env.workspace = "c:/my_data/test_data.gdb"
fc = "polyline1"

angleMath = arcpy.UpdateCursor(fc, ["OID@", "SHAPE@","azimuth"])
for row in angleMath:
    #calc azimuth
    startpt = row[1].firstPoint
    x1 = startpt.X
    y1 = startpt.Y
    endpt = row[1].lastPoint
    x2 = endpt.X
    y2 = endpt.Y
    deltax = x2 - x1
    deltay = y2 - y1
    angle_rad = atan2(deltay,deltax)
    angle_deg = angle_rad*180.0/pi

    #write azimuth
    row.azimuth = angle_deg
    angleMath.updateRow(row)

 del row, angleMath
 


Might need some tweaks (parameters for the Update cursor), but should be pretty close.
0 Kudos
deleted-user-iREoAvyfnLld
Deactivated User
Thanks for the post Matt, it got me going in the right direction. With the path I was going down, I needed to use the arcpy.da.UpdateCursor to access the geometry "SHAPE@". However, this cursor list would not recognize the "azimuth" field for some reason. So, I scrapped the arcpy.da.UpdateCursor to access the geometry and went with another method mentioned in the ArcGIS 10.1 help. I also tweaked my math to get north azimuth. The code below worked!


import arcpy from arcpy import env from math import atan2, pi env.workspace = "c:/my_data/test_data.gdb" fc = "polyline1" # polyline1 has pre-created "azimuth" field and is a single segment feature class  desc = arcpy.Describe(fc) shapefieldname = desc.ShapeFieldName  angleMath = arcpy.UpdateCursor(fc)   for row in angleMath:      feat = row.getValue(shapefieldname)          startpt = feat.firstPoint     x1 = startpt.X     y1 = startpt.Y     endpt = feat.lastPoint     x2 = endpt.X     y2 = endpt.Y     deltax = x2 - x1     deltay = y2 - y1     angle_rad = atan2(deltay,deltax)     angle_deg_pre = (((angle_rad*180.0/pi) - 90)*-1)      if angle_deg_pre < 0:         angle_deg = (((angle_rad*180.0/pi) - 90)*-1)+360     else:         angle_deg = (((angle_rad*180.0/pi) - 90)*-1)              row.azimuth = angle_deg     angleMath.updateRow(row)        del row, angleMath
0 Kudos