Select to view content in your preferred language

Programmatically update metadata

155
0
2 weeks ago
Labels (1)
HanliePetoors
Occasional Contributor

Hi,

I'm working in ArcGIS Pro 3.3.0.

I have a script that updates an SDE feature class and then I want to update the publication date in the metadata. This is not part of the common metadata properties so it's not directly accessible from a metadata object properties.

I'm using the ISO19139 metadata standard.

My algorithm is:

  1. Create the metadata object.
  2. Export the metadata to XML.
  3. Modify the XML using ElementTree.
  4. Save the modified XML.
  5. Import the modified XML to the metadata object.

My code is:

import xml.etree.ElementTree as ET
from arcpy import metadata as md
import datetime

item = r'D:\Projects\WCG General\Python 2 to 3\DOH_HealthFacilities.gdb\DOH_Facilities_AllHealthSites'
CurrentDate = datetime.datetime.now().strftime("%Y-%m-%d")

currentXMLPath = r'D:\Projects\WCG General\Python 2 to 3\temp.xml'
newXMLPath = r'D:\Projects\WCG General\Python 2 to 3\temp_updated.xml'

# get the item's metadata xml
item_md = md.Metadata(item)
metadata_xml_string = item_md.xml

# export item's metadata
item_md.exportMetadata(outputPath=currentXMLPath,
                metadata_export_option='ISO19139')

# create an ElementTree object and get its root
tree = ET.parse(currentXMLPath)
root = tree.getroot()

# check that the correct element exists
# root[10][0][0][0][2][0][1][0].text must be = 'publication'
# root[10][0][0][0][2][0][0][0] is the field that must be set

# set the value of the publication date to the current date
if root[10][0][0][0][2][0][1][0].text == 'publication':
    root[10][0][0][0][2][0][0][0].text = CurrentDate

# save the changes to the the new XML file
tree.write(newXMLPath)

# import the new XML to the metadata object
item_md.importMetadata(sourceUri=newXMLPath,
                       metadata_import_option='ISO19139')

Everything works fine until the last step, when I get an error

Traceback (most recent call last):
  File "D:\Projects\WCG General\Python 2 to 3\read_metadata.py", line 50, in <module>
    item_md.importMetadata(sourceUri=newXMLPath,
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py", line 186, in fn_
    return fn(*args, **kw)
           ^^^^^^^^^^^^^^^
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\metadata.py", line 126, in importMetadata
    return _convertArcObjectToPythonObject(arcgisscripting.metadata.Metadata.importMetadata(*_gp_fixargs((self, sourceUri, metadata_import_option, customStylesheetPath), True)))
                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SystemError: <built-in method importMetadata of MetadataObject object at 0x000001B4105315C0> returned a result with an exception set
 
Anybody have any ideas for a better way of doing this or fixing what's going wrong here?
 
Thanks
Hanlie
Tags (1)
0 Kudos
0 Replies