Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Polygon Added to Feature Class has Zero Area and Length

$
0
0
I'm copying features (polygons) from one feature class to a new feature class, using arcpy.Polygon() and an insert cursor. (I'm basically doing a copy, the hard way. But Append() wasn't working for me...long story, so here I am...) In my code below, the record for the polygon is added, but the area and length fields are zero. When I print out the area and length values from the object created from Polygon(), I get zero values as well. The last point in the array of points for the polygon is also the first point, so the polygon should be closed. Any ideas/suggestions about what I'm doing wrong?
Here is my code:

import os, arcpy

coordList = []

copyFeats = arcpy.SearchCursor(r"C:\temp\GDBs\20130521203526.gdb\polygon")
for feat in copyFeats:
poly = []
for feat in feat.shape.getPart():
for pnt in feat:
poly.append([pnt.X,pnt.Y])
coordList.append(poly)

print "coordList: %s" % str(coordList)

# Create empty Point and Array objects
point = arcpy.Point()
array = arcpy.Array()

# A list that will hold each of the Polygon objects
featureList = []
sr = arcpy.Describe(r"C:\temp\GDBs\20130515145336.gdb\polygon").spatialReference
for feature in coordList:
# For each coordinate pair, set the x,y properties and add to the
# Array object.
for coordPair in feature:
point.X = coordPair[0]
point.Y = coordPair[1]
print "add %s to array" % (str(coordPair[0]) + ' ' + str(coordPair[1]))
array.add(point)

# Create a Polygon object based on the array of points
print "creating polygon"
polygon = arcpy.Polygon(array)
print 'area: %s, length: %s' % (str(polygon.area),str(polygon.length))
# Clear the array for future use
array.removeAll()

# Append to the list of Polygon objects
featureList.append(polygon)

arcpy.CreateFeatureclass_management(r"C:\temp\GDBs\20130521203526.gdb", "polygon_py", "POLYGON", "","DISABLED","DISABLED", sr)

ins = arcpy.InsertCursor(r"C:\temp\barbados\GDBs\Ruth_20130521203526.gdb\polygon_py")
for poly in featureList:
print "insert polygon"
row = ins.newRow()
row.shape = poly
ins.insertRow(row)

del row
del ins

Viewing all articles
Browse latest Browse all 2485

Trending Articles