How I can view this .tif file?

I have a tif file which represents the altitude for a country, I can simply open it in Esri's ArcGIS (Arc Map), Here is the result from ArcMap:
You can see the High and Low elevations are shown by a color range. I used this code below to open this tif file in Matlab:
x = imread('DEM_30s.tif', 'tif');
imshow(x)
But I see a figure like this:
So, If anyone could tell me how I can read my tif file in Matlab with a color bar that specifies high and low elevations (like Arcmap picture above), I would be so grateful.
Note: Since the size of the file is 7 MB, I split it into the 2 part then attach here; I also upload the .tif in my google drive in this link and compressed version of it (.zip) (just one part) here in my google drive.
Thank you

8 Comments

geotiffread()
Dear Wlater Roberson,
Thank you, I used this:
clear
[X,R] = geotiffread('DEM_30s.tif');
figure
mapshow(X,R);
axis image off
But I got this error:
Error using mapshow
Expected input number 1, I or X or RGB, to be one of these types:
uint8, uint16, double, logical
Instead its type was single.
Error in validateMapRasterData>parseImageInputs (line 108)
validateattributes(A, {'uint8', 'uint16', 'double', 'logical'}, ...
Error in validateMapRasterData>validateImageComponent (line 92)
[A, R] = parseImageInputs(mapfcnname, dataArgs{:}, cmap, rules );
Error in validateMapRasterData (line 26)
[Z, SpatialRef] = validateImageComponent(mapfcnname, ...
Error in maprastershow (line 131)
validateMapRasterData('mapshow', dataArgs, displayType, HGpairs);
Error in mapshow (line 240)
h = showFcn(varargin{:});
After that I used this:
clear
[A,R] = readgeoraster('DEM_30s.tif','OutputType','double');
geoshow(A,R,'DisplayType','surface')
demcmap(A)
But it generate this image below which not ok again:
Thank you again
BN
BN on 13 Apr 2020
Edited: BN on 13 Apr 2020
Thanks but it's failed too. I used this:
clear
[X,R] = geotiffread('DEM_30s.tif');
figure
mapshow(double(X), R)
axis image off
Here is the error:
Error using checkRefObj (line 25)
Function mapshow expected input number 2, R, to be either a 3-by-2
referencing matrix or a scalar map raster reference object. Instead its
type was: map.rasterref.GeographicCellsReference.
Error in validateMapRasterData>parseImageInputs (line 112)
refmat = checkRefObj(mapfcnname, R, size(A), R_position);
Error in validateMapRasterData>validateImageComponent (line 92)
[A, R] = parseImageInputs(mapfcnname, dataArgs{:}, cmap, rules );
Error in validateMapRasterData (line 26)
[Z, SpatialRef] = validateImageComponent(mapfcnname, ...
Error in maprastershow (line 131)
validateMapRasterData('mapshow', dataArgs, displayType, HGpairs);
Error in mapshow (line 240)
h = showFcn(varargin{:});
I don't know what to do, Do you think it is even possible?
Thanks
Here is the results of R:
R =
GeographicCellsReference with properties:
LatitudeLimits: [24.9677959090911 39.7833203454547]
LongitudeLimits: [44.0349540000001 63.3751296000001]
RasterSize: [1778 2321]
RasterInterpretation: 'cells'
ColumnsStartFrom: 'north'
RowsStartFrom: 'west'
CellExtentInLatitude: 0.0083326909090909
CellExtentInLongitude: 0.0083326909090909
RasterExtentInLatitude: 14.8155244363636
RasterExtentInLongitude: 19.3401756
XIntrinsicLimits: [0.5 2321.5]
YIntrinsicLimits: [0.5 1778.5]
CoordinateSystemType: 'geographic'
AngleUnit: 'degree'
I guess I should use demcmap (Colormaps appropriate to terrain elevation data), But I don't know how to use it.

Sign in to comment.

 Accepted Answer

Ameer Hamza
Ameer Hamza on 13 Apr 2020
Edited: Ameer Hamza on 13 Apr 2020
First, the problem is somewhat related to the encoding of your tiff file. The data values are encoded as single-precision numbers, and the background points are encoded as 0xff7fffff in hexadecimal (equal to -3.40282346639e+38, minimum possible number in IEEE754 single-precision floating-point format).
Second, the tiff file only has 1 channel, and have no color information, so you need to create your colormap.
Both problems can be solved by converting the loaded matrix from single to uint16. Note that we are converting the values, not typecasting. Converting to uint16 will convert the background pixels from -3.40282346639e+38 to 0. The elevation values lie in the range [0 - 10000], uint16 is sufficient to hold these values. The integer values will make it an indexed image that can take value from a colormap. Try the following code
[A,R] = readgeoraster('DEM_30s.tif', 'OutputType', 'double');
low_single = typecast(uint8([255 255 127 255]), 'single');
mask = low_single==A;
A = A - min(A(~mask), [], 'all');
% creating colormap changing from white to brown as shown in image in the question
brown_color = [0.8 0.2 0.1];
light_brown = [0.95 0.9 0.8];
t = linspace(0, 1, max(A,[],'all'))';
cmap = t.*brown_color + (1-t).*white;
gs = geoshow(A, cmap, R);
gs.CData(repmat(mask,1,1,3)) = 255;

5 Comments

BN
BN on 13 Apr 2020
Edited: BN on 13 Apr 2020
Dear Ameer Hamza,
First of all, I want to thank you for reading my question and answering. It's solved my problem, thank you. Actually I have 2 quick questions:
1. So the elevation in the generated figure is the same as the arc map results? (I ask this because I thought there were some elevations under 0 (I mean under the sea level) in the country but you said elevation values lie in the range [0 - 10000]. However, the figure really seems like arc map image.
2. I extracted X and Y of the country from its shapefile and plot a border around it using this code,
hold on
plot (X,Y,'-','color','k')
The result is: ( I changed white to very very light brown):
Do you think it is possible to change all colors outside the country to white? In this way, I can set any color instead of white for low elevations, for example, brown and yellow (without changing the background of all figure to yellow). Also is it possible to have a correct color bar?
Really Appreciate and Thank you a lot.
Best Regards
Yes, you are correct. There are depressions in the map. The maximum one at -169 meters. I elevated the whole map by 169 so that they will also have a different color. However, they occupy a tiny region, so they are not easily visible. Also, I have added the line to make the background color white. Please check the updated code. You can add the borderline using the updated code.
Awesome, Thank you so much ???
I am glad to be of help.
Hi Ameer Hamza,
Your solution is great, I have a question related. How do you add a colorbar to the plot with the colormap you defined?

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!