I eventually managed to crack this with help from Peter McDaid and Nobbir Ahmed, Craig and Cedric. Thank you everyone else who replied to my threads and emails!
I include my final code below. Note that I have also added additional functionality to the script which now creates square buffers from the point features.
import arcgisscripting
gp = arcgisscripting.create(9.3)
gp.overwriteOutput = 1
# Example Argument Inputs: C:\Python_Test\output\final_fishnet_pnt.shp C:\Python_Test\output\final_fishnet_sqr.shp 0.5 TRUE
inPoints = sys.argv[1] #Input point feature class - must be shapefile
outPolys = sys.argv[2] #Output polygon feature class - must be shapefile
bufDist = float(sys.argv[3]) #Buffer distance
keepFields = sys.argv[4] #Boolean type: Maintain fields and field values of the input in the output
#This is the input polygon
inpolys = r"C:\Python_Test\Polygons.shp"
infc = inpolys
rows = gp.SearchCursor(infc)
try:
# take me to first row in polygon file
row = rows.next()
# this index is used as the fid value in the where clause query
id = 0
while row:
feat = row.shape
ext = feat.Extent
xmin = ext.XMin
xmax = ext.XMax
ymin = ext.YMin
ymax = ext.YMax
origin = str(xmin) + " " + str(ymin)
direction = ymin + float(100)
opp_corner = str(xmax) + " " + str(ymax)
y_axis = str(xmin) + " " + str(direction)
outfc = r"C:\Python_Test\output\myfishnettemp.shp"
gp.CreateFishnet_management(outfc, origin, y_axis, "5", "5", "0", "0", opp_corner, 'LABELS')
#fid = 0 # as it is a shapefile, it starts with zero for row in rows: # you are using just to iterate not to use the row object
outIndivPolygonfc = r"C:\Python_Test\output\indiv_poly.shp"
where_clause = '"FID" = %i' % (id)
gp.Select_analysis(infc, outIndivPolygonfc, where_clause)
gp.Clip_analysis(r"C:\Python_Test\output\myfishnettemp_label.shp", r"C:\Python_Test\output\indiv_poly.shp", r"C:\Python_Test\output\append_fishnet_pnt.shp")
gp.Append_management(r"C:\Python_Test\output\append_fishnet_pnt.shp", r"C:\Python_Test\output\final_fishnet_pnt.shp", "NO_TEST")
row = rows.next()
id = id + 1
# Prepare the output based on whether field and field values are desired in the output
if keepFields:
# Create empty output polygon feature class that includes fields of the input
gp.CreateFeatureClass(os.path.dirname(outPolys), os.path.basename(outPolys), "POLYGON", inPoints, "", "", inPoints)
# Create a short-list of fields to ignore when moving fields values from input to output
ignoreFields = []
# Use Describe properties to identify the ShapeFieldName and OIDFieldName
desc = gp.Describe(inPoints)
ignoreFields.append(desc.ShapeFieldName)
ignoreFields.append(desc.OIDFieldName)
# Create a list of fields to use when moving field values from input to output
fields = gp.ListFields(inPoints)
#field = fields.Next()
#fieldList = []
#while field:
# if field.Name not in ignoreFields:
# fieldList.append(field.Name)
# field = fields.Next()
fieldList = []
for field in fields:
if field.Name not in ignoreFields:
fieldList.append(field.Name)
else:
# Create empty output polygon feature class without fields of the input
gp.CreateFeatureClass(os.path.dirname(outPolys), os.path.basename(outPolys), "POLYGON", "", "", "", inPoints)
# Open searchcursor
inRows = gp.SearchCursor(inPoints)
inRow = inRows.Next()
# Open insertcursor
outRows = gp.InsertCursor(outPolys)
# Create point and array objects
pntObj = gp.CreateObject("Point")
arrayObj = gp.CreateObject("Array")
while inRow: # One output feature for each input point feature
inShape = inRow.Shape
pnt = inShape.GetPart(0)
# Need 5 vertices for square buffer: upper-right, upper-left, lower-left,
# lower-right, upper-right. Add and subtract distance from coordinates of
# input point as appropriate.
for vertex in [0,1,2,3,4]:
pntObj.id = vertex
if vertex in [0,3,4]:
pntObj.x = pnt.x + bufDist
else:
pntObj.x = pnt.x - bufDist
if vertex in [0,1,5]:
pntObj.y = pnt.y + bufDist
else:
pntObj.y = pnt.y - bufDist
arrayObj.add(pntObj)
# Create new row for output feature
feat = outRows.NewRow()
# Shift atttributes from input to output
if keepFields:
for fieldName in fieldList:
feat.SetValue(fieldName, inRow.GetValue(fieldName))
# Assign array of points to output feature
feat.Shape = arrayObj
# Insert the feature
outRows.InsertRow(feat)
# Clear array of points
arrayObj.RemoveAll()
# Get next feature in searchcursor
inRow = inRows.Next()
# Delete inputcursor
del outRows
except:
print gp.GetMessages(2)