Home » excel » python – updating metadata for feature classes programatically using arcpy

python – updating metadata for feature classes programatically using arcpy

Posted by: admin May 14, 2020 Leave a comment

Questions:

I would like to be able to take an excel file that contains a record for each feature class, and some metadata fields, like summary, description, etc., and convert that to the feature class metadata. From the research I’ve done it seems like I need to convert each record in the excel table to xml, and then from there I may be able to import the xml file as metadata. Looks like I could use ElementTree, but I’m a little unsure of how to execute. Has anyone done this before and if so could you provide some guidance?

How to&Answers:

Man, this can be quite a process! I had to update some metadata information for a project at work the other day, so here goes nothing. It would be helpful to stored all of the metadata information in the excel table as a dictionary list or other data structure of your choosing (I work with csvs and try to stay away from excel spreadsheets for experience reasons).

metaInfo = [{"featureClass":"fc1",
             "abstract":"text goes here", 
             "description":"text goes here",
             "tags":["tag1","tag2","tag3"]},
            {"featureClass":"fc2",
             "abstract":"text goes here", 
             "description":"text goes here",
             "tags":["tag1","tag2","tag3"]},...]

From there, I would actually export the current metadata feature class using the Export Metadata function to convert your feature class metadata into an xml file using a FGDC schema. Here is a code example below:

#Directory containing ArcGIS Install files
installDir = arcpy.GetInstallInfo("desktop")["InstallDir"]
#Path to XML schema for FGDC
translator = os.path.join(installDir, "Metadata/Translator/ARCGIS2FGDC.xml")
#Export your metadata
arcpy.ExportMetadata_conversion(featureClassPath, translator, tempXmlExportPath)

From there, you can use the xml module to access the ElementTree class. However, I would recommend using the lxml module (http://lxml.de/index.html#download) because it allows you to incorporate html code into your metadata through the CDATA factory if you needed special elements like line breaks in your metadata. From there, assuming that you have imported lxml, parse your local xml document:

import lxml.etree as ET
tree = ET.parse(tempXmlExportPath)
root = tree.getroot()

If you want to update the tags use the code below:

idinfo = root[0]

#Create keyworks element
keywords = ET.SubElement(idinfo, "keywords")
tree.write(tempXmlExportPath)

#Create theme child
theme = ET.SubElement(keywords, "theme")
tree.write(tempXmlExportPath)

#Create themekt and themekey grandchildren/insert tag info
themekt = ET.SubElement(theme, "themekt")
tree.write(tempXmlExportPath)
for tag in tags: #tags list from your dictionary
    themekey = ET.SubElement(theme, "themekey")
    themekey.text = tag
    tree.write(tempXmlExportPath)

To update the Summary tags, use this code:

#Create descript tag
descript = ET.SubElement(idinfo, "descript")
tree.write(tempXmlExportPath)

#Create purpose child from abstract
abstract = ET.SubElement(descript, "abstract")
text = #get abstract string from dictionary
abstract.text = text
tree.write(tempXmlExportPath)

If a tag in the xml already exists, store the tag as an object using the parent.find(“child”) method, and update the text similar to the code examples above. Once you have updated your local xml file, use the Import Metadata method to import the xml file back into the feature class and remove the local xml file.

arcpy.ImportMetadata_conversion(tempXmlExportPath, "FROM_FGDC", featureClassPath)
shutil.rmtree(tempXmlExportPath)

Keep in mind that these tools in Arc are only for 32 bit, so if you are scripting through the 64 bit background geoprocessor, this will not work. I am working off of ArcMap 10.1. If you have any questions, please let me know or consult the documentation below:

lxml module
http://lxml.de/index.html#documentation

Export Metadata arcpy
http://resources.arcgis.com/en/help/main/10.1/index.html#//00120000000t000000

Import Metadata arcpy
http://resources.arcgis.com/en/help/main/10.1/index.html#//00120000000w000000