Select to view content in your preferred language

Divide Irregular Polygon into 730 parts

7173
18
Jump to solution
05-06-2014 06:25 AM
TysonBarlow
Occasional Contributor
Hi guys and gals!

Is there a utility or something that I could use to divide an irregularly shaped polygon into 730 pieces, preferably of equal area? For example, take any given state polygon in the US and divide it into 730 pieces.

Thank you!!
Tags (2)
18 Replies
JackDavis
Deactivated User

Yeah, I noticed this morning that my code didn't match what was here, so I updated it. When I ran the script, I got 100 grids, but it was 100 over the entire map instead of each polygon. Not sure if I set something up wrong or what? Thanks again for the help!

0 Kudos
JoshuaChisholm
Frequent Contributor

I think it might be because I was not clearing the "tempGrid" in between iterations. Try adding these line after specifying the location of the "tempGrid" (line 25):

  #delete tempGrid if exists:

  if arcpy.Exists(tempGrid):

    arcpy.Delete_management(tempGrid)

I've also edited my post above to reflect these changes.

0 Kudos
JackDavis
Deactivated User

Well Joshua, it's still just dividing up the entire map layer into 100 grids instead of each polygon in the layer. Not sure if I am doing something wrong on the setup...In looking at the code, is each polygon supposed to be in it's own layer?

0 Kudos
JoshuaChisholm
Frequent Contributor

Ohhh, I think I completely misunderstood you. I thought you made multiple layers (pointing to multiple shapefiles/FCs) in an MXD that you wanted to split into a given number of grids. It sounds like you actually want to take a single layer in an MXD and split it into multiple grids. Is this correct?

Do they have to be split into a specific number of grids or into a grids of a specific dimension?

0 Kudos
JackDavis
Deactivated User

Sorry...I was afraid of that after I started looking closer at the code...Here is a sample of what I'm talking about. It is 1 layer with many polygons in a grid. I need to split each polygon into 100 sub-grids with number labels from 1-100. When I saw your original post, I liked the way the grids were laid out over the layer extents and thought it might work for my situation.

PDEC_System_Maps.jpg

0 Kudos
JoshuaChisholm
Frequent Contributor

Hello Jack,

Thank you for the explanation and the picture. I think I now have a much better understanding of your needs. I've created another version of the script. Unfortunately I'm a little short on time and don't have time to test it. If you could let me know what errors pop up, we can fix the script as needed.

import arcpy   

arcpy.env.overwriteOutput = True   

 

#set feature classes: 

inFC=r'C:\Path\To\Poly.shp' 

tempGrid=r'C:\Path\To\tempGrid.shp' #just a temporary file 

outFC=r'C:\Path\To\grid.shp' #final grid

#set number of desired cells:   

x=100

#set ID field in the input feature class:

idField="ParentID" #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+'"') #change double quotes "" to [] if using geodatabases

rows=arcpy.da.SearchCursor(idField,shapeName)

for row in rows:

  where='"'+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!!!"   

0 Kudos
ClaytonHayes
New Contributor

Hi Joshua, I am trying to use your script and I keep getting a runtime error on line 30. Also, I am new to python, what parts of the code do I need to change in order to so it works with my feature class.

0 Kudos
Zeke
by
Honored Contributor

I don't know if all the quote marks on that line line up properly, but the final character should be ), not <.

0 Kudos
TysonBarlow
Occasional Contributor
Joshua, it worked beautifully! Thank you so much for your help!!
0 Kudos