Clear Filters
Clear Filters

Saving matrix as a .tiff file with projection?

11 views (last 30 days)
Dankur Mcgoo
Dankur Mcgoo on 19 Dec 2019
Answered: Yan Tong on 16 Mar 2020
I have a matrix (618x1169 single) with values ranging from -6e(36) to 38355.4 that I am trying to save as a raster/Tiff. I would prefer to keep the geospatial information from the imported raster, however, I could alternatively reset the projection later if I coud find a way to save the raster without any sort of geospatial data. I have tried any different solutions from this website but I cannot find anything that works. I have attached the .mat file of the imported .tiff I am using.
The former code block has this error:
The input, R, is a map.rasterref.MapCellsReference object indicating that you are working in a projected coordinate system. If so, then specify a projected coordinate system by setting the appropriate values for the 'CoordRefSysCode' or 'GeoKeyDirectoryTag' optional parameters.
The latter results in a raster that looks like the image attached, which is the US flipped upside down. I don't think it's because I have it incorrectly projected.
In the end, I would like this matrix saved as a tif, with or without any sort of geospatial info attached to it, as I can set it in QGIS.
%% Atempt 1: I have tried the following, which results in an error.
[tifRaster, R] = geotiffread('importRaster.tif');
% Numbers are transformed (not shown) but the result is still a 618x1169 single matrix.
geotiffwrite('exportRaster.tif',tifRaster,R); % Error
%% Attempt 2: I have also tried the following based on a previous Answer but the projection is messed up and my raster is flipped.
[tifRaster] = geotiffread('importRaster.tif');
% Numbers are transformed (not shown) but the result is still a 618x1169 single matrix.
R=georasterref();
R.RasterSize=size(tifRaster);
geotiffwrite('exportRaster.tif',tifRaster,R); % Incorrect result

Answers (1)

Yan Tong
Yan Tong on 16 Mar 2020
According to the error shown in MATLAB, you should do as followings:
 [A, R]=geotiffread('filename');
 info=geotiffinfo('filename');
geotiffwrite('newfilename', A, R, 'GeoKeyDirectoryTag', info.GeoTIFFTags.GeoKeyDirectoryTag)

Community Treasure Hunt

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

Start Hunting!