{"id":346963,"date":"2023-09-21T14:00:00","date_gmt":"2023-09-21T20:00:00","guid":{"rendered":"https:\/\/r.iresmi.net\/posts\/2023\/mountains\/index.html"},"modified":"2023-09-21T14:00:00","modified_gmt":"2023-09-21T20:00:00","slug":"mountains","status":"publish","type":"post","link":"https:\/\/www.r-bloggers.com\/2023\/09\/mountains\/","title":{"rendered":"Mountains"},"content":{"rendered":"<!-- \r\n<div style=\"min-height: 30px;\">\r\n[social4i size=\"small\" align=\"align-left\"]\r\n<\/div>\r\n-->\r\n\r\n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;\">\r\n[This article was first published on  <strong><a href=\"https:\/\/r.iresmi.net\/posts\/2023\/mountains\/index.html\"> r.iresmi.net<\/a><\/strong>, and kindly contributed to <a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers<\/a>].  (You can report issue about the content on this page <a href=\"https:\/\/www.r-bloggers.com\/contact-us\/\">here<\/a>)\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>\n \n\n\n\n\n<div class=\"quarto-figure quarto-figure-center\">\n<figure class=\"figure\">\n<p><img src=\"https:\/\/i0.wp.com\/r.iresmi.net\/posts\/2023\/mountains\/images\/isolines_aconcagua.png?w=578&#038;ssl=1\" class=\"img-fluid figure-img\" alt=\"A map of the Aconcagua mountain consisting of isohypses\" data-recalc-dims=\"1\"><\/p>\n<figcaption class=\"figure-caption\">Aconcagua &#8211; 6991 m<\/figcaption>\n<\/figure>\n<\/div>\n<p>If you like mountains, R and T-shirts, I got you covered. Here we use {elevatr} to get a DEM around some well known summits and make a map consisting only of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Contour_line\" rel=\"nofollow\" target=\"_blank\">isohypses<\/a>. It produces (sometimes) very nice visualizations.<\/p>\n<p>And from these designs we can make nice T-shirts. You can buy some on <a href=\"https:\/\/shirt.iresmi.net\/\" class=\"uri\" rel=\"nofollow\" target=\"_blank\">https:\/\/shirt.iresmi.net\/<\/a> and if your favorite mountain is not there, <a href=\"mailto:michael@@@iresmi.net\" rel=\"nofollow\" target=\"_blank\">ask me<\/a> and I\u2019ll add it (or make it yourself, of course).<\/p>\n<p>I intended to automate the process but sadly the Spreadshirt API doesn\u2019t have an upload feature (!?). If you know a better shop, let me know\u2026<\/p>\n<div class=\"cell\">\n<pre># Config ------------------------------------------------------------------\n\nlibrary(tidyverse)\nlibrary(elevatr)\nlibrary(sf)\n# remotes::install_github(&quot;clauswilke\/ggisoband&quot;)\nlibrary(ggisoband)\nlibrary(terra)\nlibrary(tidyterra)\nlibrary(glue)\nlibrary(janitor)\nlibrary(memoise)\n\n\n# Parameters --------------------------------------------------------------\n\n# Opentopo API\n# https:\/\/portal.opentopography.org\/myopentopo\n# elevatr::set_opentopo_key(&quot;xxx&quot;)\n\n\n# Data --------------------------------------------------------------------\n\n# Locations (decimal degrees, WGS84)\nlocations &lt;- tribble(\n  ~name,                     ~lon,       ~lat, ~alti, ~radius, ~iso_interval, ~source,\n  &quot;Aconcagua&quot;,         -70.017650, -32.658564,  6961,   10000,           150, &quot;aws&quot;,\n  &quot;\u042d\u043b\u044c\u0431\u0440\u0443\u0441&quot;,            42.437876,  43.352404,  5643,   10000,           150, &quot;aws&quot;,\n  &quot;Macizo Vinson&quot;,     -85.617388, -78.525399,  4892,   20000,           150, &quot;aws&quot;,\n  &quot;Puncak Jaya&quot;,       137.158613,  -4.078606,  4884,   10000,           150, &quot;aws&quot;,\n  &quot;Mount Kosciuszko&quot;,  148.263510, -36.455832,  2228,   15000,           100, &quot;aws&quot;,\n  &quot;K2&quot;,                 76.513739,  35.881869,  8611,    5000,           180, &quot;alos&quot;)\n \n\n# Area of interest --------------------------------------------------------\n\nbuild_target &lt;- function(lon, lat, radius) {\n    tibble(x = lon, \n           y = lat) |&gt; \n    st_as_sf(coords = c(&quot;x&quot;, &quot;y&quot;), \n             crs = &quot;EPSG:4326&quot;) |&gt; \n    st_buffer(radius) |&gt;\n    st_convex_hull() \n}\n\n#' Get elevation data\n#' \n#' This function is memoised, so that we won't request the data if it has \n#' already been downloaded\n#'\n#' @param target (sf) : area of interest\n#' @param zoom (int) : zoom level ; z = 10 is good, z = 14 max resolution\n#' @param source (char) : \n#'\n#' @return (df) : with x, y, z, coords\nget_elevation &lt;- memoise(\n  function(target, zoom = 10, source = &quot;aws&quot;) {\n    get_elev_raster(target, z = zoom, src = source) |&gt; \n      rast() |&gt; \n      fortify() |&gt; \n      rename(z = 3)\n})\n\n#' convert decimal degrees to DMS\n#'\n#' @param coord (vector of num, length=2)\n#'\n#' @return (char) : DD\u00b0MM'SS.SSH\n#'\n#' @examples dec_to_sex(c(1.23, 2.55))\ndec_to_sex &lt;- function(coord) {\n  conv_dec_to_sex &lt;- function(deg_dec, type) {\n    deg &lt;- abs(trunc(deg_dec))\n    min_dec &lt;- (abs(deg_dec) - deg) * 60\n    min &lt;- trunc(min_dec)\n    sec &lt;- round(((min_dec - min) * 60), 2)\n    h &lt;- case_when(deg_dec &lt; 0  &#038; type == &quot;lon&quot; ~ &quot;W&quot;,\n                   deg_dec &gt;= 0 & type == &quot;lon&quot; ~ &quot;E&quot;,\n                   deg_dec &lt; 0  &#038; type == &quot;lat&quot; ~ &quot;S&quot;,\n                   deg_dec &gt;= 0 & type == &quot;lat&quot; ~ &quot;N&quot;)\n    sprintf(&quot;%02i\u00b0%02i\u2032%.2f\u2033%s&quot;, deg, min, sec, h)\n  }\n  glue(&quot;{conv_dec_to_sex(coord[2], type = 'lat')} {conv_dec_to_sex(coord[1], type = 'lon')}&quot;)\n} \n\n\n# Final plot --------------------------------------------------------------\n\nplot_isolines &lt;- function(name, lon, lat, alti, radius, iso_interval, \n                          zoom = 10, source = &quot;aws&quot;) {\n  target &lt;- build_target(lon, lat, radius)\n  target_bbox &lt;- st_bbox(target)\n  \n  p &lt;- get_elevation(target, zoom, source) |&gt; \n    ggplot() +\n    geom_sf(data = target, fill = NA, color = NA) +\n    geom_isobands(aes(x, y, z = z, fill = z),\n                  binwidth = iso_interval, fill = NA) +\n    coord_sf(xlim = target_bbox[c(1, 3)],\n             ylim = target_bbox[c(2, 4)]) +\n    labs(title = glue(&quot;{name} \u2014 {alti} m&quot;),\n         subtitle = dec_to_sex(c(lon, lat)),\n         caption = &quot;https:\/\/shirt.iresmi.net\/&quot;) +\n    theme_void() +\n    theme(plot.title = element_text(family = &quot;Arial narrow&quot;, \n                                    face = &quot;bold&quot;, size = 22),\n          plot.title.position = &quot;plot&quot;,\n          plot.subtitle = element_text(family = &quot;Arial narrow&quot;,\n                                       face = &quot;bold&quot;, size = 12),\n          plot.caption = element_text(family = &quot;Arial&quot;, size = 9))\n  \n    ggsave(glue(&quot;{janitor::make_clean_names(name)}_{alti}_m.png&quot;), p,\n           dpi = 300, width = 20, height = 20, units = &quot;cm&quot;)\n}\n\nlocations |&gt; \n  pwalk(plot_isolines, .progress = TRUE)<\/pre>\n<\/div>\n\n\n<!-- -->\n\n\n \n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;\">\r\n<div style=\"text-align: center;\">To <strong>leave a comment<\/strong> for the author, please follow the link and comment on their blog: <strong><a href=\"https:\/\/r.iresmi.net\/posts\/2023\/mountains\/index.html\"> r.iresmi.net<\/a><\/strong>.<\/div>\r\n<hr \/>\r\n<a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers.com<\/a> offers <strong><a href=\"https:\/\/feedburner.google.com\/fb\/a\/mailverify?uri=RBloggers\" rel=\"nofollow\">daily e-mail updates<\/a><\/strong> about <a title=\"The R Project for Statistical Computing\" href=\"https:\/\/www.r-project.org\/\" rel=\"nofollow\">R<\/a> news and tutorials about <a title=\"R tutorials\" href=\"https:\/\/www.r-bloggers.com\/how-to-learn-r-2\/\" rel=\"nofollow\">learning R<\/a> and many other topics. <a title=\"Data science jobs\" href=\"https:\/\/www.r-users.com\/\" rel=\"nofollow\">Click here if you're looking to post or find an R\/data-science job<\/a>.\r\n\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>","protected":false},"excerpt":{"rendered":"<div style = \"width:60%; display: inline-block; float:left; \">\n<p>Aconcagua &#8211; 6991 m<\/p>\n<p>If you like mountains, R and T-shirts, I got you covered. Here we use {elevatr} to get a DEM around some well known summits and make a map consisting only of isohypses. It produces (sometimes) very nice visualizations.<br \/>\nAnd&#8230;<\/p><\/div>\n<div style = \"width: 40%; display: inline-block; float:right;\"><\/div>\n<div style=\"clear: both;\"><\/div>\n","protected":false},"author":80,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"aioseo_notices":[],"jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/346963"}],"collection":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/users\/80"}],"replies":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/comments?post=346963"}],"version-history":[{"count":23,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/346963\/revisions"}],"predecessor-version":[{"id":377666,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/346963\/revisions\/377666"}],"wp:attachment":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/media?parent=346963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/categories?post=346963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/tags?post=346963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}