Skip to main content
deleted 6 characters in body
Source Link
Shawn
  • 1.9k
  • 12
  • 23
def match(
        infile: Union[str, pathlib.Path], 
        matchfile: Union[str, pathlib.Path], 
        outfile: Union[str, pathlib.Path],
        resampling_method: Resampling=Resampling.nearest
    ):
    """Reprojects the input raster so that it matches the 'matching' one.

    Parameters
    ----------
    infile : Union[str, pathlib.Path]
        The input raster
    matchfile : Union[str, pathlib.Path]
        The raster to match
    outfile : Union[str, pathlib.Path]
        The path where to save the resulting reprojection
    resampling_method : Resampling, optional
        The resampling method to use, by default Resampling.nearest
    """
    with rasterio.open(infile) as src:
        with rasterio.open(matchfile) as match_src:
            dst_kwargs = src.meta.copy()
            match_profile = match_src.profile
            dst_kwargs.update({"crs"       : match_profile['crs'],
                               "transform" : match_profile['transform'],
                               "width"     : match_profile['width'],,
                               "height"    : match_profile['height'],})

        with rasterio.open(outfile, "w", **dst_kwargs) as dst:
            for i in range(1, src.count + 1):
                reproject(
                    source=rasterio.band(src, i),
                    destination=rasterio.band(dst, i),
                    src_transform=src.transform,
                    src_crs=src.crs,
                    dst_transform=match_profile['transform'],
                    dst_crs      =match_profile['transform']=match_profile['crs'],
                    resampling=resampling_method)
def match(
        infile: Union[str, pathlib.Path], 
        matchfile: Union[str, pathlib.Path], 
        outfile: Union[str, pathlib.Path],
        resampling_method: Resampling=Resampling.nearest
    ):
    """Reprojects the input raster so that it matches the 'matching' one.

    Parameters
    ----------
    infile : Union[str, pathlib.Path]
        The input raster
    matchfile : Union[str, pathlib.Path]
        The raster to match
    outfile : Union[str, pathlib.Path]
        The path where to save the resulting reprojection
    resampling_method : Resampling, optional
        The resampling method to use, by default Resampling.nearest
    """
    with rasterio.open(infile) as src:
        with rasterio.open(matchfile) as match_src:
            dst_kwargs = src.meta.copy()
            match_profile = match_src.profile
            dst_kwargs.update({"crs"       : match_profile['crs'],
                               "transform" : match_profile['transform'],
                               "width"     : match_profile['width'],,
                               "height"    : match_profile['height'],})

        with rasterio.open(outfile, "w", **dst_kwargs) as dst:
            for i in range(1, src.count + 1):
                reproject(
                    source=rasterio.band(src, i),
                    destination=rasterio.band(dst, i),
                    src_transform=src.transform,
                    src_crs=src.crs,
                    dst_transform=match_profile['transform'],
                    dst_crs      =match_profile['transform'],
                    resampling=resampling_method)
def match(
        infile: Union[str, pathlib.Path], 
        matchfile: Union[str, pathlib.Path], 
        outfile: Union[str, pathlib.Path],
        resampling_method: Resampling=Resampling.nearest
    ):
    """Reprojects the input raster so that it matches the 'matching' one.

    Parameters
    ----------
    infile : Union[str, pathlib.Path]
        The input raster
    matchfile : Union[str, pathlib.Path]
        The raster to match
    outfile : Union[str, pathlib.Path]
        The path where to save the resulting reprojection
    resampling_method : Resampling, optional
        The resampling method to use, by default Resampling.nearest
    """
    with rasterio.open(infile) as src:
        with rasterio.open(matchfile) as match_src:
            dst_kwargs = src.meta.copy()
            match_profile = match_src.profile
            dst_kwargs.update({"crs"       : match_profile['crs'],
                               "transform" : match_profile['transform'],
                               "width"     : match_profile['width'],,
                               "height"    : match_profile['height'],})

        with rasterio.open(outfile, "w", **dst_kwargs) as dst:
            for i in range(1, src.count + 1):
                reproject(
                    source=rasterio.band(src, i),
                    destination=rasterio.band(dst, i),
                    src_transform=src.transform,
                    src_crs=src.crs,
                    dst_transform=match_profile['transform'],
                    dst_crs      =match_profile['crs'],
                    resampling=resampling_method)
Source Link
Shawn
  • 1.9k
  • 12
  • 23

You do not need to pre-calculate the transform to do the resample, you can just give it the transform of the raster you want to match and reproject will do what is needed. That should ensure the resulting pixels match exactly.

Like so:

def match(
        infile: Union[str, pathlib.Path], 
        matchfile: Union[str, pathlib.Path], 
        outfile: Union[str, pathlib.Path],
        resampling_method: Resampling=Resampling.nearest
    ):
    """Reprojects the input raster so that it matches the 'matching' one.

    Parameters
    ----------
    infile : Union[str, pathlib.Path]
        The input raster
    matchfile : Union[str, pathlib.Path]
        The raster to match
    outfile : Union[str, pathlib.Path]
        The path where to save the resulting reprojection
    resampling_method : Resampling, optional
        The resampling method to use, by default Resampling.nearest
    """
    with rasterio.open(infile) as src:
        with rasterio.open(matchfile) as match_src:
            dst_kwargs = src.meta.copy()
            match_profile = match_src.profile
            dst_kwargs.update({"crs"       : match_profile['crs'],
                               "transform" : match_profile['transform'],
                               "width"     : match_profile['width'],,
                               "height"    : match_profile['height'],})

        with rasterio.open(outfile, "w", **dst_kwargs) as dst:
            for i in range(1, src.count + 1):
                reproject(
                    source=rasterio.band(src, i),
                    destination=rasterio.band(dst, i),
                    src_transform=src.transform,
                    src_crs=src.crs,
                    dst_transform=match_profile['transform'],
                    dst_crs      =match_profile['transform'],
                    resampling=resampling_method)