File Exchange

image thumbnail

kml2struct

version 1.1.0.0 (1.95 KB) by James Slegers
Import a .kml file as a series of shapefile structs. Like kml_shapefile, but more stable.

71 Downloads

Updated 08 Jun 2012

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

Import a Google Earth .kml file of points, lines, polygons, or a combination, as a Matlab struct, compatible with the Mapping Toolbox features, and with shapefiles imported with shaperead().

Structs are composed of the following fields:

- Geometry
- Name
- Description
- Lon
- Lat
- BoundingBox

Mapping Toolbox polygons require a clockwise convention to specify between negative and positive areas. These are not specified in .kml files, so all polygons are converted to clockwise orientation to guarantee that they represent positive space.

Comments and Ratings (21)

Marc Compere

requires Mapping Toolbox. No workee

Davide Lovell's comment on this file is pretty necessary if the KML file you're loading in only has 2-D geometry. Inserting that at line 77 allowed the code to run without error on my 2-D only data.

Amanda Wild

The description contains a height value for each point after the latitude and longitude. Then there is an east and north component before another height is listed. Which height, the first one or the second is the altitude? The two height values for one point are different. What are they representing and why are they different?

Can you help me to make 2D matrix representation of KML file to plot 3D surface?

Thanks so much! your function works so good!

When I put in the kml file name as the argument it tells me it needs more arguments. Why?

I get an error when loading a kml file generated via Google Earth Engine API. The kml only contains points, no lines or polygons. Also as mentioned by another, you will need the Mapping Toolbox for this to work.

I came across a kml file that had only 2-D geometry, and that generated with this file. This can be fixed by counting commas, however. The following code can be substituted in the relevant part of this file:

numOfCommas = length(strfind(coordStr, ','));
[m,n] = size(coordMat);
if (m*n/numOfCommas == 2)
% Rearrange coordinates to form an x-by-2 matrix
coordMat = reshape(coordMat,2,m*n/2)';
else
% Rearrange coordinates to form an x-by-3 matrix
coordMat = reshape(coordMat,3,m*n/3)';
end

Bruno Dias

Once I wrote a function that would allow me to read files generated by Google Earth and get the names and coordinates of points or paths contained in that file. However, these files had to be generated by Google Earth. This m-file enabled me to overcome this problem.
Thank you.

Bruno Dias

However, these files had to be generated by Google Earth. This m-file enabled me to overcome this problem.
Thank you.

Chad Greene

This function solved my problem perfectly, and it's easy to use. Thanks for sharing.

Lex Lombardi

Found a bug with the regular expression used to match XML fields. '.+?' is used to match the value between an xml tag pair, but this does not match the empty case. '.*?' will match an empty tag pair.

Gene

Does just what I need with no significant effort on my part - Thanks

David Young

Repeating Brian Emery's point: it's important that the "Required Products" list includes the mapping toolbox. At the time of writing this comment, it's not listed as required.

John Paden

Brian Emery

Note that this requires poly2ccw.m which is in the mapping toolbox.

Stephen

This works well, but it has a couple minor bugs.
1. The "cell2double" function on line 77 should be replaced with "str2double".
2. The regular expressions on lines 44-45 fail to trim off the <description> tags.

Stephen

Updates

1.1.0.0

Fixed description tag clipping, and the cell2double issue - both of which were mentioned by Stephen in the comments.

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