zonal static as table重复区域无法统计的问题

1. 问题

arcgis的zonal static as table功能,发现如果矢量有重叠的话,会导致其像元值只会被统计一次。

2. 解决方案

就采用遍历矢量要素,然后对每一个矢量进行区域统计,得到结果再汇总。

3. 安装pandas依赖库

3.1 找到自己arcpy所带的python目录

image
博主的为D:\Mr.pan\app\python2.7\ArcGIS10.8

3.2 到该目录下安装pandas

使用cmd,cd到该目录下,运行pip install pandas
image

4. 代码

# -*- coding: utf-8 -*- # -*- coding: utf-8 -*- import arcpy, os from arcpy.sa import * from arcpy import env import pandas as pd arcpy.env.overwriteOutput = 1 # 可覆盖 # 矢量图层 inputshp = r"H:\01UrbanWaterSourceSecurity\Task\03Zonel\shp\World_Watershed8_dissolve_by_city.shp" Zone_field = "OBJECTID" # 唯一值字段,数字 # 栅格图层 inputtif = r"H:\01UrbanWaterSourceSecurity\01Data\LUH\states\ssp126\c3ann\ssp126_c3ann_2015.tif" # 分区统计 ignore_nodata = "NODATA" # 或者DATA,与ArcGIS中一样 statistics_type = "SUM" # ALL | MEAN | MAXIMUM | MINIMUM | RANGE | STD | SUM | MIN_MAX | MEAN_STD | MIN_MAX_MEAN,与ArcGIS中一样 # 输出字段 dbfPath = r"H:\01UrbanWaterSourceSecurity\Task\03Zonel\result" # 输出路径,不是mdb或者gdb数据库 outputname = "result.dbf" # 输出结果名称 # 下面的无需改动 env.workspace = dbfPath inRows = arcpy.SearchCursor(inputshp) inRows.reset() inRow = inRows.next() print("begin") count = 0 while inRow: count += 1 lyr = "Zone {0}".format(inRow.getValue(Zone_field)) print(str(count), inRow.getValue(Zone_field)) arcpy.MakeFeatureLayer_management(inputshp, lyr, '"{0}" = {1}'.format(Zone_field, inRow.getValue(Zone_field))) tempTable = "{0}/DBF_{1}.dbf".format(dbfPath, inRow.getValue(Zone_field)) try: ZonalStatisticsAsTable(lyr, Zone_field, inputtif, tempTable, ignore_nodata,statistics_type) except arcpy.ExecuteError: message = arcpy.GetMessages() print(message) inRow = inRows.next() continue tempExcel = "{0}/DBF_{1}.xls".format(dbfPath, inRow.getValue(Zone_field)) arcpy.TableToExcel_conversion(tempTable, tempExcel) if count == 1: df = pd.read_excel(tempExcel) else: df1 = pd.read_excel(tempExcel) df = pd.concat([df,df1]) os.remove(tempTable) os.remove("{0}/DBF_{1}.cpg".format(dbfPath, inRow.getValue(Zone_field))) os.remove("{0}/DBF_{1}.dbf.xml".format(dbfPath, inRow.getValue(Zone_field))) os.remove(tempExcel) inRow = inRows.next() del df['OID'] df.to_excel(dbfPath + "/" + "result.xls",index=False) arcpy.ExcelToTable_conversion(dbfPath + "/" + "result.xls", dbfPath + "/" + outputname) os.remove(dbfPath + "/" + "result.xls")

注意:使用ArcMap查看dbf后必须关闭ArcMap后才能再次运行,只移除dbf不行

参考资料
https://blog.csdn.net/A873054267/article/details/83387142


__EOF__

本文作者skypanxh
本文链接https://www.cnblogs.com/skypanxh/p/16441021.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   skypanxh  阅读(386)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示