Select to view content in your preferred language

How to setup BATCH process for my Script.

5448
29
Jump to solution
01-11-2013 10:07 AM
ChristopherClark1
Deactivated User
Ok, I have the following python script which migrates and maps fields and the values of those fields into a target Feature Class.
# Import arcpy module import arcpy  arcpy.env.workspace = "G:\ChrisGIS\WDFW_NWIFC_EVENT_17100101.gdb" arcpy.env.overwriteOutput = "True"                def GetFieldMappings(inputFC, targetFC, dico):     field_mappings = arcpy.FieldMappings()     field_mappings.addTable(inputFC)      for input, output in dico.iteritems():         field_map = arcpy.FieldMap()         field_map.addInputField(inputFC, input)                 field = field_map.outputField                 field.name = output         field_map.outputField = field         field_mappings.addFieldMap(field_map)         del field, field_map      return field_mappings  #Variables inputFC = "WDFW_CHFA_2HEM_evt_1" targetFC = "WDFW_CHFA_2HEM_evt_olay_1"  dico = {'Permanent_Identifier': 'Permanent_ID',                 'STRM_NAME':        'LLID_STRM_NAME',     'DATE_SRVY': 'SOURCE_DATE',     'SPPCODE': 'SPECRCODE',     'SRC_AGENCY': 'SOURCE_AGENCY',     'SRC_WHO': 'SOURCE_WHO'}      Mapper = GetFieldMappings(inputFC, targetFC, dico) # Process: Append arcpy.Append_management(arcpy.env.workspace+"\\"+inputFC, arcpy.env.workspace+"\\"+targetFC,"NO_TEST",Mapper)

I have several feature classes to run this script on. AN exmaple of the Feature Classes that I need to run my script on are :

WDFW_CCT_2HEM_evt_olay
WDFW_CHFA_2HEM_evt_olay
WDFW_CHMF_2HEM_evt_olay
WDFW_CHSP_2HEM_evt_olay
WDFW_CHSU_2HEM_evt_olay
WDFW_COHO_2HEM_evt_olay
WDFW_DBT_2HEM_evt_olay
WDFW_KOK_2HEM_evt_olay
WDFW_RBT_2HEM_evt_olay
WDFW_SOCK_2HEM_evt_olay

These feature classes are represented in several geodatabases (see image)
[ATTACH=CONFIG]20670[/ATTACH]
I could simply go to each individual geodatabase and run the script for each featureclass in that geodatabase. This however seems redundant and I would like to figure out how to batch my scrip or automate this process. All geodatabases are located in one working directory.

I appreciate any help or advice!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
I sent you a private message with my email info.  I'll take a look at it.

View solution in original post

0 Kudos
29 Replies
by Anonymous User
Not applicable
Yes, you can use the ListWorkspaces().

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v0000001w000000

arcpy.env.workspace = r'some\path'
for ws in arcpy.ListWorkspaces("*", "FileGDB"):
    arcpy.env.workspace = ws
    for fc in arcpy.ListFeatureClasses('WDFW*'):
         # paste your code here
         # do stuff

0 Kudos
ChristopherClark1
Deactivated User
Will try this evening. Thanks Caleb!
0 Kudos
ChristopherClark1
Deactivated User
I messed around with Calebs suggestion and could not get it to work properly.

Notice in my script I have a input FC and a Target FC. In every GDB there will be several paired Input and target FC (1 pair for each species). For example here is a list of FCs found in one gdb. Every inputFC and its targetFC for my script in this gdb would be :

InputFC: ---------------------------TargetFC:


  1. WDFW_CHFA_2HEM_evt_1-------WDFW_CHFA_2HEM_evt_olay_1

  2. WDFW_CCT_2HEM_evt_1--------WDFW_CCT_2HEM_evt_olay_1

  3. WDFW_CHMF_2HEM_evt_1-------WDFW_CHMF_2HEM_evt_olay_1

  4. WDFW_CHSP_2HEM_evt_1-------WDFW_CHSP_2HEM_evt_olay_1

  5. WDFW_COHO_2HEM_evt_1-------WDFW_COHO_2HEM_evt_olay_1

  6. WDFW_KOK_2HEM_evt_1---------WDFW_KOK_2HEM_evt_olay_1



The long way would be to change my workspace each time to the correct GDB then run my script for each InputFC. The list above for example would have me run the script 6 times. Each time I would edit the variables : inputFC and targetFC.

Trying to automate this is proving a nightmare!
0 Kudos
by Anonymous User
Not applicable
  WDFW_CHFA_2HEM_evt_1-------WDFW_CHFA_2HEM_evt_olay_1 
WDFW_CCT_2HEM_evt_1--------WDFW_CCT_2HEM_evt_olay_1 
WDFW_CHMF_2HEM_evt_1-------WDFW_CHMF_2HEM_evt_olay_1 
WDFW_CHSP_2HEM_evt_1-------WDFW_CHSP_2HEM_evt_olay_1 
WDFW_COHO_2HEM_evt_1-------WDFW_COHO_2HEM_evt_olay_1 
WDFW_KOK_2HEM_evt_1---------WDFW_KOK_2HEM_evt_olay_1 

The long way would be to change my workspace each time to the correct GDB then run my script for each InputFC. The list above for example would have me run the script 6 times. Each time I would edit the variables : inputFC and targetFC. 

Trying to automate this is proving a nightmare!  


If these really are the field names for FCs in each GDB, it can be easy to set the input and targets. Programming the inputs and outputs is a lot easier than you think if the same names exist in each GDB. Try this:

# Import arcpy module
import arcpy
arcpy.env.overwriteOutput = True
from os import path as p
        
def GetFieldMappings(inputFC, targetFC, dico):
    field_mappings = arcpy.FieldMappings()
    field_mappings.addTable(inputFC)

    for input, output in dico.iteritems():
        field_map = arcpy.FieldMap()
        field_map.addInputField(inputFC, input)        
        field = field_map.outputField        
        field.name = output
        field_map.outputField = field
        field_mappings.addFieldMap(field_map)
        del field, field_map

    return field_mappings

#Variables
dico = {'Permanent_Identifier': 'Permanent_ID',
                'STRM_NAME':        'LLID_STRM_NAME',
    'DATE_SRVY': 'SOURCE_DATE',
    'SPPCODE': 'SPECRCODE',
    'SRC_AGENCY': 'SOURCE_AGENCY',
    'SRC_WHO': 'SOURCE_WHO'}

arcpy.env.workspace = r"G:\ChrisGIS"
for ws in arcpy.ListWorkspaces("*", "FileGDB"):
    arcpy.env.workspace = ws
    for fc in arcpy.ListFeatureClasses():
        if fc[-6:] != 'olay_1':
            inputFC = fc
            targetFC = fc[:-1] + 'olay_1'   
            Mapper = GetFieldMappings(inputFC, targetFC, dico)
            # Process: Append
            arcpy.Append_management(p.join(ws,inputFC), p.join(ws,targetFC),"NO_TEST",Mapper)
            print 'Appended "{0}" to "{1}" in {2}'.format(inputFC, targetFC, p.basename(ws))





EDIT: I added the part in red. I forgot to add this when I originally posted the code. This is to exclude the target features when looping through each GDB (since you only need the ones that end in 'evt_1' as inputFC's).
0 Kudos
ChristopherClark1
Deactivated User
Thanks for the help Caleb! I cleaned up my gedodatabases, and modified the your script to reflect my changes. Now, in a given GeoDatabase I have File geodatabase tables such as :

WDFW_CCT_2HEM_evt
WDFW_CHFA_2HEM_evt
WDFW_CHMF_2HEM_evt
WDFW_COHO_2HEM_evt
WDFW_KOK_2HEM_evt

These are going to be the InputFC for my field mapping and append processes. Here is the code I am trying to run. I get this error every time and do not understand why. ERROR :

"Runtime error <class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid. ERROR 000732: Target Dataset: Dataset G:\ChrisGIS\COMBO_fishdist_events\WDFW_NWIFC_EVENT_17100101.gdb\WDFW_CCT_2HEM_evt_olay does not exist or is not supported Failed to execute (Append)."

# Import arcpy module
import arcpy
arcpy.env.overwriteOutput = True
from os import path as p
        
def GetFieldMappings(inputFC, targetFC, dico):
    field_mappings = arcpy.FieldMappings()
    field_mappings.addTable(inputFC)

    for input, output in dico.iteritems():
        field_map = arcpy.FieldMap()
        field_map.addInputField(inputFC, input)        
        field = field_map.outputField        
        field.name = output
        field_map.outputField = field
        field_mappings.addFieldMap(field_map)
        del field, field_map

    return field_mappings

#Variables
dico = {'Permanent_Identifier': 'Permanent_ID',
                'STRM_NAME':        'LLID_STRM_NAME',
    'DATE_SRVY': 'SOURCE_DATE',
    'SPPCODE': 'SPECRCODE',
    'SRC_AGENCY': 'SOURCE_AGENCY',
    'SRC_WHO': 'SOURCE_WHO'}

arcpy.env.workspace = r"G:\ChrisGIS\COMBO_fishdist_events"
for ws in arcpy.ListWorkspaces("*", "FileGDB"):
 listFCS = arcpy.ListTables("WDFW*evt")
 arcpy.env.workspace = ws
 for inputFC in listFCS:
  targetFC = inputFC +"_olay"   
  Mapper = GetFieldMappings(inputFC, targetFC, dico)
 # Process: Append
 arcpy.Append_management(p.join(ws,inputFC), p.join(ws,targetFC),"NO_TEST",Mapper)
 print 'Appended "{0}" to "{1}" in {2}'.format(inputFC, targetFC, p.basename(ws))


Any ideas?
0 Kudos
by Anonymous User
Not applicable
"Runtime error <class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid. ERROR 000732: Target Dataset: Dataset G:\ChrisGIS\COMBO_fishdist_events\WDFW_NWIFC_EVENT_17100101.gdb\WDFW_CCT_2HEM_evt_olay does not exist or is not supported Failed to execute (Append)."


I think you were almost there.  The indentation for the append needed to be in line with the for loop (otherwise it would not create an input and target fc since it was outside the loop where the tables are listed).

So the input will look like this?
WDFW_CCT_2HEM_evt

and the output will look like this?
WDFW_CCT_2HEM_evt_olay


If those are the correct names, this code should work with the corrected indentation.
# Import arcpy module
import arcpy
arcpy.env.overwriteOutput = True
from os import path as p
        
def GetFieldMappings(inputFC, targetFC, dico):
    field_mappings = arcpy.FieldMappings()
    field_mappings.addTable(inputFC)

    for input, output in dico.iteritems():
        field_map = arcpy.FieldMap()
        field_map.addInputField(inputFC, input)        
        field = field_map.outputField        
        field.name = output
        field_map.outputField = field
        field_mappings.addFieldMap(field_map)
        del field, field_map

    return field_mappings

#Variables
dico = {'Permanent_Identifier': 'Permanent_ID',
                'STRM_NAME':        'LLID_STRM_NAME',
    'DATE_SRVY': 'SOURCE_DATE',
    'SPPCODE': 'SPECRCODE',
    'SRC_AGENCY': 'SOURCE_AGENCY',
    'SRC_WHO': 'SOURCE_WHO'}

arcpy.env.workspace = r"G:\ChrisGIS\COMBO_fishdist_events"
for ws in arcpy.ListWorkspaces("*", "FileGDB"):
    arcpy.env.workspace = ws
    listFCS = arcpy.ListTables("WDFW*evt")
    for inputFC in listFCS:
        targetFC = inputFC +"_olay"   
        Mapper = GetFieldMappings(inputFC, targetFC, dico)
        # Process: Append
        arcpy.Append_management(p.join(ws,inputFC), p.join(ws,targetFC),"NO_TEST",Mapper)
        print 'Appended "{0}" to "{1}" in {2}'.format(inputFC, targetFC, p.basename(ws))
0 Kudos
ChristopherClark1
Deactivated User
Thanks for the quick replies Caleb. When I try to run the last script you provided I keep getting a syntax error on line 34 which reads

if inputFC[-5:] != "_olay"

what is the error?

Do I have to exclude the olay files? I do not have any olays in my geodatabases to start, after running the script there will be tables name ***_olay. I thought when I make my FeatureClass.list tables the append will only be run on the values found in that list. If this is the case there are no _olay tables, as they are only created when I run my script. It may seen confusing from the earlier posts b/c I cleaned out all the tables name **_olay.
0 Kudos
by Anonymous User
Not applicable
Thanks for the quick replies Caleb. When I try to run the last script you provided I keep getting a syntax error on line 34 which reads

if inputFC[-5:] != "_olay"

what is the error?

Do I have to exclude the olay files? I do not have any olays in my geodatabases to start, after running the script there will be tables name ***_olay. I thought when I make my FeatureClass.list tables the append will only be run on the values found in that list. If this is the case there are no _olay tables, as they are only created when I run my script. It may seen confusing from the earlier posts b/c I cleaned out all the tables name **_olay.


No, sorry, that was a mistake by me.  I saw that and realized it wasn't necessary and took it out. Your code *should* work once you put the last 3 lines inside the for loop so the code ends like this:

    for inputFC in listFCS:
        targetFC = inputFC +"_olay"   
        Mapper = GetFieldMappings(inputFC, targetFC, dico)
        # Process: Append
        arcpy.Append_management(p.join(ws,inputFC), p.join(ws,targetFC),"NO_TEST",Mapper)
        print 'Appended "{0}" to "{1}" in {2}'.format(inputFC, targetFC, p.basename(ws))
0 Kudos
by Anonymous User
Not applicable
Actually, I just realized... If the olay tables do not exist beforehand you will get an error because append works on only existing features.  You should use the CreateTable_management tool to create the table first right above the append.

Try this:

# Import arcpy module
import arcpy
arcpy.env.overwriteOutput = True
from os import path as p
        
def GetFieldMappings(inputFC, targetFC, dico):
    field_mappings = arcpy.FieldMappings()
    field_mappings.addTable(inputFC)

    for input, output in dico.iteritems():
        field_map = arcpy.FieldMap()
        field_map.addInputField(inputFC, input)        
        field = field_map.outputField        
        field.name = output
        field_map.outputField = field
        field_mappings.addFieldMap(field_map)
        del field, field_map

    return field_mappings

#Variables
dico = {'Permanent_Identifier': 'Permanent_ID',
                'STRM_NAME':        'LLID_STRM_NAME',
    'DATE_SRVY': 'SOURCE_DATE',
    'SPPCODE': 'SPECRCODE',
    'SRC_AGENCY': 'SOURCE_AGENCY',
    'SRC_WHO': 'SOURCE_WHO'}

arcpy.env.workspace = r"G:\ChrisGIS\COMBO_fishdist_events"
for ws in arcpy.ListWorkspaces("*", "FileGDB"):
    arcpy.env.workspace = ws
    listFCS = arcpy.ListTables("WDFW*evt")
    for inputFC in listFCS:
        targetFC = inputFC +"_olay"   
        Mapper = GetFieldMappings(inputFC, targetFC, dico)
        # Process: Append
        arcpy.CreateTable_management(ws, targetFC))
        arcpy.Append_management(p.join(ws,inputFC), p.join(ws,targetFC),"NO_TEST",Mapper)
        print 'Appended "{0}" to "{1}" in {2}'.format(inputFC, targetFC, p.basename(ws))

0 Kudos