{"id":10438,"date":"2019-01-25T09:00:12","date_gmt":"2019-01-25T14:00:12","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=10438"},"modified":"2019-01-21T18:17:33","modified_gmt":"2019-01-21T23:17:33","slug":"distance-from-points-to-polyline-or-polygon","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2019\/01\/25\/distance-from-points-to-polyline-or-polygon\/","title":{"rendered":"Distance from Points to Polyline or Polygon"},"content":{"rendered":"<div xmlns:mwsh=\"http:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\n   <introduction><\/p>\n<p><a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>&#8216;s pick this week is <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/12744\">Distance from points to polyline or polygon<\/a> by <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/870637\">Michael Yoshpe<\/a>.\n      <\/p>\n<p>   <\/introduction><\/p>\n<p>I recently had a problem where I was trying to identify which corners of a property lot were abutting a street.<\/p>\n<p>Below is an example; the polygons are stored in the relatively new <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/polyshape.html\"><tt>polyshape<\/tt><\/a> class.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">load <span style=\"color: #A020F0\">Polyshapes.mat<\/span>\r\n\r\np(1) = plot(lot, <span style=\"color: #A020F0\">\"FaceColor\"<\/span>, <span style=\"color: #A020F0\">\"b\"<\/span>);\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\np(2) = plot(street, <span style=\"color: #A020F0\">\"FaceColor\"<\/span>, [0.5 0.5 0.5]);\r\nxticks([])\r\nyticks([])\r\nlegend([<span style=\"color: #A020F0\">\"Lot\"<\/span> <span style=\"color: #A020F0\">\"Street\"<\/span>])<\/pre>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/pick\/files\/mainPPolyDist_01.png\"> <\/p>\n<p>I looked at the methods for <tt>polyshape<\/tt> and first thought <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/polyshape.nearestvertex.html\"><tt>nearestvertex<\/tt><\/a> would be the correct way to do it.  I.e. given the nearest vertex for each point, calculate the distance to it and apply a threshold.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">nidx = nearestvertex(street, lot.Vertices);\r\np(3) = plot(street.Vertices(nidx, 1), street.Vertices(nidx, 2), <span style=\"color: #A020F0\">\"r*\"<\/span>, <span style=\"color: #A020F0\">\"DisplayName\"<\/span>, <span style=\"color: #A020F0\">\"Nearest Vertex on Street\"<\/span>);<\/pre>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/pick\/files\/mainPPolyDist_02.png\"> <\/p>\n<p>At first this looks good, but let&#8217;s zoom in:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">load <span style=\"color: #A020F0\">zoomlimits.mat<\/span>\r\nxlim(xlimits)\r\nylim(ylimits)<\/pre>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/pick\/files\/mainPPolyDist_03.png\"> <\/p>\n<p>Nearest vertex is only looking at the vertices for a street.  There can be long distances between the vertices along a boundary so it could return ones that make absolutely no sense e.g. the far side of the street.  What I actually need is the distance from the lot vertices to the boundaries of the street.\n   <\/p>\n<p>Unfortunately, there was no obvious polyshape method in a computational geometry sense to do it.  I could use linspace to add thousands of points to each boundary to up the number of vertices so <tt>nearestvertex<\/tt> could get &#8220;close enough&#8221; but that seemed like a less than optimal means for solving this problem.\n   <\/p>\n<p>I searched <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/\">MATLAB Answers<\/a> and came across <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/95608\">this answer<\/a> from MathWorks&#8217; Tech Support.  This provided a means for finding distance to a line.  One could loop over all of the boundaries of the street, calculate the shortest distance, and then keep the minimum at the end.  Instead, I searched the File Exchange and found Michael&#8217;s <tt>p_poly_dist<\/tt> function. It does exactly what I need (though requires looping over vertices instead of boundaries).\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Grab the boundary from the street.<\/span>\r\n[xbnd, ybnd] = boundary(street);\r\n\r\n<span style=\"color: #228B22\">% Loop over lot vertices, calculating shortest distance to street boundary.<\/span>\r\n<span style=\"color: #0000FF\">for<\/span> ii = size(lot.Vertices, 1):-1:1\r\n    dists(ii, 1) = p_poly_dist(lot.Vertices(ii, 1), lot.Vertices(ii, 2), xbnd, ybnd);\r\n<span style=\"color: #0000FF\">end<\/span>\r\n\r\n<span style=\"color: #228B22\">% A vertex is on the border if it's within threshold or street interior.<\/span>\r\nthreshold = 2;\r\nonborder = (dists &lt; threshold) | isinterior(street, lot.Vertices);\r\ndelete(p(3))\r\nplot(lot.Vertices(onborder, 1), lot.Vertices(onborder, 2), <span style=\"color: #A020F0\">\"mp\"<\/span>, <span style=\"color: #A020F0\">\"DisplayName\"<\/span>, <span style=\"color: #A020F0\">\"On Street\"<\/span>)\r\nxlim <span style=\"color: #A020F0\">auto<\/span>\r\nylim <span style=\"color: #A020F0\">auto<\/span><\/pre>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/pick\/files\/mainPPolyDist_04.png\"> <\/p>\n<p>I&#8217;d also like to give a shout out to <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64396\">Point To Line Distance<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3073010\">Rik Wisselink<\/a> &#8211; who provided input checking to the tech support answer and extended it to work on multiple points.  I didn&#8217;t use it for this because I did not need 3d and looping over boundaries was many more iterations than looping over vertices.\n   <\/p>\n<h3>Comments<a name=\"6\"><\/a><\/h3>\n<p>If you want this functionality in base MATLAB, please contact MathWorks Tech Support and add your vote by letting them know this answer was useful!\n   <\/p>\n<p>Give it a try and let us know what you think <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=10438#respond\">here<\/a> or leave a <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/12744#comments\">comment<\/a> for Michael.\n   <\/p>\n<p><script language=\"JavaScript\">\n<!--\n\n    function grabCode_b86c6657178c45eeb0c8caed44286455() {\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='b86c6657178c45eeb0c8caed44286455 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b86c6657178c45eeb0c8caed44286455';\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 2019 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_b86c6657178c45eeb0c8caed44286455()\"><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; R2019a<\/p>\n<\/div>\n<p><!--\nb86c6657178c45eeb0c8caed44286455 ##### SOURCE BEGIN #####\n%% Distance from Points to Polyline or Polygon\n%\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick this week is\n% <http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/12744 Distance from points to polyline or polygon> by\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/870637 Michael Yoshpe>.\n% \n%% \n%\n% I recently had a problem where I was trying to identify which corners of\n% a property lot were abutting a street.  \n%\n% Below is an example; the polygons are stored in the relatively new\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/polyshape.html |polyshape|>\n% class.\n\nload Polyshapes.mat\n\np(1) = plot(lot, \"FaceColor\", \"b\");\nhold on\np(2) = plot(street, \"FaceColor\", [0.5 0.5 0.5]);\nxticks([])\nyticks([])\nlegend([\"Lot\" \"Street\"])\n\n%%\n% I looked at the methods for |polyshape| and first thought\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/polyshape.nearestvertex.html\n% |nearestvertex|> would be the correct way to do it.  I.e. given the\n% nearest vertex for each point, calculate the distance to it and apply a\n% threshold.\n\nnidx = nearestvertex(street, lot.Vertices);\np(3) = plot(street.Vertices(nidx, 1), street.Vertices(nidx, 2), \"r*\", \"DisplayName\", \"Nearest Vertex on Street\");\n\n%%\n% At first this looks good, but let's zoom in:\n\nload zoomlimits.mat\nxlim(xlimits)\nylim(ylimits)\n\n%%\n% Nearest vertex is only looking at the vertices for a street.  There can\n% be long distances between the vertices along a boundary so it could\n% return ones that make absolutely no sense e.g. the far side of the\n% street.  What I actually need is the distance from the lot vertices to\n% the boundaries of the street.\n%\n% Unfortunately, there was no obvious polyshape method in a computational\n% geometry sense to do it.  I could use linspace to add thousands of points\n% to each boundary to up the number of vertices so |nearestvertex| could\n% get \"close enough\" but that seemed like a less than optimal means for\n% solving this problem.\n%\n% I searched <https:\/\/www.mathworks.com\/matlabcentral\/answers\/ MATLAB\n% Answers> and came across\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/95608 this answer> from\n% MathWorks' Tech Support.  This provided a means for finding distance to a\n% line.  One could loop over all of the boundaries of the street, calculate\n% the shortest distance, and then keep the minimum at the end.  Instead, I\n% searched the File Exchange and found Michael's |p_poly_dist| function. It\n% does exactly what I need (though requires looping over vertices instead\n% of boundaries).\n\n% Grab the boundary from the street.\n[xbnd, ybnd] = boundary(street);\n\n% Loop over lot vertices, calculating shortest distance to street boundary. \nfor ii = size(lot.Vertices, 1):-1:1\n    dists(ii, 1) = p_poly_dist(lot.Vertices(ii, 1), lot.Vertices(ii, 2), xbnd, ybnd);\nend\n\n% A vertex is on the border if it's within threshold or street interior.\nthreshold = 2; \nonborder = (dists < threshold) | isinterior(street, lot.Vertices);\ndelete(p(3))\nplot(lot.Vertices(onborder, 1), lot.Vertices(onborder, 2), \"mp\", \"DisplayName\", \"On Street\")\nxlim auto\nylim auto\n\n%%\n% I'd also like to give a shout out to\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64396 Point To Line\n% Distance> by\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3073010 Rik\n% Wisselink> who provided input checking to the tech support answer and\n% extended it to work on multiple points.  I didn't use it for this because\n% I did not need 3d and looping over boundaries was many more iterations\n% than looping over vertices.\n\n%% Comments\n% If you want this functionality in base MATLAB, please contact MathWorks\n% Tech Support and add your vote by letting them know this answer was\n% useful!\n%\n% Give it a try and let us know what you think\n% <http:\/\/blogs.mathworks.com\/pick\/?p=10438#respond here> or leave a\n% <http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/12744#comments\n% comment> for Michael.\n%\n\n##### SOURCE END ##### b86c6657178c45eeb0c8caed44286455\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"http:\/\/blogs.mathworks.com\/pick\/files\/mainPPolyDist_01.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Sean&#8216;s pick this week is Distance from points to polyline or polygon by Michael Yoshpe.<\/p>\n<p>I recently had a problem where I was trying to identify which corners of a property lot&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2019\/01\/25\/distance-from-points-to-polyline-or-polygon\/\">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\/10438"}],"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=10438"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10438\/revisions"}],"predecessor-version":[{"id":10466,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10438\/revisions\/10466"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=10438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=10438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=10438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}