Select to view content in your preferred language

Problems with Model builder and Script PYTHON

2311
3
12-05-2013 10:48 AM
MirkoDelfin_Soruco
New Contributor
I made a Model to delimit River basins with Model Builder. When exporting in Python format and proving it in another Machine, it takes almost 40 minutes in processing and it always leaves error, not as this problem can be solved. In this file I have two models, one to calcular the direction of the flow and in the other model to delimit the River basin. The second model depends on the exits of first. There is some form to have a single model, instead of two or three. Thank you very much
Tags (2)
0 Kudos
3 Replies
MaxSquires
Frequent Contributor
Hello.  I think I understand your question, but it would be helpful if you could provide your code so that we know more precisely what you mean. 

import arcpy
if arcpy.Exists(output1):
    arcpy.SolveNetwork(output1, output2)


however, you may mean something more along the lines of a tool validator. These are not so easy to explain but I would go to an existing script tool (denoted by the scroll -like icon within a toolbox) and right click the tool.  in the context menu that pops up, select the Validation tab.  In here you will see the validator class that can run prerequisite components of your script tool. You can start from scratch or you can try to use the validation classes that esri has created as a starting point.

For example, the data management toolbox contains python script tool for converting points to a line. You can view the source code for this tool but to address the issue you are having you may be interested in the validator tab which shows:
import locale

class ToolValidator:
  """Class for validating a tool's parameter values and controlling
  the behavior of the tool's dialog."""

  def __init__(self):
    """Setup the Geoprocessor and the list of tool parameters."""
    import arcgisscripting as ARC
    self.GP = ARC.create(9.3)
    self.params = self.GP.getparameterinfo()

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""

    self.params[1].Schema.GeometryType = "Polyline"
    self.params[1].Schema.FieldsRule = "AllFIDsOnly"

    return

  def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parmater
    has been changed."""

    if self.params[2].altered:
        if self.params[0].value and self.params[2].value:
            field = self.GP.CreateObject("Field")
            field.name = self.params[2].value.value
            self.params[1].Schema.AdditionalFields = [field]
    return

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return




You can see from this example that if the self.params[2] parameter is modified, a function is called to add a field to the point feature.  This is where your first model could be initialized/checked for the existence of.

Good luck!
0 Kudos
MirkoDelfin_Soruco
New Contributor
---------------------------------------------------------------------------
# delimita.py
# Created on: Thu Dec 05 2013 07:18:26 AM
#   (generated by ArcGIS/ModelBuilder)
# Description:
# Es importante que e Modelo de Elevacion tenga Proyeccion UTM, caso contrario, cambie al Sistema de Proyeccion Sugerido
# ---------------------------------------------------------------------------

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Check out any necessary licenses
gp.CheckOutExtension("spatial")

# Load required toolboxes...
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")

# Set the Geoprocessing environment...
gp.extent = "DEFAULT"


# Local variables...
lleno = "C:\\cuenca\\lleno"
direccion = "C:\\cuenca\\direccion"
Output_drop_raster = ""
acumula = "C:\\cuenca\\acumula"
condicional = "C:\\cuenca\\condicional"
Input_true_raster_or_constant_value = "1"
Expression = "Value > 4000"
drenaje = "C:\\cuenca\\drenaje.shp"
dem = "C:\\cuenca\\dem"

# Process: Fill...
gp.Fill_sa(dem, lleno, "")

# Process: Flow Direction...
gp.FlowDirection_sa(lleno, direccion, "NORMAL", Output_drop_raster)

# Process: Flow Accumulation...
gp.FlowAccumulation_sa(direccion, acumula, "", "FLOAT")

# Process: Con...
gp.Con_sa(acumula, Input_true_raster_or_constant_value, condicional, "", Expression)

# Process: Stream to Feature...
gp.StreamToFeature_sa(condicional, direccion, drenaje, "SIMPLIFY")
0 Kudos
MaxSquires
Frequent Contributor
The following works in extreem cases where you don't want to recreate anything that is already on disk.  If you check for the existance of something then you can choose to not create it as is done below. 
#---------------------------------------------------------------------------
# delimita.py
# Created on: Thu Dec 05 2013 07:18:26 AM
# 
# Description: 
# Es importante que e Modelo de Elevacion tenga Proyeccion UTM, caso contrario, cambie al Sistema de Proyeccion Sugerido
# ---------------------------------------------------------------------------

# Import system modules
import sys, string, os, arcpy#arcgisscripting

# Create the Geoprocessor object
#not used in 10.1
#gp = arcgisscripting.create()

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

# Load required toolboxes...
arcpy.AddToolbox(r"C:\Program Files\ArcGIS\Desktop10.1\ArcToolbox\Toolboxes\Spatial Analyst Tools.tbx")

# Set the Geoprocessing environment...
#not used in working example
#arcpy.extent = "DEFAULT"

#The only input variable to this script:
dem = r"C:\Users\Me\Desktop\M130_B90\M130_B90\M130_38076DSQ_BIG.dem"
# downloaded from:
# http://estuarinebathymetry.noaa.gov/bathy_htmls/M130.html


# Local variables...
lleno = r"C:\Users\Me\Desktop\M130_B90\lleno"
direccion = r"C:\Users\Me\Desktop\M130_B90\direccion"
Output_drop_raster = ""
acumula = r"C:\Users\Me\Desktop\M130_B90\acumula"
condicional = r"C:\Users\Me\Desktop\M130_B90\condicional"
Input_true_raster_or_constant_value = "1"
Expression = "Value > 4000"
drenaje = r"C:\Users\Me\Desktop\M130_B90\drenaje.shp"


# Process: Fill...
if not os.path.exists(lleno):
    arcpy.Fill_sa(dem, lleno, "")

# Process: Flow Direction...
if not os.path.exists(direccion):
    arcpy.FlowDirection_sa(lleno, direccion, "NORMAL", Output_drop_raster)

# Process: Flow Accumulation...
if not os.path.exists(acumula):
    arcpy.FlowAccumulation_sa(direccion, acumula, "", "FLOAT")

# Process: Con...
if not os.path.exists(condicional):
    arcpy.Con_sa(acumula, Input_true_raster_or_constant_value, condicional, "", Expression)

# Process: Stream to Feature...
if not os.path.exists(drenaje):
    arcpy.StreamToFeature_sa(condicional, direccion, drenaje, "SIMPLIFY")


This same method will work for this example, but if you are looking to see if a property exists or if a file geodatabase feature class exists, you will need to use the arcpy.Exists boolean function.

I would consider using the script as a toolbox script tool by providing input parameters.
for example, arcpy, or in your case:
dem = gp.GetParameterAsText(0)
output = gp.SetParemeter)(1)


and then configure the parameter values using the parameters tab of the script tool properties page in arccatalog. 

I realize you are using arcgis 9.x and it may not work exaclty like this but this should get you going in the right direction.
0 Kudos