AddJoin failed

2498
22
Jump to solution
06-30-2013 02:44 AM
ElaineKuo
Occasional Contributor
System: Vista, ArcGIS 9.3

Hello,

I wrote a python code to AddJoin a grid shapefile (File A) to a bird range shapefile (File B).
They have a field in common (GID).

However, the error message is
ERROR 000732: Input Features: Dataset H:/temp_D/testt does not exist or is not supported
Failed to execute (MakeFeatureLayer).

Please kindly advise how to modify the code.
Thank you.
(The file location is
"H:/temp_D/testt/grid.shp"
"H:/temp_D/test/birdrange.shp")

#Import standard library modules import arcgisscripting import os  #Create the Geoprocessor object gp = arcgisscripting.create(9.3)  #Get a list of the featureclasses in the input folder source_fc = "H:/temp_D/testt" Entity = "H:/temp_D/test" outputfolder = "H:/temp_D"  gp.Toolbox = "Data Management"      # convert a featureclass to a layer  gp.MakeFeatureLayer_management (source_fc, "source_lyr")  # have Layers for AddJoin gp.AddJoin_management("source_lyr", "GID", Entity, "GID", "KEEP_ALL")  # convert a layer to a featureclass  gp.CopyFeatures_management ("source_lyr", source_fc) gp.FeatureClassToShapefile_conversion(source_fc, outputfolder)  # clear memory of layers gp.Delete("source_lyr")      gp.AddMessage(gp.GetMessages()) print gp.GetMessages()
Tags (2)
0 Kudos
22 Replies
RichardFairhurst
MVP Honored Contributor
Thank you for the help.
The AddJoin code below worked well.
As you said, I want to add a loop to the AddJoin of a grid and multiple bird range.
Please kindly advise how to code it.
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)
gp.QualifiedFieldNames = "UNQUALIFIED"

#Get a list of the featureclasses in the input folder
source_fc = r"H:/temp_D/testt/A_grid.shp"
Entity = r"H:/temp_D/test/geoc0283.shp"
output_fc = r"H:/temp_D/test/grid_rng.shp"

gp.Toolbox = "Data Management"
    
# convert a featureclass to a layer 
gp.MakeFeatureLayer_management (source_fc, "source_lyr")

# have Layers for AddJoin
gp.AddJoin_management("source_lyr", "GID", Entity, "GID", "KEEP_ALL")

# convert a layer to a featureclass 
gp.CopyFeatures_management ("source_lyr", output_fc)

# delete Field
gp.deletefield (output_fc, "FID_1; GID_1")

# Remove the join
gp.RemoveJoin("source_lyr", "geoc0283")
    
# clear memory of layers
gp.Delete("source_lyr")
    
gp.AddMessage(gp.GetMessages())
print gp.GetMessages()


Great.  So this is the core of any loop you would build if you needed to process multiple bird range shape files against the grid.

In another post you mentioned you have one grid shape file and 50 bird range shape files.  Do you need one final shape file that has 50 fields for all of those bird ranges or do you want 50 separate shape files that match each one of the bird ranges to the grid separately?
0 Kudos
RichardFairhurst
MVP Honored Contributor
Just for the record, a single forward slash ("/") works fine, it's the backslash ("\") that's illegal.

The following techniques will all work in a path:

1.) forward slashes = /
2.) double backslashes = \\
3.) raw string with backslashes = r"H:\InfoRequest"


Thanks for the clarification.  Python is not my language of choice, and the escape character and path string handling is something that trips me up and many other posts in this forum.  So I figured it could be related to his problem (even though my answer made the problem worse on that issue).  One of the reasons I use Model Builder instead of typing in Python directly when I get errors about paths.
0 Kudos
RDHarles
Occasional Contributor
Thanks for the clarification.  Python is not my language of choice, and the escape character and path string handling is something that trips me up and many other posts in this forum.  So I figured it could be related to his problem (even though my answer made the problem worse on that issue).  One of the reasons I use Model Builder instead of typing in Python directly when I get errors about paths.


I agree with you, getting the path (and slashes) correct seems to be a common problem out there.  In fact a great number of people I come across incorrectly think that this is a back slash ("/") and this is a forward slash ("\"), which in fact is the opposite.
0 Kudos
ElaineKuo
Occasional Contributor
Great. 
So this is the core of any loop you would build if you needed to process multiple bird range shape files against the grid.


Thanks for the encouragement.
I want "one final shape file that has 50 fields for all of those bird ranges."
Actually, I tried the below code for a grid to a bird range only.
It failed in RemoveJoin, saying "ERROR 000800: The value is not a member of geoc0283.
Failed to execute (RemoveJoin)." (geoc0283 is the name of one bird range).

################################################## ########################
##AddJoin feature layers to a feature layer
##Elaine Kuo
##01 July 2013
################################################## #######################

#Import standard library modules
import arcgisscripting
import os

#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
gp.QualifiedFieldNames = "UNQUALIFIED"

#Set the input workspace
workingfolder= r"H:/temp_D/test"
gp.Workspace = workingfolder

#Set the output workspace
outputfolder= r"H:/temp_D"
outWorkspace= outputfolder

source_fc = r"H:/temp_D/testt/A_grid.shp"
Entity = r"H:/temp_D/test/geoc0283.shp"

#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:

    gp.Toolbox = "Data Management"

    # Make a FeatureClass layer    
    gp.MakeFeatureLayer_management (source_fc, "source_lyr")

    # have Layers for AddJoin
    gp.AddJoin_management("source_lyr", "GID", fc, "GID", "KEEP_ALL")

    # convert a layer to a featureclass 
    out_name = gp.Describe("source_lyr").name
    out_name = gp.ValidateTableName(out_name, outputfolder)
    out_shape = outputfolder + "/" + out_name + ".shp"
    gp.CopyFeatures_management ("source_lyr", out_shape)
    
    # delete Field
    gp.deletefield (out_shape, "FID_1; GID_1")

    # Remove the join
    gp.RemoveJoin("source_lyr", fc)
    
    # clear memory of layers
    gp.Delete("source_lyr")
    
gp.AddMessage(gp.GetMessages())
print gp.GetMessages()
0 Kudos
RichardFairhurst
MVP Honored Contributor
While the core of your loop was contained in the code you wrote, only the join belongs within loop.  Now that I know what you really want at the end try this (not sure it will work, since I never tried 50 joins in a loop before).

################################################## ########################
##AddJoin feature layers to a feature layer
##Elaine Kuo
##01 July 2013
################################################## #######################

#Import standard library modules
import arcgisscripting
import os

#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
gp.QualifiedFieldNames = "UNQUALIFIED"

#Set the input workspace
workingfolder= "H:/temp_D/test"
gp.Workspace = workingfolder

#Set the output workspace
outputfolder= "H:/temp_D"
outWorkspace= outputfolder

source_fc = "H:/temp_D/testt/A_grid.shp"
Entity = "H:/temp_D/test/geoc0283.shp"

#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()

gp.Toolbox = "Data Management"

# Make a FeatureClass layer    
gp.MakeFeatureLayer_management (source_fc, "source_lyr")

# Loop through every item in the list that was just generated
for fc in fcs:

    # have Layers for AddJoin
    gp.AddJoin_management("source_lyr", "GID", fc, "GID", "KEEP_ALL")

# convert a layer to a featureclass 
out_name = gp.Describe("source_lyr").name
out_name = gp.ValidateTableName(out_name, outputfolder)
out_shape = outputfolder + "/" + out_name + ".shp"

gp.CopyFeatures_management ("source_lyr", out_shape)
    
# delete Field  (need to clean up with a lot more ID fields that the code you have below since now there are 50 GID values.)
gp.deletefield (out_shape, "FID_1; GID_1")

# Don't bother removing 50 joins, just kill the layer
# clear memory of layers
gp.Delete("source_lyr")
    
gp.AddMessage(gp.GetMessages())
print gp.GetMessages()


The error you reported is due to the fact that you were not doing a single fc, but a loop of all the fcs.  I also would just assign a name for the final output rather than deriving it from the layer, since that step only runs once now to create a single shape file at the end.

If this code does not throw an error and creates an output, do not be surprised if the output only contains one join or no join.  That is the part I am not sure about when you do multiple joins on top of each other and a possibility related to the error you reported in your last post.
0 Kudos
RichardFairhurst
MVP Honored Contributor
I agree with you, getting the path (and slashes) correct seems to be a common problem out there.  In fact a great number of people I come across incorrectly think that this is a back slash ("/") and this is a forward slash ("\"), which in fact is the opposite.


There is actually a fourth case that works (although it is unnecessary since the other 3 path syntax formats you gave work more elegantly)

4.) raw string with slashes = r"H:/InfoRequest"

In essence it is the same as just using slashes, but nonetheless it will work and not throw an error if it is used.  So it technically is a 4th working case, even if it is not recommended.
0 Kudos
RichardFairhurst
MVP Honored Contributor
I just tried creating a Model Builder model where I did three successive joins.  For the second and third join I had to use the qualified field name of the original layer feature class.  So the loop probably needs to be modified to keep track of the first time a join is done and all other times the join is done.

################################################## ########################
##AddJoin feature layers to a feature layer
##Elaine Kuo
##01 July 2013
################################################## #######################

#Import standard library modules
import arcgisscripting
import os

#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
gp.QualifiedFieldNames = "UNQUALIFIED"

#Set the input workspace
workingfolder= "H:/temp_D/test"
gp.Workspace = workingfolder

#Set the output workspace
outputfolder= "H:/temp_D"
outWorkspace= outputfolder

source_fc = "H:/temp_D/testt/A_grid.shp"
Entity = "H:/temp_D/test/geoc0283.shp"

#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()

gp.Toolbox = "Data Management"

# Make a FeatureClass layer    
gp.MakeFeatureLayer_management (source_fc, "source_lyr")

$ A counter to separate the first time through the loop from all other times
counter = 0
# Loop through every item in the list that was just generated
for fc in fcs:
    if counter == 0:
        # set up first Layer for AddJoin
        gp.AddJoin_management("source_lyr", "GID", fc, "GID", "KEEP_ALL")
    else:
        # set up second and following Layers for AddJoin
        gp.AddJoin_management("source_lyr", "A_grid.GID", fc, "GID", "KEEP_ALL")
    counter += 1

# convert a layer to a featureclass 
out_name = gp.Describe("source_lyr").name
out_name = gp.ValidateTableName(out_name, outputfolder)
out_shape = outputfolder + "/" + out_name + ".shp"

gp.CopyFeatures_management ("source_lyr", out_shape)
    
# delete Field  (need to clean up with a lot more ID fields that the code you have below since now there are 50 GID values.)
gp.deletefield (out_shape, "FID_1; GID_1")

# Don't bother removing 50 joins, just kill the layer
# clear memory of layers
gp.Delete("source_lyr")
    
gp.AddMessage(gp.GetMessages())
print gp.GetMessages()
0 Kudos
ElaineKuo
Occasional Contributor

################################################## ########################
##AddJoin feature layers to a feature layer
##Elaine Kuo
##01 July 2013
################################################## #######################

#Import standard library modules
import arcgisscripting
import os

#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
gp.QualifiedFieldNames = "UNQUALIFIED"

#Set the input workspace
workingfolder= "H:/temp_D/test"
gp.Workspace = workingfolder

#Set the output workspace
outputfolder= "H:/temp_D"
outWorkspace= outputfolder

source_fc = "H:/temp_D/testt/A_grid.shp"
Entity = "H:/temp_D/test/geoc0283.shp"

#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()

gp.Toolbox = "Data Management"

# Make a FeatureClass layer    
gp.MakeFeatureLayer_management (source_fc, "source_lyr")

# Loop through every item in the list that was just generated
for fc in fcs:

    # have Layers for AddJoin
    gp.AddJoin_management("source_lyr", "GID", fc, "GID", "KEEP_ALL")

# convert a layer to a featureclass 
out_name = gp.Describe("source_lyr").name
out_name = gp.ValidateTableName(out_name, outputfolder)
out_shape = outputfolder + "/" + out_name + ".shp"

gp.CopyFeatures_management ("source_lyr", out_shape)
    
# delete Field  (need to clean up with a lot more ID fields that the code you have below since now there are 50 GID values.)
gp.deletefield (out_shape, "FID_1; GID_1")

# Don't bother removing 50 joins, just kill the layer
# clear memory of layers
gp.Delete("source_lyr")
    
gp.AddMessage(gp.GetMessages())
print gp.GetMessages()




Thanks. Here are the test report:
1. For grid and 1 bird range shape file => the code worked well.
2. For grid and 2 bird range shape files=> the error message: gp.AddJoin_management("source_lyr", "GID", fc, "GID", "KEEP_ALL")
ExecuteError: ERROR 999999: Error executing function.
A locator with this name does not exist.
Failed to execute (AddJoin).

Please kindly help.
0 Kudos
ElaineKuo
Occasional Contributor

################################################## ########################
##AddJoin feature layers to a feature layer
##Elaine Kuo
##01 July 2013
################################################## #######################

#Import standard library modules
import arcgisscripting
import os

#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
gp.QualifiedFieldNames = "UNQUALIFIED"

#Set the input workspace
workingfolder= "H:/temp_D/test"
gp.Workspace = workingfolder

#Set the output workspace
outputfolder= "H:/temp_D"
outWorkspace= outputfolder

source_fc = "H:/temp_D/testt/A_grid.shp"
Entity = "H:/temp_D/test/geoc0283.shp"

#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()

gp.Toolbox = "Data Management"

# Make a FeatureClass layer    
gp.MakeFeatureLayer_management (source_fc, "source_lyr")

$ A counter to separate the first time through the loop from all other times
counter = 0
# Loop through every item in the list that was just generated
for fc in fcs:
    if counter == 0:
        # set up first Layer for AddJoin
        gp.AddJoin_management("source_lyr", "GID", fc, "GID", "KEEP_ALL")
    else:
        # set up second and following Layers for AddJoin
        gp.AddJoin_management("source_lyr", "A_grid.GID", fc, "GID", "KEEP_ALL")
    counter += 1

# convert a layer to a featureclass 
out_name = gp.Describe("source_lyr").name
out_name = gp.ValidateTableName(out_name, outputfolder)
out_shape = outputfolder + "/" + out_name + ".shp"

gp.CopyFeatures_management ("source_lyr", out_shape)
    
# delete Field  (need to clean up with a lot more ID fields that the code you have below since now there are 50 GID values.)
gp.deletefield (out_shape, "FID_1; GID_1")

# Don't bother removing 50 joins, just kill the layer
# clear memory of layers
gp.Delete("source_lyr")
    
gp.AddMessage(gp.GetMessages())
print gp.GetMessages()


Thanks a lot.
The code worked wonderfully for three shapefiles.
As for delete fields, I am thinking if wildcard will help in this condition.
(The field names of the three AddJoin bird range are  "FID_1; GID_1", "FID_12; GID_12", and "FID_12_13; GID_12_13")

Therefore, I replaced the gp.delete with the following code.
# delete Field  (need to clean up with a lot more ID fields that the code you have below since now there are 50 GID values.)
gp.deletefield (out_shape, "FID_*; GID_*")


The code worked well, but none of the three FID and GID fields were deleted.
Please kindly help and thank you.
(It is the first time my problem can be solved so quickly. Thanks greatly.)
0 Kudos
RichardFairhurst
MVP Honored Contributor
Thanks a lot.
The code worked wonderfully for three shapefiles.
As for delete fields, I am thinking if wildcard will help in this condition.
(The field names of the three AddJoin bird range are  "FID_1; GID_1", "FID_12; GID_12", and "FID_12_13; GID_12_13")

Therefore, I replaced the gp.delete with the following code.
# delete Field  (need to clean up with a lot more ID fields that the code you have below since now there are 50 GID values.)
gp.deletefield (out_shape, "FID_*; GID_*")


The code worked well, but none of the three FID and GID fields were deleted.
Please kindly help and thank you.
(It is the first time my problem can be solved so quickly. Thanks greatly.)


Try this instead:

fieldList = gp.ListFields(out_shape)
for field in fieldList:
    if field.name[1:3] == "ID_":
        gp.deletefield (out_shape, field.name)