Résumé
The Metadata class allows you to access an item's metadata, and export metadata to a standard metadata format. If you have appropriate privileges, the class allows you to modify an item's metadata, or import a standard-format metadata XML file's content to an item.
Syntaxe
Metadata ({uri})
Paramètre | Explication | Type de données |
uri | A URI that identifies the item whose metadata you want to update. | String |
When a URI is provided, the Metadata object returned will provide access to the metadata for the item identified by the URI. If metadata hasn't been created yet to describe the item, and you have sufficient privileges to create it, metadata will be created for the item and the contents of the new metadata document will be represented by the object returned. If the item doesn't support metadata, or if the item doesn't exist, an empty object is created; changes can't be saved back to the item.
When the Metadata object is created without providing a URI, an empty object is created. You can create or import metadata using the Metadata object, then save its content to a new XML file or to another item as its metadata.
You can also get a Metadata object from projects, maps, map layers, layouts, and reports and use the object to access and manage their metadata.
Create a Metadata object and define its content, then get a Metadata object for a feature class. Assign content from the new Metadata object to the feature class.
import arcpy
from arcpy import metadata as md
# Create a new Metadata object and add some content to it
new_md = md.Metadata()
new_md.title = 'My Title'
new_md.tags = 'Tag1, Tag2'
new_md.summary = 'My Summary'
new_md.description = 'My Description'
new_md.credits = 'My Credits'
new_md.accessConstraints = 'My Access Constraints'
# Assign the Metadata object's content to a target item
streets_path = r'C:\Data\LocalArea.gdb\Streets'
tgt_item_md = md.Metadata(streets_path)
if not tgt_item_md.isReadOnly:
tgt_item_md.copy(new_md)
tgt_item_md.save()
Get a Metadata object from a project.
import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\Projects\data_management.aprx")
aprx_metadata = aprx.metadata
print(aprx_metadata.title)
print(aprx_metadata.description)
Get a Metadata object from the current map, using the Python window.
import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
current_map = aprx.activeMap
map_metadata = current_map.metadata
print(map_metadata.title)
print(map_metadata.description)
Get a Metadata object from a map layer.
import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\Projects\data_management.aprx")
layer = aprx.listMaps()[0].listLayers()[0]
layer_metadata = layer.metadata
print(layer_metadata.title)
print(layer_metadata.description)
Propriétés
Propriété | Explication | Type de données |
accessConstraints (Lecture et écriture) | Documented limitations with respect to using the item. Derived from Use Limitations defined in the item's metadata. | String |
credits (Lecture et écriture) | Information that gives credit to the producer or provider of the item. Derived from Credits defined in the item's metadata. | String |
description (Lecture et écriture) | A description of the item. Derived from the Description (Abstract), or for geoprocessing tools the Summary (Abstract), defined in the item's metadata. This property may contain plain text, or it may contain a snippet of HTML. When the description is written in the ArcGIS Metadata editor in ArcGIS Desktop or ArcGIS Pro, or in ArcGIS Online or an Enterprise Portal, the description will include HTML tags. | String |
isReadOnly (Lecture seule) | Check if you can update metadata for the item whose metadata is represented by the Metadata object. If a new, empty Metadata object was created, the Metadata object is not yet associated with an item or an XML file on disk, but it will be writable, in that you can modify the contents of the Metadata object and then write it to disk or save the content to an item for which you have the appropriate privileges to edit metadata. | Boolean |
maxScale (Lecture seule) | The maximum map scale at which the item should draw. Derived from the maximum value in the Appropriate Scale Range defined in the item's metadata. | Double |
minScale (Lecture seule) | The minimum map scale at which the item should draw. Derived from the minimum value in the Appropriate Scale Range defined in the item's metadata. | Double |
summary (Lecture et écriture) | A summary describing the purpose of the item. Derived from the Summary (Purpose) defined in the item's metadata. | String |
tags (Lecture et écriture) | A set of tags that describe the item. Derived from Tags defined in the item's metadata. The value is a string that defines a comma-separated sequence of tags. | String |
thumbnailUri (Lecture et écriture) | A local or network path, or a URL, to a graphic file that describes and helps to identify the item. Derived from the Thumbnail that is extracted from the item's metadata. When setting this value, if a URL is provided, the identified file will be downloaded and incorporated into the item’s metadata. | String |
title (Lecture et écriture) | A descriptive title for the item. Derived from the Title in the item's metadata. | String |
xmax (Lecture seule) | The maximum value along the x-axis for the item’s minimum bounding rectangle. Derived from the East coordinate of the Bounding Box recorded in the item's metadata. | Double |
xmin (Lecture seule) | The minimum value along the x-axis for the item’s minimum bounding rectangle. Derived from the West coordinate of the Bounding Box recorded in the item's metadata. | Double |
xml (Lecture et écriture) | The item’s ArcGIS metadata XML document as a string. Not all items support metadata. When you are getting an item’s metadata, be sure to check for an empty string as the return value. | String |
ymax (Lecture seule) | The maximum value along the y-axis for the item’s minimum bounding rectangle. Derived from the North coordinate of the Bounding Box recorded in the item's metadata. | Double |
ymin (Lecture seule) | The minimum value along the y-axis for the item’s minimum bounding rectangle. Derived from the South coordinate of the Bounding Box recorded in the item's metadata. | Double |
Vue d’ensemble des méthodes
Méthode | Explication |
copy (inputMetadata) | Copy metadata content from the input Metadata object to the current Metadata object. For example, this supports copying metadata from one item to another item. |
deleteContent (metadata_delete_option) | Deletes the indicated content from a Metadata object that represents an item's metadata. |
importMetadata (sourceUri, {metadata_import_option}, {customStylesheetPath}) | Import content to the Metadata object from the item identified by the sourceURI. |
exportMetadata (outputPath, {metadata_export_option}, {metadata_removal_option}, {customStylesheetPath}) | Converts a Metadata object that represents an item's metadata to a standard-format metadata XML file. |
reload () | The Metadata object is reinitialized with the item's current metadata content. |
save () | Save updates made to the content in the Metadata object back to the original item from which the Metadata object was derived. |
saveAsUsingCustomXSLT (outputPath, customStylesheetPath) | Saves a copy of the item's ArcGIS metadata to a stand-alone metadata XML file using a custom XSLT stylesheet. |
saveAsXML (outputPath, {metadata_save_as_xml_option}) | Save a copy of the item's ArcGIS metadata to a stand-alone metadata XML file. |
synchronize (metadata_sync_option, interval) | Updates metadata content in the item with the current properties of the item and reloads that content into the Metadata object. |
upgrade (metadata_upgrade_option) | Upgrades the metadata of the current item. |
Méthodes
copy (inputMetadata)
Paramètre | Explication | Type de données |
inputMetadata | A Metadata object that is created and populated with metadata content in the ArcGIS metadata format. For example, this Metadata object can be derived from a source feature class and copied to the feature class associated with the current Metadata object. | Object |
An item's metadata must be stored in the ArcGIS metadata XML format. Only metadata content in the ArcGIS metadata format can be copied.
Copy metadata from a metadata template to a map in the current project.
import arcpy
from arcpy import metadata as md
# Get metadata content from a metadata template XML file
template_path = r'C:\Metadata\ArcGIS_metadata_template.xml'
src_item_md = md.Metadata(template_path)
# Copy the template's content to a map in the project
current_aprx = arcpy.mp.ArcGISProject('CURRENT')
map0 = current_aprx.listMaps()[0]
map0_md = map0.metadata
map0_md.copy(src_item_md)
map0_md.save()
current_aprx.save()
deleteContent (metadata_delete_option)
Paramètre | Explication | Type de données |
metadata_delete_option | Specifies the content that will be deleted from the Metadata object. To make the change permanent, use the save method.
| String |
Metadata documents are not typically very large even when they include robust text describing the item that is fully compliant with a geospatial metadata standard. Documents typically range from 10 to 100 KB in size. Thumbnails are recommended to help describe the item and support using the item and its metadata throughout ArcGIS software including ArcGIS Online. Depending on how the thumbnail is created, documents with full metadata and a thumbnail typically average 250 KB in size.
With ArcCatalog, it was possible to base64 encode files up to 5 MB in size and store them within an item's metadata in the same manner that files can be attached to an email; this can't be accomplished using ArcGIS Pro. Enclosing files in an item's metadata is not recommended. This practice will further increase the size of an item's metadata document and can affect database performance. Files containing important information should be stored independently and referenced by the item's metadata.
When geoprocessing history is recorded in an item's metadata, and geoprocessing tools are constantly used to analyze, evaluate, and manage data as with utility networks and parcel datasets, over time the geoprocessing history can grow to be very large in size. Metadata documents containing years of geoprocessing history have been known to be 2 GB in size. When all the items in an enterprise geodatabase contain metadata documents of this size, performance of the database as a whole is affected. To optimize performance, you may want to delete the geoprocessing history from the metadata of every item in the database.
The options provided with this method allow you to remove unwanted content from an item's metadata and reduce the overall size of a metadata document as much as possible.
Delete content from an item's metadata.
import arcpy
from arcpy import metadata as md
# Get the target item's Metadata object
poles_path = r'C:\Data\LocalArea.gdb\UtilityPoles'
tgt_item_md = md.Metadata(poles_path)
# Delete all geoprocessing history and any enclosed files from the item's metadata
if not tgt_item_md.isReadOnly:
tgt_item_md.deleteContent('GPHISTORY')
tgt_item_md.deleteContent('ENCLOSED_FILES')
tgt_item_md.save()
importMetadata (sourceUri, {metadata_import_option}, {customStylesheetPath})
Paramètre | Explication | Type de données |
sourceUri | A URI that identifies an item from which metadata will be imported to the Metadata object. | String |
metadata_import_option | This option determines how content is imported to the Metadata object from the source item. This value indicates the XML format of metadata stored in the source item. The source item's content will be converted from the indicated format to the ArcGIS metadata XML format before being saved to the Metadata object.
(La valeur par défaut est DEFAULT) | String |
customStylesheetPath | A local or network file path, or a URL, identifying the custom XSLT stylesheet that will be used to import metadata from a custom XML format. The value in this parameter will only be considered if the metadata_export_option is CUSTOM. If a URL, the referenced file will be downloaded, and the downloaded file will be used. | String |
An item's metadata must be stored in the ArcGIS metadata XML format. The imported XML file will be converted from the standard metadata XML format to the ArcGIS metadata format using the conversion identified by the metadata_import_option. The resulting content is stored in the Metadata object. Many of the item's properties, its thumbnail, and geoprocessing history will remain. However, any metadata content that was previously typed using a metadata editor will be removed and replaced by the imported metadata content.
If the format of the stand-alone metadata file has an XML format that does not match the format specified by the metadata_import_option, the wrong conversion will be performed and there will be no metadata content to import to the item. However, the item's existing typed metadata content will still be removed as part of the process.
Import metadata content from a standard-format XML file, when you are not sure of the file's format. The DEFAULT import option detects the file's format and converts it to the ArcGIS metadata format.
import arcpy
from arcpy import metadata as md
# Set the standard-format metadata XML file's path
src_file_path = r'C:\Metadata\trees_metadata.xml'
# Get the target item's Metadata object
trees_path = r'C:\Data\LocalArea.gdb\Trees'
tgt_item_md = md.Metadata(trees_path)
# Import the standard-format metadata content to the target item
if not tgt_item_md.isReadOnly:
tgt_item_md.importMetadata(src_file_path)
tgt_item_md.save()
Import metadata content from an FGDC CSDGM-format XML file to a target item.
import arcpy
from arcpy import metadata as md
# Get the standard-format metadata XML file's object
src_csdgm_path = r'C:\Metadata\parks_csdgm.xml'
# Get the target item's Metadata object
parks_path = r'C:\Data\LocalArea.gdb\Parks'
tgt_item_md = md.Metadata(parks_path)
# Import the standard-format metadata content to the target item
if not tgt_item_md.isReadOnly:
tgt_item_md.importMetadata(src_csdgm_path, 'FGDC_CSDGM')
tgt_item_md.save()
exportMetadata (outputPath, {metadata_export_option}, {metadata_removal_option}, {customStylesheetPath})
Paramètre | Explication | Type de données |
outputPath | The output XML file. | String |
metadata_export_option | Specifies how the item's metadata is exported. The value indicates what XML format will be used by the output XML file. The item's ArcGIS metadata content will be converted to the appropriate XML format.
(La valeur par défaut est ISO19139) | String |
metadata_removal_option | Specifies whether sensitive information is removed from the item's metadata as part of the export process. The value indicates what content is filtered out. The remaining metadata content is exported.
(La valeur par défaut est REMOVE_ALL_SENSITIVE_INFO) | String |
customStylesheetPath | A local or network file path, or a URL, identifying the custom XSLT stylesheet that will be used to export metadata to a custom XML format. The value in this parameter will only be considered if the metadata_export_option is CUSTOM. If a URL, the referenced file will be downloaded, and the downloaded file will be used. | String |
An item’s metadata is stored in the ArcGIS metadata XML format. The item's metadata will be converted to a standards-compliant metadata XML format as identified by the metadata_export_option parameter. The resulting XML document will be saved to an XML file.
Before the item's metadata is converted, you can choose to filter an item's ArcGIS metadata content or not. For example, if you plan to share the exported metadata document with people outside your organization, it is a good idea to exclude any information that may be considered sensitive to your organization. The amount of content that is filtered is determined by the metadata_removal_option parameter.
Before the export process begins, you may want to synchronize the item's metadata so it contains the most up-to-date properties of the data.
Synchronize an item's properties, and export the most current metadata content. Export the item's metadata to a standard metadata XML format to share this information outside the ArcGIS platform.
import arcpy
from arcpy import metadata as md
# Get the source item's Metadata object
streets_path = r'C:\Data\LocalArea.gdb\Streets'
src_item_md = md.Metadata(streets_path)
# Synchronize the item's metadata now
src_item_md.synchronize('ALWAYS')
# Export the item's metadata using the default export format (ISO 19139)
# after removing sensitive information from the metadata, if it exists.
export_19139_path = r'C:\MetadataExport\streets_19139.xml'
src_item_md.exportMetadata(export_19139_path,
metadata_removal_option='REMOVE_ALL_SENSITIVE_INFO')
# Export the item's metadata to the FGDC CSDGM XML format as well
export_csdgm_path = r'C:\MetadataExport\streets_csdgm.xml'
src_item_md.exportMetadata(export_csdgm_path, 'FGDC_CSDGM',
'REMOVE_ALL_SENSITIVE_INFO')
# Export the metadata using the specified custom XSLT stylesheet, for
# example, to generate a custom HTML format for display outside ArcGIS
export_custom_path = r'C:\MetadataExport\streets_custom.html'
custom_html_export_path = r'C:\MetadataExport\custom_html_output.xslt'
src_item_md.exportMetadata(export_custom_path, 'CUSTOM',
'REMOVE_ALL_SENSITIVE_INFO',
custom_html_export_path)
reload ()
The Metadata object is initialized with an item's metadata. In the process of your work, it is possible that the item's metadata content could be modified by an external process. This function allows you to reload the item's current metadata content into the Metadata object. For example, if you save changes to an item's metadata using the ArcGIS Pro metadata editor, that change would not be immediately reflected in the Metadata object you are working with in the Python window. Use this function to reinitialize the Metadata object before continuing with your work.
Reload the item's metadata from the source to see the latest changes. This script is designed to work in the Python window.
import arcpy
from arcpy import metadata as md
# Get a map layer's Metadata object
aprx = arcpy.mp.ArcGISProject("CURRENT")
currentMap = aprx.activeMap
first_lyr = currentMap.listLayers()[0]
lyr_metadata = first_lyr.metadata
# Print the original set of tags
lyr_tags_orig = lyr_metadata.tags
if lyr_tags_orig is not None:
print('Original tags: ' + lyr_tags_orig)
else:
print('Original tags: None')
# Update tags in the item's metadata, but don't save the changes
lyr_metadata.tags = 'Apple, Banana, Cherry'
# Print the updated set of tags
print('Updated tags: ' + lyr_metadata.tags)
# Reloading metadata lets you access updates made externally,
# but you will lose any unsaved changes
lyr_metadata.reload()
# Print the reloaded set of tags
lyr_tags_reload = lyr_metadata.tags
if lyr_tags_reload is not None:
print('Reloaded tags: ' + lyr_tags_reload)
else:
print('Reloaded tags: None')
save ()
As you make changes to the metadata content stored in the Metadata object, those changes are not automatically saved back to the item from which the Metadata object was derived. Changes will only be saved back to the item when the save function is used. You must have the appropriate privileges to update the original item's metadata.
For example, if the Metadata object represents the metadata for a map in the current project, this function will update the map's metadata. For items stored in a project, metadata changes aren't truly saved until the project is saved; if the project is closed without saving, the changes you made to the map's metadata will be lost.
If a new, empty Metadata object was created and you add content to this new metadata document, this function will fail with an error message since it is not associated with an existing item. Assign the Metadata object to an item to update its metadata with the content defined in the Metadata object.
Update the tags in an item's metadata. You must have the appropriate privileges to update an item's metadata.
import arcpy
from arcpy import metadata as md
# Get the target item's Metadata object
hydrants_path = r'C:\Data\LocalArea.gdb\Hydrants'
item_md = md.Metadata(hydrants_path)
# Save the changes if you have privileges in the geodatabase to do so
if not item_md.isReadOnly:
# Update the tags in the item's metadata, then save the changes
item_md.tags = 'One, Two, Three'
item_md.save()
saveAsUsingCustomXSLT (outputPath, customStylesheetPath)
Paramètre | Explication | Type de données |
outputPath | The output XML file. | String |
customStylesheetPath | A local or network file path, or a URL, identifying the custom XSLT stylesheet that will be used to transform the item's ArcGIS metadata. If a URL, the referenced file will be downloaded, and the downloaded file will be used. | String |
Use a custom XSLT stylesheet to modify an item's ArcGIS metadata and save the result to an XML file. The updated metadata content is then saved back to the original item. The custom XSLT stylesheet used in this example is one that was provided with ArcMap to remove an item's geoprocessing history from its metadata, and which will continue to work with ArcGIS Pro.
import arcpy
from arcpy import metadata as md
# Get the item's Metadata object
street_lights_path = r'C:\Data\LocalArea.gdb\StreetLights'
src_item_md = md.Metadata(street_lights_path)
# Save the item's metadata to an xml file using a custom XSLT
target_file_path = r'C:\MetadataInternal\street_lights_no_gp_history.xml'
custom_xslt_path = r'C:\MetadataInternal\remove geoprocessing history.xslt'
src_item_md.saveAsUsingCustomXSLT(target_file_path, custom_xslt_path)
# Save the modified ArcGIS metadata output back to the source item
tgt_item_md = md.Metadata(target_file_path)
src_item_md.copy(tgt_item_md)
src_item_md.save()
saveAsXML (outputPath, {metadata_save_as_xml_option})
Paramètre | Explication | Type de données |
outputPath | The output XML file. The output file will contain metadata stored in the ArcGIS Metadata XML format. | String |
metadata_save_as_xml_option | Specifies whether information is removed from the item's metadata as part of the process of saving the item's metadata to a stand-alone metadata XML file. The value indicates the amount and type of content that is filtered out of the item's metadata.
(La valeur par défaut est EXACT_COPY) | String |
Save two copies of an item's ArcGIS metadata to disk. One copy exactly matches the metadata that is stored with the item, and the other file's metadata content has been filtered to remove sensitive content.
import arcpy
from arcpy import metadata as md
# Get the item's Metadata object
street_lights_path = r'C:\Data\LocalArea.gdb\StreetLights'
src_item_md = md.Metadata(street_lights_path)
# Save a copy of the item's metadata to an xml file as a backup.
# This copy is for internal use only.
target_copy_path = r'C:\MetadataInternal\street_lights_copy.xml'
src_item_md.saveAsXML(target_copy_path)
# Save a version of the item's metadata without sensitive information
# separately so it can be shared externally.
target_filtered_path = r'C:\MetadataExternal\street_lights_no_sensitive.xml'
src_item_md.saveAsXML(target_filtered_path, 'REMOVE_ALL_SENSITIVE_INFO')
synchronize (metadata_sync_option, interval)
Paramètre | Explication | Type de données |
metadata_sync_option | Specifies how and which elements in the metadata are updated with current properties of the data. Some metadata elements should always be derived from the item's properties, such as its spatial reference. Some metadata elements can be initialized by the properties of the item, but would typically be updated by a person to better characterize the item before the metadata is shared, such the title of the item. When a metadata element is created and its value is initialized by synchronization, the XML element in which the value is stored has a Sync attribute, and its value is set to True. Typically, a metadata element's value will only be synchronized when the Sync attribute is set to True. When the element's value is updated using the ArcGIS metadata editor, the Sync attribute is removed. Alternatively, an application could change the attribute's value to False to achieve the same result.
| String |
interval | The number of seconds that should elapse before the item is synchronized again. For example, if the Metadata object represents a feature class in a geodatabase and another user has recently synchronized the item's metadata, this function will not synchronize the item's metadata content again unless sufficient time has passed. Providing a value of zero will cause the item's metadata to be synchronized again immediately. The interval is only considered when the metadata_sync_option value is ACCESSED, CREATED, and NOT_CREATED. | Integer |
If the Metadata object represents a feature class's metadata, this function will update the metadata content to reflect current properties of the data, such as the number of features it has, its spatial reference, the current list of attributes, and so on. If the item doesn't have any metadata content, it may or may not be created for the item, depending on the metadata_sync_option. Also, the interval time is considered, depending on the metadata_sync_option.
Some metadata elements should always be derived from the item's properties, such as its spatial reference. Some metadata elements can be initialized by the properties of the item, but would typically be updated by a person to better characterize the item before the metadata is shared, such the title of the item. The metadata_sync_option determines which properties of the item are updated, and how metadata elements are handled if a person has typed a value in that element.
Import metadata content from an ISO 19139-format XML file to a feature class, and update the properties of the feature class that are recorded in the metadata so they match the item's current properties.
import arcpy
from arcpy import metadata as md
# Get the standard-format metadata XML file's object
src_19139_path = r'C:\Metadata\street_lights_19139.xml'
# Get the target item's Metadata object
street_lights_path = r'C:\Data\LocalArea.gdb\StreetLights'
tgt_item_md = md.Metadata(street_lights_path)
if not tgt_item_md.isReadOnly:
# Import standard-format metadata content to the target item
tgt_item_md.importMetadata(src_19139_path, 'ISO19139_UNKNOWN')
tgt_item_md.save()
# Use SELECTIVE so you don't lose the imported title
# other properties will update on synchronize as data changes
tgt_item_md.synchronize('SELECTIVE')
upgrade (metadata_upgrade_option)
Paramètre | Explication | Type de données |
metadata_upgrade_option | Specifies how the item's metadata will be upgraded to the ArcGIS metadata format. This option is provided to update metadata that was created with ArcGIS Desktop 8.x or 9.x.
| String |
A Metadata document that was created with ArcGIS Desktop 8.x or 9.x can be upgraded to the ArcGIS metadata format.
Upgrade an archived item's ArcGIS Desktop 9.x metadata content to the ArcGIS metadata format.
import arcpy
from arcpy import metadata as md
# Get the item's Metadata object
archive_item_path = r'C:\Data\Old.gdb\Buildings'
archive_item_md = md.Metadata(archive_item_path)
# Upgrade the item's metadata content to the ArcGIS metadata format
archive_item_md.upgrade('FGDC_CSDGM')
Exemple de code
Access information that describes an item from its metadata.
import arcpy
from arcpy import metadata as md
highways_path = r'C:\Data\LocalArea.gdb\Highways'
item_md = md.Metadata(highways_path)
print("Title:", item_md.title)
print("Tags:", item_md.tags)
print("Summary:", item_md.summary)
print("Description:", item_md.description)
print("Credits:", item_md.credits)
print("Access Constraints:", item_md.accessConstraints)
Vous avez un commentaire à formuler concernant cette rubrique ?