I have some code here that I have pieced together using snippets off the internet. I am a beginner python programmer so please bear with me. My ultimate goal here is to split a single polygon layer into equal area polygons based on a user input value (number of desired pieces) or based on an area measurement.
Specifically, on line 31 the script is failing. Any suggestions?
import arcpy
arcpy.env.overwriteOutput = True
#set feature classes:
inFC=r'C:\Users\diun\Downloads\florida_state.shp'
tempGrid=r'C:\Users\diun\Downloads\tempGrid.shp' #just a temporary file
outFC=r'C:\Users\diun\Downloads\final_grid.shp' #final grid
#set number of desired cells:
x=100
#set ID field in the input feature class:
idField="DESCRIPT" #set to a field you can use to identify with original
record each grid was part of *MUST BE UNIQUE
#get shape field
shapeName = arcpy.Describe(inFC).shapeFieldName
#delete tempGrid if exists:
if arcpy.Exists(tempGrid):
arcpy.Delete_management(tempGrid)
#delete outFC if exists:
if arcpy.Exists(outFC):
arcpy.Delete_management(outFC)
#Create output FC (the where statement will select 0 records
#arcpy.Select_analysis(inFC, outFC, '"'+idField+'" <> "'+idField+'"''"'+idField+'" <> "'+idField+'"') #change double quotes "" to [] if using geodatabases
arcpy.Select_analysis(inFC, outFC, '"'+idField+'" <> "'+idField+'"')
rows=arcpy.da.SearchCursor(idField,shapeName)
for row in rows:
where='"'+idField+'" = '+str(row.getValue(idField))
#Use this line if your idField is a string: where='"'+idField+'" = \''+row.getValue(idField)+'\'''"'+idField+'" = \''+row.getValue(idField)+'\'' #change double quotes "" to [] if using geodatabases
#Use this line if your idField is a number: where='"'+idField+'" = '+str(row.getValue(idField)) #change double quotes "" to [] if using geodatabases
arcpy.MakeFeatureLayer_management(inFC, "master_lyr",where)
feat = row.getValue(shapeName)
#set default grid variables:
extent = feat.extent
origin_coord=str(extent.XMin)+" "+str(extent.YMin)
oppositeCoorner=str(extent.XMax)+" "+str(extent.YMax)
y_axis_coord=str(extent.XMin)+" "+str(extent.YMin+10)
number_rows=number_columns=0
geometryType = 'POLYGON'
#set starting cellSize and increment:
cellSize=max(float(extent.width),float(extent.height))
inc=cellSize
countOutFC=0.0 #needed to start while loop
#Find an appropriate cell size:
while countOutFC!=x:
if cellSize==inc:
inc=inc/2.0
continue
cell_width=cell_height=cellSize
arcpy.CreateFishnet_management(tempGrid,origin_coord,y_axis_coord,cell_width,cell_height,number_rows,number_columns,oppositeCoorner,"LABELS","#",geometryType)
arcpy.MakeFeatureLayer_management(tempGrid, "temp_lyr")
arcpy.SelectLayerByLocation_management("temp_lyr", "INTERSECT", "master_lyr")
countOutFC=float(arcpy.GetCount_management("temp_lyr").getOutput(0))
print "Cell size: "+str(cellSize)+" Count: "+str(countOutFC)
if countOutFC>x:
print "Overshot..."
cellSize=prevCellSize
inc=inc/2.0
else:
prevCellSize=cellSize
cellSize-=inc
cellSize=prevCellSize
print '\nFound an appropriate cell size: '+str(cellSize)
#Find a simplified cell size:
print "\nTrying to simplify..."
for i in range(len(str(cellSize))):
simpCellSize=round(cellSize,i)
if simpCellSize==0:
continue
cell_width=cell_height=simpCellSize
arcpy.CreateFishnet_management(tempGrid,origin_coord,y_axis_coord,cell_width,cell_height,number_rows,number_columns,oppositeCoorner,"LABELS","#",geometryType)
arcpy.MakeFeatureLayer_management(tempGrid, "temp_lyr")
arcpy.SelectLayerByLocation_management("temp_lyr", "INTERSECT", "master_lyr")
countOutFC=float(arcpy.GetCount_management("temp_lyr").getOutput(0))
print "Cell size: "+str(simpCellSize)+" Count: "+str(countOutFC)
if countOutFC==x:
break
if simpCellSize==cellSize:
print "Could not easily find a simplified cell size."
#rerun for last good cellsize (not simplified):
cell_width=cell_height=cellSize
arcpy.CreateFishnet_management(tempGrid,origin_coord,y_axis_coord,cell_width,cell_height,number_rows,number_columns,oppositeCoorner,"LABELS","#",geometryType)
arcpy.MakeFeatureLayer_management(tempGrid, "temp_lyr")
arcpy.SelectLayerByLocation_management("temp_lyr", "INTERSECT", "master_lyr")
else:
print "A simplified cell size: "+str(simpCellSize)
arcpy.Append_management("master_lyr",outFC)
del rows
print "Done!!!"