automated polygon movement?

1108
11
09-18-2012 12:44 PM
leifolson
New Contributor
Hi,

I am trying to write a script that takes a single polygon shapefile ( a facility footprint), a grid of planning units, and a landcover shapefile, and, for each planning unit gridcell, shifts the geographic location of the footprint shapefile, and uses the newly located footprint to erase the underlying landcover. What's the best way to iterate through the cells, and aligns the footprint shapefile with the center of the gridcell? Thanks...
Tags (2)
0 Kudos
11 Replies
markdenil
Occasional Contributor III
Collect the x and y locations of each polygon vertex, in order, as offsets from the lower left corner of the planning unit cell.
Also collect the lower left corner locations for each cell.

Use an Insert Cursor to draw new polygons, vertex by vertex.
For each polygon, use the lower left corner of each cell as the base point, and add the offsets to that for each vertex.

This will give a feature class with all the polygons you want: same shape, different location,
each located relative to the cell in which it lands.

Then do just one erase on the landcover.

This is likely easier than hopping the same polygon around and erasing each time.
0 Kudos
leifolson
New Contributor
thanks, that sounds like it will do the trick, much obliged!
0 Kudos
leifolson
New Contributor
So, i feel like i'm almost there, but im getting this error

Traceback (most recent call last):
** IDLE Internal Exception:
File "C:\Python27\ArcGISx6410.1\lib\idlelib\run.py", line 298, in runcode
   exec code in self.locals
File "N:\Projects\120802 CEMA Landscape Simulation Modelling for RMWB\07- Working Products and Drafts\code\trial_run.py", line 51, in <module>
  pntObj.X = pnt.X + xoffset
AttributeError: 'NoneType' object has no attribute 'X'



using the following script...
can anyone offer any insight? Thanks!
import arcpy, os

dist = r"N:\Projects\120802 CEMA Landscape Simulation Modelling for RMWB\05- Data\glink_seismic_Buffer_ClipExample.shp"
origin = r"N:\Projects\120802 CEMA Landscape Simulation Modelling for RMWB\05- Data\section_for_clipping_disturbance_features.shp"
sqrs = r"N:\Projects\120802 CEMA Landscape Simulation Modelling for RMWB\05- Data\sections_analysis.shp"
outfolder = r"N:\Projects\120802 CEMA Landscape Simulation Modelling for RMWB\05- Data"
outpoly = r"seismic_tile_example.shp"

try:
    arcpy.Delete_management(outfolder + "/" + outpoly)
except:
    pass

arcpy.CreateFeatureclass_management(outfolder, outpoly, "POLYGON", "", "", "", dist)
distdesc = arcpy.Describe(dist)
distshapefieldname = distdesc.ShapeFieldName
squaredesc = arcpy.Describe(sqrs)
squareshapefieldname = squaredesc.ShapeFieldName

originpoly = arcpy.SearchCursor(origin)
squares = arcpy.SearchCursor(sqrs)
inRows = arcpy.SearchCursor(dist)
outRows = arcpy.InsertCursor(outfolder + "/" + outpoly)

polyarray = arcpy.Array()


origindesc = arcpy.Describe(origin)
originshapefieldname = origindesc.ShapeFieldName
orig = originpoly.next()
originvalue = orig.getValue(originshapefieldname)
origincentroid = originvalue.centroid


for square in squares:

    squarevalue = square.getValue(squareshapefieldname)
    squarecentroid = squarevalue.centroid
    xoffset = squarecentroid.X - origincentroid.X
    yoffset = squarecentroid.Y - origincentroid.Y

    polyIndex = 0
    for inRow in inRows:
        inShape = inRow.shape
        pntObj = arcpy.Point()
        arrayObj = arcpy.Array()
        for pnt in inShape.getPart(polyIndex):
            pntObj.ID = polyIndex
            pntObj.X = pnt.X + xoffset
            pntObj.Y = pnt.Y + yoffset
            arrayObj.add(pntObj)
        outShape = outRows.newRow()
        outShape.shape = arrayObj
        outRows.insertRow(outShape)
        polyIndex += 1



0 Kudos
ClayPerry
New Contributor
I don't see where 'pnt' is defined, so it can't have an attribute 'X'.
0 Kudos
leifolson
New Contributor
I don't see where 'pnt' is defined, so it can't have an attribute 'X'.


its defined in the for loop

for pnt in inShape.getPart(polyIndex):



and i should note that if i add a try: except: routine, some polygons will be written, but not all.
0 Kudos
ClayPerry
New Contributor
Ha, now I see it.  It's hard to read unformatted.

So, it looks like you initially get an array of x,y values for the first part of the polygon with this:

for pnt in inShape.getPart(polyIndex):

Now you need to pull the the individual points from the array that is the first part of the first polygon record in your feature class.  Think of multipart features.
0 Kudos
leifolson
New Contributor
Ha, now I see it.  It's hard to read unformatted.

So, it looks like you initially get an array of x,y values for the first part of the polygon with this:

for pnt in inShape.getPart(polyIndex):

Now you need to pull the the individual points from the array that is the first part of the first polygon record in your feature class.  Think of multipart features.



yeah, the posting software seems to be dropping the tabs from the code, my apologies.

But the feature im copying is not multipart...
0 Kudos
markdenil
Occasional Contributor III
First off: use the '#' button in the Forum reply formatting bar to wrap your code in code tags

you want:
    this

instead of:
    this

I would take the original polygon point array and build a nested list of the x,y pairs from it. (featureList)
Then I would make another nested list of the individual squares' origin points.  (originList)

One advantage is that it is easy to inspect these lists if things are not coming out right.

Then the insert cursor just gets created once, and you loop through originList.
for each origin in originList, you draw a feature
(one loop through featureList adding the origin x or y to each coordinate)

Your script seems to have nested or overlapping cursors, and you get each origin
(and each feature point? ...reading unformtted code is confusing at the best of times)
directly from the reference fc. That just seems overly complex.

Eschew Complexity.
0 Kudos
leifolson
New Contributor
ahh, thanks, hadn't known what the # button did. I've corrected my post above.


What do you mean by a 'nested list'?
0 Kudos