HOW TO

Rotate polygon features based on angles using ArcPy in ArcGIS Pro

Last Published: January 8, 2025

Summary

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.

Map displaying polygon features before rotation

Procedure

  1. Open the ArcGIS Pro project.
  2. Open the Python window. Refer to ArcGIS Pro: Python window for instructions.
  3. Run the following script:
    1. Import the necessary modules.
import arcpy
import math
  1. Specify the function to rotate the polygon geometry.
def rotate_polygon_geometry(polygon, rotation_angle, centroid):
    rotated_parts = []
  1. Convert the angle from degrees to radians.
    angle_rad = -math.radians(rotation_angle)
  1. Create a loop to iterate through each part of the polygon.
    for part in polygon:
        rotated_part = []
        for point in part:
            if point:
  1. Calculate the rotating point using the centroid as the pivot.
                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)
  1. Return the new rotated polygon geometry.
    return arcpy.Polygon(arcpy.Array(rotated_parts))
  1. Specify the function to process the feature class. Replace <feature_layer_name> with the feature layer name and <field_name> with the field name that stores the rotation angles.
def rotate_feature_class():
    feature_class = "<feature_layer_name>"  
    rotation_field = "<field_name>"  
  1. Create an update cursor to iterate through the features.
    try:
        with arcpy.da.UpdateCursor(feature_class, ['SHAPE@', rotation_field]) as cursor:
            for row in cursor:
                geometry = row[0]
                rotation_angle = row[1]
  1. Calculate the centroid of each polygon.
                centroid = geometry.centroid
  1. Rotate the polygon geometry.
                new_geometry = rotate_polygon_geometry(geometry, rotation_angle, centroid)
  1. Update the row with the newly rotated geometry.
                row[0] = new_geometry
                cursor.updateRow(row) 

        print(f"Rotation applied successfully to {feature_class}.")
    
    except Exception as e:
        print(f"Error: {e}")
  1. Run the rotation process.
rotate_feature_class()
  1. Place the cursor at the end of the script and press Enter twice to run the script.

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()
  1. Zoom or pan the map to view the features. Refer to ArcGIS Pro: Navigation options for more information.

The map below displays the polygon features rotated based on the angle values in ArcGIS Pro.

Map displaying rotated polygon features

Article ID: 000034276

Software:
  • ArcGIS Pro 3 3
  • ArcGIS Pro 3 2
  • ArcGIS Pro 3 4

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options