How can I create contour on non-regular shape such as maps?
Show older comments
Hello all,
I have written a code in which the final aim is to provide contour map outlining frost depth on Alaska sate. For each weather station having specific lat. and long., I have estimated the probability that frost depth exceeds 1, 2 ,3, 4, and 5 feet (The first and second columns of "POFDE" are latitude and longitude respectively and columns 3 to 7 are showing the probability that frost depth exceeds 1 to 5 feet respectively) . Hence, I want to draw 5 contour maps, each showing the probabilties exceeding the mentioned depths. To do this, it is required to create a mesh on Alaska map and use scatteredInterpolant function to do interpolation between all existing weather station for which I estimated the probabilities. Can anyone help me in this regard?
In the code below, I created a mesh on a rectangular extending from minimum longitude to maximum longitude in the X direction, and from minimum latitude to maximum latitude in Y direction. However, that would cause to have contour lines in the water which is not acceptable.
Please use 'tl_2018_02_anrc.shp' file located in the shared zip folder for superimposing Alaska map. POFDE is also attached here.
clear
close all
clc
load('POFDE.mat');
S=shaperead('tl_2018_02_anrc.shp','UseGeoCoords',true);
data = cell2mat(POFDE);
%%%%%%generating mesh as query points based on the Alaska bounding box
X=linspace(-179.148909,-130.1,1500);
Y=linspace(51.214183,71.365162,1500);
[lon,lat] = meshgrid(X,Y);
for i = 1:(size(data, 2)-2)
figure
geoshow(S,"DisplayType","multipoint")
xlim([-179.148909,-130.1]);
ylim([51.214183,71.365162]);
I = scatteredInterpolant(data(:,[1 2]), data(:,i+2));
I.Method='linear';
I.ExtrapolationMethod='linear';
contourm(lat,lon,min(1,max(0,I(lat,lon))))
colorbar
title(['The probability of frost depth exceedance of \Omega_{\delta} = ' num2str(i) ' feet'])
exportgraphics(gca, sprintf('FrostPlot_%d_feet.png', i))
end
Answers (1)
Kausthub
on 5 Sep 2023
I understand that you want the contor lines only on the land and not in water. There are two possible workarounds:
- Consider random coordinates provided in the S which can be accessed using S(i).Lat and S(i).Lon for creating “lon” and “lat” instead of using linspace. This will ensure all the points are inside Alaska.
for i = 1:12
idx = randi(size(S(i).Lat),[125,1]);
lon = [lon S(i).Lon(idx)];
lat = [lat S(i).Lat(idx)];
end
- Utilize the bounding boxes provided which can be accessed using S(i).BoundingBox and check if the interpolated point lies inside the boxes, which can be done using rectangle (https://www.mathworks.com/help/matlab/ref/rectangle.html) and inpolygon (https://www.mathworks.com/help/matlab/ref/inpolygon.html)then it can be included in the contour plot.
for j = 1:12
arr = S(j).BoundingBox;
arr = reshape(arr',[1,size(arr,1)*size(arr,2)]);
arr(3) = arr(3)-arr(1);
arr(4) = arr(4)-arr(2);
rectangle('position',arr);
end
I hope this helps and addresses your question regarding potting contour lines over non-regular shapes like maps!
Categories
Find more on Line Plots 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!