{"id":13857698,"url":"https://github.com/statnmap/maps2ray","last_synced_at":"2025-04-19T17:10:17.801Z","repository":{"id":117563057,"uuid":"213312816","full_name":"statnmap/maps2ray","owner":"statnmap","description":"Transform maps (vector with {sf}, {raster}) to be added in a {rayshader} scene","archived":false,"fork":false,"pushed_at":"2019-12-06T13:21:25.000Z","size":5039,"stargazers_count":9,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-29T10:43:23.673Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/statnmap.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2019-10-07T06:41:59.000Z","updated_at":"2020-03-05T16:30:50.000Z","dependencies_parsed_at":"2024-02-09T01:57:18.505Z","dependency_job_id":"f93e298e-b05f-4042-8886-6bf9852439dc","html_url":"https://github.com/statnmap/maps2ray","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statnmap%2Fmaps2ray","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statnmap%2Fmaps2ray/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statnmap%2Fmaps2ray/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statnmap%2Fmaps2ray/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/statnmap","download_url":"https://codeload.github.com/statnmap/maps2ray/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249746041,"owners_count":21319581,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-05T03:01:44.477Z","updated_at":"2025-04-19T17:10:17.785Z","avatar_url":"https://github.com/statnmap.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# maps2ray\n\n\u003c!-- badges: start --\u003e\n\u003c!-- badges: end --\u003e\n\nThe goal of maps2ray is to transform spatial objects from {sf} and {raster} to\n    be used in {rayshader} 3D outputs.\n\n## Installation\n\nYou can install  the development version from [GitHub](https://github.com/) with:\n\n``` r\n# install.packages(\"devtools\")\ndevtools::install_github(\"statnmap/maps2ray\")\n```\n## Example\n\nThis procedure is explained in a blog post at: https://statnmap.com/2019-10-06-follow-moving-particle-trajectory-on-raster-with-rayshader/\n\n```{r, message=FALSE}\nlibrary(maps2ray)\nlibrary(rayshader)\nlibrary(sf)\nlibrary(raster)\nlibrary(rasterVis)\nlibrary(ggplot2)\nlibrary(dplyr)\nlibrary(rgl)\n```\n\n- Read internal raster file\n```{r}\nf \u003c- system.file(\"extdata/altitude_l93.tif\", package = \"maps2ray\")\naltitude \u003c- raster(f)\n```\n\n### Create rayshaded raster basis\n\nCreate the {rayshader} terrain object to be used in both 2D and 3D outputs  \n\n```{r, message=FALSE}\n# Transform raster as matrix\ndatamat \u003c- t(as.matrix(altitude))\n# Rayshade raster\nzscale \u003c- 5\nambmat \u003c- ambient_shade(datamat, zscale = zscale)\nraymat \u003c- ray_shade(datamat, zscale = zscale, lambert = TRUE)\n\n# Create ray_image\nray_image \u003c- datamat %\u003e%\n  sphere_shade(texture = \"imhof4\") %\u003e%\n  add_shadow(raymat, max_darken = 0.5) %\u003e%\n  add_shadow(ambmat, max_darken = 0.5) \n\n# Plot 2D\nplot_map(ray_image)\n```\n\n### Example with points trajectory\n\n- Create a trajectory as {sf} object\n```{r}\nline_mat \u003c- matrix(c(280000, 370000, 6700000, 6700000), ncol = 2)\nline_traject \u003c- st_linestring(x = line_mat)\npoint_traject \u003c- st_sample(line_traject, size = 50, type = \"regular\") %\u003e% \n  st_set_crs(2154) %\u003e% \n  st_sf()\n\n# Plot\ngplot(altitude) +\n  geom_tile(aes(fill = value)) +\n  geom_sf(data = point_traject, colour = \"white\", inherit.aes = FALSE) +\n  scale_fill_viridis_c()\n```\n\n- Transform {sf} trajectory to {rayshader} coordinates reference system, relative to the `altitude` raster\n\n```{r, message=FALSE, warning=FALSE}\npoints_ray \u003c- sf_proj_as_ray(r = altitude, sf = point_traject,\n                             z_pos = maxValue(altitude) + 10,\n                             zscale = zscale)\n\n```\n\n- Plot rayshader image in 2D and add points with new coordinates reference system of {rayshader}: `sf` list in the output of `sf_proj_as_ray()`.  \n\n```{r ray2dtotal, warning=FALSE}\n# plot rayshader in 2D\nplot_map(ray_image)\nplot(points_ray$sf, col = \"blue\", pch = 20, add = TRUE, reset = FALSE)\n```\n\n- Plot {rayshader} image in 3D and overlay points with the new coordinates reference system of {rayshader}: `coords` list in the output of `sf_proj_as_ray()`.  \n\n```{r, eval=FALSE}\n# Create 3D scene\nray_image %\u003e% \n  plot_3d(\n    datamat,\n    zscale = zscale, windowsize = c(500, 500),\n    soliddepth = -max(datamat, na.rm = TRUE)/zscale,\n    water = TRUE, wateralpha = 0,\n    theta = 15, phi = 40,\n    zoom = 0.6, \n    fov = 60)\n\n# Add points over rayshader scene\nspheres3d(\n  bind_rows(points_ray$coords$coords),\n  col = \"blue\", add = TRUE, radius = 10,\n  alpha = 1)\n\n# rgl::snapshot3d(file.path(\"img\", \"ray3dtotal.png\"))\n# rgl::rgl.close()\n```\n```{r, echo=FALSE}\nknitr::include_graphics(\"img/ray3dtotal.png\")\n```\n\n### Example with line\n\n```{r, eval=FALSE}\nline_mat \u003c- matrix(c(280000, 370000, 6680000, 6720000), ncol = 2)\nline_mat2 \u003c- matrix(c(280000, 370000, 6680000, 6680000), ncol = 2)\nline_traject \u003c- st_sf(\n  geometry = st_sfc(list(\n    st_multilinestring(list(st_linestring(x = line_mat))),\n    st_multilinestring(list(st_linestring(x = line_mat2)))\n    )), crs = 2154)\n\n# With no tranformation\nplot(altitude)\nplot(line_traject, col = \"blue\", pch = 20, add = TRUE)\n\n# Transform for rayshader\nzscale \u003c- 5\nlines_ray \u003c- sf_proj_as_ray(r = altitude, sf = line_traject,\n                             z_pos = maxValue(altitude) + 10,\n                             zscale = zscale)\n\n# Plot\n# plot rayshader in 2D\nplot_map(ray_image)\nplot(lines_ray$sf, col = \"blue\", pch = 20, add = TRUE, reset = FALSE)\n```\n\nIn 3D\n```{r, eval=FALSE}\nray_image %\u003e% \n  plot_3d(\n    datamat,\n    zscale = zscale, windowsize = c(500, 500),\n    soliddepth = -max(datamat, na.rm = TRUE)/zscale,\n    water = TRUE, wateralpha = 0,\n    theta = 15, phi = 40,\n    zoom = 0.6, \n    fov = 60)\n\n# Add points over rayshader scene\nlines3d(\n  bind_rows(lines_ray$coords$coords),\n  col = \"blue\", add = TRUE, radius = 10,\n  alpha = 1)\n\n# rgl::snapshot3d(file.path(\"img\", \"ray3d_lines.png\"))\n# rgl::rgl.close()\n```\n```{r, echo=FALSE}\nknitr::include_graphics(\"img/ray3d_lines.png\")\n```\n\n### Example with polygons\n\n```{r}\npol_mat \u003c- matrix(\n  c(280000, 370000, 370000, 280000,\n    6680000, 6720000, 6680000, 6680000), ncol = 2)\npol_mat2 \u003c- matrix(\n  c(280000, 370000, 280000, 280000,\n    6680000, 6720000, 6720000, 6680000), ncol = 2)\n\npol_traject \u003c- st_sf(\n  geometry = st_sfc(list(\n    st_multipolygon(list(st_polygon(x = list(pol_mat)))),\n    st_multipolygon(list(st_polygon(x = list(pol_mat2))))\n    )), crs = 2154)\n\n# With no tranformation\nplot(altitude)\nplot(pol_traject, col = c(\"red\", \"blue\"), pch = 20, add = TRUE)\n\n# Transform for rayshader\nzscale \u003c- 5\npols_ray \u003c- sf_proj_as_ray(r = altitude, sf = pol_traject,\n                             z_pos = maxValue(altitude) + 10,\n                             zscale = zscale)\n\n# Plot\n# plot rayshader in 2D\nplot_map(ray_image)\nplot(pols_ray$sf, col = c(\"red\", \"blue\"), pch = 20, add = TRUE, reset = FALSE)\n```\n\nIn 3D\n```{r, eval=FALSE}\nray_image %\u003e% \n  plot_3d(\n    datamat,\n    zscale = zscale, windowsize = c(500, 500),\n    soliddepth = -max(datamat, na.rm = TRUE)/zscale,\n    water = TRUE, wateralpha = 0,\n    theta = 15, phi = 40,\n    zoom = 0.6, \n    fov = 60)\n\n# Add points over rayshader scene\nlines3d(\n  bind_rows(pols_ray$coords$coords),\n  col = \"blue\", add = TRUE, radius = 10,\n  alpha = 1)\n\n# rgl::snapshot3d(file.path(\"img\", \"ray3d_polygons.png\"))\n# rgl::rgl.close()\n```\n```{r, echo=FALSE}\nknitr::include_graphics(\"img/ray3d_polygons.png\")\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstatnmap%2Fmaps2ray","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstatnmap%2Fmaps2ray","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstatnmap%2Fmaps2ray/lists"}