Select to view content in your preferred language

Python script tool will not stop running in Model Builder

808
2
Jump to solution
09-25-2012 02:48 PM
RobinCheskin
Occasional Contributor
My python script tool will not stop running inside model builder.  Here is an overview of what the model does:

1. Python script called "WeightRiskLayers" creates weighted rasters based on user input and saves them into a workspace.
2. Raster iterator iterates through these weighted rasters and puts them in the Collect Values tool.
3. Cell Statistics sums the rasters together to create a "Risk Terrain."

The problem is that when I run everything outside of model builder, the python script repeats and subsequently the entire model runs again.  I even tried adding the Model Only Tool "Stop" but that did not work.

Here is the python script (sry it is long)
#Import modules import os import arcpy from arcpy import env from arcpy.sa import * arcpy.CheckOutExtension("Spatial")  #Set local variables wks = arcpy.GetParameterAsText(0) # input workspace inRaster1 = arcpy.GetParameterAsText(1) # input raster layer inRaster2 = arcpy.GetParameterAsText(2) # input raster layer inRaster3 = arcpy.GetParameterAsText(3) # input raster layer inRaster4 = arcpy.GetParameterAsText(4) # input raster layer inRaster5 = arcpy.GetParameterAsText(5) # input raster layer inRaster6 = arcpy.GetParameterAsText(6) # input raster layer inRaster7 = arcpy.GetParameterAsText(7) # input raster layer inRaster8 = arcpy.GetParameterAsText(8) # input raster layer inRaster9 = arcpy.GetParameterAsText(9) # input raster layer inRaster10 = arcpy.GetParameterAsText(10) # input raster layer env.workspace = wks cellSize = 100 outExtent = Extent(871599.89, 982834.44, 912502.09, 1070920.9) ReMapTable_dbf = "P:\\Crime_Analysis\\Testing_Training\\RiskTerrainModeling\\RobinAdrienne\\AggAssaults\\ReMapTable.dbf"  #Multiply Raster by a constant with CreateConstantRaster constantValue1 = arcpy.GetParameterAsText(11) # input weight value outConstRaster1 = CreateConstantRaster(constantValue1, "FLOAT", cellSize, outExtent) outReclass1 = ReclassByTable(inRaster1, ReMapTable_dbf , "FROM", "TO", "VALUE") outTimes1 = outReclass1* outConstRaster1 outRaster1 = "Wt_" + os.path.basename(inRaster1) outTimes1.save(outRaster1) # save raster        constantValue2 = arcpy.GetParameterAsText(12) # input weight value outConstRaster2 = CreateConstantRaster(constantValue2, "FLOAT", cellSize, outExtent) outReclass2 = ReclassByTable(inRaster2, ReMapTable_dbf ,"FROM","TO","VALUE") outTimes2 = outReclass2* outConstRaster2 outRaster2 = "Wt_" + os.path.basename(inRaster2) outTimes2.save(outRaster2) arcpy.SetParameterAsText(13,outRaster2)  #isinstnace test for parameter input       constantValue3 = arcpy.GetParameterAsText(14) # input weight value if constantValue3:       outConstRaster3 = CreateConstantRaster(constantValue3, "FLOAT", cellSize, outExtent)       outReclass3 = ReclassByTable(inRaster3, ReMapTable_dbf ,"FROM","TO","VALUE")       outTimes3 = outReclass3* outConstRaster3       outRaster3 = "Wt_" + os.path.basename(inRaster3)       outTimes3.save(outRaster3) else:        sys.exit()  constantValue4 = arcpy.GetParameterAsText(15) # input weight value if constantValue4:       outConstRaster4 = CreateConstantRaster(constantValue4, "FLOAT", cellSize, outExtent)       outReclass4 = ReclassByTable(inRaster4, ReMapTable_dbf ,"FROM","TO","VALUE")          outTimes4 = outReclass4* outConstRaster4       outRaster4 = "Wt_" + os.path.basename(inRaster4)       outTimes4.save(outRaster4) else:        sys.exit()  constantValue5 = arcpy.GetParameterAsText(16) # input weight value if constantValue5:       outConstRaster5 = CreateConstantRaster(constantValue5, "FLOAT", cellSize, outExtent)       outReclass5 = ReclassByTable(inRaster5, ReMapTable_dbf ,"FROM","TO","VALUE")         outTimes5 = outReclass5* outConstRaster5       outRaster5 = "Wt_" + os.path.basename(inRaster5)       outTimes5.save(outRaster5) else:        sys.exit()  constantValue6 = arcpy.GetParameterAsText(17) # input weight value       if constantValue6:       outConstRaster6 = CreateConstantRaster(constantValue6, "FLOAT", cellSize, outExtent)       outReclass6 = ReclassByTable(inRaster6, ReMapTable_dbf ,"FROM","TO","VALUE")       outTimes6 = outReclass6* outConstRaster6       outRaster6 = "Wt_" + os.path.basename(inRaster6)       outTimes6.save(outRaster6) else:        sys.exit()  constantValue7 = arcpy.GetParameterAsText(18) # input weight value if constantValue7:       outConstRaster7 = CreateConstantRaster(constantValue7, "FLOAT", cellSize, outExtent)       outReclass7 = ReclassByTable(inRaster7, ReMapTable_dbf ,"FROM","TO","VALUE")       outTimes7 = outReclass7* outConstRaster7       outRaster7 = "Wt_" + os.path.basename(inRaster7)       outTimes7.save(outRaster7) else:        sys.exit()  constantValue8 = arcpy.GetParameterAsText(19) # input weight value if constantValue8:       outConstRaster8 = CreateConstantRaster(constantValue8, "FLOAT", cellSize, outExtent)       outReclass8 = ReclassByTable(inRaster8, ReMapTable_dbf ,"FROM","TO","VALUE")        outTimes8 = outReclass8* outConstRaster8       outRaster8 = "Wt_" + os.path.basename(inRaster8)       outTimes8.save(outRaster8) else:        sys.exit()  constantValue9 = arcpy.GetParameterAsText(20) # input weight value if constantValue9:       outConstRaster9 = CreateConstantRaster(constantValue9, "FLOAT", cellSize, outExtent)       outReclass9 = ReclassByTable(inRaster9, ReMapTable_dbf ,"FROM","TO","VALUE")       outTimes9 = outReclass9* outConstRaster9       outRaster9 = "Wt_" + os.path.basename(inRaster9)       outTimes9save(outRaster9) else:        sys.exit()  constantValue10 = arcpy.GetParameterAsText(21) # input weight value if constantValue10:       outConstRaster10 = CreateConstantRaster(constantValue10, "FLOAT", cellSize, outExtent)       outReclass10 = ReclassByTable(inRaster10, ReMapTable_dbf ,"FROM","TO","VALUE")       outTimes10 = outReclass10* outConstRaster10       outRaster10 = "Wt_" + os.path.basename(inRaster10)       outTimes10.save(outRaster10) else:        sys.exit()       


Here is the script inside model builder:
[ATTACH=CONFIG]17981[/ATTACH]

Can I stop the model by changing the python code? Or can I do it through model builder?  Help will be met with the utmost appreciation 😮
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinGasior
Frequent Contributor
Your script tool runs multiple time because the iterator is used. To run the script tool once and iterate through script's output rasters, you have to separate script from iterator.
It can be done using nested models technique - your script with parameters is a master model and iterator+statistics is a submodel.
Here's an example how it may look like:
[ATTACH=CONFIG]17993[/ATTACH]

View solution in original post

0 Kudos
2 Replies
MarcinGasior
Frequent Contributor
Your script tool runs multiple time because the iterator is used. To run the script tool once and iterate through script's output rasters, you have to separate script from iterator.
It can be done using nested models technique - your script with parameters is a master model and iterator+statistics is a submodel.
Here's an example how it may look like:
[ATTACH=CONFIG]17993[/ATTACH]
0 Kudos
RobinCheskin
Occasional Contributor
Thanks m.gasior!

The funny part is that I have another model for this project with 5 nested models; sometimes you need an outside perspective to see very simple solutions, so thanks again.
0 Kudos