2013-11-26 24 views
1

我希望能夠爲包含每個要素類的記錄以及一些元數據字段(如摘要,描述等)並將其轉換爲該功能的Excel文件類元數據。從我做的研究看來,我需要將Excel表中的每個記錄轉換爲xml,然後從那裏我可以將xml文件導入爲元數據。看起來我可以使用ElementTree,但我有點不確定如何執行。有沒有人曾經這樣做過,如果可以,你能提供一些指導嗎?使用arcpy以編程方式更新元數據的元數據

回答

2

男人,這可能是一個相當的過程!我不得不在某天更新某個項目的某些元數據信息,所以這裏什麼也不做。將所有元數據信息存儲在excel表中作爲字典列表或您選擇的其他數據結構(我使用csvs並嘗試遠離Excel電子表格出於經驗原因)將會很有幫助。

metaInfo = [{"featureClass":"fc1", 
      "abstract":"text goes here", 
      "description":"text goes here", 
      "tags":["tag1","tag2","tag3"]}, 
      {"featureClass":"fc2", 
      "abstract":"text goes here", 
      "description":"text goes here", 
      "tags":["tag1","tag2","tag3"]},...] 

從那裏,我會實際使用導出元數據功能,要素類的元數據轉換爲使用FGDC架構的XML文件中導出當前的元數據要素類。以下是一個代碼示例:

#Directory containing ArcGIS Install files 
installDir = arcpy.GetInstallInfo("desktop")["InstallDir"] 
#Path to XML schema for FGDC 
translator = os.path.join(installDir, "Metadata/Translator/ARCGIS2FGDC.xml") 
#Export your metadata 
arcpy.ExportMetadata_conversion(featureClassPath, translator, tempXmlExportPath) 

從那裏,您可以使用xml模塊訪問ElementTree類。但是,我建議使用lxml模塊(http://lxml.de/index.html#download),因爲如果您需要元數據中的換行符等特殊元素,它允許您通過CDATA工廠將HTML代碼合併到元數據中。從那裏,假設您已經導入LXML,解析您的本地XML文檔:

import lxml.etree as ET 
tree = ET.parse(tempXmlExportPath) 
root = tree.getroot() 

如果要更新標籤使用下面的代碼:

idinfo = root[0] 

#Create keyworks element 
keywords = ET.SubElement(idinfo, "keywords") 
tree.write(tempXmlExportPath) 

#Create theme child 
theme = ET.SubElement(keywords, "theme") 
tree.write(tempXmlExportPath) 

#Create themekt and themekey grandchildren/insert tag info 
themekt = ET.SubElement(theme, "themekt") 
tree.write(tempXmlExportPath) 
for tag in tags: #tags list from your dictionary 
    themekey = ET.SubElement(theme, "themekey") 
    themekey.text = tag 
    tree.write(tempXmlExportPath) 

要更新彙總標籤,使用此代碼:

#Create descript tag 
descript = ET.SubElement(idinfo, "descript") 
tree.write(tempXmlExportPath) 

#Create purpose child from abstract 
abstract = ET.SubElement(descript, "abstract") 
text = #get abstract string from dictionary 
abstract.text = text 
tree.write(tempXmlExportPath) 

如果在XML標籤已經存在,存儲所述標籤作爲使用parent.find(「孩子」)的方法的對象,並更新類似於上述示例代碼的文本。更新本地xml文件後,使用導入元數據方法將xml文件導回到要素類中,並刪除本地xml文件。

arcpy.ImportMetadata_conversion(tempXmlExportPath, "FROM_FGDC", featureClassPath) 
shutil.rmtree(tempXmlExportPath) 

請記住,這些工具在弧是僅適用於32位,所以如果你是通過64位的背景地理處理器腳本,這是不行的。我正在使用ArcMap 10.1。如果您有任何疑問,請讓我知道或查閱下列文件:

LXML模塊 http://lxml.de/index.html#documentation

導出元數據ArcPy中 http://resources.arcgis.com/en/help/main/10.1/index.html#//00120000000t000000

導入元數據ArcPy中 http://resources.arcgis.com/en/help/main/10.1/index.html#//00120000000w000000

相關問題