how to create a georefernce matrix from 2d lat and lon matrix

16 views (last 30 days)
How can I create a GeoTIFF file from a 2d matrix of latitude and a 2D matrix of longitude and a 2d matrix of data like temperature? I need to combine the lat long and data to create a GeoTIFF file. The purpose is to create a temp file with each pixel representing lat-long and temperature.

Accepted Answer

Andreas Westergaard
Andreas Westergaard on 14 Apr 2021
I have tried to find a solution to this, but have yet to find a way in Matlab. The example from KSSV seems to work nicely if you have 1D arrays with lat lon coordinates which can be converted to an evenly spaced grid. However, for the problem OP refer to with a 2D lat lon which may not be evenly spaced or aligned within meridians it may not work/give a wrong georeference.
If you have a matrix "lat" and matrix "lon" and matrix "data" in a WGS84 coordinate system, then the function
geoshow(lat,lon,data,'DisplayType', 'surface');
seems to work to plot the data as a map. The problem from there is to export the map as e.g. a geotiff.
So any ideas on either exporting the map from the geoshow-figure or creating a reference matrix "R" from a 2D lat, 2D lon, 2D data input would be fantastic!?
  5 Comments
rajasweta datta
rajasweta datta on 7 Feb 2023
Edited: rajasweta datta on 7 Feb 2023
please try this link
the function coverts 2d matrix of lat, lon and data into a georeferenced dataset, helpful for model outputs.
all 3 values can be csv files, 2 for lat and long and other for data
code for example
lon <- raster(as.matrix(read.csv(paste0(basefolder,"lon.csv"),header=FALSE)))
lat <- raster(as.matrix(read.csv(paste0(basefolder,"lat.csv"),header=FALSE)))
x <- raster(as.matrix(read.csv(file,header=FALSE)))
result <- rasterizeRegCM(x,lat,lon)
writeRaster(result,paste0(substr(file,1,nchar(file)-4),".tif"))
Natalia
Natalia on 29 Feb 2024
Edited: Natalia on 29 Feb 2024
i was wondering if can i ask you for help to solve this? I am facing the same problem but in my case i have sometimes matrices with 2D dimensions, let's say ccaug=111x74 where 111 is longitude and 74 latitude, also my lat=111x74 and lon=111x74 and i have another matrix ccyraug=111x74x30, where 30 is my time (30 years), so in this case i have values within my domain 111x74. Should i use the same approach ? convert my matrices to .cvs and how can i convert each of the 30 years? and then how can i use the function you provided in R to apply it to my 30 years matrix? I've never used R before so it may be a bit tricky for me to use it for the first time.
Thanks in advance

Sign in to comment.

More Answers (1)

KSSV
KSSV on 28 Mar 2017
Let x, y be your vectors of longitude,latitude respectively. And data be your temperature matrix.
xmin = min(x) ; xmax = max(x) ;
ymin = min(y) ; ymax = max(y) ;
R = georasterref('RasterSize',size(data),'LatitudeLimits',[ymin,ymax],'LongitudeLimits',[xmin,xmax]);
geotiffwrite('myfile.tif',data,R) % write data to .tif file
%%Read geotiff file
[A, R] = geotiffread(tiffile);
figure
mapshow(A, R);
axis image off
  2 Comments
Hamidreza Norouzi
Hamidreza Norouzi on 10 Jan 2019
But she asked when x and y are two 2-D matrices, not vectors. I have the same question.
rajasweta datta
rajasweta datta on 18 Jan 2019
Hi ,
I got the solution but I actually had take help of R for this, if you still have the problem I might be able to help.
regards
Raj

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!