[GIS] ArcGIS 10.1 Converting a model tool to a script tool

arcgis-10.1arcgis-desktopmodelbuilderpython

I have recently created a model tool for a class project and my instructor would like it to be converted to a script tool for practice purposes. I have exported the script from my model and loaded it into a script within a toolbox but there are several things I am unsure about. I have defined the parameters as they were in the model but when I run the script tool it give me multiple errors. I know that the model doesn't pass model only tools to the script (i.e. Select Data) so I'm sure my problem stems from that but I'm not sure what exactly I need to add to fix the problem.

I will attach a copy of my script, tool parameters, tool properties, the original model and the errors I get. I am an amateur with Python so bare with me if I'm asking too much.

NOTE: the '*' represents the actual values but can't be shared

# ---------------------------------------------------------------------------
# CROB1.py
# Created on: 2013-10-08 11:45:20.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: CROB1 <ArcSDE_Connection_File_Location> <Select_Your_AP_County> <Select_Your_AP> <Your_Counties> <Your_AP> 
# Description: 
# This tool creates the layers for management plan map for your AP boundary. The user must still format the Layout View before exporting.
# ---------------------------------------------------------------------------

# Set the necessary product code
import arceditor    
# Import arcpy module
import arcpy


arcpy.env.overwriteOutput = True

# Script arguments
ArcSDE_Connection_File_Location = arcpy.GetParameterAsText(0)
Select_Your_AP_County = arcpy.GetParameterAsText(1)  
Select_Your_AP = arcpy.GetParameterAsText(2)

# Local variables:
FDEP_SDE_Connection_sde = ArcSDE_Connection_File_Location
Counties = FDEP_SDE_Connection_sde
Aquatic_Preserves = FDEP_SDE_Connection_sde

# Process: Create ArcSDE Connection File

arcpy.CreateArcSDEConnectionFile_management(ArcSDE_Connection_File_Location, "FDEP_SDE_Connection", "******", "****", "", "DATABASE_AUTH", "****", "*****", "SAVE_USERNAME", "SDE.DEFAULT", "SAVE_VERSION")

# Process: Select Data
arcpy.SelectData_management(FDEP_SDE_Connection_sde, "BASE.FL_SHORELINE_AREAS")

# Process: Select
arcpy.Select_analysis(Counties, Your_Counties, "\"NAME\" = '%Select Your AP County%' ")

# Process: Select Data (2)
arcpy.SelectData_management(FDEP_SDE_Connection_sde, "DEP.AQUATIC_PRESERVES")

# Process: Select (2)
arcpy.Select_analysis(Aquatic_Preserves, Your_AP, "\"LONG_NAME\" = '%Select Your AP%'")`

enter image description here
enter image description here
enter image description here
enter image description here

Best Answer

I agree with Jason's comment about avoiding relying too much (or at all) on the export to Python functionality in ModelBuilder. I also think that if you are brand new to Python and programming in general, it's better to cut your teeth on "pure" Python than Python + ArcGIS. But since your task is to do exactly that, here are my suggestions:

  1. Think about what your model or code is actually trying to accomplish. Write a written description of what the intended purpose of the tool is and place it as a comment at the top of your script, to remind you of your goal.

    Think about how you would do it manually/interactively in ArcGIS. Write this down as a series of steps, or flowchart it if you prefer.

    Try grouping these steps into logical sections, which can later be turned into functions. Think about what inputs and outputs (if any) apply to each of these sections. These will be your function's arguments and return values.

  2. Stub out the basic structure of the script, or use a template.

    • I like to just start off with a few imports I know I'll probably end up using:

      import arcpy, os, sys
      
    • Stub out the functions I think I'll need:

      def buildWhereClause(table_name, field_name, value):
          # TODO: construct a SQL WHERE clause based on the given parameters
          return "(some hardcoded WHERE clause)"
      
      def selectFeatures(layer, where_clause=None):
          # TODO: select the features in the given layer and using the optional SQL WHERE clause
          pass
      

      (These are just examples and may be overly simplistic but it hopefully gets the point across.)

    • After that I'll usually have an if __name__ == "__main__" section that either contains or calls a main procedure that is the entry point to the code and starts taking inputs and calling other functions. The purpose of this check is to prevent the code from running if all you want to do is import the module and call its functions independently:

      if __name__ == "__main__":
          # TODO: Get user-entered parameters, hardcode them for now
          input_table = r"C:\test.shp"
          field_name = "NAME"
          field_value = "12345"
      
          whereclause = buildWhereClause(input_table, field_name, field_value)
          layer = arcpy.MakeFeatureLayer_management(input_table)
          selectFeatures(layer, whereclause)
      
  3. Test the script, either from the script tool dialog, a Python IDE (if it doesn't need to run inside ArcMap), or by importing the module into the ArcMap Python window and calling specific functions.

  4. Implement the rest of the code, referring to documentation as needed, testing frequently, and iteratively improving the script until it is complete.

See also:

Related Question