Home

Awesome

DEVELOPMENT HAS MOVED

Future development of this library will now be at https://github.com/Office-of-Digital-Services/arcpy_metadata, where the library is being maintained.

This repository will be wound down in the future.

Arcpy Metadata Editor (arcpy_metadata)

Whether you create it or not, metadata is a critical part of GIS analysis. ArcGIS includes a built-in GUI metadata editor, but has scant access to metadata properties from Python. The arcpy_metadata package provides this access, allowing large Python packages that generate their own geospatial outputs in ArcGIS to properly document the data.

Code Issues

Getting arcpy_metadata

arcpy_metadata is pure Python and its only dependency is arcpy (installed with ArcGIS). It's available on the Python Package Index so you can get arcpy_metadata via pip (pip install arcpy_metadata).

If you don't have or don't know how to use pip, you can install arcpy_metadata by cloning/downloading this repository and running setup.py install in the root folder

Using arcpy_metadata

Creating the Metadata Editor

Edit existing metadata for Shapefiles, Rasters, FeatureClasses, RasterDatasets, MosaicDatasets or Layers

import arcpy_metadata as md
metadata = md.MetadataEditor(path_to_some_feature_class)  # currently supports Shapefiles, FeatureClasses, RasterDatasets and Layers

Edit or create an XML file directly

import arcpy_metadata as md
metadata = md.MetadataEditor(metadata_file="path/to/metadata_file.xml")  # currently supports Shapefiles, FeatureClasses, RasterDatasets and Layers

Choose your log level

metadata = md.MetadataEditor(path_to_some_feature_class, loglevel="DEBUG")  # use any of CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET, dafault is INFO

Get text items (returns string)

title = metadata.title
abstract = metadata.abstract

Change text items

metadata.title = "The new title"
metadata.abstract = "This is the abstract"

Get list items (returns list)

tags = metadata.tags
for tag in tags:
    print tag

Change list items

metadata.tags = ["tag1", "tag2"]
metadata.tags[1] = "another tag"
metadata.tags.append("new tag")
metadata.tags.insert(0, "first tag")
metadata.tags.remove("tag1")
metadata.tags.pop()

Get numeric items (return int or float)

min_scale = metadata.min_scale
max_scale = metadata.max_scale

Change numeric items

metadata.min_scale = 500000
metadata.max_scale = 500

Get date items (returns date object)

last_update = metadata.last_update
last_update_year = metadata.last_update.year

Change date items (excepts datetime objects and formated string (yyyymmdd, yyyy-mm-ddThh:mm:ss)

from datetime import date
today = date.today()
metadata.last_update = today
metadata.last_update = "20160221"

Add and edit field definitions

metadata.fields.new()  # add the new field
metadata.fields[-1].name = "MyFieldName" # the item at index -1 will be the new one
metadata.fields[-1].definition = "Here I am describing how the field was created and how to use and interpret its values for a reader"

# or find an existing field and update its definition
search_for_field = "OBJECTID"
for field in metadata.fields:
    if field.name == search_for_field:
        field.definition = "Some updated information about the field defintiion"
        break  # not necessary, but faster as it stops searching once you've found the field

Get contact items (returns contact object)

contact = metadata.point_of_contact
contact_name = metadata.point_of_contact.contact_name
contact_email = metadata.point_of_contact.email

Change contact items (all contact items are string)

metadata.point_of_contact.contact_name = "First and Last Name"
metadata.point_of_contact.email = "email@address.com"

Edited nested lists

# make sure you have the right number of elements
# add new ones
while metadata.online_resource < 3:
    metadata.online_resource.new()
# or delete spare once
while metadata.online_resource > 3:
	metadata.online_resource.pop()
	
metadata.online_resource[0].name = "First download link"
metadata.online_resource[0].link = "http://somelink"
metadata.online_resource[0].function = "download"
metadata.online_resource[1].name = "Second download link"
metadata.online_resource[1].link = "http://someotherlink"
metadata.online_resource[1].function = "download"
metadata.online_resource[2].name = "Third download link"
metadata.online_resource[2].link = "http://yetanotherdownloadlink"
metadata.online_resource[2].function = "download"

Remove all items from the geoprocessing history

metadata.rm_gp_history()

Saving the changes back to the file

metadata.save() # save the metadata back to file.
metadata.cleanup() # remove all temporary files.

or

metadata.finish()  # save() and cleanup() as one call

If you want to enable automatic updates of your metadata (feature classes only) call.

metadata.finish(True) 

Supported items

Item descriptionInternal nameTypeCatalog Edit ViewPath in ArcGIS XML file
TitletitleStringOverview/ Item Description/ TitledataIdInfo/idCitation/resTitle
AbstractabstractStringOverview/ Item Description/ DescriptiondataIdInfo/idAbs
PurposepurposeStringOverview/ Item Description/ SummerydataIdInfo/idPurp
TagstagsListOverview/ Item Description/ TagsdataIdInfo/searchKeys/keyword
Place Keywordsplace_keywordsListOverview/ Topics & Keywords/ Place KeyworddataIdInfo/placeKeys/keyword
Extent Descriptionextent_descriptionStringResource/ Extents/ Extent/ DescriptiondataIdInfo/dataExt/exDesc
Temporal Extent Descriptiontemporal_extent_descriptionStringdataIdInfo/dataExt/tempDesc
Temporal Extent Instancetemporal_extent_instanceDateResource/ Extents/ Temporal Instance Extent/ Instance DatedataIdInfo/dataExt/tempEle/exTemp/TM_Instant/tmPosition
Temporal Extent Start Datetemporal_extent_startDateResource/ Extents/ Temporal Period Extent/ Begin DatedataIdInfo/dataExt/tempEle/exTemp/TM_Period/tmBegin
Temporal Extent End Datetemporal_extent_endDateResource/ Extents/ Temporal Period Extent/ End DatedataIdInfo/dataExt/tempEle/exTemp/TM_Period/tmEnd
Minimum Scalemin_scaleIntegerItem Description/ Appropriate Scale Range/ Min ScaleEsri/scaleRange/minScale
Maximum Scalemax_scaleIntegerItem Description/ Appropriate Scale Range/ Max ScaleEsri/scaleRange/maxScale
Scale Resolutionscale_resolutionIntegerResource/ Details/ Scale ResolutiondataIdInfo/dataScale/equScale/rfDenom
Last Updatelast_updateDateOverview/ Citation/ Dates/ ReviseddataIdInfo/idCitation/date/reviseDate
Update Frequency Descriptionupdate_frequency_descriptionStringResource/ Maintenance/ Custom FrequencydataIdInfo/resMaint/usrDefFreq/duration
CreditscreditsStringOverview/ Item Description/ CreditsdataIdInfo/idCredit
CitationcitationStringOverview/ Citation/ Other DetailsdataIdInfo/idCitation/otherCitDet
LimitationlimitationStringOverview/ Item Description/ Use LimitationdataIdInfo/resConst/Consts/useLimit
Supplemental Informationsupplemental_informationStringResource/ Supplemental InformationdataIdInfo/suppInfo
SourcesourceStringResource/ Lineage/ Data Source/ Source DescriptiondqInfo/dataLineage/dataSource/srcDesc
Points of contactpoint_of_contactContactObjResource/ Details/ Points of Contact/ Contact/dataIdInfo/idPoC
Maintenance Contactsmaintenance_contactContactObjResource/ Maintenance/ Maintenance Contact/dataIdInfo/maintCont
Citation Contactscitation_contactContactObjOverview/ Citation Contact/ Contact/dataIdInfo/idCitation/citRespParty
LanguagelanguageStringResource/ Detail/ Languages/ LanguagedataIdInfo/dataLang
Metadata Languagemetadata_languageStringMetadata/ Detail/ LanguagedataIdInfo/mdLang
Alternate Titlealternate_titleStringOverview/Citation/Titles/Alternate TitledataIdInfo/idCitation/resAltTitle
Identifier Code (1)identifier_code1StringOverview/Citation/Identifier/CodedataIdInfo/idCitation/citId/identCode
Identifier Code (2)identifier_code2StringOverview/Citation/Identifier/Authority Citation/Identifier/CodedataIdInfo/idCitation/citId/identAuth/citId/identCode
Identifier Code (3)identifier_code3StringOverview/Citation/Identifier/Authority Citation/Identifier/Authority Citation/Identifier/CodedataIdInfo/idCitation/citId/identAuth/citId/identAuth/citId/identCode
Identifier Code (4)identifier_code4StringResource/Lineage/Data Source/Reference System/Authority Citation/Identifier/CodedqInfo/dataLineage/dataSource/srcRefSys/identAuth/citId/identCode
Metadata File Identifierfile_identifierStringMetadata/Details/File IdnetifiermdFileID
Dataset URIdataset_uriStringMetadata/Details/Dataset URIdataSetURI
Resource Labelresource_labelStringResource/Fields/Details/Labeleainfo/detailed/enttyp/enttypl
FormatformatStringResource/Distribution/Distribution Format/Format NamedistInfo/distFormat/formatName
FieldattrFieldObjResource/Fields/Entity and Attribute Information/Detailseainfo/detailed/attr

Field items

Item descriptionInternal nameTypeRelative path in ArcGIS XML file
Field NamenameString./attrlabl
Field DefinitiondefinitionString./attrdef

Contact items

Item descriptionInternal nameTypeRelative path in ArcGIS XML file
Contact Namecontact_nameString./rpIndName
PositionpositionString./rpPosName
OrganizationorganizationString./rpOrgName
EmailemailString./rpCntInfo/eMailAdd
AddressaddressString./rpCntInfo/cntAddress/delPoint
CitycityStringrpCntInfo/./cntAddress/City
StatestateStringrpCntInfo/./cntAddress/adminArea
ZipzipStringrpCntInfo/./cntAddress/postCode
CountrycountryString./rpCntInfo/cntAddress/country
Phone Nbphone_nbString./rpCntInfo/cntPhone/voiceNum
Fax Nbfax_nbString./rpCntInfo/cntPhone/faxNum
HourshoursString./rpCntInfo/cntHours
InstructionsinstructionsString./rpCntInfo/cntInstr
Website LinklinkString./rpCntInfo/cntOnlineRes/linkage
ProtocolprotocolString./rpCntInfo/cntOnlineRes/protocol
ProfileprofileString./rpCntInfo/cntOnlineRes/appProfile
Website Nameor_nameString./rpCntInfo/cntOnlineRes/orName
Website Descriptionor_descString./rpCntInfo/cntOnlineRes/orDesc

Online Resource Items

Item descriptionInternal nameTypeRelative path in ArcGIS XML file
LinklinkString./linkage
ProtocolprotocolString./protocol
ProfileprofileString./appProfile
NamenameString./orName
DescriptiondescriptionString./orDesc
FunctionfunctionString./orFunct/OnFunctCd

Don't see the item you need? Read more about how to extend arcpy_metadata to work with other metadata elements it doesn't yet handle in CONTRIBUTING.md.

Python and ArcGIS Support

arcpy_metadata version 1.x supports Python 3 and ArcGIS Pro only.

arcpy_metadata version 0.x runs on Python 2 and 3, which means it can, at a basic level, be used both with ArcMap and ArcGIS Pro. When 0.x was developed, ArcGIS Pro didn't yet have some of the metadata export functions that arcpy_metadata relies on though, so, as of version 0.5, you must specify a path to a metadata XML file, or use a dataset that already has its metadata in an accessible XML format (e.g. Shapefile), if you want to use that branch in ArcGIS Pro. Otherwise, for ArcGIS Pro, upgrade to version 1.x.

Under the hood

arcpy_metadata uses the strategy of exporting the metadata from the layer, then edits the xml export based on your method calls. When you're done, use finish() to save your data back to the source.

Known limitations

Does not yet support all metadata items. Work is in progress to support server-based dataset editing.

Legacy ArcMap Versions

arcpy_metadata version 0.x only works with 32-bit Python. We use arcpy.XSLTransform_conversion() to extract metadata from geodatabases. 64bit arcpy python bindings for background processing do not support tools inside the metadata conversion toolset. arcpy_metadata version 1.x for ArcGIS Pro does not have these limitations.

How to contribute or extend arcpy_metadata

Contributions are well come! Please fork and submit pull requests.

See CONTRIBUTING.md for more information on how to extend arcpy_metadata to new attributes

Acknowledgements

arcpy_metadata is maintained by Nick Santos at the California Department of Technology.

arcpy_metadata was initially a project of the UC Davis Center for Watershed Sciences and received significant contributions from the World Resources Institute. It was created as part of a larger project funded by the California Department of Fish and Wildlife Biogeographic Data Branch and further developed for Global Forest Watch. We thank our funders for their support and their commitment to high quality geospatial data.