Metadata and python

1188
3
Jump to solution
03-16-2012 10:28 AM
KarstenRank
Occasional Contributor III
Hello,

I'm trying to find a nice way to write the metadata in a feature class, when I create it with a python script from a text file.

Is this possible?

Thanks a lot

Karsten
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KimOllivier
Occasional Contributor III
I found that I could edit a template XML file fairly easily, rather than creating the whole file.
# DOCLoadMetadata.py # with altered dates for current month # using element tree # create original Metadata with same name as layer # run this to alter to name_et.xml # reload into filegeodatabase # Note there is no tool to unload metadata # 15 March 2010 # 2.6 upgrade for element tree 12 Nov 2011 # alter fc names 10 March 2012  import arcgisscripting,sys,os import xml.etree.ElementTree as ET import sys,os,datetime print print  def alter(xmlfile,edDate,publishDate,createDate) :     """     read xml file for featureclass or table     change dates to today,loading date and LINZ extract date     empty processing logs     write out file with _et suffix     return file name     """     # print xmlfile     tree = ET.parse(xmlfile)     ## print tree.getroot().tag, tree.getroot().text,tree.getroot().tail,tree.getroot().attrib      # Edition Date     elem = list(tree.getiterator("resEdDate"))[0]      # print elem.tag,elem.text     elem.text = edDate     ## print elem.text      # Reference Date 001 (Creation)     elem = list(tree.getiterator("refDate"))[0]      # print elem.tag,elem.text     elem.text = createDate     ## print elem.text     # note there may be two of these dates     # DateTypCd 001 and 002     # Reference Date 002 (Publication)     if len(list(tree.getiterator("refDate"))) > 1 :         elem = list(tree.getiterator("refDate"))[1]         # print elem.tag,elem.text         elem.text = publishDate     else :         # print "Skipping publication date",xmlfile         pass     ## print elem.text     # clear out lineag if it exists     try :         lin = list(tree.getiterator("lineage"))[0]         # print lin.tag         lin.clear()         lin.text = "Cleared"     except :         gp.AddMessage("Skipping clear lineage")     outfile = xmlfile.replace(".","_et.")     tree.write(outfile)     return outfile  # ---------------------- main ----------------------  try :     publishDate = sys.argv[1]     createDate  = sys.argv[2]     if createDate == '#' :         createDate = publishDate     gp.AddMessage(publishDate+type(publishDate)) except :     # 12 = 5 (sat) + 7 to keep positive in modulo 7     tday = datetime.datetime.now()     div,offset = divmod((12 - tday.replace(day=1).weekday()),7)     firstSat = tday.replace(day=1) + datetime.timedelta(days=offset)     publishDate = firstSat.strftime("%Y%m%d")     createDate  = publishDate     print publishDate,firstSat.ctime() print # override # publishDate = '20100804' # createDate = '20100710'  gp = arcgisscripting.create(9.3)  os.chdir("e:/crs/customer/conservation/metadata") edDate = str(datetime.datetime.now().date()).replace("-","") edDate = publishDate # '20100914' gp.AddMessage(edDate+" edit date") gp.AddMessage(publishDate+" publish date") gp.AddMessage(createDate+" create date")  ws = "e:/crs/customer/conservation/corax.gdb" metasrc = "e:/crs/customer/conservation/metadata" gp.Workspace = ws  os.chdir(metasrc) print print ws print metasrc print    lstFC = gp.ListFeatureClasses("*")  for fc in lstFC :     # print fc     fcxml = metasrc+"/"+fc+".xml"     if os.path.exists(fcxml) :         etxml = alter(fcxml,edDate,publishDate,createDate)          gp.MetadataImporter_conversion(etxml,fc)         print fc,"updated"         gp.AddMessage(fc+" updated")     else :         print fcxml,"not found"         gp.AddError(fcxml+" not found")          lstTab = gp.ListTables("*") for tab in lstTab :     # print tab     tabxml = metasrc+"/"+tab+".xml"     if os.path.exists(tabxml) :         etxml = alter(tabxml,edDate,publishDate,createDate)          gp.MetadataImporter_conversion(etxml,tab)         print tab,"updated"         gp.AddMessage(tab+" updated")     else :         print tabxml,"not found"         gp.AddError(tabxml+" not found") # geodatabase metadata etxml = alter(metasrc+"/corax.xml",edDate,publishDate,createDate)


Note that at 10.x there is now a tool to unload metadata and better tools to import other formats.
But this still works.

View solution in original post

0 Kudos
3 Replies
MathewCoyle
Frequent Contributor
You can take a look at the Metadata Toolset to get some ideas.
KimOllivier
Occasional Contributor III
I found that I could edit a template XML file fairly easily, rather than creating the whole file.
# DOCLoadMetadata.py # with altered dates for current month # using element tree # create original Metadata with same name as layer # run this to alter to name_et.xml # reload into filegeodatabase # Note there is no tool to unload metadata # 15 March 2010 # 2.6 upgrade for element tree 12 Nov 2011 # alter fc names 10 March 2012  import arcgisscripting,sys,os import xml.etree.ElementTree as ET import sys,os,datetime print print  def alter(xmlfile,edDate,publishDate,createDate) :     """     read xml file for featureclass or table     change dates to today,loading date and LINZ extract date     empty processing logs     write out file with _et suffix     return file name     """     # print xmlfile     tree = ET.parse(xmlfile)     ## print tree.getroot().tag, tree.getroot().text,tree.getroot().tail,tree.getroot().attrib      # Edition Date     elem = list(tree.getiterator("resEdDate"))[0]      # print elem.tag,elem.text     elem.text = edDate     ## print elem.text      # Reference Date 001 (Creation)     elem = list(tree.getiterator("refDate"))[0]      # print elem.tag,elem.text     elem.text = createDate     ## print elem.text     # note there may be two of these dates     # DateTypCd 001 and 002     # Reference Date 002 (Publication)     if len(list(tree.getiterator("refDate"))) > 1 :         elem = list(tree.getiterator("refDate"))[1]         # print elem.tag,elem.text         elem.text = publishDate     else :         # print "Skipping publication date",xmlfile         pass     ## print elem.text     # clear out lineag if it exists     try :         lin = list(tree.getiterator("lineage"))[0]         # print lin.tag         lin.clear()         lin.text = "Cleared"     except :         gp.AddMessage("Skipping clear lineage")     outfile = xmlfile.replace(".","_et.")     tree.write(outfile)     return outfile  # ---------------------- main ----------------------  try :     publishDate = sys.argv[1]     createDate  = sys.argv[2]     if createDate == '#' :         createDate = publishDate     gp.AddMessage(publishDate+type(publishDate)) except :     # 12 = 5 (sat) + 7 to keep positive in modulo 7     tday = datetime.datetime.now()     div,offset = divmod((12 - tday.replace(day=1).weekday()),7)     firstSat = tday.replace(day=1) + datetime.timedelta(days=offset)     publishDate = firstSat.strftime("%Y%m%d")     createDate  = publishDate     print publishDate,firstSat.ctime() print # override # publishDate = '20100804' # createDate = '20100710'  gp = arcgisscripting.create(9.3)  os.chdir("e:/crs/customer/conservation/metadata") edDate = str(datetime.datetime.now().date()).replace("-","") edDate = publishDate # '20100914' gp.AddMessage(edDate+" edit date") gp.AddMessage(publishDate+" publish date") gp.AddMessage(createDate+" create date")  ws = "e:/crs/customer/conservation/corax.gdb" metasrc = "e:/crs/customer/conservation/metadata" gp.Workspace = ws  os.chdir(metasrc) print print ws print metasrc print    lstFC = gp.ListFeatureClasses("*")  for fc in lstFC :     # print fc     fcxml = metasrc+"/"+fc+".xml"     if os.path.exists(fcxml) :         etxml = alter(fcxml,edDate,publishDate,createDate)          gp.MetadataImporter_conversion(etxml,fc)         print fc,"updated"         gp.AddMessage(fc+" updated")     else :         print fcxml,"not found"         gp.AddError(fcxml+" not found")          lstTab = gp.ListTables("*") for tab in lstTab :     # print tab     tabxml = metasrc+"/"+tab+".xml"     if os.path.exists(tabxml) :         etxml = alter(tabxml,edDate,publishDate,createDate)          gp.MetadataImporter_conversion(etxml,tab)         print tab,"updated"         gp.AddMessage(tab+" updated")     else :         print tabxml,"not found"         gp.AddError(tabxml+" not found") # geodatabase metadata etxml = alter(metasrc+"/corax.xml",edDate,publishDate,createDate)


Note that at 10.x there is now a tool to unload metadata and better tools to import other formats.
But this still works.
0 Kudos
KarstenRank
Occasional Contributor III
Thanks a lot for your answer! This is what I searched!

Karsten
0 Kudos