Python problem: How to get workspace of a FeatureLayer?

4312
7
12-08-2010 01:46 AM
DuncanHornby
MVP Notable Contributor
Python Gurus,

As you all know there is the annoying problem of SQL syntax between different storage formats, by this I mean for a Personal Geodatabase a field is enclosed in [] whilst a file geodatabase has fields enclosed in "".

I've recently became aware of a useful method of the geoprocessor AddFieldDelimiters that automates the correct enclosure based upon a workspace requirements. The Object Model diagram shows that the syntax is gp.AddfieldDelimiters(fieldname,workspace).

Now I often expose my scripts as tools within ArcToolbox so my input parameters are usually a FeatureLayer.

My problem is that having looked at the Object Model I can't work out how one gets a handle on the Workspace of a FeatureLayer which I could then use to feed into the AddfieldDelimiters method for building the correct SQL syntax.  I thought the Describe method would help with this but it seems it is not possible.

What I want to avoid is the user having to select not only the FeatureLayer but then navigate to it's Workspace in the ArcToolbox interface.

So my question is simply how does one get the workspace of a featurelayer using the geoprocessor in Python? I am using ArcGIS 9.3.

Duncan
0 Kudos
7 Replies
JoeVondracek
New Contributor III
Given a feature layer, your Python code could use Describe to find the path of the underlying data, and then parse that to determine if it's a file GDB, personal GDB, or something else.  Kind of like the following.
# Import system modules
import sys, string, os, arcgisscripting

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

# Script arguments...
ftrLyr = sys.argv[1]
if ftrLyr == '#':
   gp.AddError("  You must provide a feature layer.")

# Check to see if the input exists
if gp.Exists(ftrLyr):
   desc = gp.Describe(ftrLyr)
   ftrPath = desc.Path
   ext = ftrPath.rpartition('.')[2]   # Get part after last period.
   ext = ext.lower()
   if ext == "gdb":
      gp.AddMessage(" Workspace is file geodatabase")
   elif ext == "mdb":
      gp.AddMessage(" Workspace is personal geodatabase")
   else:
      gp.AddMessage(" Workspace is directory based (shapefile, coverage, etc.)")


Incidentally, you might get some better responses if you asked this question over in the Python forum or the Geoprocessing forum.
0 Kudos
DuncanHornby
MVP Notable Contributor
Hi,

Thats a good way of determining the workspace, but my question is where is the method Path documented in the Desktop help file? I find no mention of this method in the help file or object model!

I get your point about placing this question in the other thread but I've always considered that to be about processing not annoying specifics of Python!

Thanks for your help.

Duncan
0 Kudos
JoeVondracek
New Contributor III
If you open ArcGIS Desktop Help and go to the Contents tab, you can find information about the Describe object and its properties under the following:
Geoprocessing -> Automating your work with scripts -> Data properties and access when scripting -> Describing data

The Geoprocessor Programming Model can be found as a PDF on your system at:
C:\Program Files\ArcGIS\Documentation\Geoprocessor_93.pdf

Also, Path is a property, not a method.
0 Kudos
DuncanHornby
MVP Notable Contributor
Joe,

Sorry to bang on about this but I looked at the Help page you identified, a page I have looked at before but I see NO mention of the property (thanks for correcting me) Path.  There is the property CatalogPath which you use on a dataset object but this actually returns something slightly different (with a shapefile). So where did you find out about the property Path?

Duncan
0 Kudos
JoeVondracek
New Contributor III
Well, here is the online version of the ArcGIS Desktop 9.3 Help:
http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Describing_data

On that page, under Describe Object properties, is a graphic that shows the properties for the Describe object.  The Path property is the last one listed in the graphic.  Beneath that graphic is a table that lists all of the properties with a short description.  The Path property is the last one in that table.

Also, you can look at this page, which should also be in your local Help:
http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Describe_Object_property

My installed Help documentation matches what is on the website.  However, I am running ArcGIS 9.3.1, and I see that both of these Help pages say that they were updated for 9.3.1.  Maybe the 9.3 versions of these pages didn't include the Path property.  Are you able to reference that property of the Describe object in your scripts?  If you can, then I would expect it to appear in your Help documentation.  If not, then this is all moot, eh?
0 Kudos
DuncanHornby
MVP Notable Contributor
Joe,

Finally! I'm always telling people to RTFM and I practise what I preach but it seems the 9.3 desktop help is not the same as the 9.3 online desktop help. I note it says on that page (despite it saying 9.3 Help) that it has been update for 9.3.1 and there bold as brass is that property Path...

There are a load of other properties too I never knew the describe object could do, this is very frustrating.

The sooner they give up on python and go back to VBA the better! 😉

Thanks for your help.

Duncan
0 Kudos
JoeVondracek
New Contributor III
You might also want to check the Geoprocessor Programming Model PDF on your system to see if it shows the "extra" properties or not:
C:\Program Files\ArcGIS\Documentation\Geoprocessor_93.pdf
0 Kudos