help,how to add a plygon to a shpefile with arcpy

301
1
07-25-2011 11:22 PM
montezwhite
New Contributor
i worte a script to create a shapefile ,but i don't know how to draw a polygon in this shapefile.
i used  the code on help and draw some polygons to a new shapefile, but i don't know how to write these polygons to the shapefile i had created at beginning.
my code is:
# Import system modules
import arcpy
from arcpy import env
print arcpy.ListEnvironments()
# Set workspace
env.workspace = "D:/temp"


# Set local variables
out_path = "d:/temp"
out_name = "Mapindex.shp"
geometry_type = "POLYGON"
#template = "study_quads.shp"
has_m = "DISABLED"
has_z = "DISABLED"
# Creating a spatial reference object
prjfile = "C:\Program Files\ArcGIS\Desktop10.0\Coordinate Systems\Geographic Coordinate Systems\Asia\Beijing 1954.prj"
spatial_reference = arcpy.SpatialReference(prjFile)
print spatial_reference.name
# Execute CreateFeatureclass

if arcpy.Exists(out_path +"/" + out_name):
   arcpy.Delete_management(out_path +"/" + out_name)
   arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type,"" , has_m, has_z, spatial_reference)
else:
   arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type,"" , has_m, has_z, spatial_reference)
# Set local variables
inFeatures = out_path +"/" + out_name
fieldName = "Mapcode"
fieldLength = 10
# Execute AddField twice for two new fields
arcpy.AddField_management(inFeatures, fieldName, "TEXT", "", "", fieldLength)

# A list of features and coordinate pairs
coordList = [[[108,32], [108,36], [114,36],[114,32]],[[108,32], [108,36], [102,36],[102,32]]]
# 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.
# env.OverwriteOutput = True
# arcpy.CopyFeatures_management(featureList, inFeatures)
and the error is 000725 : Dataset "d:\temp\Mapindex.shp"  already exists.

how can i do?
hope help,thanks
Tags (2)
0 Kudos
1 Reply
ChrisFox3
Occasional Contributor III
You could either not create the feature class to begin with and create the output with the geometry of the polygon using copy features at the end of the script or use an insert cursor to add a new row to the feature class and assign the geometry of the polygon to the shape field in the row.
0 Kudos