desc0107

help needed with python FOR loop.

Discussion created by desc0107 on Jun 30, 2011
Latest reply on Jul 3, 2011 by desc0107
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!!")

Outcomes