Update map layer's metadata using arcpy

820
3
04-17-2022 12:47 PM
Labels (1)
julian_svcs
Occasional Contributor

I am looking to bulk update metadata for each layer in my ArcGIS Pro's map. When I try to update it, it is not saved. I have tried two methods (see below code examples) but none work. I might be missing a step somewhere to save the metadata. Any help will be appreciated. Thanks.

Option 1:

import arcpy
import json
from arcpy import metadata as md

aprx = arcpy.mp.ArcGISProject(r"CURRENT")
m = aprx.listMaps(aprx.activeMap.name)[0]
metadata_json_orig = {}
metadata_json_new = {}
for lyr in m.listLayers():
    if lyr.isFeatureLayer:
        metadata_json_orig[lyr.metadata.title] = {"summary": lyr.metadata.summary, "description": lyr.metadata.description, "tags": lyr.metadata.tags, "credits": lyr.metadata.credits, "access_constraints": lyr.metadata.accessConstraints}
        item_md = md.Metadata(lyr)
        item_md.title = lyr.name
        item_md.summary = "My new layer's summary here"
        item_md.description = "My new layer's summary here"
        item_md.tags = "new,tag,list,here"
        item_md.credits = "Credit to me"
        item_md.accessConstraints = "You may use this layer"
        item_md.save()

        metadata_json_new[lyr.metadata.title] = {"summary": lyr.metadata.summary, "description": lyr.metadata.description, "tags": lyr.metadata.tags, "credits": lyr.metadata.credits, "access_constraints": lyr.metadata.accessConstraints}
print(f"{json.dumps(metadata_json_orig, indent=2)}\n#################################################\n")
print(f"{json.dumps(metadata_json_new, indent=2)}\n#################################################\n")

 

Option 2:

import arcpy
import json

aprx = arcpy.mp.ArcGISProject(r"CURRENT")
m = aprx.listMaps(aprx.activeMap.name)[0]
metadata_json_orig = {}
metadata_json_new = {}
for lyr in m.listLayers():
    if lyr.isFeatureLayer:
        metadata_json_orig[lyr.metadata.title] = {"summary": lyr.metadata.summary, "description": lyr.metadata.description, "tags": lyr.metadata.tags, "credits": lyr.metadata.credits, "access_constraints": lyr.metadata.accessConstraints}
        lyr.metadata.title = lyr.name
        lyr.metadata.summary = "My new layer's summary here"
        lyr.metadata.description = "My new layer's summary here"
        lyr.metadata.tags = "new,tag,list,here"
        lyr.metadata.credits = "Credit to me"
        lyr.metadata.accessConstraints = "You may use this layer"

        metadata_json_new[lyr.metadata.title] = {"summary": lyr.metadata.summary, "description": lyr.metadata.description, "tags": lyr.metadata.tags, "credits": lyr.metadata.credits, "access_constraints": lyr.metadata.accessConstraints}
print(f"{json.dumps(metadata_json_orig, indent=2)}\n#################################################\n")
print(f"{json.dumps(metadata_json_new, indent=2)}\n#################################################\n")

Thanks

0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor

Did you check to see if it .... isReadOnly ?

Metadata—ArcGIS Pro | Documentation


... sort of retired...
0 Kudos
julian_svcs
Occasional Contributor

isReadOnly is False for all my layers in my map.

 

0 Kudos
julian_svcs
Occasional Contributor

Hi @DanPatterson.

I had to have a look at this again and I think I found the issue.

It seems as though the geodatabase that my layer's metadata is being updated must be in the geodatabase that is set as default. I can update the metadata for the layer when it is in the default geodatabase. As soon as I set the default geodatabase to another geodatabase, it shows that the metadata's isReadOnly is False, but it does not update the metadata. This is the same if the geodatabase is a file or enterprise geodatabase.

I am not able to update the metadata of a query layer though. I set the default geodatabase to the enterprise geodatabase where my feature classes are being read from. This does show the metadata's isReadOnly is True for this query layer in the Pro map.

I would have thought that if I am updating the metadata for a layer in my Pro map it would not matter what the data source's workspace is as it is "attached" to the layer in the Pro map and not the feature class.

0 Kudos