简体   繁体   中英

Getting (some) points of a polygon

I am trying to grab the vertices from a polygon and do some stuff to them to recreate the polygon in a new location/rotation (essentially this: https://community.esri.com/thread/46497 ). The example code below is not exactly what I am doing, but showcases the issue. The code would work except that after it grabs the last vertex of the polygon, it throws an error message which breaks the script and stops everything else from running to draw the new polygon. Otherwise, if I go through my code line-by-line I can continue on and create the new polygon feature:

AttributeError: 'NoneType' object has no attribute 'X'

Is there a way that I can use the loop to run through all except the "last" vertex, which either has an issue or doesn't exist?

import arcpy
import os
import random
import math

pa = 'protected_areas' # protected areas
sr = arcpy.Describe(pa).spatialReference # spatial ref
sa = 'study_area' # study area
x = [] # placeholder

with arcpy.da.SearchCursor(pa,'SHAPE@',spatial_reference=sr) as cursor: # for each polygon
    for row in cursor:
        centroid = row[0].centroid # calculate centroid
        poly = row[0]

for part in poly: # for each polygon part
    for pnt in part: # for each vertex
        x.append(pnt.X)

You can iterate over an index and skip the las element, changing

 for pnt in part: # for each vertex
      x.append(pnt.X)

To

for k in range(len(pnt)-1): # for each vertex
    x.append(pnt[k].X)

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM