I have a shapefile with an xml metadata file. Most of the elements are pre-populated using the previous editions xml file. However, I am trying to automatically update the current edition within a larger script.
I cannot update my Python library with any new modules (for example the metadata module seen in other questions) as I am using a work computer which is locked down and cannot install anything.
The three elements that need updating are the title, the revision date and the edition date.
I have managed to update these using the tree and an index to find the tags.
However, how do I search for the metadata element using the tag title? So for example, the the edition date tag is "redEdDate".
In addition, will the metadata tree index ever change if/when additional information is added? Or do the trees have a set format, therefore the same indexes. Im worried that any update to ArcGIS metadata, will affect the tree, and therefore the index for the specific tags mentioned above, and cause error within the script.
The issue above is shown via the script below. Both datasets use the ArcGIS metadata format. However the same tags have different indexes. For example, the MCMS Polygon dataset uses an index of "root[4][0][0]" for the edition date. However the MCMS Exclusion Zone edition date has an index of "root 3[0][0]".
My script so far is:
import arcpy, os, sys, datetime
import xml
import xml.etree.ElementTree as ET
ws = arcpy.env.workspace = r"path/to/folder"
today = datetime.date.today()
date = today.strftime("%Y%m%d")
#Update the MCMS polygon metadata titles, update date and edition date
for f in os.listdir(ws):
#Find the polygon xml file
if f.endswith("A.shp.xml"):
fpath = os.path.join(ws, f)
#Identify the metadata tree
tree = ET.parse(fpath)
root = tree.getroot()
#Set the title and date variables to the relevant metadata tag index
editiondate = root[4][0][0]
reviseddate = root[4][0][5][0]
title = root[4][0][7]
#Update the tags with the new data
editiondate.text = today.strftime("%Y-%m-%d")
reviseddate.text = today.strftime("%Y-%m-%d") + "T00:00:00"
title.text = "MCMS (polygon)"
#Write the updates to the xml file
tree.write(fpath)
#Update the MCMS exclusion metadata titles, update date and edition date
for f in os.listdir(ws):
#Find the exclusion zone xml file
if f.endswith("Zones.shp.xml"):
fpath = os.path.join(ws, f)
#Identify the metadata tree
tree = ET.parse(fpath)
root = tree.getroot()
#Set the title and date variables to the relevant metadata tag index
editiondate = root[3][0][0]
reviseddate = root[3][0][5][0]
title = root[3][0][7]
#Update the tags with the new data
editiondate.text = today.strftime("%Y-%m-%d")
reviseddate.text = today.strftime("%Y-%m-%d") + "T00:00:00"
title.text = "MCMS Exclusion Zones"
#Write the updates to the xml file
tree.write(fpath)
Best Answer
I have some comments/suggestions for you. You mention that you do not have any admin privileges on your machine to be able to install the metadata module. Good news here, you don't need admin to install python packages/modules. You do if you're using a binary install, however, most modules can be installed using pip. You can just download pip and put it in your
C:\Python27\ArcGIS10.x\Scripts
folder.You can also just download python packages and just place the modules somewhere in your
PYTHONPATH
such asC:\Python27\ArcGIS10.x\Lib\site-packages
. The flaws with this is you could be missing some dependencies, but that is where pip will be the better option as it should install all dependencies as well.However, with all that being said, I have never used the metadata module, but I believe the builtin xml module will do everything you need. I actually built a wrapper a while back that has convenience methods for working with
xml
files (see below). You can try this to see if it helps.As for hardcoding indices in your script for the metadata, I would avoid doing this. I am not certain if ArcGIS will add future elements to the metadata, but if anything does get added/deleted, it could definitely mess up the indices in your current structure. It is best to get at the elements by name. You can use the
xml.etree.ElementTree.Element.find()
orxml.etree.ElementTree.Element.findall()
methods to accomplish this.Here is the wrapper I built for working with
xml
files:To use this, save it in your
C:\Python27\ArcGIS10.x\Lib\site-packages
(or better yet a network share where it can be imported from) as something likexmlhelper.py
. To do some of the above stuff, you can do something like the following:To test to make sure it is working, I would make a copy of the data on your desktop first to try this out on that. If it works, then you can run it against your production data. The code above is untested.