# GEplot

## PURPOSE

## SYNOPSIS

function GEplot(filename,Lat,Lon,style,opt11,opt12,opt21,opt22)

## DESCRIPTION

     function GEplot(filename,Lat,Lon,style,opt11,opt12,opt21,opt22)

     Description: creates a file in kmz format that can be opened into Google Earth.
        GEplot uses the same syntax as the traditional plot function but
        requires Latitude and Longitudd (WGS84) instead of x and y (note that Lat is
        the first argument).
        If you need to convert from UTM to Lat/Lon you may use utm2deg.m also
        available at Matlab Central

     Arguments:
        filename Example 'rafael', will become 'rafael.kmz'.  The same name
                 will appear inside Temporary Places in Google Earth as a layer.
        dot_size Approximate size of the mark, in meters
        Lat, Lon Vectors containing Latitudes and Longitudes.  The number of marks
                 created by this function is equal to the length of Lat/Lon vectors
    (opt)style   allows for specifying symbols and colors (line styles are not
                 supported by Google Earth currently. (see bellow)
    (opt)opt...   allows for specifying symbol size and line width (see bellow)

     Example:
        GEplot('my_track',Lat,Lon,'o-r','MarkerSize',10,'LineWidth',3)
        GEplot('my_track',Lat,Lon)

     Plot style parameters implemented in GEplot
                color               symbol                line style
                -----------------------------------------------------------
                b     blue          .     point              -    solid
                g     green         o     circle          (none)  no line
                r     red           x     x-mark
                c     cyan          +     plus
                m     magenta       *     star
                y     yellow        s     square
                k     black         d     diamond
                w     white (new)   S     filled square (new)
                                    D     filled diamond (new)
                                    O     filled circle=big dot (new)

     Plot properties: 'MarkerSize' 'LineWidth'

     Additional Notes:
     'Hold on' and 'Hold off' were not implemented since one can generate a
        .kmz file for each plot and open all simultaneously within GE.
     Unless you have a lot of data point it is recomended to show the symbols
        since they are created in a separate folder so within Google Earth it
        is very easy to show/hide the line or the symbols.
     Current kml/kmz format does not support different linestyles, just solid.
        Nevertheless it is possible to define the opacity of the color (the
        first FF in the color definition means no transparency).
     Within Matlab, it is possible to generate a second plot with the
        same name, then you just need to select File/Revert within GE to update.

     Author: Rafael Palacios, Universidad Pontificia Comillas
     http://www.iit.upcomillas.es/palacios
     November 2006
     Version 1.1: Fixed an error while plotting graphs without symbols.

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 function GEplot(filename,Lat,Lon,style,opt11,opt12,opt21,opt22)
    0002 %
    0003 % function GEplot(filename,Lat,Lon,style,opt11,opt12,opt21,opt22)
    0004 %
    0005 % Description: creates a file in kmz format that can be opened into Google Earth.
    0006 %    GEplot uses the same syntax as the traditional plot function but
    0007 %    requires Latitude and Longitudd (WGS84) instead of x and y (note that Lat is
    0008 %    the first argument).
    0009 %    If you need to convert from UTM to Lat/Lon you may use utm2deg.m also
    0010 %    available at Matlab Central
    0011 %
    0012 % Arguments:
    0013 %    filename Example 'rafael', will become 'rafael.kmz'.  The same name
    0014 %             will appear inside Temporary Places in Google Earth as a layer.
    0015 %    dot_size Approximate size of the mark, in meters
    0016 %    Lat, Lon Vectors containing Latitudes and Longitudes.  The number of marks
    0017 %             created by this function is equal to the length of Lat/Lon vectors
    0018 %(opt)style   allows for specifying symbols and colors (line styles are not
    0019 %             supported by Google Earth currently. (see bellow)
    0020 %(opt)opt...   allows for specifying symbol size and line width (see bellow)
    0021 %
    0022 % Example:
    0023 %    GEplot('my_track',Lat,Lon,'o-r','MarkerSize',10,'LineWidth',3)
    0024 %    GEplot('my_track',Lat,Lon)
    0025 %
    0026 % Plot style parameters implemented in GEplot
    0027 %            color               symbol                line style
    0028 %            -----------------------------------------------------------
    0029 %            b     blue          .     point              -    solid
    0030 %            g     green         o     circle          (none)  no line
    0031 %            r     red           x     x-mark
    0032 %            c     cyan          +     plus
    0033 %            m     magenta       *     star
    0034 %            y     yellow        s     square
    0035 %            k     black         d     diamond
    0036 %            w     white (new)   S     filled square (new)
    0037 %                                D     filled diamond (new)
    0038 %                                O     filled circle=big dot (new)
    0039 %
    0040 % Plot properties: 'MarkerSize' 'LineWidth'
    0041 %
    0042 % Additional Notes:
    0043 % 'Hold on' and 'Hold off' were not implemented since one can generate a
    0044 %    .kmz file for each plot and open all simultaneously within GE.
    0045 % Unless you have a lot of data point it is recomended to show the symbols
    0046 %    since they are created in a separate folder so within Google Earth it
    0047 %    is very easy to show/hide the line or the symbols.
    0048 % Current kml/kmz format does not support different linestyles, just solid.
    0049 %    Nevertheless it is possible to define the opacity of the color (the
    0050 %    first FF in the color definition means no transparency).
    0051 % Within Matlab, it is possible to generate a second plot with the
    0052 %    same name, then you just need to select File/Revert within GE to update.
    0053 %
    0054 %
    0055 % Author: Rafael Palacios, Universidad Pontificia Comillas
    0056 % http://www.iit.upcomillas.es/palacios
    0057 % November 2006
    0058 % Version 1.1: Fixed an error while plotting graphs without symbols.
    0059 %
    0060
    0061 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0062 % Argument checking
    0063 %
    0064 error(nargchk(3, 8, nargin));  %3 arguments required, 8 maximum
    0065 n1=length(Lat);
    0066 n2=length(Lon);
    0067 if (n1~=n2)
    0068    error('Lat and Lon vectors should have the same length');
    0069 end
    0070 if (nargin==5 || nargin==7)
    0071     error('size arguments must be "MarkerSize" or "LineWidth" strings followed by a number');
    0072 end
    0073
    0074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0075 % symbol size and line width
    0076 %
    0077 markersize=7; %matlab default
    0078 linewidth=2;  %matlab default is 0.5, too thin for map overlay
    0079 if (nargin==6)
    0080     if (strcmpi(opt11,'markersize')==1)
    0081         markersize=opt12;
    0082     elseif (strcmpi(opt11,'linewidth')==1)
    0083         linewidth=opt12;
    0084     else
    0085         error('size arguments must be "MarkerSize" or "LineWidth" strings followed by a number');
    0086     end
    0087 end
    0088 if (nargin==8)
    0089     if (strcmpi(opt21,'markersize')==1)
    0090         markersize=opt22;
    0091     elseif (strcmpi(opt21,'linewidth')==1)
    0092         linewidth=opt22;
    0093     else
    0094         error('size arguments must be "MarkerSize" or "LineWidth" strings followed by a number');
    0095     end
    0096 end
    0097
    0098 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0099 % symbol, line style and color
    0100 %
    0101 symbol='none';
    0102 iconfilename='none';
    0103 linestyle='-';
    0104 color='b';
    0105 colorstring='ffff0000';
    0106
    0107 if (nargin>=4)
    0108     %linestyle
    0109     if (strfind(style,'-'))
    0110         linestyle='-';
    0111     else
    0112         linestyle='none';
    0113     end
    0114
    0115     %symbol
    0116     if (strfind(style,'.')), symbol='.'; iconfilename='dot'; end
    0117     if (strfind(style,'o')), symbol='o'; iconfilename='circle'; end
    0118     if (strfind(style,'x')), symbol='x'; iconfilename='x'; end
    0119     if (strfind(style,'+')), symbol='+'; iconfilename='plus'; end
    0120     if (strfind(style,'*')), symbol='*'; iconfilename='star'; end
    0121     if (strfind(style,'s')), symbol='s'; iconfilename='square'; end
    0122     if (strfind(style,'d')), symbol='d'; iconfilename='diamond'; end
    0123     if (strfind(style,'S')), symbol='S'; iconfilename='Ssquare'; end
    0124     if (strfind(style,'D')), symbol='D'; iconfilename='Sdiamon'; end
    0125     if (strfind(style,'O')), symbol='O'; iconfilename='dot'; end
    0126     if (strfind(style,'0')), symbol='O'; iconfilename='dot'; end
    0127
    0128     %color
    0129     if (strfind(style,'b')), color='b'; colorstring='ffff0000'; end
    0130     if (strfind(style,'g')), color='g'; colorstring='ff00ff00'; end
    0131     if (strfind(style,'r')), color='r'; colorstring='ff0000ff'; end
    0132     if (strfind(style,'c')), color='c'; colorstring='ffffff00'; end
    0133     if (strfind(style,'m')), color='m'; colorstring='ffff00ff'; end
    0134     if (strfind(style,'y')), color='y'; colorstring='ff00ffff'; end
    0135     if (strfind(style,'k')), color='k'; colorstring='ff000000'; end
    0136     if (strfind(style,'w')), color='w'; colorstring='ffffffff'; end
    0137 end
    0138
    0139 iconfilename=strcat('GEimages/',iconfilename,'_',color,'.png');
    0140 if (symbol=='.')
    0141     markersize=markersize/5;
    0142 end
    0143
    0144
    0145 %%%%%%%%%%%%%%%%%%%%%%%%%%%
    0146 % Creating kml file
    0147 %
    0148 fp=fopen(strcat(filename,'.kml'),'w');
    0149 if (fp==-1)
    0150     message=disp('Unable to open file %s.kml',filename);
    0151     error(message);
    0152 end
    0153 fprintf(fp,'<?xml version="1.0" encoding="UTF-8"?>\n');
    0154 fprintf(fp,'<kml xmlns="http://earth.google.com/kml/2.1">\n');
    0155 fprintf(fp,'<Document>\n');
    0156 fprintf(fp,'<name>%s</name>\n',strcat(filename,'.kml'));
    0157 fprintf(fp,'<description>Graph generated in Matlab (using GEplot.m by Rafael Palacios)</description>\n');
    0158 %
    0159 %Symbol styles definition
    0160 fprintf(fp,'<Style id="mystyle">\n');
    0161 fprintf(fp,'   <IconStyle>\n');
    0162 fprintf(fp,'      <scale>%.2f</scale>\n',markersize/14); %scale adjusted for .png image sizes
    0163 fprintf(fp,'      <Icon><href>%s</href></Icon>\n',iconfilename);
    0164 fprintf(fp,'   </IconStyle>\n');
    0165 fprintf(fp,'   <LineStyle>\n');
    0166 fprintf(fp,'      <color>%s</color>\n',colorstring);
    0167 fprintf(fp,'      <width>%d</width>\n',linewidth);
    0168 fprintf(fp,'   </LineStyle>\n');
    0169 fprintf(fp,'</Style>\n');
    0170 fprintf(fp,'\n');
    0171
    0172
    0173 if (linestyle=='-')
    0174     fprintf(fp,'    <Placemark>\n');
    0175     fprintf(fp,'      <description><![CDATA[Line created with Matlab GEplot.m]]></description>\n');
    0176     fprintf(fp,'      <name>Line</name>\n');
    0177     fprintf(fp,'      <visibility>1</visibility>\n');
    0178     fprintf(fp,'      <open>1</open>\n');
    0179     fprintf(fp,'      <styleUrl>mystyle</styleUrl>\n');
    0180     fprintf(fp,'      <LineString>\n');
    0181     fprintf(fp,'        <extrude>0</extrude>\n');
    0182     fprintf(fp,'        <tessellate>0</tessellate>\n');
    0183     fprintf(fp,'        <altitudeMode>clampToGround</altitudeMode>\n');
    0184     fprintf(fp,'        <coordinates>\n');
    0185     for k=1:n1
    0186       fprintf(fp,'%11.6f,%11.6f,0\n',Lon(k),Lat(k));
    0187     end
    0188     fprintf(fp,'        </coordinates>\n');
    0189     fprintf(fp,'      </LineString>\n');
    0190     fprintf(fp,'    </Placemark>\n');
    0191 end
    0192
    0193 if (strcmp(symbol,'none')==0)
    0194 fprintf(fp,'    <Folder>\n');
    0195 fprintf(fp,'      <name>Data points</name>\n');
    0196 for k=1:n1
    0197     fprintf(fp,'      <Placemark>\n');
    0198     fprintf(fp,'         <description><![CDATA[Point created with Matlab GEplot.m]]></description>\n');
    0199 %    fprintf(fp,'         <name>Point %d</name>\n',k);  %you may add point labels here
    0200     fprintf(fp,'         <visibility>1</visibility>\n');
    0201     fprintf(fp,'         <open>1</open>\n');
    0202     fprintf(fp,'         <styleUrl>#mystyle</styleUrl>\n');
    0203     fprintf(fp,'         <Point>\n');
    0204     fprintf(fp,'           <coordinates>\n');
    0205     fprintf(fp,'%11.6f,%11.6f,0\n',Lon(k),Lat(k));
    0206     fprintf(fp,'           </coordinates>\n');
    0207     fprintf(fp,'         </Point>\n');
    0208     fprintf(fp,'      </Placemark>\n');
    0209 end
    0210 fprintf(fp,'    </Folder>\n');
    0211 end
    0212
    0213 fprintf(fp,'</Document>\n');
    0214 fprintf(fp,'</kml>\n');
    0215
    0216 fclose(fp);
    0217
    0218 if (strcmp(symbol,'none')==1)
    0219    zip(filename,{strcat(filename,'.kml')});
    0220 else
    0221    zip(filename,{strcat(filename,'.kml'), iconfilename});
    0222 end
    0223 movefile(strcat(filename,'.zip'),strcat(filename,'.kmz'));
    0224 delete(strcat(filename,'.kml'));