ArcGIS批量导出栅格影像的属性表

需要将多幅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

参考地址

 

猜你喜欢

转载自www.cnblogs.com/geozho/p/10167957.html
今日推荐