desc0107

model builder tools to python to increase functionality

Discussion created by desc0107 on Jun 25, 2011
Latest reply on Jun 28, 2011 by desc0107
I have created tool in model builder and have exported them to python.  The tool convert from raster to polygon, selects gridcode>0, adds a field and calculates area then selects minimum polygon size (user entered) and exports the final results to a new shapefile.

I find the model builder output script very confusion, too many variables.  I am new to python and trying to do the following improvements:

1) As it stands the user needs to enter the proper sql as a prompt ("Area_ha" >=2.5).  I would like the user to only enter the value (2.5) at a prompt to avoid typing errors.
2) I would like the output file to be name automatically using a string concatenation=  inputname +2p5ha.

Any suggestions?
-----------------------------------
# Import arcpy module
import arcpy

# Set Geoprocessing environments
arcpy.env.mask = ""

# Script arguments
Input_Raster_Flooded_Water_Mask_ = arcpy.GetParameterAsText(0)

Minimum_Polygon_Size__ha_ = arcpy.GetParameterAsText(1)
if Minimum_Polygon_Size__ha_ == '#' or not Minimum_Polygon_Size__ha_:
    Minimum_Polygon_Size__ha_ = "\"Area_ha\" >=2.5" # provide a default value if unspecified

Ouput_Shapefile_-_Flooded_Water_Polygons = arcpy.GetParameterAsText(2)
if Ouput_Shapefile_-_Flooded_Water_Polygons == '#' or not Ouput_Shapefile_-_Flooded_Water_Polygons:
    Ouput_Shapefile_-_Flooded_Water_Polygons = "%workspace%\\DMY_mask_thr10_2p5ha.shp" # provide a default value if unspecified

# Local variables:
RasterT_water_m1_shp = Input_Raster_Flooded_Water_Mask_
RasterT_water_m1_Select_shp = RasterT_water_m1_shp
RasterT_water_m1_Select_shp__2_ = RasterT_water_m1_Select_shp
RasterT_water_m1_Select_shp__4_ = RasterT_water_m1_Select_shp__2_

# Process: Raster to Polygon
arcpy.RasterToPolygon_conversion(Input_Raster_Flooded_Water_Mask_, RasterT_water_m1_shp, "NO_SIMPLIFY", "")

# Process: Select Gridcode >0
arcpy.Select_analysis(RasterT_water_m1_shp, RasterT_water_m1_Select_shp, "GRIDCODE >0")

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

# Process: Calculate Field: Area in Ha
arcpy.CalculateField_management(RasterT_water_m1_Select_shp__2_, "Area_ha", "!Shape!.area/10000", "PYTHON_9.3", "")

# Process: Select min polygon size
arcpy.Select_analysis(RasterT_water_m1_Select_shp__4_, Ouput_Shapefile_-_Flooded_Water_Polygons, Minimum_Polygon_Size__ha_)

Outcomes