使用arcpy以编程方式更新要素类的元数据

我希望能够获取包含每个要素类的logging和一些元数据字段(如摘要,描述等)的Excel文件,并将其转换为要素类元数据。 从我做的研究,似乎我需要将Excel表中的每个logging转换为XML,然后从那里我可能能够导入XML文件作为元数据。 看起来像我可以使用ElementTree,但我有点不确定如何执行。 有没有人以前做过这个,如果有的话,你能提供一些指导?

男人,这可以是一个相当的过程! 我不得不在某天更新某个项目的一些元数据信息,所以这里什么都不做。 将所有元数据信息存储在excel表中作为字典列表或您select的其他数据结构(我使用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"]},...] 

从那里,我将使用Export Metadata函数实际导出当前元数据要素类,以使用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类。 不过,我build议使用lxml模块( http://lxml.de/index.html#download ),因为如果您需要元数据中的换行符等特殊元素,它允许您通过CDATA工厂将HTML代码合并到元数据中。 从那里,假设你已经导入了lxml,parsing你的本地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(“child”)方法将标签作为对象存储,并更新类似于上述代码示例的文本。 更新本地xml文件后,使用Import Metadata方法将xml文件导回到要素类中,并删除本地xml文件。

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

请记住,Arc中的这些工具只用于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