Select to view content in your preferred language

Divide shp in about 20-50 adjacent polygons?

402
3
Jump to solution
10-09-2019 12:25 AM
JohannesBierer
Regular Contributor

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()
0 Kudos
1 Solution

Accepted Solutions
JohannesBierer
Regular Contributor
while (int(result1.getOutput(0)) < 10):

            old_value = int(result1.getOutput(0))
            
            arcpy.SelectLayerByLocation_management (alk_sel_Layer, "INTERSECT", alk_sel_Layer)

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

            if int(result1.getOutput(0)) == old_value:
                break‍‍‍‍‍‍‍‍‍‍‍

This was the solution for me.

View solution in original post

0 Kudos
3 Replies
NeilAyres
MVP Alum

You generate a list of "i" in line 29, then in 31 you increment "i".

Then you only seem to use the value of "i" inside the while loop to identify the output.

Consider using 2 separate counters so you don't get confused and increment inside the while loop.

JohannesBierer
Regular Contributor

Thank you,

remains the question how to get out of the while loop if the result1 value didn't change.

0 Kudos
JohannesBierer
Regular Contributor
while (int(result1.getOutput(0)) < 10):

            old_value = int(result1.getOutput(0))
            
            arcpy.SelectLayerByLocation_management (alk_sel_Layer, "INTERSECT", alk_sel_Layer)

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

            if int(result1.getOutput(0)) == old_value:
                break‍‍‍‍‍‍‍‍‍‍‍

This was the solution for me.

0 Kudos