{"id":7416,"date":"2016-05-20T09:00:08","date_gmt":"2016-05-20T13:00:08","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=7416"},"modified":"2016-05-20T08:00:52","modified_gmt":"2016-05-20T12:00:52","slug":"where-are-you-flying-over","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/05\/20\/where-are-you-flying-over\/","title":{"rendered":"Where are you flying over?"},"content":{"rendered":"\r\n<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/56878-cityname\">CityName<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3983697\">Richard Moore<\/a>.<\/p><p>When I used to travel to various places in the U.S. for work, I would wonder which cities I was flying over during the flight. On some planes, they have seat back monitors that show the flight path with names of cities the plane is flying over.<\/p><p>With Richard's CityName, I can do the same. I simply call the function like this<\/p><pre class=\"language-matlab\">[city, country, dist] = CityName(lat, lon)\r\n<\/pre><p>and it returns the closest city to the latitude and logitude location based on the GeoNames 5000 cities. It also returns the distance (in km) from the city center.<\/p><p>Lets say that I am flying from Boston, Massachusetts to Los Angeles, California.<\/p><pre class=\"codeinput\">boston = [42.36 -71.06];\r\nlos = [34.052 -118.244];\r\n<\/pre><p>We can plot the map of the U.S. using the <a href=\"https:\/\/www.mathworks.com\/products\/mapping\/\">Mapping Toolbox<\/a>.<\/p><pre class=\"codeinput\">ax = usamap(<span class=\"string\">'conus'<\/span>);\r\nstates = shaperead(<span class=\"string\">'usastatehi'<\/span>, <span class=\"string\">'UseGeoCoords'<\/span>, true,<span class=\"keyword\">...<\/span>\r\n  <span class=\"string\">'Selector'<\/span>, {@(name) ~any(strcmp(name,{<span class=\"string\">'Alaska'<\/span>,<span class=\"string\">'Hawaii'<\/span>})), <span class=\"string\">'Name'<\/span>});\r\ngeoshow(ax, states, <span class=\"string\">'DisplayType'<\/span>, <span class=\"string\">'polygon'<\/span>, <span class=\"string\">'FaceColor'<\/span>, [0.5 1 0.5])\r\nframem <span class=\"string\">off<\/span>, gridm <span class=\"string\">off<\/span>, mlabel <span class=\"string\">off<\/span>, plabel <span class=\"string\">off<\/span>\r\n<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_cityname\/potw_cityname_us.png\" alt=\"\"> <\/p><p>Next, we can calculate the great circle track between the two cities, again using <a href=\"https:\/\/www.mathworks.com\/help\/map\/ref\/track2.html\"><tt>track2<\/tt><\/a> from the Mapping Toolbox. Yes, this is not the path a plane would take, but for the purpose of this post, this will do.<\/p><pre class=\"codeinput\">[lat,lon] = track2(boston(1),boston(2),los(1),los(2));\r\n<\/pre><p>Now, we can write a loop for each coordinate to find the closest city within 10 km of the path.<\/p><pre class=\"codeinput\"><span class=\"comment\">% Figure out view limits for animation purpose<\/span>\r\nsLim = [1386896     3288804     4332357     5404342];\r\neLim = [-2438739     -536831     3335946     4407931];\r\nlimits = [linspace(sLim(1),eLim(1))',linspace(sLim(2),eLim(2))',<span class=\"keyword\">...<\/span>\r\n    linspace(sLim(3),eLim(3))',linspace(sLim(4),eLim(4))'];\r\n\r\nright = true;   <span class=\"comment\">% Used for text placement<\/span>\r\n<span class=\"keyword\">for<\/span> id = 1:100\r\n    <span class=\"comment\">% Plot a segment for the path<\/span>\r\n    <span class=\"keyword\">if<\/span> id &lt; 100\r\n        plotm(lat(id:id+1),lon(id:id+1),<span class=\"string\">'Color'<\/span>,<span class=\"string\">'r'<\/span>,<span class=\"string\">'LineWidth'<\/span>,2)\r\n    <span class=\"keyword\">end<\/span>\r\n\r\n    <span class=\"comment\">% Set axis limits<\/span>\r\n    axis(limits(id,:))\r\n\r\n    <span class=\"comment\">% Find closest city<\/span>\r\n    [c,~,dist] = CityName(lat(id),lon(id));\r\n\r\n    <span class=\"comment\">% If the distance is within 10 km, display name of the city<\/span>\r\n    <span class=\"keyword\">if<\/span> dist &lt; 10\r\n        c = strtrim(c);\r\n\r\n        <span class=\"comment\">% Place point<\/span>\r\n        plotm(lat(id),lon(id),<span class=\"string\">'Color'<\/span>,<span class=\"string\">'b'<\/span>,<span class=\"string\">'Marker'<\/span>,<span class=\"string\">'o'<\/span>,<span class=\"string\">'MarkerFaceColor'<\/span>,<span class=\"string\">'b'<\/span>)\r\n\r\n        <span class=\"comment\">% Display name of city. Alternate placing text on right and left.<\/span>\r\n        <span class=\"keyword\">if<\/span> right\r\n            textm(lat(id),lon(id),c,<span class=\"keyword\">...<\/span>\r\n                <span class=\"string\">'HorizontalAlignment'<\/span>,<span class=\"string\">'left'<\/span>,<span class=\"string\">'VerticalAlignment'<\/span>,<span class=\"string\">'top'<\/span>,<span class=\"keyword\">...<\/span>\r\n                <span class=\"string\">'FontSize'<\/span>,12,<span class=\"string\">'FontWeight'<\/span>,<span class=\"string\">'bold'<\/span>,<span class=\"string\">'Color'<\/span>,<span class=\"string\">'r'<\/span>)\r\n        <span class=\"keyword\">else<\/span>\r\n            textm(lat(id),lon(id),c,<span class=\"keyword\">...<\/span>\r\n                <span class=\"string\">'HorizontalAlignment'<\/span>,<span class=\"string\">'right'<\/span>,<span class=\"string\">'VerticalAlignment'<\/span>,<span class=\"string\">'bottom'<\/span>,<span class=\"keyword\">...<\/span>\r\n                <span class=\"string\">'FontSize'<\/span>,12,<span class=\"string\">'FontWeight'<\/span>,<span class=\"string\">'bold'<\/span>,<span class=\"string\">'Color'<\/span>,<span class=\"string\">'r'<\/span>)\r\n        <span class=\"keyword\">end<\/span>\r\n        right = ~right;\r\n    <span class=\"keyword\">end<\/span>\r\n    drawnow\r\n<span class=\"keyword\">end<\/span>\r\n<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_cityname\/flightmap.gif\" alt=\"\"> <\/p><p><b>Comments<\/b><\/p><p>Give this a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=7416#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/56878-cityname#comments\">comment<\/a> for Richard.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_3bfcdd91bbf14e6f8a35103af2941bfa() {\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='3bfcdd91bbf14e6f8a35103af2941bfa ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 3bfcdd91bbf14e6f8a35103af2941bfa';\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        copyright = 'Copyright 2016 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_3bfcdd91bbf14e6f8a35103af2941bfa()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2016a<br><\/p><\/div><!--\r\n3bfcdd91bbf14e6f8a35103af2941bfa ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/56878-cityname CityName> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3983697 Richard\r\n% Moore>.\r\n%\r\n% When I used to travel to various places in the U.S. for work, I would\r\n% wonder which cities I was flying over during the flight. On some planes,\r\n% they have seat back monitors that show the flight path with names of\r\n% cities the plane is flying over.\r\n%\r\n% With Richard's CityName, I can do the same. I simply call the function\r\n% like this\r\n%\r\n%   [city, country, dist] = CityName(lat, lon)\r\n%\r\n% and it returns the closest city to the latitude and logitude location\r\n% based on the GeoNames 5000 cities. It also returns the distance (in km)\r\n% from the city center.\r\n%\r\n% Lets say that I am flying from Boston, Massachusetts to Los Angeles,\r\n% California.\r\n\r\nboston = [42.36 -71.06];\r\nlos = [34.052 -118.244];\r\n\r\n%%\r\n% We can plot the map of the U.S. using the\r\n% <https:\/\/www.mathworks.com\/products\/mapping\/ Mapping Toolbox>.\r\nax = usamap('conus');\r\nstates = shaperead('usastatehi', 'UseGeoCoords', true,...\r\n  'Selector', {@(name) ~any(strcmp(name,{'Alaska','Hawaii'})), 'Name'});\r\ngeoshow(ax, states, 'DisplayType', 'polygon', 'FaceColor', [0.5 1 0.5])\r\nframem off, gridm off, mlabel off, plabel off\r\n\r\n%%\r\n% <<potw_cityname_us.png>>\r\n%\r\n% Next, we can calculate the great circle track between the two cities,\r\n% again using <https:\/\/www.mathworks.com\/help\/map\/ref\/track2.html |track2|>\r\n% from the Mapping Toolbox. Yes, this is not the path a plane would take,\r\n% but for the purpose of this post, this will do.\r\n[lat,lon] = track2(boston(1),boston(2),los(1),los(2));\r\n\r\n%%\r\n% Now, we can write a loop for each coordinate to find the closest city\r\n% within 10 km of the path.\r\n\r\n% Figure out view limits for animation purpose\r\nsLim = [1386896     3288804     4332357     5404342];\r\neLim = [-2438739     -536831     3335946     4407931];\r\nlimits = [linspace(sLim(1),eLim(1))',linspace(sLim(2),eLim(2))',...\r\n    linspace(sLim(3),eLim(3))',linspace(sLim(4),eLim(4))'];\r\n\r\nright = true;   % Used for text placement\r\nfor id = 1:100\r\n    % Plot a segment for the path\r\n    if id < 100\r\n        plotm(lat(id:id+1),lon(id:id+1),'Color','r','LineWidth',2)\r\n    end\r\n    \r\n    % Set axis limits\r\n    axis(limits(id,:))\r\n    \r\n    % Find closest city\r\n    [c,~,dist] = CityName(lat(id),lon(id));\r\n    \r\n    % If the distance is within 10 km, display name of the city\r\n    if dist < 10\r\n        c = strtrim(c);\r\n        \r\n        % Place point\r\n        plotm(lat(id),lon(id),'Color','b','Marker','o','MarkerFaceColor','b')\r\n        \r\n        % Display name of city. Alternate placing text on right and left.\r\n        if right\r\n            textm(lat(id),lon(id),c,...\r\n                'HorizontalAlignment','left','VerticalAlignment','top',...\r\n                'FontSize',12,'FontWeight','bold','Color','r')\r\n        else\r\n            textm(lat(id),lon(id),c,...\r\n                'HorizontalAlignment','right','VerticalAlignment','bottom',...\r\n                'FontSize',12,'FontWeight','bold','Color','r')\r\n        end            \r\n        right = ~right;\r\n    end\r\n    drawnow\r\nend\r\n\r\n%%\r\n% <<flightmap.gif>>\r\n%\r\n% *Comments*\r\n%\r\n% Give this a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=7416#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/56878-cityname#comments\r\n% comment> for Richard.\r\n\r\n##### SOURCE END ##### 3bfcdd91bbf14e6f8a35103af2941bfa\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_cityname\/potw_cityname_us.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\nJiro's pick this week is CityName by Richard Moore.When I used to travel to various places in the U.S. for work, I would wonder which cities I was flying over during the flight. On some planes,... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/05\/20\/where-are-you-flying-over\/\">read more >><\/a><\/p>","protected":false},"author":35,"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\/7416"}],"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\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=7416"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7416\/revisions"}],"predecessor-version":[{"id":7418,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7416\/revisions\/7418"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=7416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=7416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=7416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}