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?
Solved! Go to Solution.
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])
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])
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.
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.
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])
Thanks! That's been bugging me for months.
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.
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?