Remove join

4623
10
08-20-2012 03:24 AM
Bart-JanSchoenmakers
New Contributor III
Hi all,

I'm having a problem removing a join with python code (ArcGIS 10.0).

With a python script I'm doing a spatial SORT for a number of polygons which fall within a certain number of regions. Within a function I do the following:

  • Create a temporary featureclass as result from the Spatial sort

  • Put the value of the ObjectID in a new field

  • Add a join between the input layer and the temporary FC

  • Calculate the value of the sorted objected for the input FC

  • Remove the join


This is the code of the python function.
def spatialsort (inlayer, outfc, nutsvalue):
    # (temp FC, spatial sort, add field, calc OID, atribuir valor join, ...)
    # temp fc Nome
    tempfcnome = tempfd + "\\SORT" + nutsvalue
    tempfcnome2 = "SORT" + nutsvalue
    if gp.Exists(tempfcnome):
        gp.Delete_management(tempfcnome, "")

    gp.Sort_management(inlayer, tempfcnome, "SHAPE ASCENDING", "PEANO")

    # Adicionar campo SORT
    gp.AddField_management(tempfcnome, "SORT_TEMP_ID", "LONG", 6)

    # Calcular campo
    # nao seria necessario pode fazer o calculate a partir OBJECTID original
    gp.CalculateField_management(tempfcnome, "SORT_TEMP_ID", "!OBJECTID!","PYTHON")

    # Adicionar Join
    joinField = "GRD_NEWID"
    fieldList = ["SORT_TEMP_ID"] # OBJECTID

    # criar table view (alternative to using featurelayer):
    theDesc = gp.Describe(inlayer)
    gp.MakeTableView_management (theDesc.catalogPath, "temptableview")
    theDesc = gp.Describe(tempfcnome)
    gp.MakeTableView_management (theDesc.catalogPath, "temptableview2")
    

    # Using tableviews
    gp.JoinField_management ("temptableview", joinField, "temptableview2", joinField, fieldList) # tempfcnome
  



Everything works fine except the last step. I tried to remove the join using as input:

Using the fullname of the layer:
[INDENT]arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid. The value cannot be a feature class[/INDENT]Using only the basename:
[INDENT]ERROR 000732: Layer Name or Table View: Dataset TEST2_GRID_1KM_CONT does not exist or is not supported[/INDENT]Using the name of layer (this case gridlayer):
[INDENT]arcgisscripting.ExecuteError: ERROR 000229: Cannot open gridlayer[/INDENT]
Using tableviews as input, error:
[INDENT]    gp.RemoveJoin_management ("temptableview",  "temptableview2")
arcgisscripting.ExecuteError: ERROR 000229: Cannot open temptableview[/INDENT]


I checked the following topic http://forums.arcgis.com/threads/13163-arcgis-10-is-there-a-way-to-remove-all-joins-with-python, but this didn�??t offer any solution.

Does anyone knows what I�??m doing wrong? Any suggestions for alternative ways?

Thank you,
Bart Schoenmakers
Tags (2)
10 Replies
curtvprice
MVP Esteemed Contributor
I don't see the Add Join or Remove Join tools in your code, did you leave that out?

Maybe this is your issue:

Join Field it does not create a join (so you can't remove one after it runs). The tool that does a tempary join (which is what you are describing in your question) is Add Join.

Join Field permanently copies data -- join/add field,calculate across/remove join in one step.
Bart-JanSchoenmakers
New Contributor III
Thanks Curtis for checking this thread. It seems I didn�??t copy well my code.

Here are the 2 ways I tried to make a join and remove a join, using table views as input and using the layer objects.
I hope you see what kind of coding error I made.

Thanks,
Bart

Without table views:

   # Without tableviews:
    gp.JoinField_management (inlayer, joinField, tempfcnome, joinField, fieldList) # tempfcnome
   
    # Calcular output campos laygrid
    gp.CalculateField_management(inlayer, nuts3field, nutsvalue,"PYTHON") # inlayer
    gp.CalculateField_management(inlayer, "SORT_ID", "!SORT_TEMP_ID!","PYTHON")

    gp.RemoveJoin_management (inlayer,  tempfcnome)


With Table views:

   # Using tableviews
    gp.JoinField_management ("temptableview", joinField, "temptableview2", joinField, fieldList) # tempfcnome
   
    # Calcular output campos laygrid
    gp.CalculateField_management("temptableview", nuts3field, nutsvalue,"PYTHON") # inlayer
    gp.CalculateField_management("temptableview", "SORT_ID", "!SORT_TEMP_ID!","PYTHON")

    theDesc = gp.Describe("temptableview")
    theDesc2 = gp.Describe("temptableview2")
    gp.RemoveJoin_management (theDesc.catalogPath,  theDesc2.catalogPath)

    # remove tableviews
    
    gp.Delete_management("temptableview")
    
    gp.Delete_management("temptableview2")
0 Kudos
KenCarrier
Occasional Contributor III
I think that you are not passing the name of the join correctly.

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

RemoveJoin_management(in_layer_or_view, {join_name})

Here the the join_name appears to be optional, have you tried without specifying the join_name like this;

gp.RemoveJoin_management ("temptableview")

if that does not work try this;

gp.RemoveJoin_management ("temptableview", "temptableview2")

or instead of catalogPath try using nameString with the Describe.
0 Kudos
Bart-JanSchoenmakers
New Contributor III
I tried already several names, like hardcoding the fullname or using the result of Describe (including the path or only the filename). Non of it worked. The example on the helppage looks quite simple. It seems you only need to specify the featureclassnames.

Like you sugested I left out the 2nd argument. It gives a Parameters not valid error. It seems all quite strange this RemoveJoin command.

To get my script working I'm now leaving out a join at the end and doing it afterwards manually.

Bart
0 Kudos
KenCarrier
Occasional Contributor III
Curious are you running this inside arcmap or as a standalone script.

The error you are getting seems like the object being passed is incorrect.

In your function I noticed you have an inlayer variable you accept, have you tried passing inlayer to the RemoveJoin tool?
0 Kudos
Bart-JanSchoenmakers
New Contributor III
I'm using the script as standalone script. I might try running it from ArcCatalog, it could be that ArcGIS interprets the objects differently. Best is to create a new script testing the problem, leaving out all the other code.

I tried passing several variables to the removejoin function, also hardcoding the tablenames used for the join. always the same result.
0 Kudos
by Anonymous User
Not applicable
Does it work with Add Join rather than using the join field? I believe the Join Field function creates a permanent join, but it seems like you only need a temporary join so the add join may be the better way to go. I don't know that it will make a difference but could be worth a shot.

gp.AddJoin_management ("temptableview", joinField, "temptableview2", joinField, fieldList)
0 Kudos
curtvprice
MVP Esteemed Contributor
   
    gp.JoinField_management (inlayer, joinField, tempfcnome, joinField, fieldList) # tempfcnome
    -- snip --    
    gp.RemoveJoin_management (inlayer,  tempfcnome) # will not work!


[post=226407]Caleb[/post] is correct.

  • Add Join creates a temporary join that can be removed with Remove Join.

  • Join Field does a permanent join, i.e. it copies over fields permanently from one table to another based an a join field match. You can't Remove Join to undo an Join Field operation -- it's permanent.

  • A nice feature of Join Field is you can specify a subset of fields to copy over, say, just the one you're interested in.
BrendanBladdick3
Esri Contributor

Solid information