Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

This to me sounded very simple so I am probably missing something basic here. What I need to do is to create a copy of a polygon shapefile to a new shapefile with slightly different attributes.

Where I got stuck is in copying the geometry - Geometry doesn't have to change at all in the new shapefile. The actual 'copying' of the polygon geometry is not the problem. I loop through the input shapefile:

> for row in rows:
>     partnum = 0
>     feat=row.getValue("SHAPE")

And then I enter this shape in the output feature class:

outFeat.Shape=feat

Problem is, I also want to calculate the Area, Perimeter and centroid of the polygon and I can't seem to 'cast' the feat variable to a polygon so I can then get the polygon properties (area, centroid, etc). Can you do that somehow?

One of the things I tried was to create the polygon from scratch, i.e. loop though the vertices of the input polygon and create a new polygon/array to use for the new polygon. This method apart from being IMO over the top, it also had the problems with the multi-part polygons. My polygons ended up corrupted - although I followed the steps outlined here

I didn't dwell too much with it since what I did instead is create a dummy Polygon which I then passed to it the current geometry value in the cursor.

# Create a dummy polygon
            dummyArray=arcpy.Array()
            dummyPoint = arcpy.Point()
            coordList = [[[1,2], [2,4], [3,7]],[[6,8], [5,7], [7,2], [9,5]]]
            for feature in coordList:
                for coordPair in feature:
                    dummyPoint.X = coordPair[0]
                    dummyPoint.Y = coordPair[1]
                    dummyArray.add(dummyPoint)
                dummyArray.add(dummyArray.getObject(0))

            parcel=arcpy.Polygon(dummyArray)
            parcel=feat ### --> This is where I replace the dummy geometry value with the current value in the loop
            parcel_c=arcpy.Point()
            parcel_c=parcel.centroid # I can now get to the polygon properties
            c_x=parcel_c.X
            c_y=parcel_c.Y

Although this works fine, I am not really happy with it as its a bit of a hacking and -as mentioned in the beginning- I think I am missing something vital!

Any insights?

share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

You're pretty close with your first script, you have to jump one more hurdle to access the geometry object. Here's the ESRI help docs on working with geometry in Python

The key is here:

shapeName = arcpy.Describe(inFeatures).shapeFieldName

Once you have a handle for the object you can use it like in your script, and you'll have access to all of the polygon properties that you're after.

for row in rows:
    partnum = 0
    feat=row.getValue(shapeName)
share|improve this answer
    
Thanks @Roy Will give that a go and let you know –  mapoholic Jul 25 '12 at 17:04
    
So thats what the .shapeFieldName does! :-) I thought it was only for getting the spatial column name at runtime... I knew I was missing something! Many thanks again @Roy ! –  mapoholic Jul 26 '12 at 8:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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