help needed with python FOR loop.

473
6
06-30-2011 09:47 AM
AliceDeschamps1
New Contributor
I need help getting this For loop working.  Not even sure if what I am trying to do is possible or if a For loop is the best way to get it done.  Can I use input parameter to set the range in my for loop? 
The codes below worked fine before I added the for loop.  Once I add my loop it skips to the last line in the code and prints "The Extract Flood and Convert to Vector tools is completed!!".

This is running as a script tool and users are asked to define their environment setting before launching the tool.

Thanks.

Alice

-----------------------------------------------------------------
# Import arcpy module
import arcpy
from arcpy import env
from arcpy.sa import *
import sys, string, os
arcpy.env.overwriteOutput = True

# Script arguments
    inRaster = arcpy.GetParameterAsText(0)
    minThr = arcpy.GetParameterAsText(1)
    maxThr = arcpy.GetParameterAsText(2)
    minHa = arcpy.GetParameterAsText(3)
    arcpy.env.mask = arcpy.GetParameterAsText(4)# Set Geoprocessing environments

    for i in range(int(minThr), int(maxThr)):

outputShapefile= os.path.splitext(inRaster)[0] + "_thr" + str(i) + "_" + str(minHa).replace('.','p') + "ha.shp"
        whereClause = '"Area_ha" >=' + minHa
        waterSql = '"Value" > 0 AND "Value" <=' + int(i)
       
         # Process: Con (needs spatial analyst license)
        outCon = Con(inRaster, 1, 0 , waterSql)
        outCon.save("outCon")
        arcpy.AddMessage("Open flood extent map is created (raster)")

        # Process: Focal Statistics (5x5 Mode)
        outFocalStats = FocalStatistics(outCon, "Rectangle 5 5 CELL", "MAJORITY", "DATA")
        outFocalStats.save("outFocalStats")
        arcpy.AddMessage("Image is filtered")

        # Process: Convert Raster to Polygon
        arcpy.RasterToPolygon_conversion(outFocalStats, "Raster2poly.shp", "NO_SIMPLIFY", "")
        arcpy.AddMessage("Converted to vector")

        # Process: Select Gridcode >0
        arcpy.Select_analysis("Raster2poly.shp", "Raster2polyCode0.shp", "GRIDCODE >0")

        # Process: Add Field to Calculate Area
        arcpy.AddField_management("Raster2polyCode0.shp", "Area_ha", "FLOAT", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")

        # Process: Calculate Field: Area in Ha
        arcpy.CalculateField_management("Raster2polyCode0.shp", "Area_ha", "!Shape!.area/10000", "PYTHON_9.3", "")
        arcpy.AddMessage("Area calculated (ha) for flooded polygons")

        # Process: Select min polygon size
        arcpy.Select_analysis("Raster2polyCode0.shp", outputShapefile, whereClause)
        arcpy.AddMessage("Applying the minimum hectare size")

        # Process: Delete intermediate files
        arcpy.Delete_management("outCon", "")
        arcpy.Delete_management("outFocalStats", "")
        arcpy.Delete_management("Raster2poly.shp", "")
        arcpy.Delete_management("Raster2polyCode0.shp", "")
        arcpy.AddMessage("Intermediate files deleted")

arcpy.AddMessage("The Extract Flood and Convert to Vector tools is completed!!")
Tags (2)
0 Kudos
6 Replies
AliceDeschamps1
New Contributor
The identations in the code did not come out in my post.  All lines after "for i in range(int(minThr), int(maxThr)):" are indented except for the last last Add.Message statement.
0 Kudos
DanPatterson_Retired
MVP Emeritus
edit your original post and throw the script within code blocks (the # symbol in the html editor)
0 Kudos
AliceDeschamps1
New Contributor
edit your original post and throw the script within code blocks (the # symbol in the html editor)


It looks fine in edit mode?  Not sure how to set it within code block?
0 Kudos
ChrisSnyder
Regular Contributor III
Just select your code text and click the little "#" button (3rd to the left of the YouTube button).
0 Kudos
AliceDeschamps1
New Contributor
Maybe I should be more specific in my question.    Can I use my parameters to define my FOR loop range as I have tried to do below or am I completely wrong?
minThr = arcpy.GetParameterAsText(1)
maxThr = arcpy.GetParameterAsText(2)
for i in range(int(minThr), int(maxThr)):

If that is not possible then the other option I can see is it to use a FOR loop to iterate over a list. That means that I need to build a list from the minThr and maxThr parameters first.  Not sure how to do that either?

Any suggestions would be greatly appreciated.

Alice
0 Kudos
AliceDeschamps1
New Contributor
Got it to work.... solved!
0 Kudos