Issue when masking a raster map by a shape file using 'vec2mtx'

25 views (last 30 days)
Hi, I'm trying to mask a raster map by a shape file. The problem is at the end the masked region is shifted. I think it should be something wrong with 'vec2mtx'. Does anyone have a suggestion on this? The code is simply:
roi = shaperead(shape_name);
info = geotiffinfo(map_name);
[A, R] = geotiffread(map_name);
zone = '38S';
[Latlim, Lonlim] = projinv(info, R.XWorldLimits, R.YWorldLimits);
refmat = makerefmat('RasterSize', R.RasterSize, 'Latlim', Latlim, ...
'Lonlim', Lonlim, 'ColumnsStartFrom', R.ColumnsStartFrom, ...
'RowsStartFrom', R.RowsStartFrom);
rx = roi.X(1:end-1);
ry = roi.Y(1:end-1);
[shape_lat, shape_lon] = projinv(info, rx, ry);
[A1, R2] = vec2mtx(shape_lat, shape_lon, A, refmat, 'filled');
fin_idx = (A1 ~= 2);
A2 = A;
A2(fin_idx) = NaN;
geotiffwrite(out_map_name, A2, R,...
'GeoKeyDirectoryTag', info.GeoTIFFTags.GeoKeyDirectoryTag);
  1 Comment
Mohammad Gohardoust
Mohammad Gohardoust on 21 Jul 2017
The above maps are from Landsat, I tested this method on Modis maps with much lower resolution which seems to work.

Sign in to comment.

Answers (1)

Pat Canny
Pat Canny on 2 Feb 2026 at 13:48
I recommend contacting MathWorks Technical Support. This question requires more information.

Products

Community Treasure Hunt

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

Start Hunting!