Skip to main content
deleted 109 characters in body
Source Link
Thomas
  • 1.2k
  • 5
  • 11

The window describes the relative location of the block within your file.

If CRS of both files are identical, you can use the bounds of the input window to compute its relative location within output window

import numpy as np
import rasterio
from rasterio import Affine
from rasterio.windows import bounds, from_bounds

# adopt as needed
input_file = "/some/file.tif"
output_file = "/another/file.tif"
out_transform = Affine(1, 0, 0, 0, -1, 0)

profile = {"driver": 'GTiff',
           "compress": "DEFLATE",
           "height": 56160,
           "width": 85196,
           "count": 1,
           "dtype": np.float64,
           "crs": '+proj=latlong',
           "transform": out_transform,
           "tiled": True}

with rasterio.open(input_file, 'r') as src:
    with rasterio.open(output_file, 'w', **profile) as dst:
        for _, src_window in src.block_windows(1):
            data = src.read(1, window=src_window)
            # convert relative input window location to relative output window location
            # using real world coordinates (bounds)
            src_bounds = bounds(src_window, transform=src.profile["transform"],
                                height=src.profile["blockysize"],
                                width=src.profile["blockxsize"], )
            dst_window = from_bounds(**src_bounds*src_bounds, transform=dst.profile["transform"])

            dst.write(data, window=dst_window, indexes=1)

The window describes the relative location of the block within your file.

If CRS of both files are identical, you can use the bounds of the input window to compute its relative location within output window

import numpy as np
import rasterio
from rasterio import Affine
from rasterio.windows import bounds, from_bounds

# adopt as needed
input_file = "/some/file.tif"
output_file = "/another/file.tif"
out_transform = Affine(1, 0, 0, 0, -1, 0)

profile = {"driver": 'GTiff',
           "compress": "DEFLATE",
           "height": 56160,
           "width": 85196,
           "count": 1,
           "dtype": np.float64,
           "crs": '+proj=latlong',
           "transform": out_transform}

with rasterio.open(input_file, 'r') as src:
    with rasterio.open(output_file, 'w', **profile) as dst:
        for _, src_window in src.block_windows(1):
            data = src.read(1, window=src_window)
            # convert relative input window location to relative output window location
            # using real world coordinates (bounds)
            src_bounds = bounds(src_window, transform=src.profile["transform"],
                                height=src.profile["blockysize"],
                                width=src.profile["blockxsize"], )
            dst_window = from_bounds(**src_bounds, transform=dst.profile["transform"])

            dst.write(data, window=dst_window, indexes=1)

The window describes the relative location of the block within your file.

If CRS of both files are identical, you can use the bounds of the input window to compute its relative location within output window

import numpy as np
import rasterio
from rasterio import Affine
from rasterio.windows import bounds, from_bounds

# adopt as needed
input_file = "/some/file.tif"
output_file = "/another/file.tif"
out_transform = Affine(1, 0, 0, 0, -1, 0)

profile = {"driver": 'GTiff',
           "compress": "DEFLATE",
           "height": 56160,
           "width": 85196,
           "count": 1,
           "dtype": np.float64,
           "crs": '+proj=latlong',
           "transform": out_transform,
           "tiled": True}

with rasterio.open(input_file, 'r') as src:
    with rasterio.open(output_file, 'w', **profile) as dst:
        for _, src_window in src.block_windows(1):
            data = src.read(1, window=src_window)
            # convert relative input window location to relative output window location
            # using real world coordinates (bounds)
            src_bounds = bounds(src_window, transform=src.profile["transform"])
            dst_window = from_bounds(*src_bounds, transform=dst.profile["transform"])

            dst.write(data, window=dst_window, indexes=1)
Source Link
Thomas
  • 1.2k
  • 5
  • 11

The window describes the relative location of the block within your file.

If CRS of both files are identical, you can use the bounds of the input window to compute its relative location within output window

import numpy as np
import rasterio
from rasterio import Affine
from rasterio.windows import bounds, from_bounds

# adopt as needed
input_file = "/some/file.tif"
output_file = "/another/file.tif"
out_transform = Affine(1, 0, 0, 0, -1, 0)

profile = {"driver": 'GTiff',
           "compress": "DEFLATE",
           "height": 56160,
           "width": 85196,
           "count": 1,
           "dtype": np.float64,
           "crs": '+proj=latlong',
           "transform": out_transform}

with rasterio.open(input_file, 'r') as src:
    with rasterio.open(output_file, 'w', **profile) as dst:
        for _, src_window in src.block_windows(1):
            data = src.read(1, window=src_window)
            # convert relative input window location to relative output window location
            # using real world coordinates (bounds)
            src_bounds = bounds(src_window, transform=src.profile["transform"],
                                height=src.profile["blockysize"],
                                width=src.profile["blockxsize"], )
            dst_window = from_bounds(**src_bounds, transform=dst.profile["transform"])

            dst.write(data, window=dst_window, indexes=1)