I maintain metadata as I always have….inconsistently and sporadically.. I know…booh to me. Anyways, I know python can pull all kinds of info out of mxd in V10. Has anybody come up with some scripts to populate basic stuff like layers and paths to mxd metadata? Seems like this should be pretty easy, but I would really like to know if there is any reason this won't work and if anybody has written anything along these lines to get me started in the right direction.
[GIS] ArcGIS metadata creation scripts
arcmapmetadatapython
Related Solutions
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.
ElementTree
does allow one to programatically edit XML metadata files. I've used this on a recent project to update many tags using information stored in a database. So basically for each shapefile I have a data description, citation, abstract, etc. stored in a table and access the tags using ElementTree
and retrieving the metadata using a search cursor. I'm not an expert on the structure of the ElementTree
library, but the long and short of it is that you create an "iterator" object which is the parent tag of the "subelement(s)" you want to edit. Say, for example you have the following tags and you want to change the publisher information:
<pubinfo>
<publish>U.S. Geological Survey, Reston, VA</publish>
</pubinfo>
You could write something like the following snippet in python:
import xml.etree.ElementTree as et
#--get xml file and parse it
root = et.parse(os.path.join(shpPath,xmlFile)).getroot()
#--feature description
iterator = root.getiterator('pubinfo')
for elem in iterator:
subelem = 'publish'
old_subelem = elem.find(subelem)
elem.remove(old_subelem)
new_subelem = et.SubElement(elem, subelem)
new_subelem.text = 'New Publisher, Anytown, USA'
Note that the iterator object will search the entire XML file looking for the tag "pubinfo". If multiple tags share the same name, the iterator object will contain one element for each occurrence. In this case you will have to dig through your XML files to make sure you are working on the correct one. Say there are 2 instances of the tag pubinfo
(one could be for the "local citation" and one could be for the "larger work citation") and you only want to change the subelement publish
in first one you would replace the for elem in iterator
with elem = iterator[0]
. If you need more I can provide you with some scripts I wrote, though I'll refrain from posting them in their entirety here.
Best Answer
It is definitely possible. The great thing about code is that you can do anything.
Why hasn't it been done? I dare say it's because Metadata is boring ;)
You could use the Arcgisscripting tools to extract the information that you want and save this information in an XML document with the same name as the data set. This would at least provide you with information like, Date Created, Date Modified, SRS/CRS/Projection, Location, etc. Most of which is available though the Python interface.
I would definitely have a look at XMl and XSLT as an implementation strategy. But if that is beyond your interest level then an CSV file would suffice. After all, any metadata (in a friendly format) is better than no Metadata at all.