2016-11-30 297 views
-1

我是新手,涉及到arcpy,并且正在尝试开发一个脚本,该脚本将使用arcpy.da.walk来清点我们的GIS数据。当它通过我们拥有的文件夹/ gdbs时,我希望它为每个要素类导出一些项目到csv(现在我会对要素类路径,文件名,空间参考名称和元数据目的感到满意)。我已经完成了脚本,直到元数据目的部分。一旦我添加行:使用arcpy.da.walk清点数据并将元数据导出到csv

arcpy.ExportMetadata_conversion(feature_class, translatorpath, xmlfile) 
tree = ElementTree() 
tree.parse(xmlfile) 
spot = tree.find("idinfo/descript/purpose") 

我的脚本不会返回任何东西。如果没有这些行,我会收到一个带有要素类路径,文件名和空间参考名称的csv文件,但如果我包含这些行,则我的csv文件是空的。没有错误,只是空的。我的脚本(以下内容)基于:https://arcpy.wordpress.com/tag/os-walk/https://gis.stackexchange.com/questions/34729/creating-table-containing-all-filenames-and-possibly-metadata-in-file-geodatab/34797#3479/

任何帮助,非常感谢!

EDITED:一些要素类可能没有定义空间参考,许多要素类可能没有任何关联的元数据。我仍然希望在csv中使用这些字段,但这些字段可以是空白的,或者沿着“没有定义空间参考”和“没有定义元数据目的”的语句说一些。

import os 
import arcpy 
import csv 
from xml.etree.ElementTree import ElementTree 
from arcpy import env 

def inventory_data(workspace, datatypes): 
    for path, path_names, data_names in arcpy.da.Walk(
      workspace, datatype=datatypes): 
     for data_name in data_names: 
      yield os.path.join(path, data_name) 

AGSHOME = arcpy.GetInstallInfo("Desktop")["InstallDir"] 
translatorpath = AGSHOME + "Metadata\\Translator\\ARCGIS2FGDC.xml" 
outfile = "C:\\GIS\\Records\\Data Management\\Inventories\\GIS_Data_Inventory_daWalk_function_outputtocsv_descitems_try_sr_meta.csv" 
xmlfile = "C:\\GIS\\Records\\Data Management\\Inventories\\TempInventoryError\\daWalk_function_outputtocsv_descitems_try_sr_meta.xml" 

with open (outfile, 'wb') as csvfile: 
    csvwriter = csv.writer(csvfile) 
    for feature_class in inventory_data(r"C:\GIS\Data\Natural_Environment\Species_and_Habitats\Habitat_Models", "FeatureClass"): 
     try: 
      desc = arcpy.Describe(feature_class) 
      sr = desc.spatialReference 
      arcpy.ExportMetadata_conversion(feature_class, translatorpath, xmlfile) 
      tree = ElementTree() 
      tree.parse(xmlfile) 
      spot = tree.find("idinfo/descript/purpose") 
      csvwriter.writerow([desc.path.encode('utf-8'), desc.file.encode('utf-8'), desc.dataType.encode('utf-8'), sr.name.encode('utf-8'), spot.text.encode('utf-8')]) 
     except: 
      pass 

回答

0

关于“我的脚本不返回任何东西”:

你已经把你的命令在try ... catch块,但你的catch没有做任何事情。如果您打印的异常消息,你可能会看到这些ArcPy中的错误:

ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000725: Output File: Dataset C:\tmp\test.xml already exists.
WARNING 000725: Output File: Dataset C:\tmp\test.xml already exists.

要解决每次迭代的这个问题,你必须删除xmlfile和开始(或结束)在for,如果我理解正确它只是被用作临时文件。

# your other imports here 
import traceback 

with open (outfile, 'wb') as csvfile: 
    csvwriter = csv.writer(csvfile) 
    for feature_class in inventory_data(r"C:\some directory", "FeatureClass"): 
     try: 
      if os.path.isfile(xmlfile): 
       os.remove(xmlfile) 

      # your other code here 
     except Exception: 
      print "Failed: ", feature_class 
      print traceback.format_exc() 

关于写作丢失的数据到CSV:

这取决于那种你正在做的操作和它产生什么样的数据。对于您的spot变量,您要检查tree.find的输出是否为None

从文档:

... Returns an element instance or None.

使用第二可变,例如spot_text,存储您的文本值,并将其写入到CSV代替:

spot = tree.find("idinfo/descript/purpose") 
if spot == None: 
    spot_text = "No metadata purpose defined" 
else: 
    spot_text = spot.text.encode('utf-8') 

或缩短:

spot = tree.find("idinfo/descript/purpose") 
spot_text = spot.text.encode('utf-8') if spot != None else "No metadata purpose defined" 
+0

感谢您的帮助!上周我通过反复试验发现了这一点,并帮助了Esri的Geonet论坛。完整答案可以在这里看到:https://geonet.esri.com/thread/186717-using-arcpydawalk-to-inventory-data-and-export-metadata-to-csv – sgroff