Python-批量计算城市热岛强度(Urban Heat Island Intensity, UHII)
数据准备
城市热岛强度(Urban Heat Island Intensity, UHII)表示热岛效应的发生程度, 在本文中将UHII定义为建成区块平均地表温度与其缓冲区平均地表温度的差值. 计算公式为: UHII = Tcity - Tbuffer .
建成区块平均地表温度与缓冲区平均地表温度可以用ArcMap中的以表格显示分区统计工具(Zonal Statistics as Table), 如果要批量处理可以在模型编辑器中插入迭代器. 当然, 也可以用arcpy来做.
代码实现
由于数据的格式是'.dbf', 所以要用到dbfread包, 对dbf格式文件批量处理.
import dbfread
import os
import openpyxl
def calculate(path1, path2, path3):
file_list1 = os.listdir(path1) # 获取目录下所有文件
file_list2 = os.listdir(path2)
filtered1 = filter(lambda x: os.path.splitext(x)[1] == '.dbf', file_list1) # 过滤出指定后缀文件
filtered2 = filter(lambda x: os.path.splitext(x)[1] == '.dbf', file_list2)
table_list1 = list(map(lambda x: os.path.join(path1, x), filtered1)) # 生成绝对路径列表
table_list2 = list(map(lambda x: os.path.join(path2, x), filtered2))
for i, j in zip(table_list1, table_list2):
with dbfread.DBF(i) as table1, dbfread.DBF(j) as table2:
wb = openpyxl.Workbook()
ws = wb.active
ws.append(['Value', 'UHII'])
for record1, record2 in zip(table1, table2):
new_mean = record1['MEAN'] - record2['MEAN']
ws.append([record1['Value'], new_mean])
wb.save(os.path.join(path3, os.path.split(i)[1][:-4] + '.xlsx'))
if __name__ == "__main__":
table_path1 = r'E:\urban_mean'
table_path2 = r'E:\buffer_mean'
save_path = r'E:\uhii'
calculate(table_path1, table_path2, save_path)