Model Builder to Python: Batch Mode

1336
7
Jump to solution
06-06-2012 03:52 PM
LarissaPizzolato
New Contributor
Hello,

I created a model using model builder, and have exported it as a python script. What I am trying to do, by using the for loop, is have it run in batch mode for a series of .txt files that are tables. I am trying to have it so that the end result its 2 separate shape files, one that has been created for the starting latlon, and the other for the endlatlon (with the values all coming from 4 separate columns in the .txt file). I have run into a lot of difficulties trying to create a python script that will run in batch mode ...

I am very new to python (as I'm sure you are able to tell by the code), and would appreciate any feedback as to how to use this model that has been exported from model builder by using python and running in batch mode.

Thank-you


 # Import system modules import sys, string, os, arcgisscripting  # Create the Geoprocessor object gp = arcgisscripting.create(9.3)   # Load required toolboxes... gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")  # Allows geoprocessing tools to overwrite the output if it already exists. gp.OverWriteOutput = True  # Set the Environment Variables gp.Workspace = r"C:\TST_Folder"  # List the .txt file input tables table = gp.ListTables("*txt")  try:     # For each .txt table found in the Workspace, perform the following series of tasks for each .txt file      for table in Workspace:           # Removes the file extension         tableName = table[:-4]          # Reports which table is being processed         gp.addmessage("Processing table" + str(tableName))                   # Process: Make XY Event Layer (E_LATLON)...Save file as the tableName with tmp1 as the suffix         gp.MakeXYEventLayer_management(tableName, "LON", "LAT", tableName + (str(tmp1)), "GEOGCS['GCS_WGS_1984',DATUM")          # Process: Copy Features (E_LATLON)...Save file as the tableName with tmp1 as the suffix         gp.CopyFeatures_management(tableName + (str(tmp1)), tableName + (str(tmp1.shp)), "", "0", "0", "0")          # Process: Project (E_LATLON)...Save file as the tableName with _e as the suffix         gp.Project_management(tableName + (str(tmp1.shp)), tableName + (str(_e.shp)), "PROJCS['LCC'])          # Process: Add Field onto(E_LATLON)...          gp.AddField_management(tableName + (str(_e.shp)), "Title", "FLOAT", "6", "4", "", "", "NON_NULLABLE", "NON_REQUIRED", "")          # Process: Calculate Field onto (E_LATLON)...          gp.CalculateField_management(tableName_e.shp, "Title", "Sqr ( X^2 )", "VB", "")          # Process: Make XY Event Layer (S_LATLON)... Save file as the tableName with tmp as the suffix         gp.MakeXYEventLayer_management(tableName, "sLon", "sLat", tableName_tmp, "GEOGCS['GCS_WGS_1984',DATUM")          # Process: Copy Features (S_LATLON)... Save file as the tableName with tmp as the suffix          gp.CopyFeatures_management(tableName + (str(tmp)), tableName + (str(tmp.shp)), "", "0", "0", "0")          # Process: Project (S_LATLON)... Save file as the tableName with _s as the suffix         gp.Project_management(tableName_tmp.shp, tableName + (str(_s.shp)), "PROJCS['LCC'])  except:     #If an error occurred while running the script, then print the messages     print gp.GetMessages()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
You have a bunch of brackets missing (or extra), that's probably the reason for the interpreter not being able to untabify properly.

Also, all your .shp files need to be in string quotes.
Eg. tableName_e.shp > "tableName_e.shp"

View solution in original post

0 Kudos
7 Replies
markdenil
Occasional Contributor III
Well, for one, you are not really looping an iteratable object.  (for table in Workspace: )
# Set the Environment Variables
gp.Workspace = r"C:\TST_Folder"        #### < THIS IS A GP VARIABLE

# List the .txt file input tables
table = gp.ListTables("*txt")               #### < THIS IS THE LIST YOU SHOULD ITTERATE

try:
    # For each .txt table found in the Workspace, perform the following series of tasks for each .txt file 
    for table in Workspace:          #### Workspace CANNOT BE ITTERATED, and YOU ARE REASSIGNING table

        # Removes the file extension
        tableName = table[:-4]       #### IN VIEW OF THE LINE ABOVE, THIS MAKES NO SENSE
                                #### IF table WAS STILL A LIST, YOU WOULD BE CHOPPING OFF THE LAST FOUR ITEMS


What you want is something more like this:
# Set the Environment Variables
gp.Workspace = r"C:\TST_Folder"

# List the .txt file input tables
table = gp.ListTables("*txt")

try:
    for t in table: 
        # Removes the file extension
        tableName = t[:-4]
        # Reports which table is being processed
        gp.addmessage("Processing table" + str(tableName))
0 Kudos
BruceNielsen
Occasional Contributor III
It's not safe anymore to assume that a file extension has exactly 3 characters
import os
tableName = os.path.splitext(table)[0]
0 Kudos
LarissaPizzolato
New Contributor
I changed the code as follows, now I am getting a tab/space error... I have de-dented all of the code, and re-indented it. I have even tried the untabify region with no success. Is there anything else could be causing this problem?

Also, I am unsure as to how to name the output files at the individual steps within the for loop ... it seems that this might be the cause of the problem?

# List the .txt file input tables
table = gp.ListTables("*txt")
print table

try:
 for t in list(table):
  # Removes the file extension
  tableName = os.path.splitext(t)[0]
  # Reports which table is being processed
  gp.addmessage("Processing table" + str(tableName))
  # Process: Make XY Event Layer (endLatLon)...
  gp.MakeXYEventLayer_management(tableName, "eLon", "eLat", tableName + (str(_tmp1)), "Coordinate System")
  # Process: Copy Features (endLatLon)...
  gp.CopyFeatures_management(tableName_tmp1), tableName + (str(_tmp1a)), "", "0", "0", "0")
  # Process: Project (endLatLon)...
  gp.Project_management(tableName_tmp1a, tableName + (str(_e.shp)), "PROJCS['LCC')]")
  # Process: Add Field onto(endLatLon)...
  gp.AddField_management(tableName_e.shp), "Vari", "FLOAT", "3", "2", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
  # Process: Calculate Field onto (endLatLon)...
  gp.CalculateField_management(tableName_e.shp, "Vari", "Sqr ( x^2 )", "VB", "")
  # Process: Make XY Event Layer (startLatLon)...
  gp.MakeXYEventLayer_management(tableName, "sLon", "sLat", tableName + (str(_tmp)), "Coordinate System")
  # Process: Copy Features (startLatLon)...
  gp.CopyFeatures_management(tableName_tmp), tableName + (str(_tmpa)), "", "0", "0", "0")
  # Process: Project (startLatLon)...
  gp.Project_management(tableName_tmpa.shp, tableName + (str(_s.shp)), "PROJCS['LCC')]")
except:
 #If an error occurred while running the script, then print the messages
 print gp.GetMessages()
0 Kudos
MathewCoyle
Frequent Contributor
You have a bunch of brackets missing (or extra), that's probably the reason for the interpreter not being able to untabify properly.

Also, all your .shp files need to be in string quotes.
Eg. tableName_e.shp > "tableName_e.shp"
0 Kudos
LarissaPizzolato
New Contributor
Hello again,

When I run the script, I recieved this error:

Failed to execute. Parameters are not valid.
ERROR 000732: Input Features: Dataset tableName_tmp1.shp does not exist or is not supported
Failed to execute (CopyFeatures).

I tried to solve it by adding the local variables (similar to the original model that was exported from Model Builder), but that didn't seem to work ... any suggestions as to where I am going wrong?

Thanks!

# List the .txt file input tables
table = gp.ListTables("*txt")
print table

try:
    for t in list(table):
        # Removes the file extension
        tableName = os.path.splitext(t)[0]
        # Reports which table is being processed
        gp.addmessage("Processing table" + str(tableName))

        # Local variables...
        tableName_tmp = tableName + str("_tmp")
        tableName_tmp1 = tableName + str("_tmp1")
        tableName_e = tableName + str("_e.shp")
        tableName_tmp1A = tableName + str("_tmp1A.shp")
        tableName_s = tableName + str("_s.shp")
        tableName_tmpA = tableName + str("_tmpA.shp")
        
        # Process: Make XY Event Layer (endLatLon)...
        gp.MakeXYEventLayer_management(t, "eLon", "eLat", tableName_tmp1, "GeographicCoordinates")
        # Process: Copy Features (endLatLon)...
        gp.CopyFeatures_management("tableName_tmp1.shp", tableName_tmp1A, "", "0", "0", "0")
        # Process: Project (endLatLon)...
        gp.Project_management("tableName_tmp1A.shp", tableName_e, "ProjectedCoordinates")
        # Process: Add Field onto(endLatLon)...
        gp.AddField_management("tableName_e.shp", "Vel_Value", "FLOAT", "6", "4", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
        # Process: Calculate Field onto (endLatLon)...
        gp.CalculateField_management("tableName_e.shp", "Vel_Value", "x^2 ", "VB", "")
        # Process: Make XY Event Layer (startLatLon)...
        gp.MakeXYEventLayer_management(tableName, "sLon", "sLat", tableName_tmp, "GeographicCoordinates")
        # Process: Copy Features (startLatLon)...
        gp.CopyFeatures_management("tableName_tmp.shp", tableName_tmpA, "", "0", "0", "0")
        # Process: Project (startLatLon)...
        gp.Project_management("tableName_tmpA.shp", tableName_s, "ProjectedCoordinates")
except:
    #If an error occurred while running the script, then print the messages
    print gp.GetMessages()
0 Kudos
MathewCoyle
Frequent Contributor
I think where you have strings like
"tableName_tmp1.shp"


You want to use
tableName_tmp1
0 Kudos
LarissaPizzolato
New Contributor
Thank-you Everyone! I've finally got it working now!
0 Kudos