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

posted @ 2022-07-04 14:43  skypanxh  阅读(373)  评论(0编辑  收藏  举报