10

Has anyone succeeded in programmatically updating metadata in ArcGIS 10? Considering using Python/arcpy but ArcObjects (C# or Python/comtypes) is also a possibility.

I need to update both the FGDC and the ArcGIS-ISO format metadata, and whatever solution is used needs to be able to retain the existing (non-blank) elements along with the added elements, except where they are in conflict in which case the added elements overwrite the existing elements.

9

The easiest way to do this from ArcPy is to create an XML file using Python and then invoking Import Metadata (Conversion). However, this will overwrite everything.

An alternative is to use ArcObjects to obtain an IName to the dataset, cast to IMetadata, and edit the IPropertyset.

| improve this answer | |
11
+50

We had a big need for a similar capability and ended up building a general, free, open source Python library for the purpose. You can find it at https://github.com/ucd-cws/arcpy_metadata or by running a "pip install arcpy_metadata". There is some documentation of features and how to use it, with some additional contributions from the World Resources Institute. We tried to keep things relatively Pythonic so that it integrates well and can be learned quickly. Here's an example:

import arcpy_metadata as md
import datetime

metadata = md.MetadataEditor(path_to_some_feature_class)  # also has a feature_layer parameter if you're working with one, but edits get saved back to the source feature class
metadata.title = "The metadata title!"

generated_time = "This layer was generated on {0:s}".format(datetime.datetime.now().strftime("%m/%d/%Y %I:%M %p"))

metadata.purpose = "Layer represents locations of the rare Snipe."

metadata.abstract.append("generated by ___ software")
metadata.abstract.append(generated_time)  # .prepend also exists
metadata.tags.add(["foo", "bar", "baz"])  # tags.extend is equivalent to maintain list semantics

metadata.finish()  # save the metadata back to the original source feature class and cleanup. Without calling finish(), your edits are NOT saved!

It still has plenty that could be added, but is pretty extensible if you subclass the items that are already there, or configure them correctly. It's still about alpha quality software, but it works and we're happy with it.


For anyone looking for this capability within ArcGIS Pro, as of version 2.5, they now include a metadata API from Python. There are more details in the Metadata class documentation.

| improve this answer | |
9

We have been struggling with this and solved it by having a simple python library built around the arcobjects metadata functions. It's based on a xml-file where the metadata values have been replaced with keywords. You can use these keywords in your python code to get or set the value of a metadata field. Code woud look something like this:

import dataprocessing
DP = dataprocessing.create()

md = DP.OpenMetadata("C:/MyShapefile.shp") # Works also on geodatabases
myKeyword = "%FGDC.abstract" # Keywords have to start with '%' but the rest is up to you

if md.GetValue(myKeyword) == "Not what I want":
    md.SetValue(myKeyword, "New value that suits me better")

The tool can be found here: http://www.aris.nl/dataprocessing_arcgis and is freely available. The version on the website is for arcgis 9.3. An arcgis 10 version will be available but it will no longer be free.

(Full disclosure: I work for a dutch government agency where we had the current version of this tool built partly from taxpayers money and partly from time donated by the programmer. That's why it's available for free but not open source. As of 2013 we will not be funding any new versions. I'm not making any money out of it in any way. I just hope it gets put to more use.)

| improve this answer | |
1

A third option I'm looking into now is to use an XSLT file along with the XSLT Transformation (Conversion) tool. I'm pretty much having to learn the language as I go though, and they don't make it easy on you by using the .NET implementation which uses the older XSLT/XPath 1.0 standards.

| improve this answer | |
1

I have some code that uses ArcPy and DOM for conversion of existing MD, creation from scratch (if none exists) and update of certain elements using DOM. It's a bit clunky, with quite a lot of hardcoded pathnames and so on.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.