class: center, middle, inverse, title-slide # Análisis de redes espaciales con
sfnetworks
## R-Ladies Guayaquil ###
Lorena Abad
, Lucas van der Meer, Andrea Gilardi, Robin Lovelace ### 2022-06-08 --- class: center, middle, hide-logo <STYLE type='text/css' scoped> PRE.fansi SPAN {padding-top: .25em; padding-bottom: .25em}; </STYLE> ### Sobre mí... -- Investigadora en la [Universidad de Salzburg](https://www.plus.ac.at/risk-hazard-climate/) 🛰 -- Amo codificar en R, aunque ahora trabajo también en Python 🐍 -- Si no estoy programando, me encuentran en la montaña en bici o de senderismo ⛰ ![:scale 30%](https://dgtzuqphqg23d.cloudfront.net/JtVzfcASWgycVOcxPF_9w22mglBk42rCOKk_LpY4IKg-2048x1536.jpg) --- class: center, middle, hide-logo <iframe src="https://wall.sli.do/event/rg8ExyZuhZ4CsVYsdKGZAK?section=a8210543-8317-4e54-97fb-23bc7aca514b" height="100%" width="100%" frameBorder="0" style="min-height: 560px;" title="Slido"></iframe> --- class: center, middle, hide-logo <iframe src="https://infographics.sli.do/?i=cHViLDg1YzkxMmIyLTQ4YTctNGUzMS1iOWVhLThmNjM2NzllMjJlMSxjYzkxNThiNC00M2ZmLTQ3ZmMtOWJjZC01NDM4MDIzNzFmZDM=" height="90%" width="150%" title="Slido"></iframe> --- class: center, middle, hide-logo ## ¡Comenzamos! Link a la presentación: https://sfnetworks.github.io/rladies-gye/slides Link al repositorio: https://github.com/sfnetworks/rladies-gye [![Binder](http://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/sfnetworks/rladies-gye/main?urlpath=rstudio) --- class: center, top, hide-logo ## <span style="color:#ffa500">Redes espaciales</span> <span style="color:#b3b3b3">*tidy* en R</span> .left[ .pull-left-70[ ![](figs/river_network.png) ] ] .pull-right-30[ Nodos y aristas se encuentran en el espacio geográfico Nodos se asocian a geometrías de puntos: **POINT** Aristas se asocian a geometías de líneas: **LINESTRING** La topología de la red no contiene *toda* la información de una red espacial La topología **_y_** la información espacial son esenciales para su análisis ] --- class: center, top, hide-logo ## <span style="color:#b3b3b3">Redes espaciales</span> <span style="color:#ffa500">*tidy* en R</span> .left[ .pull-left-70[ ```r library(tidyverse) library(sfnetworks) roxel |> as_tibble() |> select(name, type) ``` ``` # A tibble: 851 x 2 name type <chr> <fct> 1 Havixbecker Strasse residential 2 Pienersallee secondary 3 Schulte-Bernd-Strasse residential 4 <NA> path 5 Welsingheide residential 6 <NA> footway 7 <NA> footway 8 <NA> path 9 <NA> track 10 <NA> track # ... with 841 more rows ``` ] ] .pull-right-30[ ![:scale 15%](https://raw.githubusercontent.com/tidyverse/tidyverse/master/man/figures/logo.png) **Tidyverse** Colección de paquetes de R para ciencia de datos Comparten una filosofía de estructura Aplican estructura de datos *tidy* ] --- class: center, top, hide-logo ## <span style="color:#b3b3b3">Redes </span><span style="color:#ffa500">espaciales </span><span style="color:#b3b3b3">*tidy*</span><span style="color:#ffa500"> en R</span> .left[ .pull-left-70[ ```r library(sf) roxel ``` <PRE class="fansi fansi-output"><CODE>Simple feature collection with 851 features and 2 fields Geometry type: LINESTRING Dimension: XY Bounding box: xmin: 7.522594 ymin: 51.94151 xmax: 7.546705 ymax: 51.9612 Geodetic CRS: WGS 84 <span style='color: #555555;'># A tibble: 851 x 3</span> name type geometry <span style='color: #555555;'>*</span> <span style='color: #555555; font-style: italic;'><chr></span> <span style='color: #555555; font-style: italic;'><fct></span> <span style='color: #555555; font-style: italic;'><LINESTRING [°]></span> <span style='color: #555555;'> 1</span> Havixbecker Strasse residential (7.533722 51.95556, 7.533461 51.~ <span style='color: #555555;'> 2</span> Pienersallee secondary (7.532442 51.95422, 7.53236 51.9~ <span style='color: #555555;'> 3</span> Schulte-Bernd-Strasse residential (7.532709 51.95209, 7.532823 51.~ <span style='color: #555555;'> 4</span> <span style='color: #BB0000;'>NA</span> path (7.540063 51.94468, 7.539696 51.~ <span style='color: #555555;'> 5</span> Welsingheide residential (7.537673 51.9475, 7.537614 51.9~ <span style='color: #555555;'> 6</span> <span style='color: #BB0000;'>NA</span> footway (7.543791 51.94733, 7.54369 51.9~ <span style='color: #555555;'> 7</span> <span style='color: #BB0000;'>NA</span> footway (7.54012 51.94478, 7.539931 51.9~ <span style='color: #555555;'> 8</span> <span style='color: #BB0000;'>NA</span> path (7.53822 51.94546, 7.538131 51.9~ <span style='color: #555555;'> 9</span> <span style='color: #BB0000;'>NA</span> track (7.540063 51.94468, 7.540338 51.~ <span style='color: #555555;'>10</span> <span style='color: #BB0000;'>NA</span> track (7.5424 51.94599, 7.54205 51.946~ <span style='color: #555555;'># ... with 841 more rows</span> </CODE></PRE> ] ] .pull-right-30[ ![:scale 15%](https://user-images.githubusercontent.com/520851/34887433-ce1d130e-f7c6-11e7-83fc-d60ad4fae6bd.gif) **El paquete sf** Paquete de R para ciencia de datos espaciales Almacenamiento estandarizado de elementos geográficos (puntos, líneas, polígonos) Operaciones geométricas (intersecciones, uniones, ..) Compatible con tidyverse Nuevos *verbos* específicos para datos espaciales ] --- class: center, top, hide-logo ## <span style="color:#ffa500">Redes </span><span style="color:#b3b3b3">espaciales </span><span style="color:#ffa500"> *tidy* en R</span> .pull-left-70[ .left[ ```r library(tidygraph) nodes = tibble(name = letters[1:4]) edges = tibble(from = c(1,2,3,4), to = c(2,3,4,1)) tbl_graph(nodes, edges) ``` ``` # A tbl_graph: 4 nodes and 4 edges # # A directed simple graph with 1 component # # Node Data: 4 x 1 (active) name <chr> 1 a 2 b 3 c 4 d # # Edge Data: 4 x 2 from to <int> <int> 1 1 2 2 2 3 3 3 4 # ... with 1 more row ``` ] ] .pull-right-30[ ![:scale 15%](https://raw.githubusercontent.com/thomasp85/tidygraph/master/man/figures/logo.png) **El paquete tidygraph** Interfaz *tidy* al paquete `igraph` para análisis de redes Modela la red como una colección de dos tibbles Soporta algoritmos de redes (centralidad, clustering, ruteo, ..) Compatible con tidyverse Nuevos *verbos* específicos para estructuras de redes ] --- class: center, top, hide-logo ## <span style="color:#ffa500">¿Redes espaciales *tidy* en R?</span> <center><blockquote class="twitter-tweet"><p lang="en" dir="ltr">One of the biggest reasons we still have ArcGIS licenses is for Network Analysis (drive times, service areas etc). Does anyone have <a href="https://twitter.com/hashtag/foss4g?src=hash&ref_src=twsrc%5Etfw">#foss4g</a> tools for this they like? Last time we tried pgRouting (yrs ago) it didn't feel fully formed yet, <a href="https://twitter.com/hashtag/gischat?src=hash&ref_src=twsrc%5Etfw">#gischat</a></p>— Zev Ross (@zevross) <a href="https://twitter.com/zevross/status/1089908839816794118?ref_src=twsrc%5Etfw">January 28, 2019</a></blockquote></center> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> --- class: center, top, hide-logo ## <span style="color:#ffa500">¡Redes espaciales *tidy* en R!</span> .pull-left[ ![](https://user-images.githubusercontent.com/520851/34887433-ce1d130e-f7c6-11e7-83fc-d60ad4fae6bd.gif) ] .pull-right[ ![:scale 38%](https://raw.githubusercontent.com/thomasp85/tidygraph/master/man/figures/logo.png) ] .center[ ![:scale 20%](https://raw.githubusercontent.com/luukvdmeer/sfnetworks/master/man/figures/logo.png) ] --- class: center, top ## <span style="color:#ffa500">¡Redes espaciales *tidy* en R!</span> `tidygraph`: > "... una cercana aproximación de datos *ordenados* para datos relacionales son dos *tidy data frames*, uno que describa los *nodos* y otro describiendo las *aristas*." -- <br> <br> `sfnetworks`: > “Una cercana aproximación de datos *ordenados* para datos relacionales .orange[*geoespaciales*] son dos .orange[*objetos sf*], uno que describa los *nodos* y otro describiendo las *aristas*” --- class: top ## Objeto `sfnetwork` .pull-left-40[ ```r roxel |> st_geometry() |> plot() ``` ] .pull-right-60[ <img src="slides_files/figure-html/unnamed-chunk-3-1.png" width="864" /> ] --- class: top ## Objeto `sfnetwork` .pull-left-40[ ```r roxel |> as_sfnetwork() |> plot() ``` ] .pull-right-60[ <img src="slides_files/figure-html/unnamed-chunk-5-1.png" width="864" /> ] --- class: top ## Objeto `sfnetwork` .pull-left-40[ ```r roxel ``` ] .pull-right-60[ ``` Simple feature collection with 851 features and 2 fields Geometry type: LINESTRING Dimension: XY Bounding box: xmin: 7.522594 ymin: 51.94151 xmax: 7.546705 ymax: 51.9612 Geodetic CRS: WGS 84 # A tibble: 851 x 3 name type geometry * <chr> <fct> <LINESTRING [°]> 1 Havixbecker Strasse residential (7.533722 51.95556, 7.533461 51.~ 2 Pienersallee secondary (7.532442 51.95422, 7.53236 51.9~ 3 Schulte-Bernd-Strasse residential (7.532709 51.95209, 7.532823 51.~ 4 <NA> path (7.540063 51.94468, 7.539696 51.~ 5 Welsingheide residential (7.537673 51.9475, 7.537614 51.9~ 6 <NA> footway (7.543791 51.94733, 7.54369 51.9~ 7 <NA> footway (7.54012 51.94478, 7.539931 51.9~ 8 <NA> path (7.53822 51.94546, 7.538131 51.9~ 9 <NA> track (7.540063 51.94468, 7.540338 51.~ 10 <NA> track (7.5424 51.94599, 7.54205 51.946~ # ... with 841 more rows ``` ] --- class: top ## Objeto `sfnetwork` .pull-left-40[ ```r roxel |> as_sfnetwork() ``` ] .pull-right-60[ ``` # A sfnetwork with 701 nodes and 851 edges # # CRS: EPSG:4326 # # A directed multigraph with 14 components with spatially explicit edges # # Node Data: 701 x 1 (active) # Geometry type: POINT # Dimension: XY # Bounding box: xmin: 7.522622 ymin: 51.94151 xmax: 7.546705 ymax: # 51.9612 geometry <POINT [°]> 1 (7.533722 51.95556) 2 (7.533461 51.95576) 3 (7.532442 51.95422) 4 (7.53209 51.95328) 5 (7.532709 51.95209) 6 (7.532869 51.95257) # ... with 695 more rows # # Edge Data: 851 x 5 # Geometry type: LINESTRING # Dimension: XY # Bounding box: xmin: 7.522594 ymin: 51.94151 xmax: 7.546705 ymax: # 51.9612 from to name type geometry <int> <int> <chr> <fct> <LINESTRING [°]> 1 1 2 Havixbecker Strasse residential (7.533722 51.95556, 7.53~ 2 3 4 Pienersallee secondary (7.532442 51.95422, 7.53~ 3 5 6 Schulte-Bernd-Strasse residential (7.532709 51.95209, 7.53~ # ... with 848 more rows ``` ] --- class: top ## Objeto `sfnetwork` .pull-left-40[ ```r roxel |> as_sfnetwork() |> activate(nodes) |> st_as_sf() ``` ] .pull-right-60[ ``` Simple feature collection with 701 features and 0 fields Geometry type: POINT Dimension: XY Bounding box: xmin: 7.522622 ymin: 51.94151 xmax: 7.546705 ymax: 51.9612 Geodetic CRS: WGS 84 # A tibble: 701 x 1 geometry <POINT [°]> 1 (7.533722 51.95556) 2 (7.533461 51.95576) 3 (7.532442 51.95422) 4 (7.53209 51.95328) 5 (7.532709 51.95209) 6 (7.532869 51.95257) 7 (7.540063 51.94468) 8 (7.53822 51.94546) 9 (7.537673 51.9475) 10 (7.537614 51.94562) # ... with 691 more rows ``` ] --- class: top ## Objeto `sfnetwork` .pull-left-40[ ```r roxel |> as_sfnetwork() |> activate(edges) |> st_geometry() ``` ] .pull-right-60[ ``` Geometry set for 851 features Geometry type: LINESTRING Dimension: XY Bounding box: xmin: 7.522594 ymin: 51.94151 xmax: 7.546705 ymax: 51.9612 Geodetic CRS: WGS 84 First 5 geometries: ``` ``` LINESTRING (7.533722 51.95556, 7.533461 51.95576) ``` ``` LINESTRING (7.532442 51.95422, 7.53236 51.95377... ``` ``` LINESTRING (7.532709 51.95209, 7.532823 51.9523... ``` ``` LINESTRING (7.540063 51.94468, 7.539696 51.9447... ``` ``` LINESTRING (7.537673 51.9475, 7.537614 51.94562) ``` ] --- class: center, middle ## Live demo! Link al repositorio: https://github.com/sfnetworks/rladies-gye [![Binder](http://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/sfnetworks/rladies-gye/main?urlpath=rstudio) --- class: center, middle ### ¿Qué podemos hacer? #### Pre-procesamiento y limpieza de redes --- class: middle ### Pre-procesamiento y limpieza de redes #### Red inicial <img src="slides_files/figure-html/clean2-1.png" width="864" /> --- class: middle ### Pre-procesamiento y limpieza de redes #### Simplificar aristas <img src="slides_files/figure-html/clean3-1.png" width="864" /> --- class: middle ### Pre-procesamiento y limpieza de redes #### Subdividir aristas <img src="slides_files/figure-html/clean4-1.png" width="864" /> --- class: middle ### Pre-procesamiento y limpieza de redes #### Remover pseudo-nodos <img src="slides_files/figure-html/clean5-1.png" width="864" /> --- class: middle ### Pre-procesamiento y limpieza de redes #### Simplificar intersecciones <img src="slides_files/figure-html/clean6-1.png" width="864" /> --- class: center, middle ### ¿Qué podemos hacer? #### Snapping --- class: middle count: false ### Snapping #### Mover puntos al nodo más cercano <img src="slides_files/figure-html/snap_user_01_output-1.png" width="864" /> --- count: false ### Snapping #### Mover puntos al nodo más cercano <img src="slides_files/figure-html/snap_user_02_output-1.png" width="864" /> --- count: false ### Snapping #### Mover puntos al nodo más cercano <img src="slides_files/figure-html/snap_user_03_output-1.png" width="864" /> --- count: false ### Snapping #### Mover puntos al nodo más cercano <img src="slides_files/figure-html/snap_user_04_output-1.png" width="864" /> <style> .panel1-snap-user { color: black; width: 99%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-snap-user { color: black; width: NA%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-snap-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: middle count: false ### Snapping #### Integrar puntos a la red <img src="slides_files/figure-html/blend_user_01_output-1.png" width="864" /> --- count: false ### Snapping #### Integrar puntos a la red <img src="slides_files/figure-html/blend_user_02_output-1.png" width="864" /> --- count: false ### Snapping #### Integrar puntos a la red <img src="slides_files/figure-html/blend_user_03_output-1.png" width="864" /> <style> .panel1-blend-user { color: black; width: 99%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-blend-user { color: black; width: NA%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-blend-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: center, middle ### ¿Qué podemos hacer? #### Ruteo --- class: top count: false ### Ruteo #### Calcular el camino más corto <img src="slides_files/figure-html/plotpath_user_01_output-1.png" width="864" /> --- count: false ### Ruteo #### Calcular el camino más corto <img src="slides_files/figure-html/plotpath_user_02_output-1.png" width="864" /> --- count: false ### Ruteo #### Calcular el camino más corto <img src="slides_files/figure-html/plotpath_user_03_output-1.png" width="864" /> --- count: false ### Ruteo #### Calcular el camino más corto <img src="slides_files/figure-html/plotpath_user_04_output-1.png" width="864" /> --- count: false ### Ruteo #### Calcular el camino más corto <img src="slides_files/figure-html/plotpath_user_05_output-1.png" width="864" /> --- count: false ### Ruteo #### Calcular el camino más corto <img src="slides_files/figure-html/plotpath_user_06_output-1.png" width="864" /> <style> .panel1-plotpath-user { color: black; width: 99%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-plotpath-user { color: black; width: NA%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-plotpath-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: top ### Ruteo #### Obtener una matriz origen/destino ```r st_network_cost( net, from = c(p1, p2, p3), to = c(p1, p2, p3) ) ``` ``` [,1] [,2] [,3] [1,] 0.000 1614.252 2094.555 [2,] 1614.252 0.000 1315.965 [3,] 2094.555 1315.965 0.000 ``` --- class: center, middle ## Live demo! Link al repositorio: https://github.com/sfnetworks/rladies-gye [![Binder](http://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/sfnetworks/rladies-gye/main?urlpath=rstudio) --- class: center, middle ### ¿Qué más? #### Mucho más...! --- class: center, middle ## Instalación: Desde CRAN: ```r install.packages("sfnetworks") ``` Versión en desarrollo en GitHub: ```r remotes::install_github("luukvdmeer/sfnetworks") ``` --- class: center, middle ### ¿Quieren más? -- Dense una vuelta por las [viñetas del paquete](https://luukvdmeer.github.io/sfnetworks/index.html) -- ### ¿Tienen sus propios ejemplos o ideas? -- Únanse a la [Discusión en GitHub](https://github.com/luukvdmeer/sfnetworks/discussions) -- ### ¿Encontraron un bug? -- Hágannos saber en un [issue aquí](https://github.com/luukvdmeer/sfnetworks/issues)! --- class: center, middle ### Gracias por su atención! .note[ .pull-left-70[ .pull-left[ Projecto financiado por:<br>![:scale 50%](https://raw.githubusercontent.com/RConsortium/artwork/main/r_consortium/R_Consortium-logo-horizontal-color.png) ] .pull-right[ Slides powered by [xaringan](https://github.com/yihui/xaringan), [xaringanthemer](https://github.com/gadenbuie/xaringanthemer) and [flipbookr](https://github.com/EvaMaeRey/flipbookr)<br><br> ] ] .pull-right-30[ ![:scale 50%](figs/rladies.jpg) ] ]