Solved! Go to Solution.
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)
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)
Dear Caleb, a million thanks for posting the code and your instructions. This is the first time I have ever worked with Python script and it will not be the last. I got your code to work so problem solved.
Since this is my first time working with Python and I have little experience with programming, I have little clue what things like f%, targ, if__name__ == __main__, etc.. mean. I guess I will figure these things out in due time as I make my way through the Python Scripting for ArcGIS book.
A sincere thank you once again.
st = '%f %f' %(ext.XMin, ext.YMin)
Hi GeoNet Admin
I have the same problem as Alejandro and I'd love to use your script to solve it. Unfortunately I know next to nothing about Python and can't see which parts of your script are highlighted in red. What should I change in order for your script to run with my data?
Thanks in advance!!
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:
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)
Great! Thank you very much!
One last question-what do I enter instead of 100 for cell width and height if I don't want it to limited the size of the polygons (i.e. I only want to use the number of rows and columns to create the cells)
The ArcGIS website says to just put 0 in as a value, but when I tried that I got giant polygons more or less the size of the whole Earth. I would like the fishnets to be the size of the polygons.