1

This question already has an answer here:

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!!!"

marked as duplicate by whyzar, FelixIP, tinlyx, HDunn, MaryBeth Sep 25 '17 at 19:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Thanks for the effort everyone. Not looking for anything other than a method for splitting a polygon by desired number of cells/regions (not a grid with unequal edge cells) or by desired area of output polygons. – Brian Diunizio Sep 25 '17 at 18:36
  • If this is not to be a duplicate then I think you need to include a picture of an input polygon and your desired output, and more details how your code is failing. Also, I think you should review gis.meta.stackexchange.com/questions/4312/… – PolyGeo Sep 25 '17 at 21:41
  • Found a helpful QGIS plugin online at the link below. Will attach an output image when the it is through. github.com/jonnyhuck/RFCL-PolygonDivider – Brian Diunizio Sep 28 '17 at 20:32
  • Since it has no answers (due to being a duplicate), if there is no QGIS equivalent question you could heavily re-work this one as a QGIS question to have it re-opened so you can self-answer. – PolyGeo Sep 28 '17 at 22:36