File Exchange

image thumbnail


version (22.1 KB) by Serge
Export textured geometry (surface or patch) to GoogleEarth as a KML and a Collada DAE files


Updated 22 Mar 2017

View License

Export textured geometry (surface or patch) to GoogleEarth as a KML and a
Collada DAE files
mesh2kml(...) -surface or patch inputs, see bellow
mesh2kml(...out) -one common output filename or paths to {dae img kml}
mesh2kml(...out,prp) -Collada & GoogleEarth property value pairs
color: color, default=[0.7 0.7 0.7] OR..
image, RGB(A)|BW(A)|filename (optional)
alpha: opacity, default=[1] (opaque) OR..
image, BW|filename (optional)
latlon: flag to interpret xyz as Lat,Lon,Alt, default=0
twosided: flag to render faces from both sides, default=1
position: [latitude_deg longitude_deg altitude_m]
altmode: default='absolute', 'clampToGround', 'relativeToGround',
'relativeToSeaFloor', 'clampToSeaFloor'
orientation:[heading_NtoE pitch_NtoUp roll_UPtoE] (deg), default=[0,0,0]
scale: [x y z] scale factors, default=[1,1,1]
camera: [heading_deg tilt_deg range_m]
M = mesh2kml(...) -return final properties as struct
mesh2kml(x,y,z...) -NxM vertex coordinates
mesh2kml(x,y,z,c...) -NxMx3 face colors, last row & col ignored (TO DO)
mesh2kml(x,y,z,u,v...) -NxM texture coordinates
mesh2kml(..,nx,ny,nz...) -NxM vertex normals
mesh2kml(S) -above surface properties as struct
mesh2kml(P...) -patch properties as struct, with fields:
vertices: Nx3 vertex positions
faces: Mx3 triangle faces (right hand rule)
normals: Nx3 vertex normals (optional)
uv: Nx2 texture coordinates for each vertex

-Collada (dae) files can be dragged and dropped into GoogleEarth.
-A kml file is written only if 'position' is provided.
-For lat,lon 6dp gives ~15 cm precision at equator, 8dp is ~1.5 mm.
-Textures wrap circularly if UV is outside the [0-1] range.
-Texture pixels blend and image edge pixels blend with opposite edge.
-If normals are not provided they are auto generated. Vertices shared by
triangles get a smoothed normal using triangle area weighting.
-To avoid normal smoothing when specifying a patch each vertex should be
referenced by only one face.
-Faces are auto generated using vertices(:,1:2), if missing.

-Altitude exaggeration in GoogleEarth will affect the model's origin.
-The only way to visualise textured polygons in MatLab is to draw each
polygon as a quad with its own image chip, which is very limiting.
-To generate dae template draw a polygon in Google SketchUp 2017 > export
as kmz > open kmz with winzip > extract dae file > modify as needed.
-SketchUp duplicates all polygons to face both ways, but mesh2kml uses an
undocumented Collada tag: <effect><extra><technique><double_sided>.
-SketchUp can export a quad mesh dae but they don't work in GoogleEarth.
-GoogleEarth ignores camera references in dae file.

To do:
-Allow one colour per face, see surf2patch.
-Allow multi patch input using array of structures as input.
-Allow quad patch input by converting it to a tri patch.
-Allow input to be a handle to a displayed surface or patch object.
-Allow export as kmz file.
-Use ridge detection when triangulating surfaces.
-Test generated normals.
-Add more patch examples.
-Remove thin triangles from patch inputs.

Comments and Ratings (6)

James Jones

This does exactly what I need! Didn't take long to learn how to use it. Better help and more examples would be great, but I didn't have too much trouble getting it to do what I wanted. Thank you for posting it!


Hello Jules,

There are two 'positions' you may be referring to. One is the [lat lon alt] in degrees & meters, specifying where the 0,0,0 origin of the model is to be located within GoogleEarth, see example code in the Help.

Otherwise you may be asking about providing a patch struct as an input instead of a surface... all the examples use surface as an input, ie grids of x,y,z.
Try one of the examples first.

To elaborate, 'surface' grids can be re-expressed as a 'patch' struct.
Actually that is what mesh2kml does internally anyway.
A patch is more versatile, as its just a collections of polygons, and that's what dae files need.
You can convert a surface to a patch using MatLab's surf2patch. The first example in the help using patch would be:
[x,y,z] = peaks;
P = surf2patch(x,y,z,'tri')
mesh2kml(P,'peaks','color','red','position',[-33.85622 151.21535 10],'alpha',0.7);

Instead of the original surface as input:
[x,y,z] = peaks;
mesh2kml(x,y,z,'peaks','color','red','position',[-33.85622 151.21535 10],'alpha',0.7);

Help can always use improving. If you make any improvements, please send it my way, whole file or help only, my email is in the file.

PS would be great to know how people are using this function...

Jules Ray

Should improve the help.
Cant understand the inputs, what do you mean with the position, is the central coordinate or tile.... after several tries I cant make it work

Qiushi Jack

Sean Kim

Bin Li


Title change

Minor changes

Cover photo

MATLAB Release Compatibility
Created with R2012a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: Google Toolbox, KML toolbox

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor