{"id":5253,"date":"2014-04-18T09:00:35","date_gmt":"2014-04-18T13:00:35","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5253"},"modified":"2014-04-18T09:30:53","modified_gmt":"2014-04-18T13:30:53","slug":"kml2struct-easily-import-your-kml-files","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2014\/04\/18\/kml2struct-easily-import-your-kml-files\/","title":{"rendered":"KML2STRUCT &#8211; Easily Import Your KML Files"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495\">Sean<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35642-kml2struct\">kml2struct<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/125372\">James Slegers<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Import Your KML Files<a name=\"1\"><\/a><\/h3>\r\n   <p>Earlier this week, my friend sent me a Google Maps link containing our hiking tracks recorded with the GPS on his smart phone.\r\n       From Google Maps you can download the data as a KML (Keyhole Markup Language) file.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainkml\/download.png\"> <\/p>\r\n   <p>I wanted to plot it and experiment with the data in MATLAB.  Once again, the File Exchange was there for me!<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Read the KML file into a struct:<\/span>\r\nkmlS = kml2struct(<span style=\"color: #A020F0\">'2014-04-12PresidentialTraverse.kml'<\/span>);\r\n\r\n<span style=\"color: #228B22\">% Convert to table (new datatype in R2013b) to make manipulations easier<\/span>\r\nkmlT = struct2table(kmlS);<\/pre><p>Now looking at the table, we can see the four important pieces:<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Geometry: What is it? A point, line, etc.<\/li>\r\n         <li>Lon: Longitude coordinate of tracks<\/li>\r\n         <li>Lat: Latitude coordinate of tracks<\/li>\r\n         <li>Bounding Box: Bounding box if we want to draw it on a map<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainkml\/kmltable.PNG\"> <\/p>\r\n   <p>First, I'll get the bounding box of the whole trip.  To do this, we'll stack each segment's bounding box into the third dimension\r\n      and then pick the min and the max:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">boxes = kmlT.BoundingBox; <span style=\"color: #228B22\">% Extract Bounding box from table<\/span>\r\nboxes3d = cat(3,boxes{:}); <span style=\"color: #228B22\">% Stack along third dimension<\/span>\r\nbbox = [min(boxes3d(1,:,:),[],3); max(boxes3d(2,:,:),[],3)].'; <span style=\"color: #228B22\">% Min and max along third dimension give limits<\/span>\r\nlatlim = bbox(2,:)+[-0.01 0.01]; <span style=\"color: #228B22\">% Buffer them<\/span>\r\nlonlim = bbox(1,:)+[-0.01 0.01];<\/pre><p>Next, I only want to work with the lines, i.e. the actual tracks.  The points represent termini, which I don't need right\r\n      now.  Using the new <a href=\"\">categorical<\/a> data type and logical indexing, we can extract the latitude and longitude from the <a href=\"\">table<\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Make Geometry categorical<\/span>\r\nkmlT.Geometry = categorical(kmlT.Geometry);\r\n\r\n<span style=\"color: #228B22\">% Extract the latitude and longitude for the lines<\/span>\r\nlatlon = kmlT{kmlT.Geometry==<span style=\"color: #A020F0\">'Line'<\/span>, {<span style=\"color: #A020F0\">'Lat'<\/span>,<span style=\"color: #A020F0\">'Lon'<\/span>}};<\/pre><p>I'll get the elevation data from NASA using the <a href=\"\">Web Map Service<\/a> in the <a href=\"https:\/\/www.mathworks.com\/products\/mapping\/\">Mapping Toolbox<\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">nasaLayers = wmsfind(<span style=\"color: #A020F0\">'nasa*elev'<\/span>, <span style=\"color: #A020F0\">'SearchField'<\/span>, <span style=\"color: #A020F0\">'serverurl'<\/span>);\r\nned = refine(nasaLayers, <span style=\"color: #A020F0\">'usgs_ned'<\/span>);\r\n[Z, refmatZ] = wmsread(ned, <span style=\"color: #A020F0\">'Latlim'<\/span>, latlim, <span style=\"color: #A020F0\">'Lonlim'<\/span>, lonlim);\r\nZ = double(Z);<\/pre><p>And finally, plot a contour map with the tracks overlaid on it.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">figure\r\nax = usamap(latlim, lonlim);\r\ngeoshow(Z, refmatZ, <span style=\"color: #A020F0\">'DisplayType'<\/span>, <span style=\"color: #A020F0\">'texturemap'<\/span>)\r\ncontourm(Z, refmatZ, 20, <span style=\"color: #A020F0\">'Color'<\/span>, <span style=\"color: #A020F0\">'k'<\/span>)\r\ndemcmap(Z)\r\ntitle(<span style=\"color: #A020F0\">'Presidential Traverse 04\/12:13\/2014'<\/span>,<span style=\"color: #A020F0\">'FontSize'<\/span>,16)\r\n\r\n<span style=\"color: #228B22\">% Add each segment<\/span>\r\n<span style=\"color: #0000FF\">for<\/span> ii = 1:length(latlon)\r\n    geoshow(latlon{ii,:}, <span style=\"color: #A020F0\">'LineWidth'<\/span>, 2)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainkml\/mainkml_01.png\"> <h3>Comments<a name=\"6\"><\/a><\/h3>\r\n   <p>Have you ever recorded a trip and then tried to analyze it in MATLAB? The tasks above would be more straight-forward if you\r\n      had access to the original GPX files. These typically come with the elevation data and time stamps allowing you to get even more statistics\r\n      with more accuracy.\r\n   <\/p>\r\n   <p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5253#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35642-kml2struct#comments\">comment<\/a> for James.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_17b6af4fd9894ee1b9fe7ed3a82a6bcb() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='17b6af4fd9894ee1b9fe7ed3a82a6bcb ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 17b6af4fd9894ee1b9fe7ed3a82a6bcb';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Sean de Wolski';\r\n        copyright = 'Copyright 2014 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_17b6af4fd9894ee1b9fe7ed3a82a6bcb()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2014a<br><\/p>\r\n<\/div>\r\n<!--\r\n17b6af4fd9894ee1b9fe7ed3a82a6bcb ##### SOURCE BEGIN #####\r\n%% KML2STRUCT \r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495 Sean>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35642-kml2struct kml2struct> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/125372 James Slegers>.\r\n% \r\n\r\n%% Import Your KML Files\r\n%\r\n% Earlier this week, my friend sent me a Google Maps link containing our\r\n% hiking tracks recorded with the GPS on his smart phone.  From Google Maps\r\n% you can download the data as a KML (Keyhole Markup Language) file.\r\n%\r\n% <<download.png>>\r\n%\r\n% I wanted to plot it and do some statistics on it in MATLAB.  Once again,\r\n% the File Exchange was there for me!\r\n%\r\n\r\n% Read the KML file into a struct:\r\nkmlS = kml2struct('2014-04-12PresidentialTraverse.kml');\r\n\r\n% Convert to table (new datatype in R2013b) to make manipulations easier\r\nkmlT = struct2table(kmlS);\r\n\r\n%%\r\n% Now looking at the table, we can see the four important pieces:\r\n% \r\n% * Geometry: What is it? A point, line, etc.\r\n% * Lon: Longitude coordinate of tracks\r\n% * Lat: Latitude coordinate of tracks\r\n% * Bounding Box: Bounding box if we want to draw it on a map\r\n%\r\n% <<kmltable.PNG>>\r\n%\r\n% First, I'll get the bounding box of the whole trip.  To do this,\r\n% we'll stack each segment's bounding box into the third dimension and then\r\n% pick the min and the max:\r\n\r\nboxes = kmlT.BoundingBox; % Extract Bounding box from table\r\nboxes3d = cat(3,boxes{:}); % Stack along third dimension\r\nbbox = [min(boxes3d(1,:,:),[],3); max(boxes3d(2,:,:),[],3)].'; % Min and max along third dimension give limits\r\nlatlim = bbox(2,:)+[-0.01 0.01]; % Buffer them\r\nlonlim = bbox(1,:)+[-0.01 0.01];\r\n\r\n%%\r\n% Next, I only want to work with the lines, i.e. the actual tracks.  The\r\n% points represent termini, which I don't need right now.  Using the new\r\n% <\r\n% categorical> data type and logical indexing, we can extract the latitude\r\n% and longitude from the\r\n% <\r\n% table>.\r\n\r\n% Make Geometry categorical\r\nkmlT.Geometry = categorical(kmlT.Geometry);\r\n\r\n% Extract the latitude and longitude for the lines\r\nlatlon = kmlT{kmlT.Geometry=='Line', {'Lat','Lon'}};\r\n\r\n%% \r\n% I'll get the elevation data from NASA using the\r\n% <\r\n% Web Map Service> in the <https:\/\/www.mathworks.com\/products\/mapping\/\r\n% Mapping Toolbox>.\r\n%\r\n\r\nnasaLayers = wmsfind('nasa*elev', 'SearchField', 'serverurl');\r\nned = refine(nasaLayers, 'usgs_ned');\r\n[Z, refmatZ] = wmsread(ned, 'Latlim', latlim, 'Lonlim', lonlim);\r\nZ = double(Z);\r\n\r\n%% \r\n% And finally, plot a contour map with the tracks overlaid on it.\r\n%\r\n\r\nfigure\r\nax = usamap(latlim, lonlim);\r\ngeoshow(Z, refmatZ, 'DisplayType', 'texturemap')\r\ncontourm(Z, refmatZ, 20, 'Color', 'k')\r\ndemcmap(Z)\r\ntitle('Presidential Traverse 04\/12:13\/2014','FontSize',16)\r\n\r\n% Add each segment\r\nfor ii = 1:length(latlon)\r\n    geoshow(latlon{ii,:}, 'LineWidth', 2)\r\nend\r\n\r\n%% Comments\r\n% \r\n% Have you ever recorded a trip and then tried to analyze it in MATLAB? The\r\n% tasks above would be more straight-forward if you had access to the GPX\r\n% files. These typically come with the elevation data and time stamps\r\n% allowing you to get even more statistics with more accuracy.\r\n%\r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5253#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35642-kml2struct#comments\r\n% comment> for James.\r\n%\r\n \r\n\r\n##### SOURCE END ##### 17b6af4fd9894ee1b9fe7ed3a82a6bcb\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainkml\/download.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Sean's pick this week is kml2struct by James Slegers.\r\n      \r\n   \r\n   Import Your KML Files\r\n   Earlier this week, my friend sent me a Google Maps link containing our hiking... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/04\/18\/kml2struct-easily-import-your-kml-files\/\">read more >><\/a><\/p>","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5253"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5253"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5253\/revisions"}],"predecessor-version":[{"id":5258,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5253\/revisions\/5258"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}