# Calculate Polygon Angles at Vertices

Discussion created by petermetzger on Jul 11, 2012
Latest reply on Mar 14, 2013 by carlsunderman
When I started this job someone had already had a contractor survey a bunch of our buildings at our installations and look at our cad drawings to make building footprints for real property management purposes.  I've been aligning floorplans to the these footprints, and much to my chagrin I find that a lot of these buildings that were entered are not square (apparently they thought maybe we buildt structures with 95° angles, needless to say we don't).  So now I'm writing a script to go through and calculate the angles at each vertex of the polygon so we can find out which buildings need to be resurveyed.  Right now I have the script so that it will calculate the vector of each line from the X axis.  But, what I need it to do is select the lines that interesect the vertex (the polygons are converted to polylines and then exploded, and points are created at the vertices) and I think I have that part down, but now I need to add the selected two lines vectors together to get the angle and put it in the field for angles at that vertex.  I then need to step through the process for each of the thousand some vertices with either a for or while loop.  I'm new to python but I think I can figure out the loop, but doing the math with the selected features is confusing me (actually any help with the loop would be nice too).

Thanks in advance, here's the code I have.
```#
# 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 = "C:\Users\peter.a.metzger\Desktop\Projects\CGN_Building_survey_validating\Building_Validate.gdb"
inFeatures = "Building_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 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")

#Step through each OBJECTID and select the polygons that share the point and subtract their values
##Select the vertex for the angle##

#NEED TO CHANGE TO A FOR LOOP#
#THIS IS WHERE I'M STUCK#
#NEED TO BE ABLE TO SUBTRACT THE TWO SELECTED LINES FROM ONE ANOTHER#
obid=1
arcpy.SelectLayerByAttribute_management ("Poly2Point_lyr", "NEW_SELECTION", "[OBJECTID] = 'obid'")

#select the two exploded lines from which to calculate the vertex angle

arcpy.SelectLayerByLocation_management ("explodedLine_lyr", "INTERSECT","Poly2Point_lyr", "", "NEW_SELECTION")
```