필터 지우기
필터 지우기

How to resolve the error for creating mask from shape file?

조회 수: 10 (최근 30일)
Aksh
Aksh 2024년 5월 27일
편집: Shubham 2024년 6월 30일 17:57
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
KSSV
KSSV 2024년 5월 28일
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');

댓글을 달려면 로그인하십시오.

답변 (1개)

Shubham
Shubham 2024년 6월 30일 17:54
편집: Shubham 2024년 6월 30일 17:57
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!

카테고리

Help CenterFile Exchange에서 Elementary Polygons에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by