Hi
I reposted the code using the syntax highlighting available at GeoNet:
The very last line (62) is where the code starts to run and uses 6 parameters:
CreateFishnetsForFeats(polys, loc, 100, 100, 22, 22)
It will use the featureclass and folder on lines 59 and 60.
The parameters are:
- in_polys: input polygon feature class
- out_loc: folder location for new file gdb containing fishnet feature class
- cell_x: cell width in the example 100
- cell_y: cell height in the example 100
- n_rows: number of rows in the example 22
- n_cols: number of columns in the example 22
import arcpy, os
arcpy.env.overwriteOutput = True
def CreateFishnetsForFeats(in_polys, out_loc, cell_x=0, cell_y=0, n_rows=0, n_cols=0):
'''
in_polys: input polygon feature class
out_loc: folder location for new file gdb containing fishnet feature class
cell_x: cell width
cell_y: cell height
n_rows: number of rows
n_cols: number of columns
'''
# Create file gdb to store data
gdb = str(arcpy.CreateFileGDB_management(out_loc, 'Fishnets.gdb').getOutput(0))
# spatial reference
arcpy.env.outputCoordinateSystem = arcpy.Describe(in_polys).spatialReference
# Loop thru rows of input polygons
with arcpy.da.SearchCursor(in_polys, ['SHAPE@', 'OID@']) as rows:
for row in rows:
ext = row[0].extent
st = '%f %f' %(ext.XMin, ext.YMin)
orien = '%f %f' %(ext.XMin, ext.YMax)
# Create fishnet
out = os.path.join(gdb, 'fish_{0}'.format(row[1]))
arcpy.CreateFishnet_management(out, st, orien, cell_x,
cell_y, n_rows, n_cols,
labels='NO_LABELS',
geometry_type='POLYGON')
# set workspace to new gdb
arcpy.env.workspace = gdb
fishnets = arcpy.ListFeatureClasses()
targ = fishnets[0]
for i, fish in enumerate(fishnets):
# Add field for original polygon ID
fid = fish.split('_')[1]
arcpy.AddField_management(fish, 'POLY_ID', 'LONG')
with arcpy.da.UpdateCursor(fish, ['POLY_ID']) as rows:
for row in rows:
row[0] = fid
rows.updateRow(row)
# append fishnets into one feature class
if i > 0:
arcpy.Append_management([fish], targ, 'NO_TEST')
arcpy.Delete_management(fish)
print 'Appended: {0}'.format(fish)
print 'Done'
return
if __name__ == '__main__':
polys = r'C:\TEMP\test_polys.shp'
loc = r'C:\TEMP'
CreateFishnetsForFeats(polys, loc, 100, 100, 22, 22)