Tuesday, March 23, 2010

matlab: shaded world map based on coast data


function FillCoast(Is360,FillColor)
% To show a shaded worldmap based on the coast data coming with matlab
% Not perfectly, but does work some way.
% Usage:
%       FillCoast(Is360,FillColor)
% Copyright: http://scriptdemo.blogspot.com
if (nargin<1)
     Is360=0;
     FillColor='r';
elseif   (nargin==1)
     if (isnumeric(Is360) && length(Is360)==1)
        FillColor='r';
     else
        FillColor=Is360;
        Is360=0;
     end
elseif   (nargin>2)
     help FillCoast
     return
end
load coast;
if   ~ishold  hold on; end
if   Is360
     long(long<0)=long(long<0)+360;
     lat1=lat;
     lat1(long<0.5)=nan;
     plot(long,lat1,'k'); clear lat1
end
IndNaN=find(isnan(long));
Ns=1;
for   N=1:length(IndNaN)-1
      Ne=IndNaN(N)-1;
      lonseg=long(Ns:Ne);
      latseg=lat(Ns:Ne);
      Ns=Ne+2;
     if (length(lonseg)>3)
        N360=find(lonseg>340);
        NZero=find(lonseg<10);
        switch ~isempty(N360) + ~isempty(NZero)
           case {0,1}
                if (~Is360 && isempty(find(latseg>-60)))
                   % Antarctic
                   lonseg=[lonseg; lonseg(end); lonseg(1)];
                   latseg=[latseg;         -90;       -90];
                end
                hf=patch(lonseg,latseg,FillColor); set(hf,'linestyle','none')
           case {2}
                if isempty(find(latseg>-60))
                % Antarctic
                  Nmin=find(lonseg==min(lonseg));
                  lonseg=[lonseg(1:Nmin-1); lonseg(Nmin-1); lonseg(Nmin); lonseg(Nmin:end)];
                  latseg=[latseg(1:Nmin-1);         -90;       -90; latseg(Nmin:end)];
                  hf=patch(lonseg,latseg,FillColor); set(hf,'linestyle','none')
               else
                 lonseg1=lonseg(lonseg>200 & lonseg<=360);
                 if ~isempty(lonseg1)
                    lonseg1=[lonseg1;lonseg1(1)];
                    latseg1=latseg(lonseg>200 & lonseg<=360);
                    latseg1=[latseg1;latseg1(1)];
                    hf=patch(lonseg1,latseg1,'b'); set(hf,'linestyle','none')
                 end;
                 lonseg2=lonseg(lonseg<195);
                 if ~isempty(lonseg2)
                    lonseg2=[lonseg2;lonseg2(1)];
                    latseg2=latseg(lonseg<195);  latseg2=[latseg2;latseg2(1)];
                    hf=patch(lonseg2,latseg2,'g'); set(hf,'linestyle','none')
                 end
               end
           otherwise
             disp('You should not see this info...STOP')
        end
     end
end




No comments:

ShowCalendar