Clear Filters
Clear Filters

Error in geotiff display

4 views (last 30 days)
Ashrumochan Mohanty
Ashrumochan Mohanty on 14 May 2022
Answered: Siraj on 29 Sep 2023
clc
clear all
filename='E:\Data\netCDF\rainfall\_Clim_Pred_LRF_New_RF25_IMD0p251990.nc';
%ncdisp(filename);
lat=ncread(filename,'LATITUDE');
Error using internal.matlab.imagesci.nc/openToRead
Could not open E:\Data\netCDF\rainfall\_Clim_Pred_LRF_New_RF25_IMD0p251990.nc for reading.

Error in internal.matlab.imagesci.nc (line 124)
this.openToRead();

Error in ncread (line 69)
ncObj = internal.matlab.imagesci.nc(ncFile);
lon=ncread(filename,'LONGITUDE');
transpose_lon=lon';
days=ncread(filename,'TIME');
pr=ncread(filename,'RAINFALL');
for count = 1: size(lat)
lon_array(count,:)=transpose_lon;
end
for count = 1: size(lon)
lat_array(:,count)=lat;
end
value_array=pr (:,:,2);
% load the value matrix and lat and lon arrays
value = value_array';
lat_referance = lat_array';
lon_referance = lon_array';
% Create the referencing matrix (R)
R = georasterref('RasterSize',size(value), ...
'LatitudeLimits',[min(lat) max(lat)],'LongitudeLimits',[min(lon) max(lon)]);
outfilename = 'India3.tif';
geotiffwrite(outfilename, value, R);
% [A,R] = readgeoraster(outfilename);
% mapshow(A,R);
figure
mapshow India3.tif
I want to create a geo referanced tiff file(geotiff) from a netCDF file. and i have created sucessfully as I have checked it in a GIS software. Now I want to display the geotiff file. But it is not showing. I am getting some error. I have attached the pic and code.

Answers (1)

Siraj
Siraj on 29 Sep 2023
Hi!
I understand that you have created a geotiff file and saved it using the "geotiffwrite" function. The spatially referenced matrix "R" was created using the "georasterref" function. However, when you try to read the file and display its data using the "mapshow" function, an error occurs.
The error you are encountering indicates that there might be an issue with how the file was written or with the spatial reference matrix. To resolve this, you can try creating the reference matrix using either the "MapCellsReference" or "MapPostingsReference" type. For example.
R = georefpostings([0 1], [0 1], size(imageData));
You can refer to the following link to understand the expected format of the "R" matrix for the "mapshow" function and explore different functions available to create the "R" matrix:
As a workaround, you can try using the "geoshow" function instead of "mapshow" to display the data from the geotiff file. To understand how to use "geoshow" and its functionality, you can refer to the following link:
In the below provided code, the "geoshow" function is used to display the data from the geotiff file, and it does not produce any errors.
% Generate random image data
imageData = rand(100, 100);
% Define spatial reference information
R = georasterref('RasterSize', size(imageData), 'LatitudeLimits', [0 1], 'LongitudeLimits', [0 1]);
% Create GeoTIFF file
filename = 'random_image.tif';
geotiffwrite(filename, imageData, R);
% Display confirmation message
disp(['GeoTIFF file saved as: ' filename]);
GeoTIFF file saved as: random_image.tif
% Read the GeoTIFF file
[A, R] = readgeoraster(filename);
% Display the GeoTIFF image using geoshow
figure
geoshow(A, R, "DisplayType","mesh");
Hope this helps.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!