TransWikia.com

AttributeError: 'Layer' object has no attribute 'metadata' from ArcPy with ArcGIS Pro

Geographic Information Systems Asked on August 18, 2021

I am trying to get the metadata for each layer within each map within a ArcGIS Pro project (.aprx file), but get this error message: AttributeError: 'Layer' object has no attribute 'metadata'. The code is simple:

import arcpy
project = arcpy.mp.ArcGISProject(r"pathtoproject.aprx")
maps = project.listMaps()
for map in maps:
    print(map.name)
    layers = map.listLayers()
    for layer in layers:
        print("  " + layer.name)
        print("    " + layer.metadata.description)

I did not really see how this is different with the 4th code block at https://pro.arcgis.com/en/pro-app/arcpy/metadata/metadata-class.htm.

I am using ArcGIS Pro 2.4.

2 Answers

I think you are trying to use a property that only became available in versions later than ArcGIS Pro 2.4.

When I run the code below:

import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:tempProjectsTestProjectTestProject.aprx")
mapx = aprx.listMaps()[0]
print(mapx.name)
lyr = mapx.listLayers()[0]
print(lyr.name)
print(lyr.metadata.description)

it reports:

Python 3.6.10 |Anaconda, Inc.| (default, May 20 2020, 01:49:13) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
========================== RESTART: C:temptest.py ==========================
Map
TestPoly
None
>>> 

In this instance I am not sure why metadata.description is reporting as None because I have given that layer (and its underlying feature class) what I believe are valid values. In any event, my version (which is later than ArcGIS Pro 2.4) does recognise the metadata property of the layer which your version 2.4 does not.

When I run this code in the Python window of the same project I do see the expected output:

aprx = arcpy.mp.ArcGISProject("CURRENT")
layer = aprx.listMaps()[0].listLayers()[0]
layer_metadata = layer.metadata
print(layer_metadata.title)
print(layer_metadata.description)
TestPoly
<DIV STYLE="text-align:Left;"><DIV><DIV><P><SPAN STYLE="font-size:12pt">This is my test description that I added from the Catalog pane by right-clicking on the feature class and choosing Edit Metadata.  I then copied the metadata from the feature class to a layer created from it using the Metadata tab of the Layer Properties.</SPAN></P></DIV></DIV></DIV>

I reported the above to Esri, and they said:

It is currently a known issue and the metadata team is working on a fix.

Correct answer by PolyGeo on August 18, 2021

I believe the issue is to do with which metadata you are accessing. Study your code you are accessing the metadata from what? A layer, but how did you get a handle on that...from the map document. You are not accessing the metadata of the dataset but a layer object in a map.

If you go to the properties of the layer and change the dialog to layer has own metadata, then you code will run without error.

Layer properties dialog

This confusion in what metadata you are actually looking at is a downside of ArcPro. In ArcMap this layer information was simply referred to as the layer description and is a property of a layer object not a dataset object. In ArcPro they have confused things by referring to this layer property as metadata which in the classic sense is something associated with the base dataset.

Answered by Hornbydd on August 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP