热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

[ArcPyTips5]矢量版的ZonalStatisticsAsTable(仅面积)

ArcGIS自带的ZonalStatisticsAsTable在分区统计栅格面积时很好用,但是这个工具只能针对栅格,不能针对矢量。最近有这个需求,所以就做了一个,基本都是ArcPy

ArcGIS 自带的 Zonal Statistics As Table 在分区统计栅格面积时很好用,但是这个工具只能针对栅格,不能针对矢量。

最近有这个需求,所以就做了一个,基本都是 ArcPy 的自带函数,输入的参数和 Zonal Statistics As Table 高度类似,在此不作详细解释,请君查看代码。

下附:

import arcpy,os,pandas,shutil
def write2DListToCsv(listItem,fileName):
fileObject = open(fileName, 'w')
for i in range(len(listItem)):
for j in range(len(listItem[i])):
fileObject.write(str(listItem[i][j]))
if j fileObject.write(",")
if i fileObject.write("\n")
fileObject.close()
def main():
maskShp = r"E:\xxx\xxx.shp"
valueField = "xxxID"
polygon = r"E:\xxx\xxx.shp"
dstFile = r"E:\xxx\xxx.shp"

tempDir = "E:\\xxx\\xxx\\temp\\"
masksDir = tempDir+"masks\\"
shutil.rmtree(tempDir)
os.mkdir(tempDir)
os.mkdir(masksDir)
table = [[valueField,"AREA"]]
arcpy.Split_analysis(maskShp,maskShp,valueField,masksDir)
for mask in os.listdir(masksDir):
if not os.path.splitext(mask)[1] == ".shp":
continue
print(mask)
value = mask[0:-4]
tempFile = value+".shp"
arcpy.Clip_analysis(polygon,masksDir+mask,tempDir+tempFile)
featureCount = arcpy.GetCount_management(tempDir+tempFile)[0]
if int(featureCount)>0:
arcpy.CalculateAreas_stats(tempDir+tempFile, tempDir+"AREA_"+tempFile)
arcpy.TableToTable_conversion(tempDir+"AREA_"+value+".dbf",tempDir,value+".csv")
areaSum = pandas.read_csv(tempDir+value+".csv")["F_AREA"].sum()
print(areaSum)
table.append([value,areaSum])
print(table)
write2DListToCsv(table,dstFile)
if __name__ == "__main__":
main()


推荐阅读
author-avatar
不言不语都可以温柔_631
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有