So I went to the UC last week and had a very nice girl (Thanks Stephanie!) give me a hand on this, and we (mostly she) figured out how to get the angle at the vertices. After the two lines at the vertex are selected the are exported to a feature class in memory and then a search cursor is used to get their angles, which are then put into a list and then subtracted from one another. They are then put back into the layer file using an UpdateCursor. This all worked great... when I was doing it on one vertex. Once I tried to put it into a loop I keep getting an "IndexError: list index out of range". Any help on what I have done wrong would be greatly appreciated.#
#
# This Script Validates if the surveyed building footprints are square enough to be acceptable#
#
#
# Import system modules
import arcpy
import math
from arcpy import env
# Set environment settings
arcpy.env.workspace = "in_memory"
inFeatures = "C:/Users/xxxxxxxx/Desktop/Projects/CGN_Building_survey_validating/test.gdb/test_footprint"
#Turn Polygon into Polyline in scratch workspace
arcpy.PolygonToLine_management (inFeatures, "polygon2polyline")
#explode polyline into individual lines
arcpy.SplitLine_management ("polygon2polyline", "explodedLine")
#convert polygon vertices to points
arcpy.FeatureVerticesToPoints_management (inFeatures, "Poly2Point")
# Set local variables
fieldName = "Angle"
expression = "GetAzimuthPolyline(!Shape!)"
codeblock = """def GetAzimuthPolyline(shape):
radian = math.atan((shape.lastpoint.x - shape.firstpoint.x)/(shape.lastpoint.y - shape.firstpoint.y))
degrees = radian * 180 / math.pi
return degrees"""
# Execute AddField
arcpy.AddField_management("explodedLine", fieldName, "Double", 6)
arcpy.AddField_management("Poly2Point", fieldName, "Double", 6)
# Execute CalculateField
arcpy.CalculateField_management("explodedLine", fieldName, expression, "PYTHON_9.3", codeblock)
# Make Poly2Point and explodedLine a layer file so that select by attribute can read it
arcpy.MakeFeatureLayer_management("Poly2Point", "Poly2Point_lyr")
arcpy.MakeFeatureLayer_management("explodedLine", "explodedLine_lyr")
#Identify how many vertices need to be calculated
numb_vertices = int(arcpy.GetCount_management("Poly2Point_lyr").getOutput(0))
print numb_vertices
#Loop through each vertex by OBJECTID
for i in range (0, numb_vertices):
obj = "OBJECTID=%s" %(i)
arcpy.SelectLayerByAttribute_management ("Poly2Point_lyr", "NEW_SELECTION", obj)
#select the two exploded lines from which to calculate the vertex angle
arcpy.SelectLayerByLocation_management ("explodedLine_lyr", "INTERSECT","Poly2Point_lyr", "", "NEW_SELECTION")
#Export those lines to a new feature class
arcpy.FeatureClassToFeatureClass_conversion ("explodedLine_lyr", "in_memory", "calc_values")
#Create List to do angle math
Angle_math = []
#Return Angle_Values
rows = arcpy.SearchCursor ("calc_values","","","Angle")
current_Angle_Object = ""
#Iterate through the rows in the cursor and add them to the list
for row in rows:
if current_Angle_Object !=row.Angle:
current_Angle_Object = row.Angle
Angle_math.append(row.Angle)
#Calculate the angle at the vertex
Angle_Final = 180 - Angle_math[0] - Angle_math[1]
del row
del rows
print(Angle_Final)
#add data to the layer file
rows = arcpy.UpdateCursor("Poly2Point_lyr")
for row in rows:
row.Angle = Angle_Final
rows.updateRow(row)
del row
del rows
del Angle_math
#Export all the final angles in Poly2Point_lyr to a feature class
angle_output_location = "C:/Users/xxxxxxxxx/Desktop/Projects/CGN_Building_survey_validating/test.gdb/Vertex_Angles"
arcpy.CopyFeatures_management("Poly2Point_lyr", angle_output_location)