2

I want to rotate some polygons according to the value of azimuth from attribute table. I try to use this code but there is an error that I can not understand.

Polygons to rotate

CODE

import arcpy, math

fc = 'Polygons' #Change to match your data
rotatefield = 'azimuth' #Clockwise rotation in degress. Change name to match your data

#Function to rotate one input Point around a pivot point
def rotatepoint(point, pivotpoint, angle):
    #Source: https://stackoverflow.com/questions/34372480/rotate-point-about-another-point-in-degrees-python
    angle_rad = -math.radians(angle)
    ox, oy = pivotpoint.X, pivotpoint.Y
    px, py = point.X, point.Y
    qx = ox + math.cos(angle_rad) * (px - ox) - math.sin(angle_rad) * (py - oy)
    qy = oy + math.sin(angle_rad) * (px - ox) + math.cos(angle_rad) * (py - oy)    
    return arcpy.Point(qx,qy)

#Rebuild each polygon with rotated vertices
with arcpy.da.UpdateCursor(fc,['SHAPE@',rotatefield]) as cursor:
    for row in cursor:
        polylist = []
        for part in row[0]:
            partlist = []
            for pnt in part:
                if pnt is not None: #Polygons with inner rings will have None pnt(s) which can be skipped
                    partlist.append(rotatepoint(pnt, row[0].centroid, row[1])) #Centroid is pivot point
                    polylist.append(partlist)
        row[0] = arcpy.Polygon(arcpy.Array(polylist))
        cursor.updateRow(row)

Error:

_Runtime error 
Traceback (most recent call last):
  File "<string>", line 20, in <module>
TypeError: 'NoneType' object is not iterable_

closed as off-topic by BERA, Dan C, whyzar, xunilk, Erik Feb 2 at 8:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – BERA, Dan C, whyzar, xunilk, Erik
If this question can be reworded to fit the rules in the help center, please edit the question.

  • The code is working. Have you changed fc source to match your data? – BERA Jan 31 at 17:48
  • And have you spatial joined the azimuth field from your points (from your earlier question)? – BERA Jan 31 at 18:00
  • Yes, I changed. My polygon shape contains the field with the azimuths as well. I use this field in the "rotatefield". – Ana Beatriz Freitas Jan 31 at 18:04
  • @BERA Do you think that this error could be related with the format of the azimuth? What should be the format? DDD, DMM or DMS? – Ana Beatriz Freitas Jan 31 at 23:22
  • It should be in degrees from 0-360, read the comments in the code. Edit your question and replace the code with exactly the one you are trying with. – BERA Feb 1 at 5:55
1

The code is working when I try it. Make sure you have changed input fc name and you probably need to use a projected coordinate system with units of meters/feets/etc and not degrees.

Could you try if this give you error:

fc = 'somename' #Change
with arcpy.da.SearchCursor(fc,'SHAPE@') as cursor:
    for row in cursor:
        polylist = []
        for part in row[0]:
            partlist = []
            for pnt in part:
                pass
  • I did what you suggested but the error still happen. Where should I insert this new code? – Ana Beatriz Freitas Feb 1 at 15:52
  • You should try it on its own, dont use any of the other code – BERA Feb 1 at 15:56
  • I tried again and now it worked! One last question, is it possible to choose the face of the polygon that indicates the direction? The polygons were rotated but were inverted (mirrored). – Ana Beatriz Freitas Feb 1 at 17:10
  • 1
    Nice! You should be able to recalculate the angles prior to rotating the polygons – BERA Feb 1 at 17:16

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