# -*- coding: cp936 -*-"""
PROJECT_NAME: ArcPy
FILE_NAME: batch_attribute_extract
AUTHOR: JacksonZhao
DATE: 2025/04/05
"""import arcpy
import os
arcpy.CheckOutExtension('Spatial')# Set the workspace environment
input_folder =r"C:\Users\YiPei\Desktop\2Reclassify"
output_folder =r"C:\Users\YiPei\Desktop\2Reclassify_reclassify4"# Check if the output folder exists, and create it if it doesn'tifnot os.path.exists(output_folder):
os.makedirs(output_folder)# Get the list of all raster data
arcpy.env.workspace = input_folder
raster_list = arcpy.ListRasters("*","TIF")# Define the list of conditions
conditions =["Value = 0","Value = 1","Value = 2","Value = 3"]# Iterate through each raster and perform the extraction for each conditionfor raster in raster_list:print"Processing raster: %s"% raster
for condition in conditions:# Define the output raster name with the condition appended
base_name = os.path.splitext(raster)[0]
output_raster_name ="%s_%s.tif"%(base_name, condition.replace(' ',''))
out_raster = os.path.join(output_folder, output_raster_name)# Execute the raster extraction by attributes
arcpy.gp.ExtractByAttributes_sa(raster, condition, out_raster)print"Extracted raster saved to: %s"% out_raster
2. 批量分区统计(最大值、最小值和像元个数等)
# -*- coding: cp936 -*-# -*- coding: utf-8 -*-"""
PROJECT_NAME: ArcPy
FILE_NAME: batch_UHI
AUTHOR: JacksonZhao
DATE: 2025/02/23
"""import arcpy
import os
# Check out the Spatial Analyst extension
arcpy.CheckOutExtension('Spatial')# Set the input and output folders
input_folder =r"C:\Users\YiPei\Desktop\2Reclassify_reclassify4"
output_folder =r"C:\Users\YiPei\Desktop\2Reclassify_reclassify4_table"# Set the path to the shapefile
shapefile =r"C:\Users\YiPei\Desktop\GEE_data\ChinaAlbers\ChinaAlbers.shp"# Check if the output folder exists, and create it if it doesn'tifnot os.path.exists(output_folder):
os.makedirs(output_folder)# Get the list of all raster files in the input folder
arcpy.env.workspace = input_folder
raster_list = arcpy.ListRasters("*","TIF")# Iterate through each raster filefor raster in raster_list:print"Processing raster: %s"% raster
# Define the output CSV file name
base_name = os.path.splitext(raster)[0]
output_csv = os.path.join(output_folder, base_name +"_ZonalStats.csv")# Perform Zonal Statistics as Table
arcpy.gp.ZonalStatisticsAsTable(
shapefile,"name",
raster,
output_csv,"DATA","ALL")print"Zonal Statistics results saved to: %s"% output_csv