Contours and shapefiles
Show older comments
Hi,
I am trying to plot contours over a map of US climate divisions. I imported a climate division shapefile, then found the centroid of each division. Next, I used the meshgrid and triscatteredInterp functions to interpolate the data on a grid. Finally I used the contour function to get isolines.
Once I plotted my isolines over my USA climate division map, I noticed that the contours go outside the US (and outside my centroids used in the meshgrid, etc. calculations). Is there a way to "contain" the centroids to the boundaries of the USA?
Thanks! Deb
Answers (1)
Kelly Kearney
on 3 Aug 2011
Not quite sure whether your trying to contain individual points, or the contour lines themselves, and whether you're trying to mask with a polygon (the US coastlines) or just a bounding box. A few functions that might help:
help inpolygon
help polybool
help maptriml
help maptrimp
If none of these seem to do the trick, perhaps a little more detail on your data could be helpful.
EDIT: an example. I've used the contourcs function to extract isolines; I assume you already have this data, but if you want to run my example, download that from the FEX.
% Load coastlines (replace with your coastlines)
latlim = [23 50];
lonlim = [-127 -65];
Usa = shaperead('landareas', 'usegeo', true, 'bounding', [lonlim' latlim']);
[latusa, lonusa] = maptrimp(Usa(1).Lat, Usa(1).Lon, latlim, lonlim);
% Create isolines (replace with your isolines)
n = 50;
xdata = linspace(lonlim(1), lonlim(2), n);
ydata = linspace(latlim(1), latlim(2), n);
zdata = peaks(n);
S = contourcs(xdata, ydata, zdata); % FEX
% Trim to coastlines
[xc, yc] = deal(cell(length(S),1));
for ii = 1:length(S)
[xc{ii}, yc{ii}] = polybool('&', S(ii).X, S(ii).Y, lonusa, latusa);
end
% Plot
figure; axes; hold on;
plot(lonusa, latusa, 'k');
cnew = [xc yc]';
hn = plot(cnew{:}, 'r');
set(hn, 'color', 'b');
3 Comments
Debra
on 3 Aug 2011
Kelly Kearney
on 4 Aug 2011
Okay, I've added an example to demonstrate using polybool to trim isolines to a coastline.
elvis asong ZILEFAC
on 15 Apr 2013
error: Improper index matrix reference.
Hi Kelly,
I have a similar problem and your answer does it all. However, I receive the above error just after running your code.
Asong.
Categories
Find more on Standard File Formats in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!