batch join field using python

834
3
Jump to solution
06-29-2013 06:41 AM
ElaineKuo
Occasional Contributor
Hello

System vista and ArcGIS 9.3

I want to join the field of a grid shape file (file A) to 50 shape files (file B) in one folder.
File A has a field called ???GID.???
Each of the file B also has one field ???GID.???
Therefore, GID is the base field to join file A and B.

I wrote a code for the purpose but did not work.
The problems could be:

1. gp.MakeFeatureLayer_management (outFeatureClass, "outFeatureClass_lyr")
    ExecuteError: Failed to execute. Parameters are not valid.


2. ERROR 000732:
    Input Features: Dataset H:/temp_D\geoc0283_shp does not exist or is not supported
    Failed to execute (MakeFeatureLayer).

Thank you.

################################################## ######################## ##AddJoin feature layers to a feature layer ##Elaine Kuo ##30 June 2013 ################################################## #######################  #Import standard library modules import arcgisscripting import os  #Create the Geoprocessor object gp = arcgisscripting.create(9.3)  # Load required toolboxes... ## gp.AddToolbox("C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Data Management Tools.tbx") ## no need for system toolboxes  #Set the input workspace workingfolder = "H:/temp_D/test"  source = "H:/temp_D/testt/A_grid.shp"  outputfolder = "H:/temp_D"  #Set workspace gp.Workspace = workingfolder  #Set the output workspace outWorkspace= outputfolder  #Get a list of the featureclasses in the input folder fcs = gp.ListFeatureClasses()  # Loop through every item in the list that was just generated for fc in fcs:          #Validate the new feature class name for the output workspace.     outFeatureClass = outWorkspace + os.sep +gp.ValidateTableName(fc,outWorkspace)     gp.Toolbox = "Data Management"     gp.MakeFeatureLayer_management (outFeatureClass, "outFeatureClass_lyr")          # make a layer for source     gp.MakeFeatureLayer_management (source, "source_lyr")      # Make temporary featureclasses     gp.MakeFeatureLayer(fc,"lyr")      # have Layers for AddJoin     gp.AddJoin_management("source_lyr", "GID", "lyr", "GID", "KEEP_ALL")      gp.CopyFeatures_management ("source_lyr", "outFeatureClass_lyr")     gp.FeatureClassToShapefile_conversion("outFeatureClass_lyr", outputfolder)      # clear memory of layers     gp.Delete("source_lyr")     gp.Delete("lyr")     gp.Delete("outFeatureClass_lyr")      gp.AddMessage(gp.GetMessages()) print gp.GetMessages()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor
Elaine:

Curtis is right.  Layers and Feature classes are different and not every tool accepts a layer or a feature class.  If you use model builder to build and test and output a single join successfully you will then know what each tool requires and the syntax to work with.  It would be much easier to convert those variables to a loop.  Too many errors are occurring, because you are not letting Model Builder help you understand the tools you are trying to chain together and the required tools you need to process to set up each step.

For example, Model Builder would not have let you add a layer where a feature class is required and it would not let you add a feature class where a layer is required without either preventing it or throwing an easier error to interpret.  You are not familiar enough with the tools or python syntax to get through this without Model Builder's help unless you really want every step to be accompanied by an error.  You can also set the environment variable for not qualifying field name in Model Builder to verify the output creates field names you can live with.

Please indulge me and successfully build and output a model where the grid shape file is joined to just one bird range shape file with hard coded values.  And output that join to a new shape file in the model, finding a tool that does the output successfully from running Model Builder.  After that one join and output works, then export that model to a Python script.  That should be your starting point before attempting to build a Python loop.

I never build a loop until the core of the loop works all the way through for a single input.  Prevents a lot of frustrations and confusion.

View solution in original post

0 Kudos
3 Replies
curtvprice
MVP Esteemed Contributor
Hi Elaine.

gp.MakeFeatureLayer_management (outFeatureClass, "outFeatureClass_lyr")
ExecuteError: Failed to execute. Parameters are not valid.



Here are some thoughts:

- In your MakeFeatureLayer code above, outFeatureClass does not exist yet. This needs to be an existing feature class.
- The output of your CopyFeatures MUST be a new feature class path (not a layer)
- Just the first argument to AddJoin must be a layer, the third argument (join table) may be a dataset or a layer.

I suggest something more along the lines of this:

    out_name = arcpy.Describe(source_lyr).name
    out_name = gp.ValidateTableName(out_name, outfolder)
    out_shape = outputfolder + "/" + out_name + ".shp"
    gp.CopyFeatures_management ("source_lyr", out_shape)

One more thing: If you are outputting to shapefile or .dbf it's a very good idea to set

gp.qualifiedFieldNames = False
(arc 10: arcpy.env.qualifiedFieldnames = False)
0 Kudos
RichardFairhurst
MVP Honored Contributor
Elaine:

Curtis is right.  Layers and Feature classes are different and not every tool accepts a layer or a feature class.  If you use model builder to build and test and output a single join successfully you will then know what each tool requires and the syntax to work with.  It would be much easier to convert those variables to a loop.  Too many errors are occurring, because you are not letting Model Builder help you understand the tools you are trying to chain together and the required tools you need to process to set up each step.

For example, Model Builder would not have let you add a layer where a feature class is required and it would not let you add a feature class where a layer is required without either preventing it or throwing an easier error to interpret.  You are not familiar enough with the tools or python syntax to get through this without Model Builder's help unless you really want every step to be accompanied by an error.  You can also set the environment variable for not qualifying field name in Model Builder to verify the output creates field names you can live with.

Please indulge me and successfully build and output a model where the grid shape file is joined to just one bird range shape file with hard coded values.  And output that join to a new shape file in the model, finding a tool that does the output successfully from running Model Builder.  After that one join and output works, then export that model to a Python script.  That should be your starting point before attempting to build a Python loop.

I never build a loop until the core of the loop works all the way through for a single input.  Prevents a lot of frustrations and confusion.
0 Kudos
RichardFairhurst
MVP Honored Contributor
Also,, please let me know.  Will you have one shape file at the end where your grids have 50 new fields covering every bird range in those 50 shape files?  Or will you have 50 shape files at the end with each having the grids joined to each one of the bird range file inputs separately?  It makes a big difference in deciding what goes inside the loop and what is outside the loop.  It also makes a big difference in how you iterate your variables and how you deal with variables that generate output names.  If also make a difference on whether the Remove Join tool has to be part of the script or not.

Additionally, is the script only going to be run once to create a new feature class or feature classes that you will maintain without rebuilding it from the grids and ranges ever again?  Or does this script have to be run on a schedule to update from the source grid and bird range files on a regular basis?  That also makes a difference.
0 Kudos