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:
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.
James Slegers (2021). kml2struct (https://www.mathworks.com/matlabcentral/fileexchange/35642-kml2struct), MATLAB Central File Exchange. Retrieved .
Please note that the fix proposed by Davide Lovell bears a fatal flaw: what about multiples of both 2 and 3? If you have 12 values, how would you know if it's 4x (lat, lon, alt) or 6x (lat, lon)? Sure, you could compare all lat-values and all lon-values and reason about similarity but one should never add too much intelligence into basic functions. In my version I handle this with an optional parameter. (It also read KMZ files, not only KML). Published at https://se.mathworks.com/matlabcentral/fileexchange/80083-kml2struct_multi
This can only import one geometry per placemark. I therefore modified the code and published it here:
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.
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)';
% Rearrange coordinates to form an x-by-3 matrix
coordMat = reshape(coordMat,3,m*n/3)';
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.
However, these files had to be generated by Google Earth. This m-file enabled me to overcome this problem.
This function solved my problem perfectly, and it's easy to use. Thanks for sharing.
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.
Does just what I need with no significant effort on my part - Thanks
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.
Note that this requires poly2ccw.m which is in the mapping toolbox.
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.
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!