Select to view content in your preferred language

Join a Feature Class to a Table with Python

5062
9
06-03-2010 09:56 AM
SteeveDeschenes
Deactivated User
Hi there!!

I am trying to write a script that will "find" the files.  The files are in an ACCESS database and I have several database in several folders.  I am new in scripting and I spend a lot of time trying to figure it out without any success. I feel it should be simple...

This is what I have written so far:

##########################################################################
#Join a table to a feature layer
#Steeve Deschenes
#Carex Canada
#June 2010
#########################################################################

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

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

# Load required toolboxes...
gp.AddToolbox("C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Data Management Tools.tbx")


# Local variables...
workingfolder = sys.argv[1]
source = sys.argv[2]
concentration = sys.argv[3]


#Set workspace
gp.Workspace = workingfolder

#Get a list of the feature classes in the input folder
try:
    fcs = gp.ListTables()
    fcs.Reset()
    fc = fcs.Next()
    tables = fc
    i = 1
    #print tables

    gp.MakeFeatureLayer_management (source, "tempcoordinate")
    #gp.CopyFeatures_management ("tempcoordinate", "/coordinate.shp")
    gp.AddJoin_management(concentration, "NUMR_ECHN", "tempcoordinate", "NUMR_ECHN_LOCL", "KEEP_ALL")
    gp.CopyFeatures_management (concentration, fc + "/resultat.shp")
   
except:
    gp.AddError (gp.GetMessages ())
    print gp.GetMessages

With that script, I have few problems:


1-I want to batch the process. It will be very convenient not to select every filesmuself,  but "tell python" where the files are located and let him selecting them.

2-When I run the script I get this message:

Failed to execute. Parameters are not valid.
The value cannot be a table
ERROR 000840: The value is not a Raster Layer.
Failed to execute (AddJoin).

I don't see where I the raster is involved in my process.  The parameter for my arguments are Folder, Feature Class and Table. No raster around???

I am a little bit desperate!!

Thanks for your help

Steeve
0 Kudos
9 Replies
KimOllivier
Honored Contributor


1-I want to batch the process. It will be very convenient not to select every filesmuself,  but "tell python" where the files are located and let him selecting them.

2-When I run the script I get this message:

Failed to execute. Parameters are not valid.
The value cannot be a table
ERROR 000840: The value is not a Raster Layer.
Failed to execute (AddJoin).

I don't see where I the raster is involved in my process.  The parameter for my arguments are Folder, Feature Class and Table. No raster around???

I am a little bit desperate!!

Thanks for your help

Steeve



AddJoin only takes Layers and Views not featureclasses and tables, odd message but that is likely the reason.
Here is my edited version as I think you want to do. Might have to edit it a bit more
################################################## ########################
#Join a table to a feature layer
#Steeve Deschenes
#Carex Canada
#June 2010
################################################## #######################
# Import system modules
import sys, os, arcgisscripting
# Create the Geoprocessor object
gp = arcgisscripting.create()
# Load required toolboxes...
## gp.AddToolbox("C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Data Management Tools.tbx")
## no need for system toolboxes
# Local variables...
workingfolder = sys.argv[1]
source = sys.argv[2]
concentration = sys.argv[3]
#Set workspace
gp.Workspace = workingfolder
#Get a list of the feature classes in the input folder
try:
    enumFC = gp.ListFeatureClasses()
    ## fcs.Reset() never actually implemented
    fc = enumFC.Next()
    i = 0
    while fc :
        i+=1
        print fc
        gp.MakeFeatureLayer_management (source, "source_lay")
        gp.MakeTableView_management ("NUMR_ECHN", "con_lay")
        # have to have Layers or Views for AddJoin not featureclasses or tables
        gp.AddJoin_management("con_lay", "NUMR_ECHN", "out_lay", "NUMR_ECHN_LOCL", "KEEP_ALL")
        gp.CopyFeatures_management ("out_lay", fc + "_resultat.shp")
        fc = enumFC.Next()
except Exception, errmsg:
    gp.AddError(gp.GetMessages())
    print errmsg,gp.GetMessages
0 Kudos
SteeveDeschenes
Deactivated User
Thanks for your help Kim,

I still have some problems with the script.  It seems to run smoothly, but I have no output at the end of the process.  Is there a problem with the loop??
When I run the main command lines I get:

ERROR 000840: The value is not a Feature Layer.
ERROR 000840: The value is not a Raster Catalog Layer.

My data are in geodatabases.  I have several dataset located in several folders.  I define my arguments as:
workingfolder = folder
source = feature class
concentration = table

################################################## ########################
#Join a table to a feature layer
#Steeve Deschenes
#Carex Canada
#June 2010
################################################## #######################
# Import system modules
import sys, os, arcgisscripting

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

# Local variables...
workingfolder = sys.argv[1]
source = sys.argv[2]
concentration = sys.argv[3]

#Set workspace
gp.Workspace = workingfolder

#Get a list of the feature classes in the input folder
try:
    enumFC = gp.ListFeatureClasses()
    ## fcs.Reset() never actually implemented
    fc = enumFC.Next()
    i = 1
    while fc :
        i += 1
        print fc
        gp.MakeFeatureLayer_management (source, "source_lay")
        gp.MakeTableView_management (concentration, "con_lay")
        gp.AddJoin_management("con_lay", "NUMR_ECHN", "source_lay", "NUMR_ECHN_LOCL", "KEEP_ALL")
        gp.CopyFeatures_management ("con_lay", fc + "_resultat.shp")
        fc = enumFC.Next()

except Exception, errmsg:
    gp.AddError(gp.GetMessages())
    print errmsg,gp.GetMessages



Thank you again

Steeve
0 Kudos
TedCronin
MVP Honored Contributor
What version of ArcGIS are you using for this script?
0 Kudos
KimOllivier
Honored Contributor
I don't have enough information about your sources, paths and error messages to see what is going wrong.
0 Kudos
SteeveDeschenes
Deactivated User
Hi,

Sorry, I should be more explicit.  I am using 9.3.1.  In my geodatabase, I have a table (concentration) that I want to join a feature class (source) to. The "NUMR_ECHN" is the link for the table and NUMR_ECHN_LOCL" is  the link for the feature class.  I have about a hundred geodatabase located in about 20 different folders. the name of the geodatabase are 31M01_GR.mdb, 31M01_GS.mdb, 31M0-2_GR.mdb, 31M02_GS.mdb..etc.

When I run the script with the loop, I don't receive any error message but, I don't have any output either. When I run the command lines, I have this error message:

Executing: Append D:\CAREX_3\Test\Abitibi_Pontiac_A\31m\Géochimie D:\CAREX_3\Test\Abitibi_Pontiac_A\31m\Géochimie\31M01_GR.mdb\ECHANTILLON_LOCALISE_PT D:\CAREX_3\Test\Abitibi_Pontiac_A\31m\Géochimie\31M01_GR.mdb\RESULTAT_ANALYSE
Start Time: Tue Jun 08 11:45:31 2010
Running script Append...
<class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid.
ERROR 000840: The value is not a Feature Layer.
ERROR 000840: The value is not a Raster Catalog Layer.

Thank you again for your help. I am a python beginner

Steeve
Failed to execute (CopyFeatures).
0 Kudos
TedCronin
MVP Honored Contributor
How come you are not using the 9.3 Geoprocessor, List Feature Classes could be handled as a true list and would be alot easier to both write and read.

gp = arcgisscripting.create(9.3)
0 Kudos
KimOllivier
Honored Contributor
Hi,

Sorry, I should be more explicit.  I am using 9.3.1.  In my geodatabase, I have a table (concentration) that I want to join a feature class (source) to. The "NUMR_ECHN" is the link for the table and NUMR_ECHN_LOCL" is  the link for the feature class.  I have about a hundred geodatabase located in about 20 different folders. the name of the geodatabase are 31M01_GR.mdb, 31M01_GS.mdb, 31M0-2_GR.mdb, 31M02_GS.mdb..etc.

When I run the script with the loop, I don't receive any error message but, I don't have any output either. When I run the command lines, I have this error message:

Executing: Append D:\CAREX_3\Test\Abitibi_Pontiac_A\31m\Géochimie D:\CAREX_3\Test\Abitibi_Pontiac_A\31m\Géochimie\31M01_GR.mdb\ECHANTILLON_LOCALISE_PT D:\CAREX_3\Test\Abitibi_Pontiac_A\31m\Géochimie\31M01_GR.mdb\RESULTAT_ANALYSE
Start Time: Tue Jun 08 11:45:31 2010
Running script Append...
<class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid.
ERROR 000840: The value is not a Feature Layer.
ERROR 000840: The value is not a Raster Catalog Layer.
Failed to execute (CopyFeatures).


The key is just get CopyFeatures to work standalone. Even in the interactive window in pythonwin. I think the problem is your files beginning with a numeric digit.

As a conservative strategy never name folders, files or featureclasses starting with a digit. You are not allowed to start field names with a digit in most databases. Also, to make life easy avoid spaces in folders and file names as well. This avoids having to quote everything and use r"path to a strange world" for everything. Although windows and Unix allows spaces, it causes endless problems in ArcGIS and other packages if you have names, variables and anything that is not meant to be a number starting with a number.
0 Kudos
KimOllivier
Honored Contributor
Hi,

Sorry, I should be more explicit.  I am using 9.3.1.  In my geodatabase, I have a table (concentration) that I want to join a feature class (source) to. The "NUMR_ECHN" is the link for the table and NUMR_ECHN_LOCL" is  the link for the feature class.  I have about a hundred geodatabase located in about 20 different folders. the name of the geodatabase are 31M01_GR.mdb, 31M01_GS.mdb, 31M0-2_GR.mdb, 31M02_GS.mdb..etc.

When I run the script with the loop, I don't receive any error message but, I don't have any output either. When I run the command lines, I have this error message:

Executing: Append D:\CAREX_3\Test\Abitibi_Pontiac_A\31m\Géochimie D:\CAREX_3\Test\Abitibi_Pontiac_A\31m\Géochimie\31M01_GR.mdb\ECHANTILLON_LOCALISE_PT D:\CAREX_3\Test\Abitibi_Pontiac_A\31m\Géochimie\31M01_GR.mdb\RESULTAT_ANALYSE
Start Time: Tue Jun 08 11:45:31 2010
Running script Append...
<class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid.
ERROR 000840: The value is not a Feature Layer.
ERROR 000840: The value is not a Raster Catalog Layer.
Failed to execute (CopyFeatures).


The key is just get CopyFeatures to work standalone. Even in the interactive window in pythonwin. I think the problem is your files beginning with a numeric digit.

As a conservative strategy never name folders, files or featureclasses starting with a digit. You are not allowed to start field names with a digit in most databases. Also, to make life easy avoid spaces in folders and file names as well. This avoids having to quote everything and use r"path to a strange world" for everything. Although windows and Unix allows spaces, it causes endless problems in ArcGIS and other packages if you have names, variables and anything that is not meant to be a number starting with a number or including spaces.
0 Kudos
AdrianMarsden
Honored Contributor
edit - please ignore - sorted my issue
0 Kudos