3

I've created a script that creates a buffer inside of a polygon. I want it to be an ArcPy script that can run in ArcCatalog. However, it contains a while loop that won't close when run through ArcCatalog.

# import system modules 
import arcpy
from arcpy import env
import os

# Overwrite pre-existing files
arcpy.env.overwriteOutput = True

class creBuffers(object):

    def __init__(self, inFile, outPath, outFile, buffCount):
        self.inFile = inFile
        self.outFile = outFile
        self.outPath = outPath
        self.buffCount = buffCount

    def createBuffers(self):
        cnt = 1
        bdt = 0
        while cnt <= self.buffCount:
            bdt = bdt - 500
            # Buffer areas of impact around major roads
            roads = self.inFile
            outer = os.path.join(self.outPath, self.outFile + "a_" + str(bdt * (-1)))
            roadsBuffer = outer
            distance = bdt
            sideType = "OUTSIDE_ONLY"
            endType = "ROUND"
            dissolveType = "NONE"
            arcpy.Buffer_analysis(roads, roadsBuffer, distance, sideType, endType, dissolveType)
            cnt = cnt + 1

if __name__ == "__main__":

    inFile = arcpy.GetParameterAsText(0)
    outFile = arcpy.GetParameterAsText(2)
    outPath = arcpy.GetParameterAsText(1)
    buffCount = arcpy.GetParameterAsText(3)

    scope = creBuffers(inFile, outPath, outFile, buffCount)

    scope.createBuffers()

The tool params are:

  • Input File: Shapefile
  • Output Path: Workspace
  • Output File: Any Value
  • Number of Buffers: Long

The tool creates the outputs fine but the while loop never closes and thus it creates too many outputs (an infinite amount, seemingly). The same script works as a standalone script

# import system modules 
import arcpy
from arcpy import env

# Overwrite pre-existing files
arcpy.env.overwriteOutput = True

class creBuffers(object):

    def __init__(self, inFile, outPath, outFile, buffCount):
        self.inFile = inFile
        self.outFile = outFile
        self.outPath = outPath
        self.buffCount = buffCount

    def createBuffers(self):
        cnt = 1
        bdt = 0
        while cnt <= self.buffCount:
            bdt = bdt - 500
            # Buffer areas of impact around major roads
            roads = self.inFile
            roadsBuffer = self.outPath + "\\" + self.outFile + "_" + str(bdt * (-1))
            distance = bdt
            sideType = "OUTSIDE_ONLY"
            endType = "ROUND"
            dissolveType = "NONE"
            arcpy.Buffer_analysis(roads, roadsBuffer, distance, sideType, endType, dissolveType)
            cnt += 1

if __name__ == "__main__":

    # Validate input
    while True:
        # Ask for the users input & validate
        try:
            inFile = raw_input("Type the path to and name of the input file: ")
        # Is the input a number? Validate
        except:
            print("Sorry, that is not a valid filename and path")
            # Invalid input received. Return loop
            continue
        # Is the number either 1 or 2? Validate
        if len(inFile) < 1:
            # Tell the user that this input is mandatory
            print("Sorry, the input must be at least one character long")
            continue
        # If user inputs 1 or 2: break the loop
        else:
            # Valid input
            break

    # Validate input
    while True:
        # Ask for the users input & validate
        try:
            outPath = raw_input("Type the path to the output FGDB: ")
        # Is the input a number? Validate
        except:
            print("Sorry, that is not a valid filename and path")
            # Invalid input received. Return loop
            continue
        # Is the number either 1 or 2? Validate
        if len(outPath) < 1:
            # Tell the user that this input is mandatory
            print("Sorry, the output must be at least one character long")
            continue
        # If user inputs 1 or 2: break the loop
        else:
            # Valid input
            break

    # Validate input
    while True:
        # Ask for the users input & validate
        try:
            outFile = raw_input("Type the name of the output file: ")
        # Is the input a number? Validate
        except:
            print("Sorry, that is not a valid filename and path")
            # Invalid input received. Return loop
            continue
        # Is the number either 1 or 2? Validate
        if len(outFile) < 1:
            # Tell the user that this input is mandatory
            print("Sorry, the output must be at least one character long")
            continue
        # If user inputs 1 or 2: break the loop
        else:
            # Valid input
            break

    # Validate input
    while True:
        # Ask for the users input & validate
        try:
            buffCount = input("Type the number (whole) of buffers you want to add: ")
        # Is the input a number? Validate
        except:
            print("Sorry, that is not a valid whole number")
            # Invalid input received. Return loop
            continue
        # If user inputs 1 or 2: break the loop
        else:
            # Valid input
            break

    scope = creBuffers(inFile, outPath, outFile, buffCount)

    scope.createBuffers()

I was just wondering if anyone else has had this problem and if they found a way to fix it.

1 Answer 1

6

buffCount looks to be a string, you need to convert that to an integer

1
  • Yep. I replaced while cnt <= self.buffCount: with while cnt <= int(self.buffCount): and it worked. I don't know how I didn't think of this. I either need more or less coffee today... not sure which.
    – jdavid05
    Feb 26, 2020 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.