How to resolve the error for creating mask from shape file?
1 view (last 30 days)
Show older comments
I am using following MATLAB code to create mask using shape file
S = shaperead(shapefile.name);
% Shapefile projections
info = shapeinfo(shapefile.name);
crs = info.CoordinateReferenceSystem;
[S(1).lon,S(1).lat] = projinv(crs,S(1).X,S(1).Y);
%remove small triangles and squares
coord_lengths = arrayfun(@(s) numel(s.X), S);
not_useful_mask = coord_lengths < 10;
S(not_useful_mask) = [];
polygon = polyshape({S(1).lon}, {S(1).lat});
[Z,R] = readgeoraster('C:\work\ndvi_2022_23.dat');
p = R.ProjectedCRS;
[x,y] = worldGrid(R);
[lon,lat] = projinv(p,x,y);
% Create a logical mask
logical_mask = reshape(isinterior(polygon, lon(:), lat(:)), size(lon));
However, it is giving following error;
Ale.shp
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or
unexpected results. Input data has been modified to create a well-defined polyshape.
> In polyshape/checkAndSimplify (line 526)
In polyshape (line 175)
In indices_caf (line 45)
Requested 408x5604579 (17.0GB) array exceeds maximum array size preference (15.8GB). This might cause MATLAB to
become unresponsive.
Error in polyshape/isinterior>check_inpolygon (line 65)
x = x(ones(Nv,1),:);
Error in polyshape/isinterior (line 50)
[in, on] = check_inpolygon(X, Y, xv, yv, tol);
Error in indices_caf (line 53)
logical_mask = reshape(isinterior(polygon, lon(:), lat(:)), size(lon));
shape file is attached as zip file while data file ndvi.dat is uploaded in the following link;
I would appreciate any help.
1 Comment
KSSV
on 28 May 2024
Error using readgeoraster
Unable to read 'ndvi.dat'. Format may not be supported, file may be corrupt, or a supporting file may have been specified.
Error in Junk (line 13)
[Z,R] = readgeoraster('ndvi.dat');
Answers (1)
Shubham
on 30 Jun 2024
Edited: Shubham
on 30 Jun 2024
Hey Aksh,
Based on the error message, an array of size 17GB is being requested. The maximum array size preference as stated by the error message is 15.8GB.
If you have the available RAM, then you could simply change your Maximum array size limit and set it to utilize 100% capacity. You can achieve this by navigating to
MATLAB >Home >Preferences (present in Environment section) >Workspace
and modifying the Maximum array size preferences.
However, if you are working with a system having memory limitations, then this would cause an "Out of Memory" Error:
In such a case, the best practise would be to optimize your script as per the available memory.
Based on the error message you are recieving (array of size 408x5604579) you are providing query points as an array of length 5604579 while calling the "isinterior" function. Since the "isinterior" function is used to just query whether a point is present inside of a polyshape, you can achieve the same functionality by chunking the array and calling the function in batches. For this divide the arrays used for querying i.e. "lon(:)" and "lat(:)" in this case.
Larger the number of points in the polyshape, the smaller chunk size of the array for query points would be. You can try out different chunk sizes based on your memory limitations.
For more information regarding the "isinterior" function, you may refer to its documentation here:
I hope this helps!
0 Comments
See Also
Categories
Find more on Elementary Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!