[14195] | 1 | function kmlgridded(md,data,varargin)
|
---|
| 2 |
|
---|
| 3 | %process options
|
---|
| 4 | options=pairoptions(varargin{:});
|
---|
| 5 |
|
---|
| 6 | %process options
|
---|
| 7 | options=changefieldvalue(options,'coord','latlon');
|
---|
| 8 |
|
---|
| 9 | %process mesh and data
|
---|
| 10 | [x y z elements is2d isplanet]=processmesh(md,[],options);
|
---|
| 11 | [data datatype]=processdata(md,data,options);
|
---|
| 12 |
|
---|
| 13 | %check is2d
|
---|
| 14 | if ~is2d,
|
---|
| 15 | error('buildgridded error message: gridded not supported for 3d meshes, project on a layer');
|
---|
| 16 | end
|
---|
| 17 |
|
---|
| 18 | %Get xlim and ylim (used to extract radar image)
|
---|
| 19 | xlim=[min(x) max(x)];
|
---|
| 20 | ylim=[min(y) max(y)];
|
---|
| 21 | post=getfieldvalue(options,'posting',diff(xlim)/1000);
|
---|
| 22 | if(diff(xlim)/post>10000),
|
---|
| 23 | error(['posting too large']);
|
---|
| 24 | end
|
---|
| 25 |
|
---|
| 26 | %Interpolating data on grid
|
---|
[21828] | 27 | x_m = xlim(1):post:xlim(2);
|
---|
| 28 | y_m = ylim(1):post:ylim(2);
|
---|
| 29 | data_grid=InterpFromMeshToGrid(elements,x,y,data,x_m,y_m,NaN);
|
---|
[14195] | 30 | if size(data_grid,1)<3 | size(data_grid,2)<3,
|
---|
| 31 | error('data_grid size too small, check posting and units');
|
---|
| 32 | end
|
---|
| 33 | pos=find(isinf(data_grid));
|
---|
| 34 | if ~isempty(pos),
|
---|
| 35 | disp('Warning: removing Infs from vector (probably log(0)?)');
|
---|
| 36 | data_grid(pos)=NaN;
|
---|
| 37 | end
|
---|
| 38 |
|
---|
| 39 | %Process data_grid: add white in NaN and correct caxis accordingly
|
---|
| 40 | data_nan=find(isnan(data_grid));
|
---|
| 41 | data_min=min(data_grid(:));
|
---|
| 42 | data_max=max(data_grid(:));
|
---|
| 43 | if exist(options,'caxis'),
|
---|
| 44 | caxis_opt=getfieldvalue(options,'caxis');
|
---|
| 45 | data_grid(find(data_grid<caxis_opt(1)))=caxis_opt(1);
|
---|
| 46 | data_grid(find(data_grid>caxis_opt(2)))=caxis_opt(2);
|
---|
| 47 | data_min=caxis_opt(1);
|
---|
| 48 | data_max=caxis_opt(2);
|
---|
| 49 | end
|
---|
| 50 |
|
---|
| 51 | %Get colormap
|
---|
| 52 | colorm = getcolormap(options);
|
---|
| 53 | len = size(colorm,1);
|
---|
| 54 | ind = ceil((len-1)*(data_grid-data_min)/(data_max - data_min + eps) +1);
|
---|
| 55 | ind(find(ind>len))=len;
|
---|
| 56 | ind(find(ind<1) )=1;
|
---|
| 57 | ind(find(isnan(ind)))=1;
|
---|
| 58 | image_rgb=zeros(size(data_grid,1),size(data_grid,2),3);
|
---|
| 59 | r=colorm(:,1); image_rgb(:,:,1)=r(ind); clear r;
|
---|
| 60 | g=colorm(:,2); image_rgb(:,:,2)=g(ind); clear g;
|
---|
| 61 | b=colorm(:,3); image_rgb(:,:,3)=b(ind); clear b;
|
---|
| 62 |
|
---|
| 63 | %Deal with alpha
|
---|
| 64 | alpha=getfieldvalue(options,'alpha',.8);
|
---|
| 65 | alphaMatrix = alpha*ones(size(data_grid));
|
---|
| 66 | alphaMatrix(data_nan) = 0;
|
---|
| 67 |
|
---|
| 68 | %write kml
|
---|
| 69 | kmlfilename=getfieldvalue(options,'kmlfilename','tempfile.kml');
|
---|
| 70 | kmlroot=getfieldvalue(options,'kmlroot','./');
|
---|
| 71 | kmlimagename=getfieldvalue(options,'kmlimagename','tempimage');
|
---|
| 72 | kmlresolution=getfieldvalue(options,'kmlresolution',1);
|
---|
| 73 | kmlfolder=getfieldvalue(options,'kmlfolder','Ground Overlay');
|
---|
| 74 | kmlfolderdescription=getfieldvalue(options,'kmlfolderdescription','');
|
---|
| 75 | kmlgroundoverlayname=getfieldvalue(options,'kmlgroundoverlayname','ground overlay');
|
---|
| 76 | kmlgroundoverlaydescription=getfieldvalue(options,'kmlgroundoverlaydescription','description');
|
---|
| 77 |
|
---|
| 78 | %write png
|
---|
| 79 | imwrite(image_rgb,[kmlimagename '.png'],'png','alpha',alphaMatrix);
|
---|
| 80 | clear image_rgb alphaMatrix
|
---|
| 81 |
|
---|
| 82 | %prepare colorbar
|
---|
| 83 | iscolorbar=0;
|
---|
| 84 | if strcmpi(getfieldvalue(options,'colorbar','on'),'on'),
|
---|
| 85 | X = linspace(0,1,len)';
|
---|
| 86 | Xlab = round(linspace(data_min,data_max,len+1));
|
---|
| 87 | html = ['<TABLE border=' num2str(1) ' bgcolor=#FFFFFF>',10];
|
---|
| 88 |
|
---|
| 89 | for k=len:-1:1
|
---|
| 90 | f = (Xlab(k)-data_min)/(data_max-data_min);
|
---|
| 91 | if f<0, f=0; end
|
---|
| 92 | if f>1, f=1; end
|
---|
| 93 | polyColor(1,1) = interp1(X,colorm(:,1),f);
|
---|
| 94 | polyColor(1,2) = interp1(X,colorm(:,2),f);
|
---|
| 95 | polyColor(1,3) = interp1(X,colorm(:,3),f);
|
---|
| 96 | polyColorStr(1:2) = dec2hex(round(polyColor(1)*255),2);
|
---|
| 97 | polyColorStr(3:4) = dec2hex(round(polyColor(2)*255),2);
|
---|
| 98 | polyColorStr(5:6) = dec2hex(round(polyColor(3)*255),2);
|
---|
| 99 | html = [html,'<TR><TD width="15px" bgcolor=#',polyColorStr, '> </TD>','<TD bgcolor=#FFFFFF>'];
|
---|
| 100 | if k==1
|
---|
| 101 | html=[html,'<= ',num2str(Xlab(k),'%g')];
|
---|
| 102 | elseif k==len
|
---|
| 103 | html=[html,'>= ',num2str(Xlab(k),'%g')];
|
---|
| 104 | else
|
---|
| 105 | html=[html,num2str(Xlab(k),'%g'),' to ',num2str(Xlab(k+1),'%g'),'</TD>'];
|
---|
| 106 | end
|
---|
| 107 | html = [html,'</TR>',10];
|
---|
| 108 | end
|
---|
| 109 | html = [html,'</TABLE>'];
|
---|
| 110 | iscolorbar = 1;
|
---|
| 111 | end
|
---|
| 112 |
|
---|
| 113 | %now write kml file
|
---|
| 114 | fid=fopen([kmlroot '/' kmlfilename],'w');
|
---|
| 115 | fprintf(fid,'%s\n','<?xml version="1.0" encoding="UTF-8"?>');
|
---|
| 116 | fprintf(fid,'%s\n','<kml xmlns="http://earth.google.com/kml/2.1">');
|
---|
| 117 | fprintf(fid,'%s\n','<Document>');
|
---|
| 118 | fprintf(fid,'%s%s%s\n','<name>',kmlfilename,'</name>');
|
---|
| 119 | if iscolorbar,
|
---|
| 120 | fprintf(fid,'<Placemark id="colorbar">\n');
|
---|
| 121 | fprintf(fid,'%s%s%s\n','<name>','click the icon to see the colorbar','</name>');
|
---|
| 122 | fprintf(fid,'%s%s%s\n','<description>','Ground overlay colorbar','</description>');
|
---|
| 123 | fprintf(fid,'<visibility>1</visibility>\n');
|
---|
| 124 | fprintf(fid,['<description>',10,'<![CDATA[' html ']]>',10,'</description>',10,'\n']);
|
---|
| 125 | fprintf(fid,['<Style><IconStyle><scale>1</scale><Icon><href>http://maps.google.com/mapfiles/kml/shapes/donut.png</href></Icon></IconStyle><ListStyle></ListStyle></Style><Point id="poly_colorbar">\n']);
|
---|
| 126 | fprintf(fid,'<altitudeMode>clampToGround</altitudeMode>\n');
|
---|
| 127 | fprintf(fid,'<extrude>1</extrude>\n');
|
---|
| 128 | fprintf(fid,'<tessellate>1</tessellate>\n');
|
---|
| 129 | fprintf(fid,'%s%g,%g%s\n','<coordinates>',max(x),mean(y),'</coordinates>');
|
---|
| 130 | fprintf(fid,'</Point>\n');
|
---|
| 131 | fprintf(fid,'</Placemark>\n');
|
---|
| 132 | end
|
---|
| 133 | fprintf(fid,'%s\n','<GroundOverlay id="groundoverlay">');
|
---|
| 134 | fprintf(fid,'%s%s%s\n','<name>',kmlgroundoverlayname,'</name>');
|
---|
| 135 | fprintf(fid,'%s\n','<description>',kmlgroundoverlaydescription,'</description>');
|
---|
| 136 | fprintf(fid,'%s%s.%s%s\n','<Icon>',kmlimagename,'png','</Icon>');
|
---|
| 137 | fprintf(fid,'%s\n','<LatLonBox>');
|
---|
| 138 | fprintf(fid,'%s%f%s\n','<north>',max(y_m),'</north>');
|
---|
| 139 | fprintf(fid,'%s%f%s\n','<south>',min(y_m),'</south>');
|
---|
| 140 | fprintf(fid,'%s%f%s\n','<east>',max(x_m),'</east>');
|
---|
| 141 | fprintf(fid,'%s%f%s\n','<west>',min(x_m),'</west>');
|
---|
| 142 | fprintf(fid,'%s\n','<rotation>0</rotation>');
|
---|
| 143 | fprintf(fid,'%s\n','</LatLonBox>');
|
---|
| 144 | fprintf(fid,'%s\n','</GroundOverlay>');
|
---|
| 145 | fprintf(fid,'%s\n','</Document>');
|
---|
| 146 | fprintf(fid,'%s\n','</kml>');
|
---|
| 147 | fclose(fid);
|
---|