How to make a logical mask using a shape file?

I am using following MATLAB code to create logical mask.
% Create a logical mask
[Z,R] = readgeoraster(img_file.name);
disp(R);
[lat,lon] = geographicGrid(R);
[Lon, Lat] = meshgrid(lon, lat);
logical_mask = reshape(isinterior(polygon, Lon(:), Lat(:)), size(Lon));
It is giving following erros
Incorrect number or types of inputs or outputs for function geographicGrid.
Error in ds_indices_caf (line 57)
[lat,lon] = geographicGrid(R);
I request to kindly suggest me how to do it correctly.
Dava

Answers (1)

You have to define lat and lon as vectors before you use them in meshgrid to get the full matrices Lat and Lon as outputs.

8 Comments

OK, you edited the code to fix the missing definitions of lat and lon, but this is what we get
% Create a logical mask
[lat,lon] = geographicGrid(img_file.name);
Unable to resolve the name 'img_file.name'.
[Lon, Lat] = meshgrid(lon, lat);
logical_mask = reshape(isinterior(polygon, Lon(:), Lat(:)), size(Lon));
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
DGM
DGM on 27 Mar 2024
Edited: DGM on 27 Mar 2024
geographicGrid() doesn't accept a filename as an input argument.
I have attached the data (image file and shape file) in Data.zip. Test.m is the matlab code. I request you to please have a look on it and suggest me how to make it run successfuly.
Thanks a lot for your kind cooperation.
Deva
It looks like it probably requires the Mapping Toolbox, which I do not have. Does it?
help geographicGrid
--- help for map.rasterref.GeographicCellsReference/geographicGrid --- geographicGrid - Geographic coordinates of raster elements [LAT,LON] = geographicGrid(R) returns the locations of raster elements in geographic coordinates as 2-D arrays, LAT and LON. When R is a GeographicCellsReference object, the locations are cell centers. When R is a GeographicPostingsReference object, the locations are posting points. The location of raster element (i,j) is (LAT(i,j), LON(i,j)). The values of size(LAT) and size(LON) both equal the RasterSize property of R. [LAT,LON] = geographicGrid(R,gridOption), where gridOption is 'gridvectors', returns LAT and LON as row vectors. The location of raster element (i,j) is (LAT(i), LON(j)). The value of [length(LAT) length(LON)] equals the RasterSize property of R. The default for gridOption is 'fullgrid'. geographicGrid(R,'fullgrid') is equivalent to geographicGrid(R). Example ------- R = georefpostings([0 30],[-20 20],[4 5]) [lat,lon] = geographicGrid(R) [lat,lon] = geographicGrid(R,"gridvectors") Documentation for map.rasterref.GeographicCellsReference/geographicGrid doc map.rasterref.GeographicCellsReference/geographicGrid
I have installed Mapping Toolbox but it still gives the same errors
Unrecognized function or variable 'geographicGrid'.
Error in ds_indices_caf (line 57)
[lat,lon] = geographicGrid(R);
Deva
@Devendra: do you have a valid license for that toolbox?
I have installed trial version of Matlab 2024a.
Deva
I believe during the trial period you get free telephone help. Call them and work through it with an actual live person. I can't help you because I do not have that toolbox.

Sign in to comment.

Products

Release

R2024a

Asked:

on 26 Mar 2024

Commented:

on 28 Mar 2024

Community Treasure Hunt

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

Start Hunting!