使用 arcpy 如何对栅格进行读取、批量裁剪和镶嵌栅格 您所在的位置:网站首页 arcgis裁剪栅格指定区域 使用 arcpy 如何对栅格进行读取、批量裁剪和镶嵌栅格

使用 arcpy 如何对栅格进行读取、批量裁剪和镶嵌栅格

2023-05-20 03:54| 来源: 网络整理| 查看: 265

1. 使用 ArcGIS 等工具打开需要处理的栅格数据,并在其元数据中添加对应属性信息(例如:需求单位、时间戳、采集地点等)。

2. 利用 arcpy 的 ListRasters 函数读取所有需要处理的栅格数据路径。该函数可接受一个文件夹作为输入路径,并返回该文件夹下所有格式相同的栅格图层的名称和路径。 import arcpy

# Set path to the folder containing input raster files in_folder = "D:/path/to/input/folder"

# Use ListRasters function to get a list of all rasters in the input folder raster_list = arcpy.ListRasters("*", "TIF", in_folder)

for raster_file in raster_list:     # Add your processing logic here (e.g., clip/raster mosaic) ```

3. 在遍历每个栅格时,使用 GetRasterProperties 函数获取单个栅格的元数据属性值(例如:需求单位、时间戳、采集地点等)。该函数可接受栅格数据路径作为输入参数,并返回一个 Python 字典对象,包含完整的栅格元数据信息。 import arcpy

# Set path to a single input raster file in_raster = "D:/path/to/input/raster.tif"

# Use GetRasterProperties function to retrieve metadata for the input raster metadata = arcpy.GetRasterProperties(in_raster, "ALL")

# retrieve specific metadata values source = metadata['TI_SOURCE'] date_time = metadata['TI_DATETIME'] location = metadata['TEXTDEFINITION']['Location'] ```

4. 进一步对单个栅格或者一批栅格应用处理逻辑。例如,如果需要按照给定的裁剪范围来进行裁剪,则可以使用 ExtractByMask 函数实现: import arcpy

# Set path to the clip features, input folder and output folder. clip_features = "D:/path/to/input/clip_features.shp" in_folder = "D:/path/to/input/folder" out_folder = "D:/path/to/output/folder"

# Use ListRasters function to get a list of all rasters in the input folder raster_list = arcpy.ListRasters("*", "TIF", in_folder)

for raster_file in raster_list:     # Define output filename with timestamp as prefix     timestamp = raster_file.split("_")[1].split(".")[0]     out_raster = out_folder + "/{}_{}".format(timestamp, raster_file)

    # Use ExtractByMask function to clip raster by the specified feature class     arcpy.gp.ExtractByMask_sa(in_raster=raster_file, in_mask_data=clip_features, out_raster=out_raster) ```

5. 对于将多个栅格图层拼接在一起(镶嵌)的情况,可以使用 MosaicToNewRaster 函数来完成镶嵌操作。该函数可以将所有输入栅格镶嵌成一个新的栅格数据集,并将其存储为一个输出栅格文件。 import arcpy

# Set input raster list and output mosaic path in_raster_list = ["D:/path/to/raster1.tif", "D:/path/to/raster2.tif", "D:/path/to/raster3.tif"] out_mosaic_path = "D:/path/to/output/mosaic.tif"

# Use MosaicToNewRaster function to mosaic all the input rasters into a new raster dataset arcpy.MosaicToNewRaster_management(inputs=in_raster_list, output_location="D:/path/to/output/", raster_dataset_name_with_extension="mosaic.tif") ```

注意:在执行上述操作时,请确保对应文件的路径和格式信息是否正确,并检查计算机性能和资源使用情况等问题。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有