Code covered by the BSD License  

Highlights from
KML Toolbox v2.6

4.90909

4.9 | 12 ratings Rate this file 221 Downloads (last 30 days) File Size: 3.48 MB File ID: #34694
image thumbnail

KML Toolbox v2.6

by Rafael Oliveira

 

20 Jan 2012 (Updated 21 May 2013)

Create KML/KMZ files and view them in Google Earth. Supports 3D models, contours, overlays, and more

| Watch this File

File Information
Description

This class-based toolbox allows you to create many different plots in Google Earth, by automatically creating the required xml-based KML files without user interaction. Also allows to save as KMZ packages, including any model & images necessary for the plot.

With it, you can create:
- line plots, scatter plots
- 2D and 3D contours
- 2D filled contours
- 2D and 3D polygons
- 2D and 3D quiver plots
- write text in a given point
- place 3D models
- 3D surfaces
- overlay images
- transfer more complex figures as images
- folders, subfolders,... to aggregate similar plots
- animate 3D models into a pre-defined trajectory
- camera position
- animated tours (still undocumented, but there is now an example in the test folder - more info, contact me by email).

To install the toolbox into your MATLAB path, go to the toolbox directory and run:
>> kml.install

Now with documentation of each function and the available parameters. To view the help, run in MATLAB:
>> kmldoc
or
>> kmldoc kml.plot %or any other of the available plots

If you enjoy it, just drop me an email at kml@rafael.aero saying for what you're using it :).

Inspired by the original Google Earth Toolbox, but implemented using MATLAB classes and XML, which makes it much easier to use. It was tested with 2011a onwards, but it should work in older releases too... (Let me know if not!)

Acknowledgements

Google Earth Toolbox inspired this file.

Required Products MATLAB
MATLAB release MATLAB 7.12 (R2011a)
Tags for This File  
Everyone's Tags
advanced plotting, contour, earth, geography, google earth(2), kml(3), plot, quiver, scatter, trajectory, visualization
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (31)
28 Jun 2013 Richard Gueler

I found a bug with the overlay function. If I try to add an overlay image with a path in the filename, it embeds the state path in the kml file, thus when I load the kmz file into Google Earth, it goes looking for the file at the stated path. It does not use the file embedded with the kmz file.

For example, if I do the following,

k.overlay(west,east,south,north,'file','C:\temp\myImage.png');

Google Earth will grab the image "C:\temp\myImage.png", not the one embedded with the kmz file. I suggest you remove the path from the filename that is used in the kml file.

Other than that, it is a great toolbox.

06 Jun 2013 Martin Lechner

Hi Rafael,

in the file
function target = plot(this,long,lat,varargin)

I found an issue in the chech of the lat and long input value (line 14 and 15).
The function handle includes directly the check for the input value &&~isempty(lat). This must be &&~isempty(a).

This lines should be:
p.addRequired('lat', @(a)isnumeric(a) && isvector(a) &&~isempty(a));
p.addRequired('long',@(a)isnumeric(a) && isvector(a) &&~isempty(a) && numel(a)==nlat);

22 May 2013 Rafael Oliveira

@Ryan: thanks for reporting the error, you can fix it by changing that line to:

if iscell(arg.iconColor) && nc==nlat

I'll submit a fixed version of the toolbox.

@Alfredo: I've changed the save function, now it should be possible to save to other folders as well - thanks for the tip. And to avoid those lines, just make sure you end every plot command with a ;

21 May 2013 Ryan

Hi Rafael,

I really like this tool box, I have been using it quite a bit.

I wanted to report a possible error:

When using k.scatter3(lon,lat,z, ...
'iconColor','FFFF0000','iconScale',.2);

I get an error when plotting exactly 8 points
Cell contents reference from a non-cell array object.

Error in kml/scatter3 (line 131)
iconstyle.appendChild(this.textNode('color',arg.iconColor{i}));

-It seems to happen here in the scatter3 function because nc==nlat returns true, and it should probably be false.
if nc==nlat
iconstyle.appendChild(this.textNode('color',arg.iconColor{i}));
else
iconstyle.appendChild(this.textNode('color',arg.iconColor));
end

Thanks again for the toolbox

21 May 2013 Ryan  
04 Apr 2013 Alfredo

It would be cool that the k.save function supported the path in the filename. i.e.:

k.save('path\filename.kml')

Now it ignores the path and saves the kml file in the current folder.
Thank you

Also, how can I avoid the display of the following lines in the Command Window? Thanks

ans =

type: 'Placemark'
id: [1x47 char]

04 Apr 2013 Alfredo

It would be cool that the k.save function supported the path in the filename. i.e.:

k.save('path\filename.kml')

Now it ignores the path and saves the kml file in the current folder.
Thank you

03 Apr 2013 Nick Mortimer

Just a quick comment, I'm doing some stuff that requires accurate lat and longs have gone through and changed

num2str(north) to num2str(north,8) to avoid loosing position accuracy

16 Nov 2012 Jaroslaw Tuszynski

Works great

15 Nov 2012 Robert Daly

Hi Rafael,
Thanks for making this great tool. I had a minor issue with the klm.transfer function not lining up properly. I discovered that this was because my plotting script used "axis equal". This resulted in the axes not filling the whole figure window during the image capture so the image limits were not quite right. perhaps you could add "axis normal" in the code somewhere so that it will always behave as expected.

05 Nov 2012 Rafael Oliveira

Sorry Tomas, but the toolbox needs a MATLAB version that supports classes, which are definitely not available at R2007b...

05 Nov 2012 Tomas

Matlab version 2007b:
>>kml.install
??? No appropriate method or public field install for class kml.
When I try:
>>kmldoc %KML toolbox appear but its not functional.

29 Aug 2012 Rafael Oliveira

and for those interested in the animation feature, here is a little example of using it:

k = kml('testAnimation');
k.useRadians;
N = numel(longitude);
yaw = yaw - pi; %This is to correct the mis-orientation in the A320 model, you can ignore this for other models - or fix the model file
modelA320 = k.model(longitude(1),latitude(1),altitude(1),yaw(1),gamma(1),roll(1),'model','A320.dae','scale',10);
anim = k.newAnimation('Flight');

for i = 2:N
dT = time(i) - time(i-1);
anim.updateLocation(modelA320,dT,longitude(i),latitude(i),altitude(i));
anim.updateOrientation(modelA320,dT,yaw(i)-pi,gamma(i),roll(i));

anim.flyToLookAt(dT, longitude(i), latitude(i),1e4)
end

anim.flyToLookAt(10, mean(longitude),mean(latitude),1e5);

k.run;

29 Aug 2012 Rafael Oliveira

Hi John,
I've answered you by email, but in any case, the fix is to replace the num2str(id) with num2str(i). Anyway, I've submitted a fixed version of the toolbox, that also includes some other bug fixes.

28 Aug 2012 John Fuller  
17 Aug 2012 John Fuller

Rafael,
I get the following error when I run the quiver function example snippet:

k = kml('my kml file');

% Create a sample quiver plot in the kml file
[x,y] = meshgrid(-5:.2:5,-2:.15:2);
z = x .* exp(-x.^2 - y.^2) + y.*sin(x);
[px,py] = gradient(z,.2,.15);
k.quiver(5*x,10*y,8*px,8*py)

% Save the kml and open it in Google Earth
k.save;
??? Undefined function or variable 'id'.

Error in ==> kml.quiver at 61
target(i) = f.plot(long2,lat2, 'altitude',arg.altitude,...

Any thoughts?

24 Jul 2012 Seb Biass

Thanks a lot for the contourf function! Just fyi, the [~, ixMA] at line 137 gives errors on old (i.e. 2009) versions. Another question... are you implementing a way to specify the contour values rather than the number of contours?
Cheers!

09 Jul 2012 AHMET ANIL DINDAR

This is a perfect toolbox.

++Ahmet

03 Jul 2012 Rina  
28 Jun 2012 Rafael Oliveira

Hi Seb Biass, I've finally added contourf to the toolbox :)
If you find any bugs on it, let me know!
@Stephan - I've almost finished screen overlay support, I'll update it as soon as I've time to finish and test it.

12 Jun 2012 Stephan

Would it be possible to include screen overlays?

21 May 2012 Seb Biass

That is really nice, thank you! Any chance for a contourf type function?

11 May 2012 Rafael Oliveira

Hi Zeneida,
You can do it using the option 'numberOfLevels', as in:

k = kml;
[x,y,z] = peaks;
k.contour(x,y,z, 'numberOfLevels', 20);
k.run;

10 May 2012 Zeneida

Hello,
There is a way do more contours, I need that more contour lines in the map, but I don't know is it can be. In the contour.m from matlab I can specify the number of lines, but here?????
Thank you

16 Mar 2012 Rafael Oliveira

Thanks for the tip... I've included it in an updated version, together with a change removing the ~ from the function outputs. Now it should work with some older MATLAB versions too!

16 Mar 2012 Balam Willemsen

Very nice and useful! I made one very small edit to kml.m to get it to work on the Mac platform. Not sure if Linux has a common tool like start or open that is independent of distro.

function run(this)
this.save;
if (ispc)
system(['start "kmltoolbox" "' this.filename '"']);
elseif (ismac)
system(['open' ' "' this.filename '"']);
elseif (isunix)
disp('The KML file has been saved, open it in Google Earth');
end

end

14 Feb 2012 Michele Williams

This is a great program ... it has provided a great way to visualize my measurement results spatially.

02 Feb 2012 scott lee davis

hey this looks great! Why not combine the two versions into the best matlab version? I am building at multilanguage version of the googleearth toolbox and would welcome motivated matlab development and extension.

http://code.google.com/p/googleearthtoolbox/source/browse/#git%2Fmatlab

25 Jan 2012 Rafael Oliveira

Hi Travis. I've used the same name, but they're all encapsulated inside the kml class... So, to call them, you need to create an instance of the kml class, for example:

mykml = kml('Sample plot');

And then use this object "mykml" to plot, as in:

mykml.plot(1:10, sin(1:10));

or

mykml.scatter(rand(100,1),rand(100,1));

24 Jan 2012 Travis Knepp

I am wondering why you built functions with the same name as MatLab built-in functions (e.g. plot.m, plot3.m, text.m)? This seems pretty counter-productive. Can you correct me on this?

22 Jan 2012 Julien

The object oriented version of the google earth toolbox. Wonderful! Thank you so much for sharing...

Updates
24 Jan 2012

v1.2: Updated to support both degree and radian inputs.

25 Jan 2012

v 1.3: fixed a small error in the checkUnit function

08 Feb 2012

v1.4 - Fixed some bugs, added documentation, and a method to animate a model in a trajectory (kml.modelTour)

13 Feb 2012

Corrected the image in File Exchange

14 Feb 2012

v1.5: updated scatter and scatter3 to accept an different icons for each point. You must supply them as a cell array with the same size of the latitude and longitude inputs.

16 Mar 2012

v1.6: made it compatible with older versions that do not support ~ as function output. Also added the suggestion of Balam Willemsen, to make run work with OSX... Any suggestions on the Linux side are welcome :)

02 May 2012

v1.7: small correction in the quiver3d function, and implementation of the classic quiver plot in 2D, without using models anymore.

18 Jun 2012

v1.8: increased to the numeric precision when saving numbers as text. Added the polyMap function, to create a 3D map similar to surf, but composed of a polygons with height proportional to the cell value (similar to hist3, but without binning).

28 Jun 2012

v1.9: Finally implemented the filled contour plot! And also made some changes to the other contour functions to match better the color scale from MATLAB.
Added also a surf function, to plot 3D colored surfaces.

17 Jul 2012

v2.0: Some bug fixes in the filled contour, added animation support.

25 Jul 2012

v2.1: Added support for gx:LatLonQuad overlays, so now you can add non-square images to the kml

29 Aug 2012

v2.2: A few bug fixes.

05 Sep 2012

v2.3: A couple more bug fixes (thanks to Reid Pulley for reporting them!), added one example of animation to the test folder

20 May 2013

v2.6: finally possible to save KMZ files(default)- auto includes models & images. fix num2str behavior, reported by Nick Mortimer. Fix to keep path when saving, reported by Alberto. Added lineWidth/lineColor to contourf, suggested by Keith Epstein.

21 May 2013

(forgot to update version number)

Contact us