Can someone help me fix some code that simply writes points into a featureclass?

It used to work in the older gp. syntax, but I'm having difficulties rewriting it in ArcPy for ArcGIS 10.0. In this example it was meant to create a 100m grid of points.

desc = arcpy.Describe("Grid_Extent")
Ext1 = str(desc.Extent)
Ext2 = Ext1.split(' ')
SXMin = int (float(Ext2[0]))
SXStart = int (float(Ext2[0]))
SYMin = int (float(Ext2[1])) 
SXMax = int (float(Ext2[2]))
SYMax = int (float(Ext2[3]))

XMin = SXMin
XMax = SXMax
YMin = SYMin
YMax = SYMax
print str(YMin) + "    " + str(YMax)
print str(XMin) + "    " + str(XMax)
while YMin < YMax:
  while XMin < XMax:
     cur = arcpy.InsertCursor("Grid_100")
     pnt = arcpy.Point()
     pnt.x = XMin
     pnt.y = YMin
     feat = cur.newRow()
     XMin = XMin + 100
  XMin = SXStart
  YMin = YMin + 100
del cur

You are very close, just a few minor changes should make this work. First when setting the point object x and y, the code should be:

pnt.X = XMin
pnt.Y = YMin

Then you need to set the geometry of the feature object from the point:

feat = cur.newRow()
feat.shape = pnt
  • Thanks Jeremy, that drove me crazy for hours. If I could ask another question inside of this question... The point geometry does save into the featureclass now, however I had 2 items in that featureclass that are not getting populated. (They weren't shown above to keep the question simple) pnt.GRIDID = PNTNUM pnt.GRIDLINE = LINENUM any idea, or should I post this as a new question. Thanks! – User Error May 2 '13 at 16:17
  • Should be setting the attributes on the feature, not the point object. Try something like this: feat.GRIDID = PNTNUM and feat.GRIDLINE = LINENUM. Do this before you insert the new row. – JeremyFon May 2 '13 at 16:26
  • You beat me to it. feat.GRIDID and feat.GRIDLINE It works. Thanks again! – User Error May 2 '13 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.