{"id":753,"date":"2024-01-06T13:23:13","date_gmt":"2024-01-06T20:23:13","guid":{"rendered":"https:\/\/blogs.ubc.ca\/yiwang28\/?p=753"},"modified":"2024-01-06T13:23:13","modified_gmt":"2024-01-06T20:23:13","slug":"my-r-learning-notes-3-how-to-calculate-the-shortest-distance-to-coast","status":"publish","type":"post","link":"https:\/\/blogs.ubc.ca\/yiwang28\/2024\/01\/06\/my-r-learning-notes-3-how-to-calculate-the-shortest-distance-to-coast\/","title":{"rendered":"My R learning notes 3: how to calculate the shortest distance to coast?"},"content":{"rendered":"<p>Recently, I have been working on processing data from multiple sites, which involved calculating the distance to the nearest coastline for the study sites. After exploring various methods in R and ArcGIS to compute the shortest distances, I found that many were complex and tedious. However, by integrating ideas from <a href=\"https:\/\/stackoverflow.com\/questions\/51837454\/r-measuring-distance-from-a-coastline\">different sources<\/a>, I&#8217;ve developed a simplest and fastest approach for calculating the shortest distance for each site by using R. This method is capable of processing multiple sites at the same time. I&#8217;d happy to share my codes below. Please feel free to let me know if you have any questions \ud83d\ude42<\/p>\n<p><span style=\"color: #008000;\"># Load library<\/span><br \/>\n<span style=\"color: #008000;\">library(ggOceanMaps) # in order to use its function dis2land, the output are in kilometers.<\/span><br \/>\n<span style=\"color: #008000;\">library(rnaturalearth)<\/span><br \/>\n<span style=\"color: #008000;\">library(sf)<\/span><\/p>\n<p><span style=\"color: #008000;\"># Load coastline data<\/span><br \/>\n<span style=\"color: #008000;\">coast &lt;- ne_coastline(scale=10, returnclass = &#8220;sf&#8221;)<\/span><br \/>\n<span style=\"color: #008000;\">Your_sites &lt;- read.csv(&#8220;Your_sites.csv&#8221;)<\/span><\/p>\n<pre># For example, I have a csv file called \"Global sites\", with three columns: sites_codes, Longitude (LON), and latitude (LAT):\r\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-754\" src=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2024\/01\/Screenshot-2024-01-06-151140.png\" alt=\"\" width=\"245\" height=\"304\" srcset=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2024\/01\/Screenshot-2024-01-06-151140.png 346w, https:\/\/blogs.ubc.ca\/yiwang28\/files\/2024\/01\/Screenshot-2024-01-06-151140-242x300.png 242w\" sizes=\"auto, (max-width: 245px) 100vw, 245px\" \/><\/pre>\n<p><span style=\"color: #008000;\"># Calculate the shortest distance<\/span><br \/>\n<span style=\"color: #008000;\">ggOceanMaps::dist2land(Your_sites,shapefile =coast)<\/span><\/p>\n<pre># The results will look like:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-755\" src=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2024\/01\/Screenshot-2024-01-06-151548.png\" alt=\"\" width=\"663\" height=\"310\" srcset=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2024\/01\/Screenshot-2024-01-06-151548.png 663w, https:\/\/blogs.ubc.ca\/yiwang28\/files\/2024\/01\/Screenshot-2024-01-06-151548-300x140.png 300w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/>\r\nldist is the shortest distance (kilometers).<\/pre>\n<p>&nbsp;<\/p>\n<p># Additional explanations:<\/p>\n<p>The usage of ggOceanMaps library: We will use its function called &#8220;dis2land&#8221; to calculate the shortest distance. The function was originally designed for calculating the distance from a point in the ocean to the land (reference: <a href=\"https:\/\/mikkovihtakari.github.io\/ggOceanMaps\/reference\/dist2land.html#value\">R\/dist2land.R<\/a> ). But we\u00a0 can replace the shapefile of the ocean by the shapefile of the coastline, so that we can calculate distance to the closest shoreline. The output is &#8220;ldist&#8221; and it is in kilometers.<\/p>\n<p>The usage of rnaturalearth library: we can get the global coastline shapefile with the available finest resolution (scale =10) directly from this library.<\/p>\n<p>The method presents here is inspired by Mikko&#8217;s answer at https:\/\/stackoverflow.com\/questions\/51837454\/r-measuring-distance-from-a-coastline.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I have been working on processing data from multiple sites, which involved calculating the distance to the nearest coastline for the study sites. After exploring various methods in R and ArcGIS to compute the shortest distances, I found that many were complex and tedious. However, by integrating ideas from different sources, I&#8217;ve developed a &hellip; <a href=\"https:\/\/blogs.ubc.ca\/yiwang28\/2024\/01\/06\/my-r-learning-notes-3-how-to-calculate-the-shortest-distance-to-coast\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">My R learning notes 3: how to calculate the shortest distance to coast?<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":40393,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[67],"tags":[],"class_list":["post-753","post","type-post","status-publish","format-standard","hentry","category-blogs"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/posts\/753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/users\/40393"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/comments?post=753"}],"version-history":[{"count":1,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/posts\/753\/revisions"}],"predecessor-version":[{"id":756,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/posts\/753\/revisions\/756"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/media?parent=753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/categories?post=753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/tags?post=753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}