Creating Polygons from Coordinates

9855
13
Jump to solution
10-23-2012 05:04 AM
JamesMitchell
Deactivated User
Hello,

I am hoping for some help.

I am trying to create polygons from coordinates but the code i am using is not working correctly.

Please can someone point me in the right direction? I have tried everything example i have found on the internet.

Thank you

    coordList.append(str(row.getvalue(desc.OIDFieldName)) + "," +str(FirstVertexXpoint) + "," + str(FirstVertexYpoint))
    coordList.append(str(row.getvalue(desc.OIDFieldName)) + "," +str(ThirdVertexXpoint) + "," + str(ThirdVertexYpoint))
    coordList.append(str(row.getvalue(desc.OIDFieldName)) + "," +str(SecondVertexXpoint) + "," + str(SecondVertexYpoint))
    coordList.append(str(row.getvalue(desc.OIDFieldName)) + "," +str(FourthVertexXpoint) + "," + str(FourthVertexYpoint))
    coordList.append(str(row.getvalue(desc.OIDFieldName)) + "," +str(FirstVertexXpoint) + "," + str(FirstVertexYpoint))


    array = arcpy.Array()
    point = arcpy.Point()
    featureList = []

for feature in coordList:
     coordinates = string.split(feature,",")

     point.ID = coordinates[0]
     point.X = coordinates[1]
     point.Y = coordinates[2]
     array.add(point)

     polygon = arcpy.Polygon(array)
     #array.removeAll()

     featureList.append(polygon)
     #array.removeAll()

arcpy.CopyFeatures_management(featureList, "c:/polygons.shp")
Tags (2)
0 Kudos
13 Replies
T__WayneWhitley
Honored Contributor
Now I think I see what's going on - are you getting the feat ID field you defined to populate the table, just the values are wrong?

If so, it is because you're doing it base on a line in your input file:

FeatID, pnt.ID, pnt.X, pnt.Y = string.split(line,",")


I didn't immediately realize you changed that, adding a separate id for poly it pertains to...so is it true your input file then looks something like:

FeatID, ID, X, Y
poly1, point1, x1, y1
poly1, point2, x2, y2
poly1, point3, x3, y3
etc.
...
...
poly2, point1, x11, y11
poly2, point2, x22, y22
poly2, point3, x33, y33
etc.
...
...

And then, provided I have correctly assumed that you have done that, then what isn't quite right is that when you are looping, you are retrieving values from the next line when the ID change is detected and you're incorrectly loading the next FeatID (instead of making a new var to 'hold' the last FeatID.
0 Kudos
T__WayneWhitley
Honored Contributor
I think this will fix it---the key is the line 'ID = pnt.ID' and you need to do more or less the same setting a new variable for what you retrieve from the input file for FeatID, say FeatID_load = FeatID...

This ID = pnt.ID line ocurrs twice in the script, one to initialize and again to 're-initialize' for the next poly.
So place FeatID_load = FeatID immediately after:

ID = pnt.ID
FeatID_load = FeatID

And load FeatID_load into your line for 'setValue' on the table row (not FeatID, because this is the next poly ID, right?).  Should be a quick fix, test it!!  By the way, good job for your success with this so far.

EDIT - I made the change to your code in 3 places, but have not tested it:
try:
   # Open an insert cursor for the new feature class
   #
   cur = arcpy.InsertCursor(fcname)

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

   # Initialize a variable for keeping track of a feature's ID.
   #
   ID = -1
   for line in coordList: # Open the input file
      # set the point's ID, X and Y properties
      #
      FeatID, pnt.ID, pnt.X, pnt.Y = string.split(line,",")
      print FeatID, pnt.ID, pnt.X, pnt.Y
      if ID == -1:
         ID = pnt.ID
         FeatID_load = FeatID
         
      # Add the point to the feature's array of points
      #   If the ID has changed, create a new feature
      #
      #print ID, pnt.ID
      if ID != pnt.ID:
         # Create a new row or feature, in the feature class
         #

         feat = cur.newRow()
         feat.setValue("OW_ID", FeatID)
         # Set the geometry of the new feature to the array of points
         #
         feat.shape = lineArray

         # Insert the feature
         #
         cur.insertRow(feat)
         lineArray.removeAll()
      lineArray.add(pnt)
      ID = pnt.ID
      FeatID_load = FeatID

   # Add the last feature
   #
   feat = cur.newRow()
   feat.setValue("OW_ID", FeatID_load)
   feat.shape = lineArray
   cur.insertRow(feat)
      
   lineArray.removeAll()
   del cur
except Exception as e:
   print e.message
0 Kudos
JamesMitchell
Deactivated User
Thank you so much it is working now. I knew it was something to do with that ID but couldn't get my head around it.

I have finally managed to create the square buffers and associate the correct id to the polygons now.

I find vba much easier to understand but am getting there slowly.

Thanks again.
0 Kudos
T__WayneWhitley
Honored Contributor
Excellent work.  You helped me as well with troubleshooting your interesting adaptation.  Glad it works!
If you haven't already, would you please mark this thread as 'Answered' ?

EDIT:
Sorry, I'm making too many clerical errors.  For anyone else reading this, figured I should own up and fix this so as not to mislead later.

The above is an adapted script "Create a new line feature class using a text file of coordinates."
Here's the link:

Writing geometries
Resource Center » Professional Library » Geoprocessing » Geoprocessing with Python » Accessing geographic data in Python
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000001v000000

The originally suggested input file for the script contains IDs to group points making up lines.  In the loop for the sample, when the ID changes, the array of points is loaded to the geometry object defining that feature...(which is loaded into the insertcursor row, then the array is 'emptied', and the next group begins loading the array).

The idea is more or less the same for loading polygon geometry (single-part, no 'holes').  James wanted each feature assigned a user-defined feature ID (FeatID).  Actually, the shared ID from the input file for the points in the group can be assigned respectively to each created polygon feature.
The script entered above shows retrieving 4 params from each line in the file:

FeatID, pnt.ID, pnt.X, pnt.Y = string.split(line,",")

But is this necessary?  Probably not if you want to maintain a '3 parameter' line file so that line is unaltered (except for the delimiter; change that according to your input file content):

pnt.ID, pnt.X, pnt.Y = string.split(line,",")

...then you can assign the feature ID directly with the following (just make sure your field type and var type match):

feat.setValue("OW_ID", ID)

No need for an 'extra' FeatID param unless for your purposes you have unique point identifiers that need to be written somewhere too.  So the 2 lines of code I added for FeatID_load = FeatID can be eliminated and the 2 lines for 'feat.setValue' can be changed to the line above (I forgot to change one of them anyway).
0 Kudos