dustfurn

difference between assigning an arcpy.Polygon or an arcpy.Array to feature.shape

Discussion created by dustfurn on Oct 7, 2011
Latest reply on Oct 7, 2011 by dustfurn
Hi,

I'm continuing to have problems creating polygons programmatically. 

For example:

(This polygon is correct)


was created with this script:
#interior
coordList = [[[-88.680463013034853, 38.152519617763254], [-88.680463013034853, 38.15068957953018], [-88.678182297717754, 38.15068957953018], [-88.678182297717754, 38.152519617763254], [-88.680463013034853, 38.152519617763254]]]

def main():

    outputShapeDir = r"c:\temp"
    outputShapeFile = r"test.shp"
    prjFile = os.path.join(arcpy.GetInstallInfo()["InstallDir"],
                       "Coordinate Systems/Geographic Coordinate Systems/North America/NAD 1983.prj")
    spatialRef = arcpy.SpatialReference(prjFile)
    newFC = arcpy.CreateFeatureclass_management( outputShapeDir, outputShapeFile, "POLYGON", "", "", "", spatialRef )

    cur = arcpy.InsertCursor(newFC)
    array = arcpy.Array()
    point = arcpy.Point()
    
    # Create a list to store the features
    features = []
    # Read the coordinates
    for part in coordList:
        for coordPair in part:
            point.X = coordPair[0]
            point.Y = coordPair[1]
            array.add(point)

    print "points added to array: "
    for point in array:
      print point.X,point.Y

    #print "points in polygon: "
    #polygon = arcpy.Polygon( array )
    #for part in polygon:
    #  for point in part:
    #    print point.X,point.Y
       
    feat = cur.newRow()
    feat.shape = array
    cur.insertRow( feat )

if __name__ == '__main__':
    import arcpy
    import os
    arcpy.env.overwriteOutput = True
    main()

Note that I never created an arcpy.Polygon, i simply assigned my arcpy.Array, filled with arcpy.Points, to the feature's "shape" property.

with this printed output:
points added to array: 
-88.680463013 38.1525196178
-88.680463013 38.1506895795
-88.6781822977 38.1506895795
-88.6781822977 38.1525196178
-88.680463013 38.1525196178


However, a null (empty) geometry was produced with the following code.  Note the very minor difference of creating an arcpy.Polygon with "array" and assigning that result to "feat.shape" instead of assigning "array" directly (which is actually how the arcpy documentation shows how to write polygons, by the way).

coordList = [[[-88.680463013034853, 38.152519617763254], [-88.680463013034853, 38.15068957953018], [-88.678182297717754, 38.15068957953018], [-88.678182297717754, 38.152519617763254], [-88.680463013034853, 38.152519617763254]]]

def main():

    outputShapeDir = r"c:\temp"
    outputShapeFile = r"test.shp"
    prjFile = os.path.join(arcpy.GetInstallInfo()["InstallDir"],
                       "Coordinate Systems/Geographic Coordinate Systems/North America/NAD 1983.prj")
    spatialRef = arcpy.SpatialReference(prjFile)
    newFC = arcpy.CreateFeatureclass_management( outputShapeDir, outputShapeFile, "POLYGON", "", "", "", spatialRef )

    cur = arcpy.InsertCursor(newFC)
    array = arcpy.Array()
    point = arcpy.Point()
    
    # Create a list to store the features
    features = []
    # Read the coordinates
    for part in coordList:
        for coordPair in part:
            point.X = coordPair[0]
            point.Y = coordPair[1]
            array.add(point)

    print "points added to array: "
    for point in array:
      print point.X,point.Y

    print "points in polygon: "
   polygon = arcpy.Polygon( array )
    for part in polygon:
      for point in part:
        print point.X,point.Y
       
    feat = cur.newRow()
   feat.shape = polygon
    cur.insertRow( feat )

if __name__ == '__main__':
    import arcpy
    import os
    arcpy.env.overwriteOutput = True
    main()


Here is the print output:
points added to array: 
-88.680463013 38.1525196178
-88.680463013 38.1506895795
-88.6781822977 38.1506895795
-88.6781822977 38.1525196178
-88.680463013 38.1525196178
points in polygon: 


As you can see, after the arcpy.Polygon, "polygon", is created with the contents of "array", the resultant polygon is empty. Why?  No exceptions are thrown, and the program completes normally.  What is the difference, functionally, in creating geometry between these two methods?

Thanks for any help,
Dustin

Outcomes