Solved! Go to Solution.
If each fishnet should cover each polygon, then the code should be adapted (as far as I can tell). The n_row and n_cols should be calculated for each polygon depending the extent of the polygon.
If this is what you want:
Then you should use this code:
import arcpy, os arcpy.env.overwriteOutput = True def CreateFishnetsForFeats(in_polys, out_loc, cell_x=0, cell_y=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 ''' # 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) if cell_y == 0: n_rows = 1 cell_y = ext.height else: n_rows = int((ext.height - (ext.height % cell_y)) / cell_y) + 1 if cell_x == 0: n_cols = 1 cell_x = ext.width else: n_cols = int((ext.width - (ext.width % cell_x)) / cell_x) + 1 # 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)