ArcGIS+Python读取flt文件并进行区域统计

功能描述:通过ARCGIS的Python脚本,读取flt文件格式,然后根据一个shape文件的区域进行统计其平均值,最后读取出来保存到txt文件中。

1、由于统计后生成的为dbf文件,所以读取需要一个包,下载dbfpy-2.3.0.win32.exe,专门读取写dbf文件的包http://sourceforge.net/projects/dbfpy/files/dbfpy/   

2、读取.flt的Python代码

# -*- coding: cp936 -*-
# Import system modules
import arcpy
import os
from arcpy import env
from arcpy.sa import *
from dbfpy import dbf

#函数体:把链表中的内容写到指定的txt文件中
def WriteResultToTxt(txtpath,list):  
    #print “写出结果”
    #print Result_ET_List
    #print txtpath
    f = open(txtpath, ‘w’)
    for record in list:
        #print record    
        f.write(record+”\n”)
    f.close()

#函数体:读取指定dbf文件的平均值
def ReadDBF(dbfPath):
    #打开统计的结果表,读取平均值
    #print dbfPath+”李艳忠”
    if (os.path.exists(dbfPath)):
        #print “文件存在!”
        db = dbf.Dbf(dbfPath, True)
        for record in db:
            averageValue=record[‘MEAN’]
            return averageValue
            #print “平均值为:”+str(averageValue)
    else:
        print “查无此文件!”
        return

#主程序

env.workspace = “F:\项目工程\科研数据\Zhang_E”                                         # 设置环境变量
rawDataPath=env.workspace+”\\Raster”
inZoneData = env.workspace+”\\border\\border.shp”                                                     #用于统计的边界shape
zoneField = “FID”                                                              #边界使用统计字段
outTablePath = “D:\\study\\aa\\result\\”                              #D:\study\aa\result                                 #MonthlyET_1983Apr.dbf
outResultTXT=env.workspace+”\\result\\ET.txt”                                  #导出的txt路径
Result_ET_List=[]                                                             #用于存储计算出来的ET结果的链表
#print outTablePath
#print inZoneData
#print outResultTXT

#构建所有的文件名称路径
monthArr=[‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’,’Jul’,’Aug’,’Sep’,’Oct’,’Nov’,’Dec’]  #定义12个月
for year in range(1983,2007,1): 
    for month in monthArr:
        infilename=”MonthlyET_”+str(year)+str(month)
        inFileFullpath=rawDataPath+”\\”+infilename+”.flt”
        #print inFileFullpath
        if (os.path.exists(inFileFullpath)):                                        #输入文件存在,进行处理
            #print str(infilefullpath)+”—-存在”            
            outTableFullPath=str(outTablePath)+infilename+”.dbf”                            #输出文件路径
            # Check out the ArcGIS Spatial Analyst extension license
            arcpy.CheckOutExtension(“Spatial”)                                    #一定要加上这句话,否则会报“The tool is not licensed.”的错误
            #outZSaT = ZonalStatisticsAsTable(inZoneData,zoneField,inFileFullpath,outTableFullPath, “NODATA”, “MEAN”)  #区域统计到表中
            #print “输出的dbf路径为:”+outTableFullPath
            average=ReadDBF(outTableFullPath)                                         #读取dbf中的结果值
            #print average
            Result_ET_List.append(infilename+”     “+str(average))                      #把当前的结果存到链表中      
            print infilename+”—-已完成!”            
        else:
            #print str(filefullpath)+”—-不存在”
            aa=2
            
print “正准备写出结果链表!记录数为:”+str(len(Result_ET_List))
WriteResultToTxt(outResultTXT,Result_ET_List)                      #最后把结果写出到txt文件中
print str(len(Result_ET_List))+”条记录成功写出结果链表!”
print “恭喜您,全部文件成功完成!”

转载自:https://blog.csdn.net/liyanzhong/article/details/40342063

You may also like...