You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - Geoprocessing Scripting (Python, JavaScript, VB) forum

Unable to do Spatial join in Python   Charlton Lewis Feb 22, 2005
Re: Unable to do Spatial join in Python   Ghislain Prince Feb 22, 2005
Re: Unable to do Spatial join in Python   Charlton Lewis Feb 23, 2005
Re: Unable to do Spatial join in Python   Charlton Lewis Feb 28, 2005
Re: Unable to do Spatial join in Python   larry camp Mar 30, 2005
Re: Unable to do Spatial join in Python   Charlton Lewis Mar 30, 2005
Re: Unable to do Spatial join in Python   larry camp Mar 30, 2005
Re: Unable to do Spatial join in Python   Charlton Lewis Mar 31, 2005
Re: Unable to do Spatial join in Python   larry camp Mar 31, 2005
Re: Unable to do Spatial join in Python   larry camp Mar 31, 2005
Re: Unable to do Spatial join in Python   Dan Patterson Mar 31, 2005
Re: Unable to do Spatial join in Python   larry camp Mar 31, 2005
Re: Unable to do Spatial join in Python   Charlton Lewis Mar 31, 2005
Re: Unable to do Spatial join in Python   larry camp Mar 31, 2005
Re: Unable to do Spatial join in Python   larry camp Mar 31, 2005
Re: Unable to do Spatial join in Python   Charlton Lewis Apr 01, 2005
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Unable to do Spatial join in Python 
Author Charlton Lewis 
Date Feb 22, 2005 
Message I'm having a problem with a join I thought would be straightforward. A small portion of the code and the resulting errors are included. The join field exists in both shapefiles. Line 74 is the 'Addjoin_management' line.

Any help would be appreciated.

Error:
>>> Unhandled exception while debugging...
Traceback (most recent call last):
File "C:\Python21\win32com\client\dynamic.py", line 402, in __getattr__
return new.instancemethod(self._builtMethods_[attr], self, self.__class__)
KeyError: AddJoin_management
>>> Unhandled exception while debugging...
Traceback (most recent call last):
File "X:\ArcGIS\Python Scripts\MB_Join3.py", line 74, in ?
gp.AddJoin_management(ptxVar, "Folio", lpropVar, "Folio", "KEEP_ALL")
File "", line 2, in AddJoin_management
com_error: (-2147467259, 'Unspecified error', None, None)
>>> Failed to execute. Parameters are not valid.
Layer Name or Table View: Value is not valid for data type
out_layer_or_view parameter not GPTableView, GPLayer or DELayer
Failed to execute (AddJoin_5). 
 
try:
    gp.AddJoin_management(shapefile1, "Field1", shapefile2, "Field1", "KEEP_ALL")
except:
    print gp.GetMessages(2)
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Unable to do Spatial join in Python 
Author Ghislain Prince 
Date Feb 22, 2005 
Message Hi Charlton,
you can't put a join directly of a shp file. You have to put it on a Layer. Change your code as follows. 
 
try:
    gp.MakeFeatureLayer(shapefile1,lyr1)
    gp.AddJoin_management(lyr1, "Field1", shapefile2, "Field1", "KEEP_ALL")
except:
    print gp.GetMessages(2)
 
  Ghislain Prince
ESRI 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Unable to do Spatial join in Python 
Author Charlton Lewis 
Date Feb 23, 2005 
Message Thanks Ghislain. It worked when using the layer. I could then use Copyfeatures to put in the records into a shapefile. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Unable to do Spatial join in Python 
Author Charlton Lewis 
Date Feb 28, 2005 
Message Hello again. I guess I was a little hasty in thinking all was done. I was able to do a regular attribute join after using MakeFeaturaLayer. When I tried to do another join using the "Shape" field (between a point and a polygon shapefile), all of the points had the same value from one of the polygons.

Any ideas what may have gone wrong here?

Thanks again. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Unable to do Spatial join in Python 
Author larry camp 
Date Mar 30, 2005 
Message When I run this:
try:
gp.MakeFeatureLayer(shapefile1,lyr1)
gp.AddJoin_management(lyr1, "Field1", shapefile2, "Field1", "KEEP_ALL")

I get a NameError: name 'lyr1' is not defined.

My code attempt below, thanks for any help. These are geodatabases rather than shapefiles. How do I define lyr1?

 
 
# Local variables...
LoudParcelSample = "I:\\Projects\\PAAGE\\testjunk\\PAAGEsample.mdb\LoudParcelSample"
PAAGE = "I:\\Projects\\PAAGE\\testjunk\\PAAGEprojects.mdb\\PAAGE"


# Process: Add Join...
gp.MakeFeatureLayer(LoudParcelSample,lyr1)
gp.AddJoin_management(lyr1, "MCPI", PAAGE, "MCPI", )
# Process: Select Layer By Attribute...
gp.SelectLayerByAttribute_management(LoudParcelSample, "NEW_SELECTION", "samplerecords.MCPI is not NULL")
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Unable to do Spatial join in Python 
Author Charlton Lewis 
Date Mar 30, 2005 
Message LYR1 is not really being used as a variable but as a virtual layer. Use quotes around it whenever you set or reference it and see if that helps. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Unable to do Spatial join in Python 
Author larry camp 
Date Mar 30, 2005 
Message Thanks Charlton,
I tried it w/ double-quotes in the MakeFeature and the AddJoin,, and received this error:

Traceback (most recent call last):
File "C:\Python21\Pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "I:\Projects\PAAGE\testjunk\Select_Copy.py", line 25, in ?
gp.MakeFeatureLayer("I:\Projects\PAAGE\testjunk\PAAGEtest.mdb\LoudParcelSample","lyr1")
File "", line 2, in MakeFeatureLayer
com_error: (-2147467259, 'Unspecified error', None, None) 
 
# Local variables...
ParcelSample = "I:\Projects\PAAGE\testjunk\PAAGEtest.mdb\LoudParcelSample"
PAAGE = "I:\Projects\PAAGE\testjunk\PAAGEprojects.mdb\PAAGE"

# Process: Add Join...
gp.MakeFeatureLayer("I:\Projects\PAAGE\testjunk\PAAGEtest.mdb\LoudParcelSample","lyr1")

gp.AddJoin_management("lyr1", "MCPI", PAAGE, "MCPI", )
# Process: Select Layer By Attribute...
gp.SelectLayerByAttribute_management("lyr1", "NEW_SELECTION", "samplerecords.MCPI is not NULL")

 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Unable to do Spatial join in Python 
Author Charlton Lewis 
Date Mar 31, 2005 
Message I'm not sure where your error is coming from. In your previous reply, you were using a variable in MakeFeaturelayer. The variable was created using the double back-slash "\\" in the directory path.

Maybe you can try using the variable again or make sure the path is being recognized.

Also, before doing the SelectLayerByAttribute, you may want to use gp.Copyfeatures to create a new feature class from "lyr1" and then use this new layer in your processing.

I hope this helps. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Unable to do Spatial join in Python 
Author larry camp 
Date Mar 31, 2005 
Message Now that I've completely hijacked this thread and turned it into an *attribute* join...
I really appreciate your help Charlton!

Yesterday's problem was a dataset/path problem, which I fixed this morning. Now, I'm getting an error which could be related to your suggestion about doing an additional CopyFeatures before the SelectLayerByAttribute command.

Can you explain more about this? I still want to get the selected/copied features into the existing GDB called PAAGE. I'm getting an error stating that PAAGE already exists.

Error:
Executing: CopyFeatures lyr1 I:\Projects\PAAGE\testjunk\PAAGEprojects.mdb\PAAGE # 0 0 0
Start Time: Thu Mar 31 09:16:28 2005
Failed to execute. Parameters are not valid.
Output Feature Class: Dataset I:\Projects\PAAGE\testjunk\PAAGEprojects.mdb\PAAGE already exist
Failed to execute (CopyFeatures)
 
 
# Local variables...
    ParcelSample = "I:\\Projects\\PAAGE\\testjunk\\PAAGEtest.mdb\\LoudParcelSample"
    PAAGE = "I:\\Projects\\PAAGE\\testjunk\\PAAGEprojects.mdb\\PAAGE"
    samplerecords = "I:\\Projects\\PAAGE\\testjunk\\PAAGEtest.mdb\\samplerecords"
    
    # Make a layer from the feature class 
    gp.MakeFeatureLayer(ParcelSample,"lyr1")
    # Process: Add Join...
    gp.AddJoin_management("lyr1", "MCPI", samplerecords, "MCPI", )
    # Process: Select Layer By Attribute...
    gp.SelectLayerByAttribute_management("lyr1", "NEW_SELECTION", "samplerecords.MCPI is not NULL")

    # Process: Copy Features...
    gp.CopyFeatures_management("lyr1", PAAGE)
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Unable to do Spatial join in Python 
Author larry camp 
Date Mar 31, 2005 
Message I should add,
This script works fine in the MXD with the datasets in the TOC.

But, I'd like to get it to run when ArcMap is closed (after hours). 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Unable to do Spatial join in Python 
Author Dan Patterson 
Date Mar 31, 2005 
Message try to replace the layer names with the full path to the data sets on disk 
  Geomatics, Carleton University, Ottawa, Canada 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Unable to do Spatial join in Python 
Author larry camp 
Date Mar 31, 2005 
Message Do you mean instead of using the variable names in all 4 of these command lines?

gp.MakeFeatureLayer(ParcelSample,"lyr1")
gp.AddJoin_management("lyr1", "MCPI", samplerecords, "MCPI", )
gp.SelectLayerByAttribute_management("lyr1", "NEW_SELECTION", "samplerecords.MCPI is not NULL")
gp.CopyFeatures_management("lyr1", PAAGE) 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Unable to do Spatial join in Python 
Author Charlton Lewis 
Date Mar 31, 2005 
Message I'm not sure why, but my account takes a while to refresh the replies. Even when I click on the link in the e-mail I receive it doesn't take me to the latest entry in the reply thread.

Anyway, Copyfeatures tries to write to a new feature class. You have to make sure the feature class does not exist. Maybe during your testing, PAAGE was created but not deleted. I like to put checks in my code to delete an object if it does exist. Something like:

if gp.Exists(PAAGE):
gp.Delete_management(PAAGE)

Whatever machine you'll be running this on, ArcGIS will have to be installed there. You will also have to make sure any folder and database connections you're using in the script are established. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Unable to do Spatial join in Python 
Author larry camp 
Date Mar 31, 2005 
Message Boy, am I ever frustrated with ModelBuilder and Scripting. Seems every approach I try gets about 90% there.

I haven't tried Dan's suggestion yet of putting in full paths, but wanted to get some more clarification from Charlton.

I thought that CopyFeatures should overwrite an existing layer/FC/GDB/shapefile etc? It does so when I run the script in the MXD.

If I do delete PAAGE as you suggest, how do I specify "where" PAAGE should get newly created each time? As it is, I'm using a variable called PAAGE with it's full path specified.

Would I specify it like this, even though the dataset doesn't exist while this command is running?

if gp.Exists(PAAGE):
gp.Delete_management(PAAGE)
gp.CopyFeatures_management("lyr1", "c:\temp\PAAGE.mdb\PAAGE") 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Unable to do Spatial join in Python 
Author larry camp 
Date Mar 31, 2005 
Message Thanks Charlton,
I got it to work like this (I wasn't able to figure out how to do the "if exists" statement).

# Process: Copy Features...
gp.Delete_management(PAAGE)
gp.CopyFeatures_management("lyr1", PAAGE)

Now, on to my next problem (serving PAAGE in ArcIMS and still be editable), for which I'll start a new thread. Wish I could give you the points for this one Charlton. Thanks a ton to you and Dan. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Unable to do Spatial join in Python 
Author Charlton Lewis 
Date Apr 01, 2005 
Message No problem at all Larry.

Copyfeatures is supposed to append to an existing feature class or overwrite the 'features' in a feature class if they already exist, not overwrite the feature class itself. Some things just don't always act as advertised.

Did you do the indentation on the line following the 'if' statement? When you paste it into this message area, the indentation disappears. If the feature class doesn't exist, the delete won't happen so you shouldn't have to worry about that. If you code in a definite delete the way that you have, you have to make sure the feature class does exist or you'll get an error. If the feature class will always be there, you don't have to worry about it.

Take care..