List metadata with Python

15236
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
AndresCastillo
MVP Regular Contributor

I believe the first way with the if else is similar to Duck Typing and the "look before you leap" lbyl non-pythonic methods.

As Curtis said, the latter with the try/except is pythonic:

https://www.youtube.com/watch?v=x3v9zMX1s4s

BlakeTerhune
MVP Regular Contributor

It's also better to include the specific error type you're expecting so you don't end up passing unexpected errors too. I don't know what type of error this is supposed to produce but here's an example.

try:  
    Abstract = tree.find("idinfo/descript/abstract").text  
except AttributeError:  
    Abstract = "No abstract"
except:
    # Catch for all other unhandled exceptions
    raise
0 Kudos
MattTenold
New Contributor III
Thank you for the help with the if exception part.   I just have one last question.   I run the script and it returns a list of my feature datasets only.  Can you look at my script and see what I am doing wrong.  Sorry I am more of a vb.net C#/ArcObjects programmer.   Python is a bit strange for me.

import arcpy, sys
... from xml.etree.ElementTree import ElementTree
... from arcpy import env
... env.overwriteOutput = True
... env.workspace = r"G:\sdeConnections\win_auth\sde_published.sde"
... AGSHOME = arcpy.GetInstallInfo("Desktop")["InstallDir"]
... translatorpath = AGSHOME + r"Metadata\Translator\ARCGIS2FGDC.xml"
... xmlfile = r"C:\Users\mtenold\Downloads\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/abstract")
    if spot: # if spot is True (not None)
        print spot.text
    else:
        print "No abstract"
... # 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/abstract")
    if spot: # if spot is True (not None)
...         print "  " + fc
...         print "  " + spot.text
    else:
            print "No abstract"
0 Kudos
curtvprice
MVP Esteemed Contributor
It's hard to tell because of the spurious "..." prefixes and that [post=166129]you didn't use a [noparse]
[/noparse] block,[/post] but I think you're missing an indent. 

Hopefully these fixes address both bugs [post=124021]noted above:[/post]

if the name of your feature class has the same name as the feature dataset it resides in, it will cause the script to fail. (ex. - Data.gbd>Contours>Contours) Will also terminate if no text is found in the metadata component it is looking for.
See changes below in red
import arcpy, sys
from xml.etree.ElementTree import ElementTree
from arcpy import env
env.overwriteOutput = True
env.workspace = r"G:\sdeConnections\win_auth\sde_published.sde"
AGSHOME = arcpy.GetInstallInfo("Desktop")["InstallDir"]
translatorpath = AGSHOME + r"Metadata\Translator\ARCGIS2FGDC.xml" 
xmlfile = r"C:\Users\mtenold\Downloads\test.xml" 
# list any standalone feature classes
fcList = arcpy.ListFeatureClasses()
fcList.sort()
for fc in fcList:
    print fc
    arcpy.ExportMetadata_conversion(fc, translatorpath, xmlfile) 
    tree = ElementTree() 
    tree.parse(xmlfile)
    spot = tree.find("idinfo/descript/abstract") 
    if spot: # if spot is True (not None)
        print spot.text 
    else:
        print "No abstract" 
# list feature datasets
datasetList = arcpy.ListDatasets("","Feature")
datasetList.sort()
for dataset in datasetList:
    print dataset
    # list feature classes inside the dataset
    arcpy.env.workspace = dataset
    fcList = arcpy.ListFeatureClasses()
    fcList.sort()
    for fc in fcList:
        print "  " + fc
        arcpy.ExportMetadata_conversion(fc, translatorpath, xmlfile) 
        tree = ElementTree() 
        tree.parse(xmlfile) 
        spot = tree.find("idinfo/descript/abstract") 
        if spot: # if spot is True (not None)
            print "  " + spot.text
        else:
            print "  No abstract"
curtvprice
MVP Esteemed Contributor
If you're using 10.1 SP1 or later, there's a new approach for inventorying workspaces: arcpy.da.walk.

It could be very handy for the application discussed in this thread.

http://arcpy.wordpress.com/2012/12/10/inventorying-data-a-new-approach/
BrianColson
New Contributor II
Curtis,

This appears to be really helpful code, but I am certainly still learning Python.  I am trying to use it on multiple datasets, and loop through with multiple metadata xml files, and to pull out only three parts (Abstract, Summary, and Credits).  The code appears to be working but it never pulls the xml file to get any information out of it.  If you wish I can send you a sample xml file as well so you can see how it�??s formatted.

My ultimate goal was output a csv file that gives a dataset name, abstract, summary and credits for everything within a folder or geodatabase.  I was going to set this up as a Toolbox Script, so the end user doesn't have to work with the python code.  I don't think I am quiet there, but that�??s why there is a bunch of stuff commented out at the top. 

Any suggestions you can provide would be greatly appreciated. 

Thank you.

Brian
# Create the Geoprocessor object 
import arcpy, sys, os
from xml.etree.ElementTree import ElementTree
from arcpy import env

# Get input parameters
##env.workspace = arcpy.GetParameterAsText(0)
##xmlfile = arcpy.GetParameterAsText(1)
##outfile = arcpy.GetParameterAsText(2)
env.overwriteOutput = True
##AGSHOME = arcpy.GetInstallInfo("Desktop")["InstallDir"]
##translatorpath = AGSHOME + r"Metadata\Translator\ARCGIS2FGDC.xml"

#Output file
##outfile=open(outfile,'w')

#Dataset list function

# list any standalone feature classes
env.workspace = r"C:\projects\Vicinity.gdb"
AGSHOME = arcpy.GetInstallInfo("Desktop")["InstallDir"]
translatorpath = AGSHOME + r"Metadata\Translator\ARCGIS2FGDC.xml" 
fcList = arcpy.ListFeatureClasses()
fcList.sort()
for fc in fcList:
    fcname = str(fc)
##    outfile.write(fcname + "\n" + ",")
    print fc
    xmlfile = r"C:\projects\Testing\ " + fcname + ".xml"
    arcpy.UpgradeMetadata_conversion (fc, "ESRIISO_TO_ARCGIS")
    arcpy.ExportMetadata_conversion(fc, translatorpath, xmlfile) 
    tree = ElementTree() 
    tree.parse(xmlfile)
    spot = tree.find("abstract")
    if spot: # if spot is True (not None) 
        print spot.text 
    else:
        print "No Abstract"
    spot = tree.find("purpose") 
    if spot: # if spot is True (not None)
        print spot.text 
    else:
        print "No Summary"
    spot = tree.find("datacred") 
    if spot: # if spot is True (not None)
        print spot.text
    else:
        print "No Credits"
# list feature datasets
datasetList = arcpy.ListDatasets("","Feature")
datasetList.sort()
for dataset in datasetList:
    print dataset
    # list feature classes inside the dataset
    arcpy.env.workspace = dataset
    fcList = arcpy.ListFeatureClasses()
    fcList.sort()
    for fc in fcList:
        print "  " + fc
        arcpy.ExportMetadata_conversion(fc, translatorpath, xmlfile) 
        tree = ElementTree() 
        tree.parse(xmlfile) 
        spot = tree.find("descript/abstract") 
        if spot: # if spot is True (not None)
            print "  " + spot.text
        else:
            print "  No abstract"

0 Kudos