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

1881
4
10-07-2011 10:42 AM
DustinReagan
New Contributor
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
Tags (2)
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus
Is suspect that your latter fails since you are passing an array and not an array of point objects to the polygon class.  follow the example in the help file
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Polygon/000v000000n1000000/
and in the previous discussion on creating polygons with donuts in them
http://forums.arcgis.com/threads/18985-Bug-Creating-polygons-with-holes-using-Arcpy?p=61005&posted=1...
0 Kudos
DustinReagan
New Contributor
Is suspect that your latter fails since you are passing an array and not an array of point objects to the polygon class.  follow the example in the help file
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Polygon/000v000000n1000000/
and in the previous discussion on creating polygons with donuts in them
http://forums.arcgis.com/threads/18985-Bug-Creating-polygons-with-holes-using-Arcpy?p=61005&posted=1...


Hi, I may be missing something, but as far as I can tell I am very clearly passing an arcpy.Array filled with arcpy.Point objects to the arcpy.Polygon factory:

    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 )


Is this not actually passing an array of point objects to the polygon object? 

Thanks for your time,
Dustin
0 Kudos
DustinReagan
New Contributor
Is suspect that your latter fails since you are passing an array and not an array of point objects to the polygon class.  follow the example in the help file
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Polygon/000v000000n1000000/


Hi,

My method is functionally equivalent to the example described in this link (in the sense that an arcpy.Polygon is created with an arcpy.Array filled with the exact same data, in both my example and the example you linked to).  To double check, I used the example script, except with my data.  It still silently generates a null (empty) polygon:

import arcpy

# A list of features and coordinate pairs
coordList = [[[-88.680463013034853, 38.152519617763254], [-88.680463013034853, 38.15068957953018], [-88.678182297717754, 38.15068957953018], [-88.678182297717754, 38.152519617763254]]]

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

# A list that will hold each of the Polygon objects 
# 
featureList = []

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]
        array.add(point)

    # Add the first point of the array in to close off the polygon
    #
    array.add(array.getObject(0))

    # Create a Polygon object based on the array of points
    #
    polygon = arcpy.Polygon(array)

    # Clear the array for future use
    #
    array.removeAll()

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

# Create a copy of the Polygon objects, by using featureList as input to 
#  the CopyFeatures tool.
#
arcpy.CopyFeatures_management(featureList, "c:/geometry/polygons.shp")
0 Kudos
DustinReagan
New Contributor
To clarify, the polygon shown in my first post is correct.  I'm just wondering why I have to use the method that I did to create it, and why the method elaborated on in your example does not work and results in a null polygon.
0 Kudos