Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrong location of tif files #2

Open
crivisan opened this issue Sep 14, 2023 · 0 comments
Open

wrong location of tif files #2

crivisan opened this issue Sep 14, 2023 · 0 comments

Comments

@crivisan
Copy link

crivisan commented Sep 14, 2023

It seems like something went wrong during tif files generation. Basically the transformation matrix for (at least) '..._dem.tif' and '..._esaworlcover.tif' files carry wrong coordinates.

The following function may help you to fix the issue. Be aware it overwrites the file.

import os
import rasterio
from rasterio import Affine

def fix_transform(reference_s2_path, target_path):
    """Fix target file based on reference S2 band file.
    """
    reference_s2 = rasterio.open(reference_s2_path)
    with rasterio.open(target_path, 'r+') as target_to_fix:
        target_trans = target_to_fix.meta['transform']
        ref_trans = reference_s2.meta['transform']
        fixed_transform = Affine(target_trans[0], target_trans[1], ref_trans[2], target_trans[3],
                       target_trans[4], ref_trans[5])
        target_to_fix.transform = fixed_transform

if __name__ == '__main__':
    path1 = './S2A_MSIL2A_20170617T113321_16_43_B01.tif'
    #path2 = './S2A_MSIL2A_20170617T113321_16_43_dem.tif'
    path2 = './S2A_MSIL2A_20170617T113321_16_43_esaworldcover.tif'
    fix_transform(reference_s2_path=path1, target_path=path2) ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant