0

I found this from @crmackey at Creating rectangles on centroid points in ArcPy?

It is exactly what I was looking for and works great, however I need to add rotation to the polygons.

How could I add this to @crmackey's python script?

  • Welcome to GIS SE! Thank you for having taken the Tour. For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet in your question. There is an edit button beneath your question which will enable you to do that and a {} button that enables you to format any highlighted code nicely. – PolyGeo Feb 15 '18 at 10:31
2

You can simply add some math to your coordinates:

X = x*cos(θ) - y*sin(θ)
Y = x*sin(θ) + y*cos(θ)

or in this case, if the point is not (0,0):

X = x + xvariation*cos(θ) - yvariation*sin(θ)
Y = x + xvariation*sin(θ) + yvariation*cos(θ)

The code become:

import arcpy 
import math

arcpy.env.overwriteOutput = True
fc = mypointfeatureclass

fields = ['SHAPE@XY']

h = 10                 #hight
w = 20                 #width
r = (10/360)*math.pi*2 #rotation (10° in this case)

# We define a small function which calculate the rectangle coordinates from point
def rotatexy(x,y,w,h,r):
    ul = arcpy.Point(x-w/2*math.cos(r)-h/2*math.sin(r),-w/2*math.sin(r)+y+h/2*math.cos(r))
    ll = arcpy.Point(x-w/2*math.cos(r)+h/2*math.sin(r),-w/2*math.sin(r)+y-h/2*math.cos(r))
    lr = arcpy.Point(x+w/2*math.cos(r)+h/2*math.sin(r),w/2*math.sin(r)+y-h/2*math.cos(r))
    ur = arcpy.Point(x+w/2*math.cos(r)-h/2*math.sin(r),w/2*math.sin(r)+y+h/2*math.cos(r))
    return arcpy.Array([ul,ll,lr,ur])


polygons = []

# For each points in feature class we create a new polygon
with arcpy.da.SearchCursor(fc, fields) as cursor:
   for row in cursor:
       X = row[0][0]
       Y = row[0][1]
       print("X: {}, Y: {}".format(X,Y))
       polygon = arcpy.Polygon(arcpy.Array([rotatexy(X,Y,w,h,r)]))
       polygons.append(polygon)

# Save the output
arcpy.CopyFeatures_management(polygons,outputrectangle)      

Not the answer you're looking for? Browse other questions tagged or ask your own question.