arcpy insert cursor creates table row but does not create polygon

6744
7
Jump to solution
09-04-2014 01:50 PM
MatthewStarry
New Contributor III

I was creating polygons via a python script when I noticed that rows in the table were being inserted but some of the actual polygon shapes were missing. I had just rewritten this 9.3.1 python script to work with arcpy and version 10+.  I ran the 9.3.1 script and all polygon shapes were complete.

 

I opened ArcMap 10.2.1 and manually entered the new code into the Python window:

>>> import arcpy

>>> array = arcpy.Array()

>>> array.add(arcpy.Point(-92.09936200, 46.73024000))

>>> array.add(arcpy.Point(-92.09936200, 46.73137900))

>>> array.add(arcpy.Point(-92.09782100, 46.73137900))

>>> array.add(arcpy.Point(-92.09782100, 46.73024000))

>>> array.add(arcpy.Point(-92.09936200, 46.73024000))

>>> cur = arcpy.da.InsertCursor("onecall_poly", ["SHAPE@"])

>>> cur.insertRow([arcpy.Polygon(array)])

0L

>>> array.removeAll()

>>> del array, cur

 

This created a row in the feature class table but did not create a polygon.

 

I then typed the following code into the Python window:

>>> import arcgisscripting

>>> gp = arcgisscripting.create(9.3)

>>> array = gp.createobject("Array")

>>> pnt = gp.createobject("Point")

>>> pnt.x = -92.09936200

>>> pnt.y = 46.73024000

>>> array.add(pnt)

>>> pnt.x = -92.09936200

>>> pnt.y = 46.73137900

>>> array.add(pnt)

>>> pnt.x = -92.09782100

>>> pnt.y = 46.73137900

>>> array.add(pnt)

>>> pnt.x = -92.09782100

>>> pnt.y = 46.73024000

>>> array.add(pnt)

>>> pnt.x = -92.09936200

>>> pnt.y = 46.73024000

>>> array.add(pnt)

>>> cur = gp.insertcursor("onecall_poly")

>>> feat = cur.newrow()

>>> feat.shape = array

>>> cur.insertrow(feat)

>>> array.removeall()

>>> del cur, array

 

This created the table record and the polygon feature.

 

Am I doing something wrong? Why doesn't the new code work? In the help it says that "All geometries are validated before they are written to a feature class."  What does 0L mean after the insertRow command in my first example?

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Building off Ian's answer, and for some reason remembering what I think were Dan Pattersonwise words, "You must set a spatial reference when creating geometries", behold:

>>> sr = arcpy.Describe("onecall_poly").spatialReference
array = arcpy.Array()
array.add(arcpy.Point(-92.09936200, 46.73024000))
array.add(arcpy.Point(-92.09936200, 46.73137900))
array.add(arcpy.Point(-92.09782100, 46.73137900))
array.add(arcpy.Point(-92.09782100, 46.73024000))
array.add(arcpy.Point(-92.09936200, 46.73024000))
cur = arcpy.da.InsertCursor("onecall_poly", ["SHAPE@"])
polygon = arcpy.Polygon(array,sr)
cur.insertRow([polygon])

1.PNG

View solution in original post

7 Replies
IanMurray
Frequent Contributor


Try making your polygon object first, then using it  in the cursor.insertRow

import arcpy

array = arcpy.Array()

array.add(arcpy.Point(-92.09936200, 46.73024000))

array.add(arcpy.Point(-92.09936200, 46.73137900))

array.add(arcpy.Point(-92.09782100, 46.73137900))

array.add(arcpy.Point(-92.09782100, 46.73024000))

array.add(arcpy.Point(-92.09936200, 46.73024000))

cur = arcpy.da.InsertCursor("onecall_poly", ["SHAPE@"])

polygon = arcpy.Polygon(array)

cursor.insertRow([polygon])

0 Kudos
MatthewStarry
New Contributor III

Thanks for the suggestion but it did not work.  I created the polygon object first as you suggested.  I also created the point object and then added the coordinates similar to the way I did in the arcgisscripting example.  That didn't work either.  It seems weird to me that this works using arcgisscripting but not arcpy.

0 Kudos
IanMurray
Frequent Contributor

I'm stumped, I have a code extremely similar to this that I use all the time for making polygons from a json and it reads almost exactly the same as mine.  The only difference I had was creating the Polygon object first, instead of in the cur.insertRow.

Sorry I couldn't help more.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Building off Ian's answer, and for some reason remembering what I think were Dan Pattersonwise words, "You must set a spatial reference when creating geometries", behold:

>>> sr = arcpy.Describe("onecall_poly").spatialReference
array = arcpy.Array()
array.add(arcpy.Point(-92.09936200, 46.73024000))
array.add(arcpy.Point(-92.09936200, 46.73137900))
array.add(arcpy.Point(-92.09782100, 46.73137900))
array.add(arcpy.Point(-92.09782100, 46.73024000))
array.add(arcpy.Point(-92.09936200, 46.73024000))
cur = arcpy.da.InsertCursor("onecall_poly", ["SHAPE@"])
polygon = arcpy.Polygon(array,sr)
cur.insertRow([polygon])

1.PNG

MatthewStarry
New Contributor III

Thanks! That's been bugging me for months.

0 Kudos
NeilAyres
MVP Alum

Heh,

Dan's already got about 21 zillion points and is on level 42.

I think I was the one (several times) to point out that a sr was essential when creatring geometries, especially when they are in a GCS.

This advice I gained many moons ago from one of the kindly esri folk.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Sorry, Neil, but you are not the one I heard it from so it's hard to quote you. Why don't you link to these several references and you can have your day in the sun?

0 Kudos