List metadata with Python

15322
36
06-20-2011 01:00 PM
JoshThompson
New Contributor III
I have a list of feature classes from a geodatabase I created from a Python script and would like to add their respective metadata or a portion of (such as the description portion of the ArcGIS metadata style) to this list by feature class. Is this possible with Python or am I asking too much?

Thanks,
Josh
Tags (2)
36 Replies
DarrenWiens2
MVP Honored Contributor
Rather than call one feature class (fcpath), you'll call each feature class, one by one, from the fcList. This is totally untested, so you might run into errors (tricky areas might include overwriting the xml file, or reusing the ElementTree). You could also dynamically name the xml (e.g. with a similar name to the feature class name) inside the loop if you wanted separate xml files, rather than just one overwritten each time.
import arcpy, sys
from xml.etree.ElementTree import ElementTree
from arcpy import env
env.overwriteOutput = True
env.workspace = r"D:\GIS\GDB\Lakeland\Lakeland.gdb\AddPoints" # set the workspace for use in  ListFeatureClasses
fcList = arcpy.ListFeatureClasses() # lists all feature classes from inside env.workspace

translatorpath = r"C:\Program Files (x86)\ArcGIS\Desktop10.0\Metadata\Translator\ARCGIS2FGDC.xml" # the translator file to use
xmlfile = r"D:\GIS\metadata\test.xml" # the output xml file

for fc in fcList:
    arcpy.ExportMetadata_conversion(fc, translatorpath, xmlfile) # export the current fc's metadata to xml
    tree = ElementTree() # make an ElementTree object
    tree.parse(xmlfile) # read the xml into the ElementTree
    spot = tree.find("idinfo/descript/abstract") # find whatever tag you want
    print spot.text # print the text between the tags
 
curtvprice
MVP Esteemed Contributor
I tried today to use the XSLTransform_conversion tool to just extract out everything (avoiding a transform) but I am getting an error message that appears to be a bug in tool validation (which I sent to ESRI support). I've attached this python script that is a function to pull out the ArcGIS metadata elements more directly (no need for the FGDC transform).

The Arc 10 metadata schema is documented in the file:

\ArcGIS\Desktop10.0\Metadata\Translator\Rules\ArcGIS metadata v1.dtd

though I have found it easier to just look in an existing ".shp.xml" file.

Another approach to this problem is to create an xslt stylesheet and run the XSL Transformation tool (conversion toolbox) to pull out the information  you want into an XML file.
DarrenWiens2
MVP Honored Contributor
I tried today to use the XSLTransform_conversion tool to just extract out everything (avoiding a transform) but I am getting an error message that appears to be a bug in tool validation (which I sent to ESRI support).


I believe this was fixed in SP1. I can't remember the exact error I was getting, but it's gone now.
JoshThompson
New Contributor III
Darren,
When I attempted to use the code you supplied, it simply moves to a new prompt. No errors, no output, nothing. Just a new prompt.

Curtis,
I am getting the same error.
0 Kudos
curtvprice
MVP Esteemed Contributor
The following attached script works as a script tool. I get the error when I try to run it from IDLE or the command line... apparently there is something in the environment that is messing me up. I'm running 10.0 SP 2 on XP 64.

import sys, os, arcpy

def getMetadataText(Dataset,Element="dataIdInfo/idPurp"):
    """Extracts metadata contents from ArcGIS metadata
  arguments
    Dataset - Path to an ArcGIS dataset
    Element - XML path in ArcGIS metadata. 
    Default: "metadata/dataIDInfo/idPurp" ("Summary")
"""
    from xml.etree.ElementTree import ElementTree
    try:
        # Get arcgis to fgdc translator path
        ARCGISHOME = arcpy.GetInstallInfo("desktop")["InstallDir"]
        XSLT = os.path.normpath(os.path.join(
            ARCGISHOME,
            "Metadata/Stylesheets/gpTools/exact copy of.xslt"))
        xmlTemp =  os.path.join(os.environ["TEMP"],"xx_xmltemp.xml")
        # export the metadata to xml   
        arcpy.env.overwriteOutput = True       
        arcpy.XSLTransform_conversion(Dataset,XSLT,xmlTemp)
        # parse out Description element from ArcGIS 10 metadata
        tree = ElementTree() # make an ElementTree object
        tree.parse(xmlTemp) # read the xml into the ElementTree
        Data = tree.find(Element) # find whatever tag you want
        del tree
        arcpy.Delete_management(xmlTemp)
        return Data.text # return the contents as text
    except Exception, xmsg:
        raise Exception, str(xmsg)

# You could put a loop here around this to print out Summary for a list of datasets
dataset = r"E:\work\work.gdb\BeetleSprayArea.shp"
text = getMetadataText(dataset)
arcpy.AddMessage(text)
JoshThompson
New Contributor III
Thanks Curtis, but I'm afraid this code is beyond my skill level. I think I have gotten in over my head with this and need some more practice time with Python before trying this again. Thanks again for your help.

Josh
0 Kudos
JoshThompson
New Contributor III
All,
I brushed up on Python a little and attempted to tackle this script again. I have had some success in that I can now generate a list of feature classes and the "Summary" component of the metadata for all features in a feature dataset. The script below will produce that information.

import arcpy, sys
from xml.etree.ElementTree import ElementTree
from arcpy import env
env.overwriteOutput = True
env.workspace = r"D:\Lakeland_GIS\GDB\Lakeland\Lakeland.gdb\AddPoints"

translatorpath = r"C:\Program Files (x86)\ArcGIS\Desktop10.0\Metadata\Translator\ARCGIS2FGDC.xml"
xmlfile = r"U:\my docs\GIS Projects\Python\Scripts\List Metadata\working_test.xml"
 

datasetList = arcpy.ListDatasets()
datasetList.sort()
for dataset in datasetList:
    print dataset
fcList = arcpy.ListFeatureClasses()
fcList.sort()
for fc in fcList:
    arcpy.ExportMetadata_conversion(fc, translatorpath, xmlfile)
    tree = ElementTree()
    tree.parse(xmlfile)
    spot = tree.find("idinfo/descript/purpose")
    print fc
    print spot.text

What I am attempting to do is generate a list of all feature classes, by dataset, for an entire geodatabase. Ex. - (Dataset>Feature Class>Metadata). As of now I can only get FeatureClass>Metadata for one feature dataset at a time.


Help! If anyone could help me complete this I would be truly appreciative.

Thanks,
Josh
0 Kudos
curtvprice
MVP Esteemed Contributor
You really need to put these things in code blocks so we can see your loops.

bbcode formatting: http://forums.arcgis.com/misc.php?do=bbcode
0 Kudos
JoshThompson
New Contributor III
Sorry, Curtis. I didn't realize the format doesn't carry over into the message. Lesson learned. Sending the code as an attachment.

Thanks,
Josh
0 Kudos
curtvprice
MVP Esteemed Contributor
You were very close! I didn't handle any metadata on the feature dataset itself; you could add that.

import arcpy, sys
from xml.etree.ElementTree import ElementTree
from arcpy import env
env.overwriteOutput = True
env.workspace = r"D:\Lakeland_GIS\GDB\Lakeland\Lakeland.gdb\AddPoints"

AGSHOME = arcpy.GetInstallInfo("Desktop")["InstallDir"]
translatorpath = AGSHOME + r"Metadata\Translator\ARCGIS2FGDC.xml" 
xmlfile = r"U:\my docs\GIS Projects\Python\Scripts\List Metadata\working_test.xml" 
  
# list any standalone feature classes
fcList = arcpy.ListFeatureClasses()
fcList.sort()
for fc in fcList:
    arcpy.ExportMetadata_conversion(fc, translatorpath, xmlfile) 
    tree = ElementTree() 
    tree.parse(xmlfile) 
    spot = tree.find("idinfo/descript/purpose") 
    print fc
    print spot.text
        
# list feature datasets
datasetList = arcpy.ListDatasets()
datasetList.sort()
for dataset in datasetList:
    print dataset
    # list feature classes inside the dataset
    fcList = arcpy.ListFeatureClasses("","",dataset)
    fcList.sort()
    for fc in fcList:
        arcpy.ExportMetadata_conversion(fc, translatorpath, xmlfile) 
        tree = ElementTree() 
        tree.parse(xmlfile) 
        spot = tree.find("idinfo/descript/purpose") 
        print "  " + fc
        print "  " + spot.text