Modify the layers of a S2 observations and saving the result as a geotiff

2 次查看(过去 30 天)
Hi community. I am a PhD student.
For research reasons, I should have to read a tif file, corresponding to a S2 observation, change some bands and save the result as a geotiff. Then I should open this modified tif with SNAP, an ESA software and perform some actions, implemented in this software.
In the following code, for the sake of simplicity, I have just created a copy of trial_subset, saved as trial_subset_copiamia.
fn = "trial_subset.tif";
[A,R] = readgeoraster(fn);
info = geotiffinfo(fn);
% metadati per scrittura in tif
geoTags = info.GeoTIFFTags.GeoKeyDirectoryTag;
geotiffwrite("trial_subset_mycopy.tif",A,R,'GeoKeyDirectoryTag',geoTags);
The problem I encoutered is that, while in the original observation, read in SNAP, I have a certain organization of the optical observation, in the copied one, I don't have such organization (images attached), and this gives me problems in performing some actions in the software SNAP, because the software does not recognize the bands, having them names different from the ones in the original observation.
So, I would like to read a geotiff file, change something and write a new tif file, while maintaining all the metadata (also the name of the bands) of the original observation.
Thanks in advance for your help.
Luca Liverotti

回答(1 个)

Abhijeet
Abhijeet 2023-8-16
Hi Luca,
From the explanation, I understand that you have a geotiff file corresponding to a Sentinel-2 (S2) observation. You want to read this file, modify some bands, and save the modified version as a new geotiff file. However, when you open the modified file in SNAP (ESA software), the organization of the optical observation is different, causing problems because SNAP doesn't recognize the bands with their original names.
To address this issue, you would like to find a way to read a geotiff file, make changes to it while preserving all the metadata (including band names) of the original observation, and then save it as a new geotiff file.
To achieve your goal of modifying the geotiff file while preserving all the metadata, including band names, you can follow these steps:
Read the original geotiff file using the geotiffread function:
[A, R] = geotiffread('trial_subset.tif');
Retrieve the metadata information using the geotiffinfo function:
info = geotiffinfo('trial_subset.tif');
Make the desired modifications to the `A` array, which represents the pixel values of the bands.
Write the modified data to a new geotiff file using the geotiffwrite function. To preserve the metadata, including band names, you need to pass the `info` structure obtained from `geotiffinfo` as an argument:
geotiffwrite('trial_subset_modified.tif', A, R, 'GeoKeyDirectoryTag', info.GeoTIFFTags.GeoKeyDirectoryTag);
By following the above-mentioned steps, you can modify bands with the metadata preserved.
Thanks

类别

Help CenterFile Exchange 中查找有关 Import, Export, and Conversion 的更多信息

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by