Access metadata elements from within arcpy / python

3472
2
Jump to solution
06-20-2013 08:23 PM
CPoynter
Occasional Contributor III
Hi All,

I have a listing of feature classes and raster datasets that I want to access their metadata for creating a reference spreadsheet and customised html webpage.

Metadata elements I would like to access are description, summary, abstract, and extents for starters, and this will change if I determine a different stylesheet.

From arcpy / python, how do I access the metadata values to make them into individual variables for manipulation.

Regards,

Craig
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ShaunWalbridge
Esri Regular Contributor
ArcPy doesn't provide direct access to the metadata elements, but the broad suite of existing XML tools provided with Python should help. ElementTree in particular is worth looking at for XML manupulation. If you're using standard Esri metadata, you might be interested in this metadata script on GIS.SE which shows how to access elements of metadata documents.

cheers,
Shaun

View solution in original post

0 Kudos
2 Replies
ShaunWalbridge
Esri Regular Contributor
ArcPy doesn't provide direct access to the metadata elements, but the broad suite of existing XML tools provided with Python should help. ElementTree in particular is worth looking at for XML manupulation. If you're using standard Esri metadata, you might be interested in this metadata script on GIS.SE which shows how to access elements of metadata documents.

cheers,
Shaun
0 Kudos
CPoynter
Occasional Contributor III
Reference above helped me solve my problem. Working code follows:

import arcpy, sys
import xml.etree.ElementTree as ET

XML_List = [r'D:\Temp\file1.xml', r'D:\Temp\file2.xml']
for xml in XML_List:
    path = xml
    tree = ET.parse(path)

    for node in tree.findall('.//title'):
        title = node.text
        print 'Title: ' + node.text

    for node in tree.findall('.//westbc'):
        westbc = node.text
        print 'West: ' + node.text

    for node in tree.findall('.//eastbc'):
        eastbc = node.text
        print 'East: ' + node.text

    for node in tree.findall('.//northbc'):
        northbc = node.text
        print 'North: ' + node.text

    for node in tree.findall('.//southbc'):
        southbc = node.text
        print 'South: ' + node.text

    for node in tree.findall('.//geogunit'):
        geogunit = node.text
        print 'Geographic Units: ' + node.text

    for node in tree.findall('.//horizdn'):
        horizdn = node.text
        print 'Projection: ' + node.text

    for node in tree.findall('.//ellips'):
        ellips = node.text
        print 'Ellipsoid: ' + node.text

del tree



Regards,

Craig