Showing posts with label 2D. Show all posts
Showing posts with label 2D. Show all posts

Sunday, February 5, 2012

[Matlab] Convert a 2D mesh to a single kml file

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>');

Saturday, February 5, 2011

2D random walk using Processing

//To show 2D random [some limitation in the code] walk using Processing
// Copyright by http://scriptdemo.blogspot.com

myParticles[] myP;
int numP=2;
boolean IsLine=false;

void setup()
{
  size(400,400);
  background(0);
  myP=new myParticles[numP];

  for (int i=0; i<numP; i++)
  {
    myP[i]= new myParticles();
    myP[i].x=random(100,300);
    myP[i].y=random(100,300);
  }
  frameRate(50);
}

void draw()
{
  for(int i=0; i<numP; i++)
  {
    myP[i].move();
    if (ceil(myP[i].x)== width | ceil(myP[i].y) == height)
    {
      noLoop();
    }
  }
}
void mouseClicked()
{
  background(0);
}
void mouseReleased()
{
  for(int i=0; i<numP; i++)
  {
    myP[i].setxy(mouseX,mouseY);
  }
  IsLine=!IsLine;
  loop();
}

class myParticles
{

  public float x0;
  public float y0;
  public float x;
  public float y;

  public void myParticles()
  {
  }
  public void setxy(float tmpx, float tmpy)
  {
    x=tmpx;
    y=tmpy;
  }

  public void move()
  {
    for ( int i=20; i<=60; i=i+10 )
    {
      float tmpx, tmpy;
      float myangle;
      float dx=5;
      float dy=5;
      x0=x;
      y0=y;
      myangle=(i+floor(random(-5,5)))*PI/180.0;
      tmpx=dx*random(-1,1);
      tmpy=dy*random(-1,1);
      x=x+tmpx*cos(myangle)-tmpy*sin(myangle);
      y=y+tmpy*cos(myangle)+tmpx*sin(myangle);
      if ( x<0 ) { x=0.0; }
      if ( x>width ) { x=width; }
      if ( y<0 ) {y=0.0;}
      if ( y>height ) { y=height; }

      int rr=floor(random(255));
      int rg=floor(random(255));
      int rb=floor(random(255));
      stroke(rr,rr,rb);
      if (IsLine)
        {
          line(x0,y0,x,y);
        }
       else
      {
        point(x,y);
      }
    }
  }
}



Monday, September 1, 2008

[Matlab] Saving 2D data in ASCII with matlab

function SaveDataAscii(data,filename,FileHead,Gformat)
% to save a 2d array into a ascii file as what you see in array editor
%     a format option is also provided
% Usage:
%     SaveDataAscii(data,filename,FileHead,Gformat)
% e.g.,
%       my2ddata=rand(3,5);
%       SaveDataAscii(my2ddata,'myrand2dtest.dat','it is a test','%7.4f');


isHeader=1;
if nargin==3
   Gformat='%10.4f';
elseif nargin==2
   isHeader=0;
   Gformat='%10.4f';
elseif nargin~=4
   help SaveDataAscii
   return
end
if (length(FileHead)==0)
   isHeader=0;
end

% open the file
fid=fopen(filename,'w');
if isHeader==1
   fprintf(fid,['%% ',FileHead]);
   fprintf(fid,'\n');
end

% write the datablock
for i=1:size(data,1)
    for j=1:size(data,2)
        fprintf(fid,Gformat,data(i,j));
    end;
    fprintf(fid,'\n');
end;
fclose(fid);

ShowCalendar