Select to view content in your preferred language

ArcGIS 10.0: Python Window - Try to Create 3 polygons, but no 3rd polygon showed up

552
3
09-27-2012 12:22 PM
ScottChang
Deactivated User
Hi all,
I created the following ArcPy-Python script:
# ===== newWritingGeometries3Polygons in PythonWin 2.6.5 ========
# Create a new polygon feature class using a text file of coordinates.
#   Each coordinate entry is semicolon delimited in the format of ID;X;Y

# Import ArcPy and other required modules
#
import arcpy
from arcpy import env
import fileinput
import string
import os

env.overwriteOutput = True

# Get the coordinate ASCII file
#
#infile = arcpy.GetParameterAsText(0)
infile = r"C:\TEMP\WritingGeometries\WritingGeometries_3features.txt"

# Get the output feature class
# fcname = arcpy.GetParameterAsText(1)
fcname = r"C:\TEMP\BS_Test.gdb\Polygons_fc"

# Get the template feature class
#
# template = arcpy.GetParameterAsText(2)
template = r"C:\TEMP\BS_Test.gdb\bsInFeatureClass"

try:
   # Create the output feature class
   #
   arcpy.CreateFeatureclass_management(os.path.dirname(fcname),
                                       os.path.basename(fcname), 
                                       "Polygon", template)

   # Open an insert cursor for the new feature class
   #
   cur = arcpy.InsertCursor(fcname)

   # Create an array and point object needed to create features
   #
   polygonArray = arcpy.Array()
   pnt = arcpy.Point()

   # Initialize a variable for keeping track of a feature's ID.
   #
   ID = -1 
   for line in fileinput.input(infile): # Open the input file
      # set the point's ID, X and Y properties
      #
      pnt.ID, pnt.X, pnt.Y = string.split(line,";")
      print pnt.ID, pnt.X, pnt.Y
      if ID == -1:
         ID = pnt.ID

      # Add the point to the feature's array of points
      #   If the ID has changed, create a new feature
      #
      if ID != pnt.ID:
         # Create a new row or feature, in the feature class
         #
         feat = cur.newRow()

         # Set the geometry of the new feature to the array of points
         #
         feat.shape = polygonArray

         # Insert the feature
         #
         cur.insertRow(feat)
         polygonArray.removeAll()
      polygonArray.add(pnt)
      ID = pnt.ID

   # Add the last feature
   #
   feat = cur.newRow()
   feat.shape = polygonArray
   cur.insertRow(feat)
      
   lineArray.removeAll()
   fileinput.close()
   del cur
   arcpy.SetParameterAsText (0, fcname) # newly added 18Sept2012, per Barbara S.
except Exception as e:
   print e.message

I executed the script and I just got 2 polygons - see the attached file for details.  Please kindly help and tell me why the 3rd polygon failed to show up ( I believe the following code staments do not work:
# Add the last feature
   #
   feat = cur.newRow()
   feat.shape = polygonArray
   cur.insertRow(feat)
)
Thanks,
Scott Chang
Tags (2)
0 Kudos
3 Replies
ScottChang
Deactivated User
I forgot to attach the text file of ID, and X,Y-coordinates for the 3 polygons to be created.
Please see the attached file in this thread for the text file of ID, and X,Y-coordinates for the 3 polygons to be created.

Thanks,
Scott Chang
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Scott,

Remove the extra lines after the last line in the text file and it should work.
0 Kudos
ScottChang
Deactivated User
Hi Jake, 

Thank you very much for your valuable response. 

The extra lines/space after the last line of input data is a big devil and wasted me so much time!!!

Thanks again,
Scott Chang
0 Kudos