I am using ArcGIS 10.3.1 and am trying to divide a polygon into several smaller polygons based on a percentage.
I have found a theoretical answer Dividing polygon into specific sizes using ArcGIS?
I am trying to find the tool (code) described by the user whuber or something similar, but all links found on google seem to be dead.
The suggested commercial tool is not an option for me.
Is there a working link to the code I am trying to find or some other tool that would do the same?
Answer
Interface:
Script to divide polygon into 2 single part polygons, using field fraction (0..1):
## split polygons
import arcpy, traceback, os, sys
gr=(math.sqrt(5)-1)/2
try:
def showPyMessage():
arcpy.AddMessage(str(time.ctime()) + " - " + message)
#golden section to find minimum
def gss(a,b,tol):
c=b-gr*(b-a)
d=a+gr*(b-a)
fc=f(c);fd=f(d)
while abs(c-d)>tol:
if fc b,d,fd=d,c,fc
c=b-gr*(b-a)
fc=f(c)
else:
a,c,fc=c,d,fd
d=a+gr*(b-a)
fd=f(d)
return (b+a)/2
def f(z):
global two,fraction
theP=outline.positionAlongLine (z).firstPoint
splitter=arcpy.Polyline(arcpy.Array([point,theP]),SR)
try: two=pgon.cut(splitter)
except: return 1
intR=abs(two[0].area/pgon.area-fraction)
return intR
# mxd = arcpy.mapping.MapDocument("CURRENT")
# layers = arcpy.mapping.ListLayers(mxd)
outputLR=arcpy.GetParameterAsText(0)
pgonLR=arcpy.GetParameterAsText(1)
fld=arcpy.GetParameterAsText(2)
desc=arcpy.Describe(pgonLR)
SR = desc.spatialReference
curT = arcpy.da.InsertCursor(outputLR,"Shape@")
with arcpy.da.SearchCursor(pgonLR,("Shape@",fld)) as cursor:
m=0
for row in cursor:
m+=1
arcpy.AddMessage(m)
pgon=row[0]
fraction=row[1]
outline=pgon.boundary();L=outline.length
for j in range(100):
point=outline.positionAlongLine (L*j/100).firstPoint
chainage=gss(0.01,L,0.001)
doit=f(chainage)
if two[0].partCount==1 and two[1].partCount==1:
for item in two:
curT.insertRow((item,))
break
except:
message = "\n*** PYTHON ERRORS *** "; showPyMessage()
message = "Python Traceback Info: " + traceback.format_tb(sys.exc_info()[2])[0]; showPyMessage()
message = "Python Error Info: " + str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"; showPyMessage()
Result sample after 2 runs:
No comments:
Post a Comment