ArcGIS批量匯出柵格影像的屬性表
阿新 • • 發佈:2018-12-24
需要將多幅TIF影像中的屬性表匯出後參與運算。
1. 開啟 ArcMap 或者 ArcCatalog
2. 在資料夾中新建檔案地理資料庫。
3. 匯入柵格。
4. 開啟Python視窗,修改程式碼,輸入進行計算。
以下程式碼將各檔案的屬性表分別匯出為.csv檔案。合併為一個.csv 檔案在第二段程式碼。
# coding:utf-8 #功能:批量匯出柵格檔案的屬性表。 #使用步驟 1:在相應資料夾下新建“檔案地理資料庫”,並將需要匯出屬性表的柵格檔案“匯入”到該資料庫中。#使用步驟 2:更改第二行程式碼[ws = r'D:\test\test1.gdb']為自己的檔案存放地址和資料庫名稱,第三行同樣的處理。 #使用步驟 3:複製程式碼在ArcGIS中執行即可。 import arcpy, os ws = r'D:\t\test.gdb' outPath = r'D:\test' outExt = ".csv" arcpy.env.workspace = ws rasters = arcpy.ListRasters("*") for raster in rasters: rasloc = ws + os.sep + raster fields= "*" try: lstFlds = arcpy.ListFields(rasloc) header = '' for fld in lstFlds: header += ",{0}".format(fld.name) if len(lstFlds) != 0: outCSV = outPath + os.sep + raster + outExt f = open(outCSV,'w') header = header[1:] + ',RasterName\n' f.write(header) with arcpy.da.SearchCursor(rasloc, fields) as cursor: for row in cursor: f.write(str(row).replace("(","").replace(")","") + "," + raster + '\n') f.close() except Exception as e: print (e)
匯出至同一個csv檔案。
# coding:utf-8 #功能:批量匯出柵格檔案的屬性表。 #使用步驟 1:在相應資料夾下新建“檔案地理資料庫”,並將需要匯出屬性表的柵格檔案“匯入”到該資料庫中。 #使用步驟 2:更改第二行程式碼[ws = r'D:\test\test.gdb']為自己的檔案存放地址和資料庫名稱,第三行同樣的處理。 #使用步驟 3:複製程式碼在ArcGIS中執行即可。 import arcpy, os ws = r'D:\test\test.gdb' outCSV = r'D:\test\0.csv' arcpy.env.workspace = ws rasters = arcpy.ListRasters("*") for raster in rasters: rasloc = ws + os.sep + raster fields = "*" try: lstFlds = arcpy.ListFields(rasloc) header = '' header += ",{0}".format(lstFlds[0].name)+",{0}".format(lstFlds[1].name) if len(lstFlds) != 0: f = open(outCSV,'a') header =header[0:] + ',RasterName\n' f.write(header) with arcpy.da.SearchCursor(rasloc, fields) as cursor: for row in cursor: f.write(str(row).replace("(","").replace(")","") + "," + raster + '\n') f.close() except Exception as e: print (e) del row