{"id":9486,"date":"2018-02-23T09:00:03","date_gmt":"2018-02-23T14:00:03","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=9486"},"modified":"2018-03-06T16:05:56","modified_gmt":"2018-03-06T21:05:56","slug":"contour-data","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2018\/02\/23\/contour-data\/","title":{"rendered":"Contour Data"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\n   <introduction><\/p>\n<p><a href=\"https:\/\/www.mathworks.com\/\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>&#8216;s pick this week is <a href=\"https:\/\/www.mathworks.com\/\/matlabcentral\/fileexchange\/38863\"><tt>contourdata<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/\/matlabcentral\/profile\/authors\/2916610\">Duane Hanselman<\/a>.\n      <\/p>\n<p>   <\/introduction><\/p>\n<p>Last week I was asked a simple question from a customer: &#8220;How do I export a contour plot to Google Earth?&#8221;<\/p>\n<p>I knew that this should be possible for either contour line plots or filled plots since the <a href=\"https:\/\/www.mathworks.com\/products\/mapping.html\">Mapping Toolbox<\/a> has functions for writing the needed plot primitives, i.e: lines (<a href=\"https:\/\/www.mathworks.com\/help\/map\/ref\/kmlwriteline.html\"><tt>kmlwriteline<\/tt><\/a>), polygons (<a href=\"https:\/\/www.mathworks.com\/help\/map\/ref\/kmlwritepolygon.html\"><tt>kmlwritepolygon<\/tt><\/a>), and points (<a href=\"https:\/\/www.mathworks.com\/help\/map\/ref\/kmlwritepoint.html\"><tt>kmlwritepoint<\/tt><\/a>).\n   <\/p>\n<p>So how to do it?  I knew about the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/contourc.html\"><tt>contourc<\/tt><\/a> function but had never used it.  In theory, this would give me the contour lines that I could then write to Google Earth with the kml* functions.\n   <\/p>\n<p>For this example, I&#8217;ll use peaks, and plot them roughly over New England.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">z = peaks(50);\r\nlon = linspace(-73, -70, 50);\r\nlat = linspace(42, 45, 50);\r\nnlevels = 10;\r\nc = contourc(lon, lat, z, nlevels);<\/pre>\n<p>Okay, so what is c?<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">whos(<span style=\"color: #A020F0\">'c'<\/span>)<\/pre>\n<pre style=\"font-style:oblique\">  Name      Size             Bytes  Class     Attributes\r\n\r\n  c         2x817            13072  double              \r\n\r\n<\/pre>\n<p>Huh?<\/p>\n<p>To the documentation I go.  The doc for <tt>contourc<\/tt> pointed to the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.chart.primitive.contour-properties.html#budgut_-ContourMatrix\">ContourMatrix<\/a> property of a contour plot.  The rows of the matrix are x and y preceded by the contour level and number of points.  Contourc very quickly became one of my not favorite functions.  Ughh.  I generally try to avoid structures and prefer tables but this is an <i>ideal<\/i> use for an array of structures.  It has some meta information: level, number of points, and whether it&#8217;s a closed contour or not, and some data that could be various sizes so you can&#8217;t use a table.\n   <\/p>\n<p>Am I really going to have to write a while-loop to parse this thing?<\/p>\n<p>No!  Professor Hanselman has a function that does <i>exactly<\/i> what I want.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">cs = contourdata(c);\r\ndisp(cs)<\/pre>\n<pre style=\"font-style:oblique\">  1&times;16 struct array with fields:\r\n    level\r\n    numel\r\n    xdata\r\n    ydata\r\n    isopen\r\n<\/pre>\n<p>Perfect.<\/p>\n<p>So to finish off the example what else did I have to do?<\/p>\n<p><tt>kmlwrite<\/tt> would be the easiest way to write everything in one shot.  It accepts a <a href=\"https:\/\/www.mathworks.com\/help\/map\/ref\/geoshape.html\"><tt>geoshape<\/tt><\/a>.  I need to have the string literals <i>&#8220;Lat&#8221;, &#8220;Lon&#8221;, &#8220;Latitude&#8221;<\/i> or <i>&#8220;Longitude&#8221;<\/i> as fields of a struct to pass in.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[cs.Lat] = cs.ydata;\r\n[cs.Lon] = cs.xdata;\r\ncs = rmfield(cs, {<span style=\"color: #A020F0\">'xdata'<\/span>, <span style=\"color: #A020F0\">'ydata'<\/span>});\r\ng = geoshape(cs);<\/pre>\n<p>Geoshapes can be written directly with <tt>kmlwrite<\/tt>.  However, I need to tell <tt>kmlwrite<\/tt> which color to use for each line.  The level value is stored as a feature property in the geoshape passed in from the struct.  We can get the corresponding color using the index into the unique levels which are sorted be default.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[~, ~, levidx] = unique(g.level);\r\ncmap = parula(nlevels);<\/pre>\n<p>Finally, write the kml file.  In order to guarantee that the levels can be seen, clamp them to the ground.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">kmlwrite(<span style=\"color: #A020F0\">'NewEnglandPeaks.kml'<\/span>, g, <span style=\"color: #A020F0\">'Color'<\/span>, cmap(levidx, :), <span style=\"color: #A020F0\">'AltitudeMode'<\/span>, <span style=\"color: #A020F0\">'clampToGround'<\/span>)<\/pre>\n<pre style=\"font-style:oblique\">Warning: Omitting unsupported data\r\nclass: logical \r\n<\/pre>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/nep.png\"> <\/p>\n<h3>Comments<a name=\"9\"><\/a><\/h3>\n<p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=9486#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/\/matlabcentral\/fileexchange\/38863#comments\">comment<\/a> for Prof. Hanselman.\n   <\/p>\n<p><script language=\"JavaScript\">\n<!--\n\n    function grabCode_39b2aa407d3b4aa9bba4cf507a56260a() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='39b2aa407d3b4aa9bba4cf507a56260a ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 39b2aa407d3b4aa9bba4cf507a56260a';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Sean de Wolski';\n        copyright = 'Copyright 2018 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n<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\n\\n');\n      \n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }   \n      \n-->\n<\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><a href=\"javascript:grabCode_39b2aa407d3b4aa9bba4cf507a56260a()\"><span style=\"font-size: x-small;        font-style: italic;\">Get<br \/>\n            the MATLAB code<br \/>\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>      Published with MATLAB&reg; R2017b<\/p>\n<\/div>\n<p><!--\n39b2aa407d3b4aa9bba4cf507a56260a ##### SOURCE BEGIN #####\n%% Contour Data\n%\n% <https:\/\/www.mathworks.com\/\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick this week is\n% <https:\/\/www.mathworks.com\/\/matlabcentral\/fileexchange\/38863 |contourdata|> by\n% <https:\/\/www.mathworks.com\/\/matlabcentral\/profile\/authors\/2916610 Duane Hanselman>.\n% \n\n%% \n%\n% Last week I was asked a simple question from a customer: \"How do I export a\n% contour plot to Google Earth?\"\n%\n% I knew that this should be possible for either contour line plots or\n% filled plots since the <https:\/\/www.mathworks.com\/products\/mapping.html\n% Mapping Toolbox> has functions for writing the needed plot primitives,\n% i.e: lines (<https:\/\/www.mathworks.com\/help\/map\/ref\/kmlwriteline.html\n% |kmlwriteline|>), polygons\n% (<https:\/\/www.mathworks.com\/help\/map\/ref\/kmlwritepolygon.html\n% |kmlwritepolygon|>), and points\n% (<https:\/\/www.mathworks.com\/help\/map\/ref\/kmlwritepoint.html\n% |kmlwritepoint|>).\n%\n% So how to do it?  I knew about the\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/contourc.html |contourc|>\n% function but had never used it.  In theory, this would give me the\n% contour lines that I could then write to Google Earth with the kml*\n% functions.\n%\n% For this example, I'll use peaks, and plot them roughly over New England.\n%\n\nz = peaks(50);\nlon = linspace(-73, -70, 50);\nlat = linspace(42, 45, 50);\nnlevels = 10;\nc = contourc(lon, lat, z, nlevels);\n\n%%\n% Okay, so what is c?\n\nwhos('c')\n\n%%\n% Huh?\n%\n% Do the documentation I go.  The doc for |contourc| pointed to the\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.chart.primitive.contour-properties.html#budgut_-ContourMatrix\n% ContourMatrix> property of a contour plot.  The rows of the matrix are x\n% and y preceded by the contour level and number of points.  Contourc very\n% quickly became one of my not favorite functions.  Ughh.  I generally try\n% to avoid structures and prefer tables but this is an _ideal_ use for an\n% array of structures.  It has some meta information: level, number of\n% points, and whether it's a closed contour or not, and some data that\n% could be various sizes so you can't use a table.\n%\n% Am I really going to have to write a while-loop to parse this thing?\n% \n% No!  Professor Hanselman has a function that does _exactly_ what I want.\n\ncs = contourdata(c);\ndisp(cs)\n\n%%\n% Perfect.\n%\n% So to finish off the example what else did I have to do.\n%\n% |kmlwrite| would be the easiest way to write everything in one shot.  It\n% accepts a <https:\/\/www.mathworks.com\/help\/map\/ref\/geoshape.html\n% |geoshape|>.  I need to have the string literals _\"Lat\", \"Lon\",\n% \"Latitude\"_ or _\"Longitude\"_ as fields of a struct to pass in.\n\n[cs.Lat] = cs.ydata;\n[cs.Lon] = cs.xdata;\ncs = rmfield(cs, {'xdata', 'ydata'});\ng = geoshape(cs);\n\n%% \n% Geoshapes can be written directly with |kmlwrite|.  However, I need to\n% tell |kmlwrite| which color to use for each line.  The level value is\n% stored as a feature property in the geoshape passed in from the struct.\n% We can get the corresponding color using the index into the unique\n% levels which are sorted be default.\n\n[~, ~, levidx] = unique(g.level);\ncmap = parula(nlevels); \n\n%%\n% Finally, write the kml file.  In order to guarantee that the levels can\n% be seen, clamp them to the ground.\n\nkmlwrite('NewEnglandPeaks.kml', g, 'Color', cmap(levidx, :), 'AltitudeMode', 'clampToGround')\n\n%%\n%\n% <<nep.png>>\n\n%% Comments\n% \n% Give it a try and let us know what you think\n% <https:\/\/blogs.mathworks.com\/pick\/?p=9486#respond here> or leave a\n% <https:\/\/www.mathworks.com\/\/matlabcentral\/fileexchange\/38863#comments\n% comment> for Prof. Hanselman.\n##### SOURCE END ##### 39b2aa407d3b4aa9bba4cf507a56260a\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/nep.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Sean&#8216;s pick this week is contourdata by Duane Hanselman.<\/p>\n<p>Last week I was asked a simple question from a customer: &#8220;How do I export a contour plot to Google Earth?&#8221;<br \/>\nI&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2018\/02\/23\/contour-data\/\">read more >><\/a><\/p>\n","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\/9486"}],"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=9486"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9486\/revisions"}],"predecessor-version":[{"id":9510,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9486\/revisions\/9510"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=9486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=9486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=9486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}