Select to view content in your preferred language

Issues with syncing metadata via Python

290
3
Jump to solution
05-02-2025 09:33 AM
AlanDodson
Regular Contributor

I am having considerable frustration with updating metadata via scripting.

I have an enterprise geodatabase with many feature datasets. Inside each feature dataset are many feature classes. All feature datasets and feature classes have metadata, which I need to update.

There are 3 sources for my metadata (dynamically-defined items via scripting, synchronized properties, and a template) and I cannot get them to play nice with each other when it comes to displaying the extents of feature classes.

Dynamically-defined items include the fd / fc title, summary, description and tags. Synchronized properties are extents, feature classes, fields, crs, etc. The template contains the credits, contact information (a few times), and other information - stuff that doesn't change regardless of what feature dataset or feature class is being shown.

The most logical flow (in my head) is reflected in my current order of operations:
Import template
Update dynamically-defined items
Synchronize

With that flow, I've tried each of the metadata_sync_option choices; when using 'ALWAYS' everything syncs correctly for feature datasets, and everything syncs correctly EXCEPT Extent for feature classes. How can I get the Extents of feature classes to be properly shown? When I click the Synchronize button everything is perfect, but I need to do this via scripting.

I've tried saving the metadata between steps, I've tried syncing before importing the template, after importing the template.... No joy.

 

Here is a minimal example of my code

import arcpy
import os
from arcpy import metadata as md

sde_connection = arcpy.GetParameterAsText(0)
metadata_template_path = arcpy.GetParameterAsText(1)
arcpy.env.workspace = sde_connection

fds_list = arcpy.ListDatasets(feature_type='Feature')

for fds in fds_list:
    fds_path = os.path.join(sde_connection, fds)
    fds_md = md.Metadata(fds_path)

    if not fds_md.isReadOnly:
        fds_md.importMetadata(metadata_template_path)
        # Here I add the metadata that is dynamically generated (title, summary, tags, etc)
        fds_md.synchronize(metadata_sync_option='ALWAYS')
        fds_md.save()
    else:
        arcpy.AddWarning(f"Metadata for {fds} is read-only")

    arcpy.env.workspace = fds_path
    fc_list = []
    fc_list = arcpy.ListFeatureClasses()
    
    for fc in fc_list:
        fc_path = os.path.join(sde_connection, fds, fc)
        fc_md = md.Metadata(fc_path)

        if not fc_md.isReadOnly:
            fc_md.importMetadata(metadata_template_path)
            # Here I add the metadata that is dynamically generated (title, summary, tags, etc)
            fds_md.synchronize(metadata_sync_option='ALWAYS')
            fc_md.save()
        else:
            arcpy.AddWarning(f"Metadata for {fds}\{fc} is read-only")

# reset workspace
arcpy.env.workspace = sde_connection

 

0 Kudos
1 Solution

Accepted Solutions
TonyAlmeida
MVP Regular Contributor

I see an issue, Not sure if that is the main issue but you're calling fds_md.synchronize() instead of fc_md.synchronize().

View solution in original post

3 Replies
DanPatterson
MVP Esteemed Contributor

is "reload" applicable to your situation?

Metadata—ArcGIS Pro | Documentation


... sort of retired...
0 Kudos
TonyAlmeida
MVP Regular Contributor

I see an issue, Not sure if that is the main issue but you're calling fds_md.synchronize() instead of fc_md.synchronize().

AlanDodson
Regular Contributor

@TonyAlmeida I very much appreciate your keen eye in finding that. I was going crazy - it was a simple copy-paste when trying different options for the sync and I missed changing those two characters. Much appreciated, it solved this issue indeed. And I also provided a nice script if anyone wants to build it out for their needs 🙂  Future readers - change line 34 to be fc instead of fds