function mesh2kml(kmlfile,long,lat,iskip,jskip) % convert a 2D mesh grid into a single plain kml file % will create the horizontal and vertical mesh-line seperately % usage: % mesh2kml(kml-filename,longitude,latitude [iskip, jskip]) % iskip: reduce the points when to generate i-direction-mesh-line % jskip: reduce the points when to generate j-direction-mesh-line % e.g., % lon=-90:2:20; % lat=37:65; % [lon,lat]=meshgrid(lon,lat); % mesh2kml('mesh2kml_test',lon,lat,1,1); % will produce a kml file named mesh2kml_test in current folder % % http://scriptdemo.blogspot.com if nargin<3 help mesh2kml return end if nargin==3 iskip=5; jskip=5; elseif nargin==4 jskip=5; end fid=fopen([kmlfile,'.kml'],'wt'); writekmlHeader(fid,kmlfile); if length(long)~=numel(long) % mesh [NY,NX]=size(long); %i-index longUD=flipud(long); latUD=flipud(lat); longI=long; latI=lat; longI(:,2:2:end)=longUD(:,2:2:end); latI(:,2:2:end)=latUD(:,2:2:end); clear longUD latUD jindex=sort(unique([1 jskip:jskip:NY NY])); longI=reshape(longI(jindex,:),1,[]); latI=reshape(latI(jindex,:),1,[]); fprintf(fid,'%s \n','<Placemark>'); fprintf(fid,'%s \n',['<name>',kmlfile,'-I</name>']); fprintf(fid,'%s \n','<LineString>'); fprintf(fid,'%s \n','<tessellate>1</tessellate>'); % fprintf(fid,'%s \n','<coordinates>'); fprintf(fid,'%0.6f,%0.6f,0.0 \n', [longI;latI]); fprintf(fid,'%s \n','</coordinates>'); fprintf(fid,'%s \n','</LineString>'); fprintf(fid,'%s \n','</Placemark>'); %j-index longLR=fliplr(long); latLR=fliplr(lat); longJ=long; latJ=lat; longJ(2:2:end,:)=longLR(2:2:end,:); latJ(2:2:end,:)=latLR(2:2:end,:); clear longLR latLR longJ=longJ'; latJ=latJ'; iindex=sort(unique([1 iskip:iskip:NX NX])); longJ=reshape(longJ(iindex,:),1,[]); latJ=reshape(latJ(iindex,:),1,[]); fprintf(fid,'%s \n','<Placemark>'); fprintf(fid,'%s \n',['<name>',kmlfile,'-J</name>']); fprintf(fid,'%s \n','<LineString>'); fprintf(fid,'%s \n','<tessellate>1</tessellate>'); % fprintf(fid,'%s \n','<coordinates>'); fprintf(fid,'%0.6f,%0.6f,0.0 \n', [longJ;latJ]); fprintf(fid,'%s \n','</coordinates>'); fprintf(fid,'%s \n','</LineString>'); fprintf(fid,'%s \n','</Placemark>'); else long=reshape(long,1,[]); lat=reshape(lat,1,[]); fprintf(fid,'%s \n','<Placemark>'); fprintf(fid,'%s \n',['<name>',kmlfile,'</name>']); fprintf(fid,'%s \n','<LineString>'); fprintf(fid,'%s \n','<tessellate>1</tessellate>'); % fprintf(fid,'%s \n','<coordinates>'); fprintf(fid,'%0.6f,%0.6f,0.0 \n', [long;lat]); fprintf(fid,'%s \n','</coordinates>'); fprintf(fid,'%s \n','</LineString>'); fprintf(fid,'%s \n','</Placemark>'); end writekmlEnd(fid); fclose(fid); function writekmlHeader(fid,kmlName) fprintf(fid,'%s \n','<?xml version="1.0" encoding="UTF-8"?>'); fprintf(fid,'%s','<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" '); fprintf(fid,'%s \n','xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">'); fprintf(fid,'%s \n','<Document>'); fprintf(fid,'%s \n',['<name>',kmlName,'.kml</name>']); function writekmlEnd(fid) fprintf(fid,'%s \n','</Document>'); fprintf(fid,'%s \n','</kml>'); |
Sunday, February 5, 2012
[Matlab] Convert a 2D mesh to a single kml file
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment