the .description of the featureclass itself

2933
8
Jump to solution
12-27-2012 01:30 PM
AndySwift
New Contributor
Why when I add a layer programmatically does the featureclass description visible in ArcCatalog disappear?

#Is the .description of the featureclass itself available rather than from the layer object?                          print str(cnt) + ", " + fd + ",  " + fc + ",  " + str(count) + ",  " + layname + ",  " + dattype + ",  " + typedat             mxd = arcpy.mapping.MapDocument("C:\\temp\\test.mxd")             arcpy.env.workspace = gdb + '\\' + fd             base_Folder = arcpy.env.workspace             df = arcpy.mapping.ListDataFrames(mxd, "*")[0]             outlayer = os.path.join(base_Folder, fc) + "_lyr"             layerfile = "C:\\temp\\" + fc + ".lyr"             arcpy.MakeFeatureLayer_management(os.path.join(base_Folder, fc), outlayer)              arcpy.SaveToLayerFile_management(outlayer, layerfile, "ABSOLUTE")             addLayer = arcpy.mapping.Layer(outlayer)             arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")             arcpy.RefreshTOC()             arcpy.RefreshActiveView()             for lyr in arcpy.mapping.ListLayers(mxd):                 print lyr.description mxd.save()   del mxd   ##Output Line1(First Print Statement is okay): #1, ADMIN_BOUNDARY,  AZ_cnty_bnd_100,  15,  n,  Simple,  Polygon ##Output Line2(Second Print Statement Same Layer Dropped from Arc Catalog, is okay): #Hi andy how to retrieve this text from ArcCatalog Description Tab. ##Output Line3(Second Print Statement Same Layer Added with .Addlayer, description disappears): #C:\temp\awstmp_serverprj_bak\R9GDL_Data_WM\R9GDL_WM.gdb\ADMIN_BOUNDARY\AZ_cnty_bnd_100_lyr 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
I think these are two things -- the description property of a layer and a .lyr file is a property of a layer; the Description content seen on the Description tab in ArcCatalog and in Data / Item Description is part of the proper ArcGIS metadata schema used to store metadata on datasets and objects.

I agree -- it would sure be nice if Item Description metadata content could be easily pulled off using the Describe function! Alas it is not so.

The only way I know to do it in Python and ArcGIS 10 is to use the XSLT Transformation tool to extract the metadata content you want to an XML file using an xslt transformation (for example, "ArcGIS_ItemDescription.xsl") and scrape it out of the XML with xml.etree.ElementTree. (This is just one way, it seems there are dozens of ways to handle XML in the Python standard library!)

Warning, this may be painfully slow if you are working with hundreds of feature classes.

There is documentation and tools on how to work with metadata (w/ XML, XSLT etc)  in the relatively recently-released ArcGIS Metadata Toolkit.

If you come up with a handy function, please share it!

View solution in original post

0 Kudos
8 Replies
JeffBarrette
Esri Regular Contributor
The feature class "metadata" description is independent from the layer description.  If you add a FC directly into ArcMap and via the UI look at the layer properties, the layer description is blank.  Layer decscriptions can be saved in layer files.

Jeff
0 Kudos
AndySwift
New Contributor
Jeff, Thank you for the clear restatement of the problem.  Is the metadata .description of the featureclass itself available through python rather than from the layer object?  I've searched many posts and help documents but haven't yet found the right snippit of code to get it to work.  If I drag and drop the layer into ArcMap from ArcCatalog the featureclass description is available in the layer description (as shown in the print statement posted above); but when I add the featureclass to the map programmatically the featureclass description gets overwritten by the file path in the layer object.  There must be something simple I'm missing to access the featureclass .description programmatically.  

Thanks for your help with it.
-Andy
0 Kudos
curtvprice
MVP Esteemed Contributor
  There must be something simple I'm missing to access the featureclass .description programmatically.  


The feature class has no description property. This is a property of a layer (read and write) - but not all types of layers support it.


'>arcpy.mapping: Layer


I don't see a description property exposed on the arcpy layer object, although it is clearly saved in the lyr file.


Are you saying when you programmatically add a .lyr file to the mxd with arcpy.mapping.AddLayer, the description is reset? If you are adding a feature class, there is no description on a FC object so the path must be being added when the layer is created in the arcmap document.
0 Kudos
AndySwift
New Contributor
There are hundreds of sde and fgdb featureclasses and many of them have readable English metadata descriptions associated with them which I want to retrieve along with the featureclass names by looping through all the datasets and featureclasses.  I'd rather not do it through ArcObjects VBA so I thought maybe python.  The only way I've found so far is

            for lyr in arcpy.mapping.ListLayers(mxd):
                print lyr.description


And it seems to work, almost. If I add a featureclass to the map manually this print statement returns the same text I put into the ArcCatalog description of it:


#Hi andy how to retrieve this text from ArcCatalog Description Tab.


If I add the exact same featureclass to the map programmatically as shown above this same print statement returns the file path instead of the text description:

 
#C:\temp\....\ADMIN_BOUNDARY\AZ_cnty_bnd_100_lyr



Perhaps my terminology is wrong?  "there is no description on a FC object"  There are descriptions for the featureclasses even if it�??s not exposed directly as a property on the FC object. I can read them. I could copy/paste them into a table from ArcCatalog perhaps.  However, there are a lot of them and they change sometimes, so it's best to do it programmatically. There must be someway and your help is appreciated if you have any ideas.


[ATTACH=CONFIG]20387[/ATTACH]
0 Kudos
curtvprice
MVP Esteemed Contributor
I think these are two things -- the description property of a layer and a .lyr file is a property of a layer; the Description content seen on the Description tab in ArcCatalog and in Data / Item Description is part of the proper ArcGIS metadata schema used to store metadata on datasets and objects.

I agree -- it would sure be nice if Item Description metadata content could be easily pulled off using the Describe function! Alas it is not so.

The only way I know to do it in Python and ArcGIS 10 is to use the XSLT Transformation tool to extract the metadata content you want to an XML file using an xslt transformation (for example, "ArcGIS_ItemDescription.xsl") and scrape it out of the XML with xml.etree.ElementTree. (This is just one way, it seems there are dozens of ways to handle XML in the Python standard library!)

Warning, this may be painfully slow if you are working with hundreds of feature classes.

There is documentation and tools on how to work with metadata (w/ XML, XSLT etc)  in the relatively recently-released ArcGIS Metadata Toolkit.

If you come up with a handy function, please share it!
0 Kudos
AndySwift
New Contributor
Thanks Curtis, I was just reading through this previous post about metadata.  It seems like a simple thing; but it's more involved.

http://forums.arcgis.com/threads/33080-List-metadata-with-Python

Thanks for the assistance.
-andy
0 Kudos
ChrisFox3
Occasional Contributor III
Andy,

This is not specific to Python, you can see it in ArcMap with the Make Feature Layer tool. If you run Make Feature Layer on your feature class and then examine the Layer Properties/General tab you will see the Description is the same as the layer name. When you drag and drop a FC into the map from Catalog the Description in the layer properties is taken from the item description of the feature class.

This seems like a good enhancement request to the make feature layer tool to support bringing over the item description information to the layer since the item description information is valuable in publishing and packaging of the layer as well.
0 Kudos
curtvprice
MVP Esteemed Contributor
This little function should do the trick for you. I did not get fancy with the xslt, just used the canned "exact copy of" stylesheet that's shipped with ArcGIS to dump the entire metadata record out of the feature class for you. If the metadata is FGDC and hasn't been upgraded to 10x, the description is in the FGDC tag, so I handled that.

This function could be modified to get the title or other metadata content you're interested in, as long as you know its "tag path" in the XML.

def GetDescription(item):
    """Get Description text from item metadata"""
    import os
    import arcpy
    from xml.etree.ElementTree import ElementTree as ET
    # get description
    AGSHOME = arcpy.GetInstallInfo("Desktop")["InstallDir"]
    exactCopy = os.path.join(AGSHOME,"Metadata/Stylesheets/gpTools/exact copy of.xslt")
    tmpXML = arcpy.CreateScratchName("",".xml","file",os.environ["TEMP"])
    arcpy.XSLTransform_conversion(item, exactCopy, tmpXML)
    tree = ET().parse(tmpXML)
    os.remove(tmpXML)
    node = tree.find("dataIdInfo/idPurp")
    # if not found try to extract FGDC CSDGM purpose element
    if node == None:
        node = tree.find("idinfo/descript/purpose")    
    try:
        return node.text
    except:
        return None
 


Here's a more generalized version:

def GetMetadataText(item,elementPath):
    """Get metadata node text attribute from item metadata

    example

    GetMetadataText(r"C:\Users\cprice\Documents\boxdd.shp","dataIdInfo/idPurp")
    """
    import os
    import arcpy
    from xml.etree.ElementTree import ElementTree as ET
    AGSHOME = arcpy.GetInstallInfo("Desktop")["InstallDir"]
    exactCopy = os.path.join(AGSHOME,"Metadata/Stylesheets/gpTools/exact copy of.xslt")
    tmpXML = arcpy.CreateScratchName("",".xml","file",os.environ["TEMP"])
    arcpy.XSLTransform_conversion(item, exactCopy, tmpXML)
    tree = ET().parse(tmpXML)
    os.remove(tmpXML)
    node = tree.find(elementPath)  
    if node == None:
        raise Exception, "Element %s not found in metadata" % elementPath        
    try:
        return node.text
    except:
        raise Exception, "No text content for element %s" % elementPath