AnsweredAssumed Answered

How to create a simple square polygon and add it to a feature class.

Question asked by bz89 on Dec 5, 2016
Latest reply on Dec 5, 2016 by dkwiens

So, the code below is a part of a much larger script. Within that larger script i have already successfully created and written geometries to two polyline feature classes and two point feature classes. I would like to now create and write geometries to a polygon feature class. However, every time i attempt to use "arcpy.Polygon()" the resulting object is empty despite the point objects and array object that i pass to "arcpy.Polygon()" not being empty. Am i missing something here? Every time i try and create a polygon object it is empty no matter what i pass to it. Any help would be greatly appreciated.

import os, arcpy

infc = r"C:\Users\User\GIS\single_section.shp"
polygons = r"C:\Users\User\GIS\polygons.shp"

spatialref = arcpy.Describe(infc).spatialReference

if arcpy.Exists(polygons):
arcpy.Delete_management(polygons)

arcpy.CreateFeatureclass_management(*os.path.split(polygons), geometry_type="Polygon", spatial_reference=spatialref.factoryCode)

icursor = arcpy.da.InsertCursor(polygons, ["SHAPE@"])

TLX = -104.357483001
TLY = 32.2242740047
TRX = -104.341346746
TRY = 32.2241446297
BRX = -104.356403497
BRY = 32.2233637742
BLX = -104.357479189
BLY = 32.2233727547

array = arcpy.Array()
array.add(arcpy.Point(TLX,TLY))
array.add(arcpy.Point(TRX,TRY))
array.add(arcpy.Point(BRX,BRY))
array.add(arcpy.Point(BLX,BLY))
array.add(arcpy.Point(TLX,TLY))

polygon = arcpy.Polygon(array)

icursor.insertRow([polygon])

Outcomes