当前位置: 首页 > news >正文

Python地理数据处理 28:基于Arcpy批量操作实现——按属性提取和分区统计

Arcpy批量操作

  • 1. 批量按属性提取
  • 2. 批量分区统计(最大值、最小值和像元个数等)

1. 批量按属性提取

# -*- 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't
if not 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 condition
for 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't
if not 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 file
for 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

相关文章:

  • JAVA SE:认识类和对象
  • 7. 记忆(Memory)机制:让AI拥有“短期记忆”与“长期记忆”
  • 核心知识—— RDD常用算子之数据转换
  • Hugging Face模型微调训练(基于BERT的中文评价情感分析)
  • 【BFS最小步数】魔板题解
  • 从零构建大语言模型全栈开发指南:附录与资源-3.面试与进阶-200道大模型面试真题与职业发展路线图-基础理论篇50题
  • C++_类和对象(中)
  • 【Python Cookbook】字符串和文本(五):递归下降分析器
  • 颜色性格测试:探索你的内在性格色彩
  • 现代复古电影海报品牌徽标设计衬线英文字体安装包 Thick – Retro Vintage Cinematic Font
  • [项目总结] 在线OJ刷题系统项目总结与分析(二): 技术应用(上)
  • R语言网状Meta分析---Meta回归(1)(基于gemtc)
  • VScode 玩 MCP的server
  • MySQL数据类型与Java数据类型的映射
  • 手撕LLM(一):从源码出发,探索LLM推理全流程
  • JJJ:generic netlink例程分析
  • 怎么计算感受野? 怎么增加感受野?
  • 数据仓库:规范
  • RTDETR融合[CVPR2025]DnLUT中的MuLUTUnit模块
  • QGIS获取建筑矢量图-Able Software R2V