tiff图像分割

import os, os.path
import rasterio
from rasterio.windows import Window
import rasterio.mask


def rastersplit(input_value_raster, tile_width_in, tile_height_in, outfolder):
    dataset = rasterio.open(input_value_raster)

    # height and width of the raster
    height, width = dataset.shape

    # split the large raser into number of tiles, tile size is 2000*2000
    tile_width = tile_width_in
    tile_height = tile_height_in

    tile_num_col = int(width / tile_width + 0.5)
    tile_num_row = int(height / tile_height + 0.5)

    # the output folder
    if not os.path.exists(outfolder): os.mkdir(outfolder)

    # create each tile
    for i in range(tile_num_row):
        # re-initiate the tile width when reach the right boundary
        tile_width = tile_width_in
        row_start = i * tile_height

        # loop each column
        for j in range(tile_num_col):
            print('The i and j is:', i, j)
            tilename = os.path.join(outfolder, 'row%s-col%s.tif' % (i, j))

            col_start = j * tile_width
            row_end = (i + 1) * tile_height
            col_end = (j + 1) * tile_width

            # deal with the right boundary
            if row_end > height - 1: tile_height = height - row_start
            if col_end > width - 1: tile_width = width - col_start

            # window = get_data_window(lu_dataset.read(1, masked=True))
            window = Window(col_off=col_start, row_off=row_start, width=tile_width, height=tile_height)

            kwargs = dataset.meta.copy()
            kwargs.update({
                'height': window.height,
                'width': window.width,
                'transform': rasterio.windows.transform(window, dataset.transform)})

            img_window = dataset.read(window=window)
            if img_window.min() <= 0: continue
            with rasterio.open(tilename, 'w', **kwargs) as dst:
                dst.write(img_window)


root = 'H:\WYL\Geo'  # 栅格影像 (文件路径)
data = os.path.join(root, 'gaofer3_20200316_103857725_A_HH_pwr_fil_geo_ql.tif') #文件名
label = os.path.join(root, 'result')
if not os.path.exists(label): os.mkdir(label)
tile_width = 640  # 裁剪后的尺寸
tile_height = 640

rastersplit(data, tile_width, tile_height, label)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值