Code covered by the BSD License  

Highlights from
Shaded Earth Map

image thumbnail

Shaded Earth Map

by

 

08 Aug 2011 (Updated )

Draws a path of the Earth with realistic colors.

drawEarthShaded(USAshaded,WorldShaded,Lakes)
function drawEarthShaded(USAshaded,WorldShaded,Lakes)
%DRAWEARTHSHADED Draws a shaded map of the Earth
%
%   DRAWEARTHSHADED Draw a map of the earth, with a realistic set of 
%       colors. This can be used as a background for any map plot you wish.
%       It is important to note that the shapefiles "landareas" and 
%       "usastateshi" are both used in order to generate this map. They are
%       both relatively coarse in their resolution and do not always line 
%       up, so this may not be suitable for some situations (i.e. scales of 
%       10s of kilometers near coastline).
%
%   DRAWEARTHSHADED(USAshaded,WorldShaded,Lakes) USAshaded toggles wether
%   or not the USA should be shaded and highlighted. WorldShaded toggles
%   wether of not the landareas shapefile ought to be drawn. Lakes toggles
%   wether of not the lakes should be colored in blue. By default, all 3 of
%   these values are on.
%
%   Example:
%       figure
%       drawEarthShaded
%       hold on
%       lat = 38.8904;
%       lon = -77.0320;
%       latlim = [-2 2] + lat;
%       lonlim = [-2 2] + lon;
%       plot(lon,lat,'rx')
%       text(lon,lat,'   Washington DC')
%       axis([lonlim latlim])
%       if exist('lat_lon_proportions','file')
%           lat_lon_proportions
%       end
%       title('Washington DC - Bad Coastline')
%
%       figure
%       drawEarthShaded(1,0,1)
%       hold on
%       lat = 38.8904;
%       lon = -77.0320;
%       latlim = [-2 2] + lat;
%       lonlim = [-2 2] + lon;
%       plot(lon,lat,'rx')
%       text(lon,lat,'   Washington DC')
%       axis([lonlim latlim])
%       if exist('lat_lon_proportions','file')
%           lat_lon_proportions
%       end
%       title('Washington DC - Better Coastline')
%
%   If you want your map to be proportioned correctly, call the function
%       lat_lon_proportions. This can be found on the File Exchange @
%       http://www.mathworks.com/matlabcentral/fileexchange/32462
%
% By: J Sullivan, May 2011

%Parse Inputs
if nargin < 1 || isempty(USAshaded); USAshaded = 1; end
if nargin < 2 || isempty(WorldShaded); WorldShaded = 1; end
if nargin < 3 || isempty(Lakes); Lakes = 1; end

%Define the colors
WaterColor = [143 226 255]/255;
BackgroundColor = [215 215 215]/255;
if USAshaded;
    USAColor = [215 215 175]/255;
else
    USAColor = BackgroundColor;
end

%Draw the Oceans
washold = ishold;
hold on
fill([-200 -200 200 200],[-100 100 100 -100],WaterColor);

%Draw the land areas
S1 = shaperead('landareas');
if WorldShaded
    for ii = 1:length(S1)
        f = find(isnan(S1(ii).X));
        ind = 1;
        for jj = 1:length(f)
            f1 = f(jj)-1;
            if jj == 1
                cd = BackgroundColor;
                fill(S1(ii).X(ind:f1),S1(ii).Y(ind:f1),cd);
            end
            ind = f(jj)+1;
        end
        fill(S1(ii).X(f1:end),S1(ii).Y(f1:end),cd);
    end
end

%Draw the USA
if USAshaded;
    S = shaperead('usastatehi');
    for ii = 1:length(S)
        f = find(isnan(S(ii).X));
        ind = 1;
        for jj = 1:length(f)
            f1 = f(jj)-1;
            fill(S(ii).X(ind:f1),S(ii).Y(ind:f1),USAColor);
            ind = f(jj)+1;
        end
        fill(S(ii).X(f1:end),S(ii).Y(f1:end),USAColor);
    end
end

%Draw the lakes
if Lakes
    for ii = 1:length(S1)
        f = find(isnan(S1(ii).X));
        ind = 1;
        for jj = 1:length(f)
            f1 = f(jj)-1;
            if ~(jj == 1)
                cd = WaterColor;
                fill(S1(ii).X(ind:f1),S1(ii).Y(ind:f1),cd);
            end
            ind = f(jj)+1;
        end
    end
end

%Redraw the Minnesota to shade the Northwest Angle correctly
if USAshaded;
    S = shaperead('usastatehi');
    for ii = 23
        f = find(isnan(S(ii).X));
        ind = 1;
        for jj = 1:length(f)
            f1 = f(jj)-1;
            fill(S(ii).X(ind:f1),S(ii).Y(ind:f1),USAColor);
            ind = f(jj)+1;
        end
        fill(S(ii).X(ind:f1),S(ii).Y(ind:f1),USAColor);
    end
end

%Reset the hold
if ~washold
    hold off
end

axis([-180 180 -90 90])

Contact us