Create an area mask using a shapefile
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I am trying to mask my study area using a shapefile But unable to do so. Here is the code that i was trying.. I'll be really grateful if someone can help or provide me with some alternative method to achieve it.
Thank you so much.
% Load the data
load Data.mat
shapefile = 'India_shapefile.shp';
S = shaperead(shapefile);
polygon = polyshape([S.X], [S.Y]);
% Create a logical mask
logical_mask = inpolygon(lon, lat, polygon.Vertices(:, 1), polygon.Vertices(:, 2));
% Use the logical mask to extract data from matrix
extracted_data = grid_data(logical_mask);
채택된 답변
Walter Roberson
2023년 9월 6일
0 개 추천
댓글 수: 9
Thanks for the reply but it didn't help me
You are using a shape file for India, but your lat and lon correspond mostly to the Indian Ocean up to roughly Singapore. For example Jakarta is inside your search area.
Okay, I got it. Thank you for pointing out the mistake.
Hello Walter, I have another question related to the previous one. While examining my netCDF files, which I used to extract my data, I noticed that my original data and the shapefile appear to be inversely mirrored rather than overlapping correctly. I can confirm that my shapefile has the correct projection. I've tried various rotation methods for my original grid, but none seem to align the two datasets properly. Can you offer any assistance or insights regarding this issue?
I would need something more specific -- the data file and to have you point out at least two example points for me to look at.
A common problem when examining netCDF files is that netCDF arrays are transposed compared to MATLAB conventions, and ncread does not transpose them when reading them. If the netcdf file says that the first dimension is latitude and the second dimension is longitude and you ncread() then what you get in MATLAB will have a first dimension that is longitude and a second dimension that is latitude. Another way of saying this is that arrays in netCF are indexed across rows -- netcdf_A(3,5) is third column 5th row in the file.
Here is my dataset. It appears that in this context, latitudes are considered negative in the Northern Hemisphere. Consequently, when attempting to superimpose "borders" on the existing data, they do not align as expected. Even reversing the sign of latitudes does not resolve the issue.
As you asked me to point out example points, if you see this data.. imagesc(lon, lat, data) hold on borders . I guess you will understand the problem. It will be really helpful if you can suggest something.
% Load the data
load data.mat
shapefile = fullfile('India Shapefile', 'India_shapefile.shp');
S = shaperead(shapefile);
%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.X}, {S.Y});
% Create a logical mask
[Lon, Lat] = meshgrid(lon, lat);
logical_mask = reshape(isinterior(polygon, Lon(:), Lat(:)), size(Lon));
% Use the logical mask to extract data from matrix
extracted_data = test_data(logical_mask);
subplot(3,1,1);
imagesc(lon, lat, test_data); title('data'); xlabel('lon'); ylabel('lat'); axis equal; set(gca, 'YDir', 'normal');
subplot(3,1,2);
plot(polygon); title('borders'); xlabel('lon'); ylabel('lat'); axis equal; set(gca, 'YDir', 'normal');
subplot(3,1,3);
imagesc(lon, lat, logical_mask); title('selected points'); xlabel('lon'); ylabel('lat'); axis equal; set(gca, 'YDir', 'normal');
You have to be careful with X and Y. Y is latitude, X is longitude, but you were passing in latitude (Y) into imagesc in the place it expects X coordinates. Also by default imagesc flips images, so we account for that.
Remember that extracted_data is going to be a vector, in the order that find() would report that logical_mask entries are true, which scans down columns. All of the true locations for one column will be extracted, then all of the true locations for the next column to the right, and so on. The code does not construct a "bounding box" or a convex hull. The extracted_data vector is generally not useful if you need to make any kind of spatial analysis. If you need to do something like contour the data within the bounds, then you would be better of doing something like
selected_data = test_data;
selected_data(~logical_mask) = nan;
Thank you so much...
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Coordinate Reference Systems에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
