Importing metadata to a layer file

4598
6
06-26-2015 10:16 AM
GrantHerbert
Occasional Contributor II

I have a script which creates a layer file for every layer in an MXD, and I would like to copy the metadata (especially description and tags) from the source into the layer file.

However it seems that importing metadata into a layer file using arcpy fails and nothing is visible in ArcCatalog. Even attempting to import it in ArcCatalog seems to fail, along with the metadata importing tools (Import Metadata and Metadata Importer) in the toolbox. However, if I export the layer file metadata to an XML file, I can clearly see under <dataIdInfo><idPurp> and <idAbs> etc the text that I want. For some reason this is not visible in ArcCatalog though.

I can get it to update if I edit the metadata first in Catalog (putting some text in the summary for example and saving it), and then use the Metadata Importer. This doesn't seem feasible in code, as the only way to modify the metadata is to export it, modify it and import it again, and that last step fails!

Anybody successfully managed this?

ArcCatalog 10.3, Win 7

Tags (3)
0 Kudos
6 Replies
GrantHerbert
Occasional Contributor II

Update: If I programatically export the layers metadata to XML, I get the xml file of the underlying data. Similarly, making a change to that XML file and importing it to the layer file will update the metadata of the underlying data instead.

forestknutsen1
MVP Regular Contributor

Hi Grant,

We have quite a bit of code for metadata processing. In part driven by misbehaving ArcGIS metadata tools. This one is a metadata exporter but we also have some import stuff and a batch exporter.

This code exports metadata based on a schema document for a feature class or table but will not work inside feature data sets. The XML that is generated from this I have found will import with the metadata importer tool as expected.

'''
Exports metadata based on a schema workspace document
3/3/2015
'''


import arcpy
import tempfile
import os
import xml.etree.ElementTree as ET
import codecs
import shutil


input_path = arcpy.GetParameterAsText(0)
output_path = arcpy.GetParameterAsText(1)
xml_file_out = 'out.xml'
temp_dir = tempfile.mkdtemp()
export_option = "SCHEMA_ONLY"
storage_type = "BINARY"
export_metadata = "METADATA"


arcpy.AddMessage('xml export starting.....')
arcpy.ExportXMLWorkspaceDocument_management(input_path, os.path.join(temp_dir, xml_file_out))


arcpy.AddMessage('parsing xml.....')
tree = ET.parse(os.path.join(temp_dir, xml_file_out))
root = tree.getroot()




def data_type_finder(de):
    de_type = str(de.attrib)
    index_start = de_type.find('esri:')
    de_type = de_type[index_start + 5:-2]
    return de_type




def catalog_path_finder(de):
    de_catalog_path = de.find('CatalogPath').text
    de_catalog_path = de_catalog_path[15:]
    index = de_catalog_path.find('/')
    if index != -1:
        sub_string_end = de_catalog_path[index + 4:]
        sub_string_start = de_catalog_path[:index]
        de_catalog_path = sub_string_start + "/" + sub_string_end


    de_catalog_path = de_catalog_path[3:]
    return de_catalog_path




def metadata_finder(de):
    de_metadata = de.find('Metadata')
    temp = de_metadata.find('XmlDoc')
    if temp is not None:
        metadata_temp = temp.text
    else:
        metadata_temp = ''
    return metadata_temp


arcpy.AddMessage('writing metadata xml.....')
for data_element in root.iter('DataElement'):


    data_element_type = data_type_finder(data_element)
    metadata = metadata_finder(data_element)
    catalog_path = catalog_path_finder(data_element)


    output_file = codecs.open(os.path.join(output_path, catalog_path + '.xml'), 'w', 'utf-8')
    output_file.write(metadata)
    output_file.close()


    break
shutil.rmtree(temp_dir)

Hope it helps a little.

GrantHerbert
Occasional Contributor II

Thanks for the reply, and an interesting approach there. I haven't had a chance to test it, but there is an ESRI bug affecting the metadata at present.

0 Kudos
GrantHerbert
Occasional Contributor II

Another update. There is a known bug that is affecting this:

NIM089163         Using the Import Metadata tool to import metadata from a feature class or shapefile into a layer file (.lyr) does not update unless the current metadata of the layer file is edited first.

This also seems to affect the MetadataImporter tool as well.

Workaround is the manual edit of the metadata in ArcCatalog. I have scripted automated importing of metadata from the data source, which works once the metadata has been manually edited.

MatthewCroswell
New Contributor

@Grant Herbert: I just had to do this. How has this not been fixed in the 3 years since your post???????

GrantHerbert
Occasional Contributor II

I ended up building a tool that creates a sidecar xml file that Arc reads as the metadata. It basically exports the source metadata to XML, then saves it to the layer file location with the layer file name. 

0 Kudos