Geographic Information Systems Asked on December 29, 2021
import os
import sys
import xml.etree.ElementTree as ET
import arcpy
from arcpy import metadata as md
# Script arguments...
Source_Metadata = arcpy.GetParameter(0)
# Local variables
# new purpose text
newPurpose = "This is new text for an existing Purpose metadata element."
newCredits = "This is text for a new Credits metadata element."
# function to update purpose and credits in metadata
def update_metadata(root):
num_elements = 0
# modify purpose element's text
# there is only supposed to be one purpose element in metadata
# replace purpose element text if element exists
# if element doesn't exist, do nothing
purposeEls = root.findall(".//idPurp")
for element in purposeEls:
if element.text is not None:
element.text = newPurpose
num_elements += 1
# add credits element to dataIdInfo parent, if the parent exists
# ISO allows many dataIdInfo groups; ArcGIS supports only one, so get the 1st
# ISO allows many idCredit elements, and many are supported in ArcGIS
# append a new idCredit element with appropriate text, existing elements remain
dataIdInfoEls = root.findall("./dataIdInfo[1]")
for element in dataIdInfoEls:
if element:
newCreditEl = ET.SubElement(element, "idCredit")
newCreditEl.text = newCredits
num_elements += 1
return num_elements
# get and save item metadata
for item in Source_Metadata:
arcpy.AddMessage("Item: {0}".format(item))
# get the item's metadata xml
item_md = md.Metadata(item)
metadata_xml_string = item_md.xml
# create an ElementTree object from the metadata XML string
root = ET.fromstring(metadata_xml_string)
# call the update_metadata function to modify the item's metadata
changes = update_metadata(root)
if changes > 0:
# get modified XML
updated_xml_string = ET.tostring(root, encoding="utf-8")
# import result back into metadata
arcpy.AddMessage("Saving updated metadata with the item...")
item_md.xml = updated_xml_string
item_md.save()
else:
arcpy.AddMessage("No changes to save")
arcpy.AddMessage('Finished updating metadata for all source metadata items')
Trying to take this Esri-sourced script to update multiple metadata elements. If I run this in a standalone script without editing, it doesn’t do anything, no errors. If I take the script and create a script tool in a GDB inside of ArcGIS Pro, it fails when I select the XML file as a parameter. What do I need to do to get this script to work on my metadata XML?
The script (meant for ArcGIS Pro) was sourced from:
https://pro.arcgis.com/en/pro-app/arcpy/metadata/migrating-from-arcmap-to-arcgis-pro.htm#ESRI_SECTION1_098BC26422304FAE96A19FB4B6528E07
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP