{"id":5776,"date":"2023-01-23T15:46:43","date_gmt":"2023-01-23T07:46:43","guid":{"rendered":"https:\/\/people.utm.my\/shahabuddin\/?p=5776"},"modified":"2023-01-31T13:17:34","modified_gmt":"2023-01-31T05:17:34","slug":"line-simplification-algorihtms-in-python","status":"publish","type":"post","link":"https:\/\/people.utm.my\/shahabuddin\/?p=5776","title":{"rendered":"Line Simplification Algorihtms in Python"},"content":{"rendered":"\n<p>Here is an example of how the Douglas-Peucker, Visvalingam-Whyatt, and Reumann-Witkam line simplification algorithms can be implemented in Python:<\/p>\n<p>Douglas-Peucker algorithm:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-dark-gray-background-color has-text-color has-background\"><code>\ndef douglas_peucker(points, tolerance):\n    def point_line_distance(point, start, end):\n        if (start == end):\n            return float('inf')\n        else:\n            n = len(point)\n            X, Y = point&#091;:,0], point&#091;:,1]\n            AB = &#091;end&#091;0]-start&#091;0], end&#091;1]-start&#091;1]]\n            if n == 2:\n                return abs(np.cross(np.array(&#091;X&#091;1]-X&#091;0], Y&#091;1]-Y&#091;0]]), np.array(start))\/np.linalg.norm(AB))\n            else:\n                return np.min(&#091;point_line_distance(point&#091;i:i+2,:], start, end) for i in range(n-1)])\n    def dp_recursive(points, start, end, tolerance):\n        dmax = 0\n        index = 0\n        for i in range(start+1,end):\n            d = point_line_distance(points&#091;start:end], points&#091;start], points&#091;end])\n            if d &gt; dmax:\n                index = i\n                dmax = d\n        if dmax &gt;= tolerance:\n            results = dp_recursive(points, start, index, tolerance) + dp_recursive(points, index, end, tolerance)\n        else:\n            results = &#091;points&#091;start], points&#091;end]]\n        return results\n    return dp_recursive(points, 0, len(points)-1, tolerance)\n\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Visvalingam-Whyatt algorithm:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-dark-gray-background-color has-text-color has-background\"><code>\ndef visvalingam_whyatt(points, tolerance):\n    def area(p1, p2, p3):\n        return abs((p1&#091;0]*(p2&#091;1]-p3&#091;1]) + p2&#091;0]*(p3&#091;1]-p1&#091;1]) + p3&#091;0]*(p1&#091;1]-p2&#091;1]))\/2)\n    n = len(points)\n    i = 0\n    while i &lt; n-2:\n        if area(points&#091;i], points&#091;i+1], points&#091;i+2]) &lt; tolerance:\n            points.pop(i+1)\n            n -= 1\n        else:\n            i += 1\n    return points\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Reumann-Witkam algorithm:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-dark-gray-background-color has-text-color has-background\"><code>\ndef reumann_witkam(points, tolerance):\n    def point_line_distance(point, start, end):\n        if (start == end):\n            return float('inf')\n        else:\n            n = len(point)\n            X, Y = point&#091;:,0], point&#091;:,1]\n            AB = &#091;end&#091;0]-start&#091;0], end&#091;1]-start&#091;1]]\n            if n == 2:\n                return abs(np.cross(np.array(&#091;X&#091;1]-X&#091;0], Y&#091;1]-Y&#091;0]]), np.array(start))\/np.linalg.norm(AB))\n            else:\n                return np.min(&#091;point_line_distance(point&#091;i:i+2,:], start, end) for i in range(n-1)])\n    i = 1\n    while i &lt; len(points)-1:\n        d = point_line_distance(points&#091;i], points&#091;0], points&#091;-1])\n        if d &gt; tolerance:\n            points.pop(i)\n        else:\n            i += 1\n    return points\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In these implementations, the input is a list of points, and the tolerance value is a real number used to define the level of simplification. The output is a simplified version of the input line, represented as a list of points.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s important to note that these implementations make use of numpy library and they expect the input points to be in the form of numpy array. Also, these codes are just examples and they might not work as is, they may require some adjustments based on the specific use case.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is an example of how the Douglas-Peucker, Visvalingam-Whyatt, and Reumann-Witkam line simplification algorithms can be implemented in Python: Douglas-Peucker algorithm: Visvalingam-Whyatt algorithm: Reumann-Witkam algorithm: In these implementations, the input is a list of points, and the tolerance value is a real number used to define the level of simplification. The output is a simplified [&hellip;]<\/p>\n","protected":false},"author":6946,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[24,111,25,110,5],"tags":[521,539,348,540],"class_list":["post-5776","post","type-post","status-publish","format-standard","hentry","category-knowledge","category-project","category-public","category-research","category-teaching","tag-algorithm","tag-line","tag-python","tag-simplification"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"slider-thumb":false,"pop-thumb":false},"uagb_author_info":{"display_name":"Dr. Shah","author_link":"https:\/\/people.utm.my\/shahabuddin\/?author=6946"},"uagb_comment_info":0,"uagb_excerpt":"Here is an example of how the Douglas-Peucker, Visvalingam-Whyatt, and Reumann-Witkam line simplification algorithms can be implemented in Python: Douglas-Peucker algorithm: Visvalingam-Whyatt algorithm: Reumann-Witkam algorithm: In these implementations, the input is a list of points, and the tolerance value is a real number used to define the level of simplification. The output is a simplified&hellip;","_links":{"self":[{"href":"https:\/\/people.utm.my\/shahabuddin\/index.php?rest_route=\/wp\/v2\/posts\/5776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/people.utm.my\/shahabuddin\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/people.utm.my\/shahabuddin\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/people.utm.my\/shahabuddin\/index.php?rest_route=\/wp\/v2\/users\/6946"}],"replies":[{"embeddable":true,"href":"https:\/\/people.utm.my\/shahabuddin\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5776"}],"version-history":[{"count":4,"href":"https:\/\/people.utm.my\/shahabuddin\/index.php?rest_route=\/wp\/v2\/posts\/5776\/revisions"}],"predecessor-version":[{"id":5897,"href":"https:\/\/people.utm.my\/shahabuddin\/index.php?rest_route=\/wp\/v2\/posts\/5776\/revisions\/5897"}],"wp:attachment":[{"href":"https:\/\/people.utm.my\/shahabuddin\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/people.utm.my\/shahabuddin\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/people.utm.my\/shahabuddin\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}