How can I load a portion of a .tif file using readgeoraster?

19 views (last 30 days)
I need to work with a .tif file containing elevation data relative to a region of the world. This file I got is too big to be loaded in MatLab and it saturates the memory, so I wanted to extract only a portion of it. I already know how to do it using imread, but since I want to collect all the information stored in the file I would prefer to use readgeoraster.
Do you know about a way to select a range of the picture to load using this function?
I can't load the .tif file because it's huge, but I can give you its info:
FileFormat: "GeoTIFF"
RasterSize: [85809 119862]
NumBands: 1
NativeFormat: "int16"
MissingDataIndicator: 32767
Categories: []
ColorType: "grayscale"
Colormap: []
RasterReference: []
CoordinateReferenceSystem: [1×1 geocrs]
Metadata: [1×1 struct]
Thanks in advance!

Answers (2)

prabhat kumar sharma
prabhat kumar sharma on 10 Jan 2025
This answer was flagged by Joshua
Hello Alessio,
When dealing with large GeoTIFF files, readgeoraster in MATLAB is a suitable function because it allows you to work with geospatial data and extract specific portions without loading the entire file into memory. However, readgeoraster does not directly support reading a subset of the data by specifying row and column limits. Instead, you can use georasterinfo to get metadata and then readgeoraster to read specific regions using spatial referencing.
Here's a step-by-step guide on how to extract a specific portion of a GeoTIFF file using readgeoraster:
Step 1: Get GeoTIFF Information
First, obtain the metadata from the GeoTIFF file to understand its spatial referencing and coordinate system.
info = georasterinfo('your_large_file.tif');
Step 2: Define the Region of Interest
Based on the spatial referencing, define the geographic coordinates or pixel indices of the area you want to extract. Suppose you know the geographic bounds (latitude and longitude) of the region you are interested in.
Step 3: Read the Desired Region
Use the spatial referencing to read only the desired region. You can specify a bounding box in geographic coordinates or pixel indices.
% Define geographic limits (example values)
latlim = [minLatitude, maxLatitude];
lonlim = [minLongitude, maxLongitude];
% Read the specified region
[A, R] = readgeoraster('your_large_file.tif', 'LatitudeLimits', latlim, 'LongitudeLimits', lonlim);
  • atitudeLimits and LongitudeLimits: These options allow you to specify the geographic bounding box of the region you want to extract. Replace minLatitude, maxLatitude, minLongitude, and maxLongitude with the actual coordinates of your region of interest.
  • A: The output matrix containing the elevation data for the specified region.
  • R: The spatial referencing object for the extracted data.
I hope it helps!

DGM
DGM on 2 Mar 2025
Edited: DGM on 2 Mar 2025
As far as Joshua and the documentation suggest, the accepted answer is nonsense, but I can't demonstrate it specifically with a GeoTIFF. I don't have a new enough version to use readgeoraster(), and for some reason, the TIFF examples from the mapping toolbox aren't available on the forum (probably due to their size); likewise, they're too large to be attached. I'll have to use something other than a GeoTIFF.
Adapted from the documentation for readgeoraster():
% read the file and reference data
fname = 'n39_w106_3arc_v2.dt1'; % your file
[A,R] = readgeoraster(fname,'OutputType','double');
% these are the extents of the image
% if you want to look at a subset, they need to be within these limits.
latlim = R.LatitudeLimits;
lonlim = R.LongitudeLimits;
% plot it
usamap(latlim,lonlim)
geoshow(A,R,"DisplayType","surface")
demcmap(A)
If we had followed the accepted answer, this is the error we'd get.
[A,R] = readgeoraster(fname,'LatitudeLimits', latlim, 'LongitudeLimits', lonlim);
Error using readgeoraster (line 1)
Invalid argument name 'LatitudeLimits'. Name must be 'OutputType', 'Bands', or 'CoordinateSystemType'.
This usage is not valid for readgeoraster(), and it's not similar to anything that would be valid for legacy tools like geotiffread(). If it weren't for a lone typo, I'd be certain that this is just AI chaff.
I don't know how this answer got accepted, but my guess is that the time difference means that OP never actually tested the late answer because they didn't need it anymore.

Community Treasure Hunt

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

Start Hunting!