AnsweredAssumed Answered

Divide shp in about 20-50 adjacent polygons?

Question asked by JB1234510 on Oct 9, 2019
Latest reply on Oct 10, 2019 by JB1234510

I would like to divide a shp in about 20-50 adjacent polygons. I have the following script but the while loop (line 54) becomes endless if there are less than 10 adjacent polygons to select. Any ideas for a better solution?

 

# -*- coding: cp1252 -*-

import arcpy

arcpy.env.overwriteOutput = True

arcpy.env.workspace = "in_memory"

Alk_Sel = r"R:\Daten\sgb_Planungen\2013\Saalbachniederung\Eigentuemer\ALK_merge_E.shp"
alk_sel_Layer = "alk_sel_Layer"
InFile = r"C:\temp2\InFile.shp"

def SicFeature():
   
    arcpy.CopyFeatures_management(Alk_Sel, InFile)
    arcpy.MakeFeatureLayer_management(InFile, alk_sel_Layer)

def AddField():

    arcpy.AddField_management(alk_sel_Layer, "Round", "LONG")

def selectMin():

    sum = arcpy.GetCount_management(alk_sel_Layer)
    print sum

    i = 0

    for i in range(1, int(sum.getOutput(0)), 10):

        i += 1
   
        Expression = """ "Round" = {} """.format(0)
       
        arcpy.MakeFeatureLayer_management(InFile, alk_sel_Layer, Expression )
       
        sum1 = arcpy.GetCount_management(alk_sel_Layer)
        print('{} has a sum of {} records'.format(alk_sel_Layer, sum1[0]))

        MyList = list()
        cursor = arcpy.da.SearchCursor(alk_sel_Layer, "FID")

        for row in cursor:
            MyList.append(int(row[0]))

        print 'Minimum: {0}'.format(min(MyList))

        expression1 = min(MyList)

        arcpy.SelectLayerByAttribute_management(alk_sel_Layer, "NEW_SELECTION", """ "FID" = {} """.format(expression1))
       
        result1 = arcpy.GetCount_management(alk_sel_Layer)
                   
        while (int(result1.getOutput(0)) < 10):
         
            arcpy.SelectLayerByLocation_management (alk_sel_Layer, "INTERSECT", alk_sel_Layer)

            result1 = arcpy.GetCount_management(alk_sel_Layer)
            print(result1)

        arcpy.CalculateField_management(alk_sel_Layer, "Round", i)

        OutFile = r"C:\temp2\OutFile_{}.shp".format(i)
        arcpy.CopyFeatures_management(alk_sel_Layer, OutFile)

SicFeature()
AddField()
selectMin()

Outcomes