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
Solved! Go to Solution.
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
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
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