Python地理数据处理 22:基于arcpy批量操作(四)

您所在的位置:网站首页 arcpy文档和arcgis文档一样吗 Python地理数据处理 22:基于arcpy批量操作(四)

Python地理数据处理 22:基于arcpy批量操作(四)

2023-11-15 18:56| 来源: 网络整理| 查看: 265

批量裁剪 1. 批量裁剪进阶2. 统计运算3. 栅格批量缩小n倍4. 建立属性表(简化、普适)5. 计算土地利用未变化区域(LUCC)

1. 批量裁剪进阶

代码描述:遍历a文件夹下的所有tif影像,并使用每个a文件夹中的tif影像对b文件夹下的所有tif影像进行裁剪。裁剪后的栅格将以两个tif文件进行组合命名,并保存到另一个文件夹中。

# -*- coding: cp936 -*- import arcpy import os import time start = time.clock() arcpy.CheckOutExtension("spatial") arcpy.CheckOutExtension("spatial") a_folder = r"D:\dataset\a" b_folder = r"D:\dataset\b" output_folder = r"D:\dataset\output_tif" # 设置工作环境 arcpy.env.workspace = a_folder # 获取a文件夹下的所有tif影像 a_rasters = arcpy.ListRasters("*", "TIF") # 创建输出文件夹 if not os.path.exists(output_folder): os.makedirs(output_folder) # 遍历a文件夹下的每个tif影像 for indexa, a_raster in enumerate(a_rasters): # 构建输出文件名前缀 a_name = os.path.splitext(a_raster)[0] a_raster = a_folder + "\\" + a_name + ".tif" # 设置工作环境为b文件夹 arcpy.env.workspace = b_folder # 获取b文件夹下的所有tif影像 b_rasters = arcpy.ListRasters("*", "TIF") # 遍历b文件夹下的每个tif影像 for indexb, b_raster in enumerate(b_rasters): # 构建输出文件名 output_name = a_name + "_" + os.path.splitext(b_raster)[0] + ".tif" # 构建输出路径 output_path = os.path.join(output_folder, output_name) # 设置工作环境为a文件夹 arcpy.env.workspace = a_folder b_raster = b_folder + "\\" + b_raster # 裁剪b影像到输出路径 arcpy.gp.ExtractByMask_sa(b_raster, a_raster, output_path) index = indexa + indexb + 1 print "{} have been completed and the current file is ".format(index) + output_name end = time.clock() print "All finish!!!" 2. 统计运算

获取栅格数据的平均值,并输出程序运行进度:

# -*- coding: utf-8 -*- import arcpy import os import glob from arcpy.sa import * arcpy.CheckOutExtension("ImageAnalyst") arcpy.CheckOutExtension("spatial") input_folder = r"D:\dataset" output_file = r'D:\dataset\Meandata.csv' rasters = glob.glob(os.path.join(input_folder, "*.tif")) where_clause = "VALUE = -32556" total_rasters = len(rasters) processed_rasters = 0 with open(output_file, 'w') as output: for raster in rasters: outSetNull = SetNull(raster, raster, where_clause) meanValueInfo = arcpy.GetRasterProperties_management(outSetNull, 'MEAN') meanValue = meanValueInfo.getOutput(0) output.write(os.path.basename(raster).split('.')[0] + ',' + str(meanValue) + '\n') processed_rasters += 1 progress = float(processed_rasters) / total_rasters * 100 print("Processed {} out of {} rasters. Progress: {:.2f}%".format(processed_rasters, total_rasters, progress)) print("All processing is done!")

程序运行进度:

3. 栅格批量缩小n倍

某文件夹中包含多个子文件夹,如:“2003clip”, “2004clip”, “2005clip”, “2006clip”, “2007clip”, “2008clip”, “2009clip”, 每个子文件夹中包含多个tif图像,现在需要对这些图像乘以缩放因子,如0.0001

# -*- coding: cp936 -*- import arcpy import os arcpy.CheckOutExtension('Spatial') # 输入文件夹和输出文件夹路径 input_folder = r"D:\Datasets" output_folder = r"D:\Datasets\缩小10000倍" # 遍历输入文件夹中的所有子文件夹 for folder_name in ["2003clip", "2004clip", "2005clip", "2006clip", "2007clip", "2008clip", "2009clip"]: print folder_name # 子文件夹路径 folder_path = os.path.join(input_folder, folder_name) # 获取子文件夹中的所有tif文件 tif_files = [file for file in os.listdir(folder_path) if file.endswith(".tif")] # 遍历每个tif文件 for tif_file in tif_files: # 输入tif文件路径 input_tif = os.path.join(folder_path, tif_file) # 输出tif文件路径 output_tif = os.path.join(output_folder, tif_file) # 打开Raster对象 raster = arcpy.Raster(input_tif) # 过滤像元值大于10000和小于0的像元 filtered_raster = arcpy.sa.Con((raster >= 0) & (raster


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3