# GEplot_3D

## PURPOSE

## SYNOPSIS

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

## DESCRIPTION

    3D adaptation of GEplot.  3D version will plot a 3 dimensional plot given the added elevation vector
    input--P.R. Jackson, USGS, 1-16-08

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

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