7.14.2010

Displaying Matlab data in Google Earth

[ONE UPDATE AT BOTTOM]


It struck me the other day that Google Earth would be good platform for sharing my data with other people and found out that lots of people display their own data on the platform. Google even has a website to distribute Google Earth files that they like (this one, of the world population, was one of my favorites).  

So then I looked around to see if anyone had written code to help people convert Matlab data into Google Earth files.  The Google Earth Toolbox (recommended by my colleague Amir) worked great. It allows you to display data using functions very similar to standard Matlab functions (eg. imagesc()) only the output image is projected onto the planet and can be explored using Google Earth.  The image above is a map of the tropical cyclone climatology that I imported to Google Earth with only two lines of code. 

A = ge_imagesc(LON, LAT, flipud(DATA));
ge_output('FILENAME.kml', A);

[As indicated, the only bug I found was that some of the functions flipped the data vertically, which is easy to fix with one usage of the function flipud().]

It seems to me that one huge advantage of this presentation method is that pretty much anybody can view and explore global data-sets, since the interface is entirely intuitive and requires no manipulation (similar, in many respects to the contribution of Gapminder).

With a little extra work, I made the data-set dynamic in Google Earth, so that users can view data from different dates using a slider (see code below), and made this movie for use in presentations:



Below the fold is the script I used to make a Google Earth data set that changes over time.  Making the movie above required two additional steps.

  1. Add a "Tour" in Google Earth (under "Add") which just records your browsing of your data in a movie that plays back in the Google Earth Application.
  2. Use a screen capture program to record that movie as it plays back. (My friend Pam recommended this screen capture software, which works nicely and is easy to use). 
UPDATE:  SINCE I'VE REPEATED THIS PROCESS A FEW TIMES NOW, I'VE WRITTEN A FUNCTION IN MATLAB THAT GENERATES THE GOOGLE EARTH FILE.  ITS SIMILAR TO THE SCRIPT BELOW, BUT IS EASIER TO USE FOR AN ARBITRARY DATASET. DOWNLOAD IT HERE.


clear
cd ~/DIRECTORY
load DATA.mat
%Use Google's date format:
S = 'yyyy-mm-ddTHH:MM:SSZ';
lon = LON_DATA;
lat = LAT_DATA;
kmlstring = '';
for t = 1:NUMBER_YEARS 
   % my data spans 59 years, and is just annual; higher temporal resolutions will require a bit more effort
    starting = datestr(['01/01/' num2str(t-1+STARTING_YEAR)], S);
    ending = datestr(['12/31/' num2str(t-1+STARTING_YEAR)], S); 
    kmlstring = [kmlstring, ge_imagesc(lon,lat,flipud(DATA(:,:,t)),'name',['data_name ' num2str(t-1+1950)], 'imgURL', ['./GE_images/' num2str(t-1+1950)], 'timeSpanStart',starting,'timeSpanStop',ending)];    
end
f_maxs = ge_folder('FOLDER_TITLE', kmlstring);
ge_output('FILENAME.kml', f_maxs,'name','LAYER_NAME');
%I think if you change the file name to be ".kmz" it will zip it into one file for you. I just ran this and saved it from google earth into a .kmz file.

2 comments:

  1. could you post you DATA.mat file so I can try to adapt this to my prefrences?

    ReplyDelete
  2. The form of the dataset for both this script and for the more general code posted here is:

    DATA - M x N x T array of data, where each M x N layer describes data that occurs simultaneously at a moment in time. The third index is the index for the time variable (years).
    lat - M x 1 vector describing the latitude of the M rows
    lon - N x 1 vector describing the longitude of the N rows

    In the script above, there are also the scalar variables

    NUMBER_YEARS - just T
    STARTING_YEAR - the year associated with DATA(:,:,1)

    In the general script posted here, there is just a vector

    YEARS - T x 1 vector with year values (eg. 1995, 1996…) that match the T layers in DATA

    in both files, you also need to specify the strings FILENAME and LAYER_NAME (VARIABLE_NAME in the function). The former is the name of the file it writes out, the latter is the name of the layer that will show up in google earth.

    I hope that helps.

    ReplyDelete