TransWikia.com

Using ArcPy to fit polygons inside another one

Geographic Information Systems Asked by Robert22 on December 19, 2020

I have been trying to solve a bin packing problem using a Python script I found, but when I try to use it I get an error that I don’t know how to solve.

The code that I’m trying to use is taken from here: Fitting known size polygons into irregular polygons using ArcGIS Desktop?

What I am doing is creating 2 shapefiles with polygonal entities and I create a geoprocessing toolbox, I add the python script and I open it. When I open the tool I enter the layers of the shapefiles as parameters, and I execute it.

Possibly this is a simple error, but the truth is that I don’t know how to solve this problem.


import arcpy
from arcpy import env
from math import radians,sin,cos
env.overwriteOutput = True
infc=arcpy.GetParameterAsText(0)
outFC=arcpy.GetParameterAsText(1)
d=arcpy.Describe(infc); SR=d.spatialReference
W=1;L=2;A=0.99*W*L
fnet="in_memory/fnet"
erased="in_memory/fnet"

# rotate polygon

def ShapeMake(pGon,angle):
    a=radians(angle)
    ARR=arcpy.Array()
    cX=cPoint.X;cY=cPoint.Y
    for part in pGon.boundary():
        ar=arcpy.Array()
        for p in part:
            x,y=p.X-cX,p.Y-cY
            xN=cos(a)*x+sin(a)*y
            yN=-sin(a)*x+cos(a)*y
            pN=arcpy.Point(xN+cX,yN+cY)
            ar.add(pN)
        ARR.add(ar)
    pgonRotated=arcpy.Polygon(ARR,SR)
    return pgonRotated

# create fishnet and count complete polygons

def fnetMake():
    FNET=[]
    ext=rotated.extent
    oc='%s %s' %(ext.XMin,ext.YMin)
    ya='%s %s' %(ext.XMin,ext.YMax)
    cc='%s %s' %(ext.XMax,ext.YMax)
    arcpy.CreateFishnet_management(fnet, oc,ya, W, L,"","",
                                   "","NO_LABELS", rotated,"POLYGON")
    rects=arcpy.Clip_analysis(fnet, rotated, g)
    for chop in rects:
        if chop.area<A:continue
        FNET.append(chop)
    return FNET

g=arcpy.Geometry()
PGON=arcpy.CopyFeatures_management(infc,g)[0]
theList=[PGON];bigList=[]

nBefore=0
while True:
    for toCut in theList:

        ## FIND rotation to maximise complete rectangles

        nMax=0
        cPoint=toCut.centroid

        for i in range(36):
            angle=5*i
            rotated=ShapeMake(toCut,angle)
            squares=fnetMake()
            N=len(squares)
            if N<=nMax:continue
            nMax=N
            keepers=squares[:]
            bestAngle=angle

        if nMax==0:continue
        arcpy.AddMessage("%s cell(s) found so far" %nMax)

        for item in keepers:
            rotated=ShapeMake(item,-bestAngle)
            bigList.append(rotated)

    if nBefore==len(bigList):break
    nBefore=len(bigList)
    arcpy.Erase_analysis(PGON, bigList, erased)
    theList=arcpy.MultipartToSinglepart_management(erased, g)

arcpy.CopyFeatures_management(bigList,outFC)
 Traceback (most recent call last):
  File "C:UserspcDesktopscript1.py", line 82, in <module>
    arcpy.CopyFeatures_management(bigList,outFC)
  File "c:program filesarcgisproResourcesarcpyarcpymanagement.py", line 3373, in CopyFeatures
    raise e
  File "c:program filesarcgisproResourcesarcpyarcpymanagement.py", line 3370, in CopyFeatures
    retval = convertArcObjectToPythonObject(gp.CopyFeatures_management(*gp_fixargs((in_features, out_feature_class, config_keyword, spatial_grid_1, spatial_grid_2, spatial_grid_3), True)))
  File "c:program filesarcgisproResourcesarcpyarcpygeoprocessing_base.py", line 511, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
RuntimeError: Object: error durante ejecución de herramienta
 Falló al ejecutar (calculopoligono).

 Traceback (most recent call last):
File "C:UserspcDesktopscript1.py", line 82, in 
arcpy.CopyFeatures_management(bigList,outFC)
File "c:program filesarcgisproResourcesarcpyarcpymanagement.py", line 3373, in CopyFeatures
raise e
File "c:program filesarcgisproResourcesarcpyarcpymanagement.py", line 3370, in CopyFeatures
retval = convertArcObjectToPythonObject(gp.CopyFeatures_management(*gp_fixargs((in_features, out_feature_class, config_keyword, spatial_grid_1, spatial_grid_2, spatial_grid_3), True)))
File "c:program filesarcgisproResourcesarcpyarcpygeoprocessing_base.py", line 511, in 
return lambda *args: val(*gp_fixargs(args, True))
RuntimeError: Object: error durante ejecución de herramienta
Falló al ejecutar (calculopoligono).

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP