List metadata with Python

15250
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

I don't know how to get at metadata directly, but you can export the metadata to xml (I believe this tool only works FROM INSIDE PYTHON in SP1 and beyond) and then read through the xml with an ElementTree. I can't remember if the translator doesn't export all FGDC tags or doesn't import all FGDC tags correctly, but it's one of the two, so beware.

import arcpy, sys
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element, SubElement
from arcpy import env
env.overwriteOutput = True
fcpath = r"X:\working\metadata\BlankGDB.gdb\FullMetaFC" # the input feature class
translatorpath = r"D:\Program Files (x86)\ArcGIS\Desktop10.0\Metadata\Translator\ARCGIS2FGDC.xml" # the translator file to use - it should be in an installed folder like this
xmlfile = r"X:\working\metadata\FullMetaFC.xml" # the output xml file
arcpy.ExportMetadata_conversion(fcpath, translatorpath, xmlfile) # export the metadata to xml

tree = ElementTree() # make an ElementTree object
tree.parse(xmlfile) # read the xml into the ElementTree

spot = tree.find("idinfo/citation/citeinfo/title") # find whatever tag you want
print spot.text # print the text between the tags

edit: updated paths as Curtis advises below.

curtvprice
MVP Esteemed Contributor
Danger, the code above uses normal strings with backslashes. These are escape characters in Python and will not work. Easily fixed by making them "raw" strings as follows:
fcpath = r"X:\working\metadata\BlankGDB.gdb\FullMetaFC" 


Also you may want to read up on the Arc 10 metadata story, as both FGDC and ArcGIS 10 metadata may be in the metadata file and you will want to decide how you can handle that.

A New Approach To Metadata at ArcGIS 10 (Part 1)
http://blogs.esri.com/Dev/blogs/arcgisdesktop/archive/2011/01/03/A-new-approach-for-Metadata-with-Ar...
AndresCastillo
MVP Regular Contributor

That url is not available, but this is the closest I see on Google:

http://www.faculty.umb.edu/michael.trust/A_New_Approach_for_Metadata_with_ArcGIS10.pdf 

0 Kudos
curtvprice
MVP Esteemed Contributor

Yes 10.1 was a long time ago!

The new ArcGIS Pro metadata module is totally the way to go if you just want to dump lists of basic item info from metadata. Much better than what is available in ArcMap for many things: very fast and solid read-write access (compared to Python XML parsing or the old ArcMap conversion toolbox tools). 

0 Kudos
DarrenWiens2
MVP Honored Contributor
Oh, I can never remember which is which. It works fine as is, I suppose because there were no characters to be escaped...? In any case, you should probably fill in your own paths, as I doubt you have access to my X:/.
0 Kudos
curtvprice
MVP Esteemed Contributor
It works fine as is, I suppose because there were no characters to be escaped...?


Just don't ever name any folders or files starting with n, r, or t, etc....:

>>> "a\z" # "\z" is not a special string literal, so OK
'a\\z'
>>> "a\t"  # this is "a" followed by <tab>
'a\t'
>>> 


See:
http://docs.python.org/reference/lexical_analysis.html#string-literals
JoshThompson
New Contributor III
Hey Guys,

Thanks for the help. So far, all the info you have given me has worked very well. I am able to list the "Description" portion of the metadata for an individual feature class.

Is there a way to integrate this code with the "Listfc" script? My ultimate goal is to list all feature classes, by dataset, and their respective metadata from a file GDB to give to staff in various dept. I am a beginner and am just not able to make this work on my own. I appreciate any help you can give.

Josh
curtvprice
MVP Esteemed Contributor
I am able to list the "Description" portion of the metadata for an individual feature class.


Cool! Please post your working script either in full or as an attachment. Wrapping this in a loop should be pretty straighforward.
JoshThompson
New Contributor III
Here is the script I used (per dkwiens) to extract and print the "Description" portion of the metadata for an individual feature class:

import arcpy, sys
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element, SubElement
from arcpy import env
env.overwriteOutput = True
fcpath = r"D:\GIS\GDB\Lakeland\Lakeland.gdb\AddPoints\AddressPts" # the input feature class
translatorpath = r"C:\Program Files (x86)\ArcGIS\Desktop10.0\Metadata\Translator\ARCGIS2FGDC.xml" # the translator file to use - it should be in an installed folder like this
xmlfile = r"D:\GIS\metadata\test.xml" # the output xml file
arcpy.ExportMetadata_conversion(fcpath, translatorpath, xmlfile) # export the 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

If you can help me tie this into the fcList script, I will be indebted to you.

Josh
0 Kudos