HOW TO
In ArcGIS Pro, rotating polygon features based on attribute values is important for aligning building footprints or parcels to real-world orientations. Automating this process with ArcPy ensures precision and efficiency, especially for large datasets.
This article describes the workflow to dynamically rotate polygon features using ArcPy based on the angle values stored in the attribute field. The map below shows the polygon features in the original position before rotation.
import arcpy import math
def rotate_polygon_geometry(polygon, rotation_angle, centroid): rotated_parts = []
angle_rad = -math.radians(rotation_angle)
for part in polygon: rotated_part = [] for point in part: if point:
x_new = centroid.X + math.cos(angle_rad) * (point.X - centroid.X) - math.sin(angle_rad) * (point.Y - centroid.Y) y_new = centroid.Y + math.sin(angle_rad) * (point.X - centroid.X) + math.cos(angle_rad) * (point.Y - centroid.Y) rotated_part.append(arcpy.Point(x_new, y_new)) rotated_parts.append(rotated_part)
return arcpy.Polygon(arcpy.Array(rotated_parts))
def rotate_feature_class(): feature_class = "<feature_layer_name>" rotation_field = "<field_name>"
try: with arcpy.da.UpdateCursor(feature_class, ['SHAPE@', rotation_field]) as cursor: for row in cursor: geometry = row[0] rotation_angle = row[1]
centroid = geometry.centroid
new_geometry = rotate_polygon_geometry(geometry, rotation_angle, centroid)
row[0] = new_geometry cursor.updateRow(row) print(f"Rotation applied successfully to {feature_class}.") except Exception as e: print(f"Error: {e}")
rotate_feature_class()
The code below demonstrates the full working script.
import arcpy import math def rotate_polygon_geometry(polygon, rotation_angle, centroid): rotated_parts = [] angle_rad = -math.radians(rotation_angle) for part in polygon: rotated_part = [] for point in part: if point: x_new = centroid.X + math.cos(angle_rad) * (point.X - centroid.X) - math.sin(angle_rad) * (point.Y - centroid.Y) y_new = centroid.Y + math.sin(angle_rad) * (point.X - centroid.X) + math.cos(angle_rad) * (point.Y - centroid.Y) rotated_part.append(arcpy.Point(x_new, y_new)) rotated_parts.append(rotated_part) return arcpy.Polygon(arcpy.Array(rotated_parts)) def rotate_feature_class(): feature_class = "Unmarked123" rotation_field = "RotationField" try: with arcpy.da.UpdateCursor(feature_class, ['SHAPE@', rotation_field]) as cursor: for row in cursor: geometry = row[0] rotation_angle = row[1] centroid = geometry.centroid new_geometry = rotate_polygon_geometry(geometry, rotation_angle, centroid) row[0] = new_geometry cursor.updateRow(row) print(f"Rotation applied successfully to {feature_class}.") except Exception as e: print(f"Error: {e}") rotate_feature_class()
The map below displays the polygon features rotated based on the angle values in ArcGIS Pro.
Get help from ArcGIS experts
Download the Esri Support App