class: title-slide, middle ## .font200[<svg viewBox="0 0 581 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M581 226.6C581 119.1 450.9 32 290.5 32S0 119.1 0 226.6C0 322.4 103.3 402 239.4 418.1V480h99.1v-61.5c24.3-2.7 47.6-7.4 69.4-13.9L448 480h112l-67.4-113.7c54.5-35.4 88.4-84.9 88.4-139.7zm-466.8 14.5c0-73.5 98.9-133 220.8-133s211.9 40.7 211.9 133c0 50.1-26.5 85-70.3 106.4-2.4-1.6-4.7-2.9-6.4-3.7-10.2-5.2-27.8-10.5-27.8-10.5s86.6-6.4 86.6-92.7-90.6-87.9-90.6-87.9h-199V361c-74.1-21.5-125.2-67.1-125.2-119.9zm225.1 38.3v-55.6c57.8 0 87.8-6.8 87.8 27.3 0 36.5-38.2 28.3-87.8 28.3zm-.9 72.5H365c10.8 0 18.9 11.7 24 19.2-16.1 1.9-33 2.8-50.6 2.9v-22.1z"></path></svg>asters] <br> .instructors[ .font140[Manipulate and visualize spatial data with <svg viewBox="0 0 581 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M581 226.6C581 119.1 450.9 32 290.5 32S0 119.1 0 226.6C0 322.4 103.3 402 239.4 418.1V480h99.1v-61.5c24.3-2.7 47.6-7.4 69.4-13.9L448 480h112l-67.4-113.7c54.5-35.4 88.4-84.9 88.4-139.7zm-466.8 14.5c0-73.5 98.9-133 220.8-133s211.9 40.7 211.9 133c0 50.1-26.5 85-70.3 106.4-2.4-1.6-4.7-2.9-6.4-3.7-10.2-5.2-27.8-10.5-27.8-10.5s86.6-6.4 86.6-92.7-90.6-87.9-90.6-87.9h-199V361c-74.1-21.5-125.2-67.1-125.2-119.9zm225.1 38.3v-55.6c57.8 0 87.8-6.8 87.8 27.3 0 36.5-38.2 28.3-87.8 28.3zm-.9 72.5H365c10.8 0 18.9 11.7 24 19.2-16.1 1.9-33 2.8-50.6 2.9v-22.1z"></path></svg>] <br><br><br> .authors140[David Beauchesne & Kevin Cazelles] <br><br> March 24, 2021 <br> .font200[ [<svg viewBox="0 0 496 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg>](https://github.com/inSilecoInc/wksh_raster_CWS) ] ] <br> <img src="img/logoW.png" width="140px"></img> <!-- Modify accordingly --> [![deploy workshop](https://github.com/inSilecoInc/wksh_raster_CWS/actions/workflows/deploy.yml/badge.svg)](https://github.com/inSilecoInc/wksh_raster_CWS/actions/workflows/deploy.yml) .instructors[Content under [<svg viewBox="0 0 496 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M245.83 214.87l-33.22 17.28c-9.43-19.58-25.24-19.93-27.46-19.93-22.13 0-33.22 14.61-33.22 43.84 0 23.57 9.21 43.84 33.22 43.84 14.47 0 24.65-7.09 30.57-21.26l30.55 15.5c-6.17 11.51-25.69 38.98-65.1 38.98-22.6 0-73.96-10.32-73.96-77.05 0-58.69 43-77.06 72.63-77.06 30.72-.01 52.7 11.95 65.99 35.86zm143.05 0l-32.78 17.28c-9.5-19.77-25.72-19.93-27.9-19.93-22.14 0-33.22 14.61-33.22 43.84 0 23.55 9.23 43.84 33.22 43.84 14.45 0 24.65-7.09 30.54-21.26l31 15.5c-2.1 3.75-21.39 38.98-65.09 38.98-22.69 0-73.96-9.87-73.96-77.05 0-58.67 42.97-77.06 72.63-77.06 30.71-.01 52.58 11.95 65.56 35.86zM247.56 8.05C104.74 8.05 0 123.11 0 256.05c0 138.49 113.6 248 247.56 248 129.93 0 248.44-100.87 248.44-248 0-137.87-106.62-248-248.44-248zm.87 450.81c-112.54 0-203.7-93.04-203.7-202.81 0-105.42 85.43-203.27 203.72-203.27 112.53 0 202.82 89.46 202.82 203.26-.01 121.69-99.68 202.82-202.84 202.82z"></path></svg> <svg viewBox="0 0 496 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M314.9 194.4v101.4h-28.3v120.5h-77.1V295.9h-28.3V194.4c0-4.4 1.6-8.2 4.6-11.3 3.1-3.1 6.9-4.7 11.3-4.7H299c4.1 0 7.8 1.6 11.1 4.7 3.1 3.2 4.8 6.9 4.8 11.3zm-101.5-63.7c0-23.3 11.5-35 34.5-35s34.5 11.7 34.5 35c0 23-11.5 34.5-34.5 34.5s-34.5-11.5-34.5-34.5zM247.6 8C389.4 8 496 118.1 496 256c0 147.1-118.5 248-248.4 248C113.6 504 0 394.5 0 256 0 123.1 104.7 8 247.6 8zm.8 44.7C130.2 52.7 44.7 150.6 44.7 256c0 109.8 91.2 202.8 203.7 202.8 103.2 0 202.8-81.1 202.8-202.8.1-113.8-90.2-203.3-202.8-203.3z"></path></svg>](https://creativecommons.org/licenses/by/4.0/) unless otherwise specified.] --- class: inverse, center, middle # Learning objectives ![:custom_hr]() ## .font160[<svg viewBox="0 0 576 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M560.02 32c-1.96 0-3.98.37-5.96 1.16L384.01 96H384L212 35.28A64.252 64.252 0 0 0 191.76 32c-6.69 0-13.37 1.05-19.81 3.14L20.12 87.95A32.006 32.006 0 0 0 0 117.66v346.32C0 473.17 7.53 480 15.99 480c1.96 0 3.97-.37 5.96-1.16L192 416l172 60.71a63.98 63.98 0 0 0 40.05.15l151.83-52.81A31.996 31.996 0 0 0 576 394.34V48.02c0-9.19-7.53-16.02-15.98-16.02zM224 90.42l128 45.19v285.97l-128-45.19V90.42zM48 418.05V129.07l128-44.53v286.2l-.64.23L48 418.05zm480-35.13l-128 44.53V141.26l.64-.24L528 93.95v288.97z"></path></svg> + <svg viewBox="0 0 581 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M581 226.6C581 119.1 450.9 32 290.5 32S0 119.1 0 226.6C0 322.4 103.3 402 239.4 418.1V480h99.1v-61.5c24.3-2.7 47.6-7.4 69.4-13.9L448 480h112l-67.4-113.7c54.5-35.4 88.4-84.9 88.4-139.7zm-466.8 14.5c0-73.5 98.9-133 220.8-133s211.9 40.7 211.9 133c0 50.1-26.5 85-70.3 106.4-2.4-1.6-4.7-2.9-6.4-3.7-10.2-5.2-27.8-10.5-27.8-10.5s86.6-6.4 86.6-92.7-90.6-87.9-90.6-87.9h-199V361c-74.1-21.5-125.2-67.1-125.2-119.9zm225.1 38.3v-55.6c57.8 0 87.8-6.8 87.8 27.3 0 36.5-38.2 28.3-87.8 28.3zm-.9 72.5H365c10.8 0 18.9 11.7 24 19.2-16.1 1.9-33 2.8-50.6 2.9v-22.1z"></path></svg>] --- # Learning objectives * Understand the benefits of using <svg viewBox="0 0 581 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M581 226.6C581 119.1 450.9 32 290.5 32S0 119.1 0 226.6C0 322.4 103.3 402 239.4 418.1V480h99.1v-61.5c24.3-2.7 47.6-7.4 69.4-13.9L448 480h112l-67.4-113.7c54.5-35.4 88.4-84.9 88.4-139.7zm-466.8 14.5c0-73.5 98.9-133 220.8-133s211.9 40.7 211.9 133c0 50.1-26.5 85-70.3 106.4-2.4-1.6-4.7-2.9-6.4-3.7-10.2-5.2-27.8-10.5-27.8-10.5s86.6-6.4 86.6-92.7-90.6-87.9-90.6-87.9h-199V361c-74.1-21.5-125.2-67.1-125.2-119.9zm225.1 38.3v-55.6c57.8 0 87.8-6.8 87.8 27.3 0 36.5-38.2 28.3-87.8 28.3zm-.9 72.5H365c10.8 0 18.9 11.7 24 19.2-16.1 1.9-33 2.8-50.6 2.9v-22.1z"></path></svg> to manipulate spatial data -- * Learn how to use `sf` to manipulate vectors -- * Learn how to use `stars` to manipulate rasters -- * Learn how to use `tmap` to visualize spatial data <!-- Day1 --> --- class: inverse, center, middle # <svg viewBox="0 0 512 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> Rationalize ![:custom_hr]() ## Why use <svg viewBox="0 0 581 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M581 226.6C581 119.1 450.9 32 290.5 32S0 119.1 0 226.6C0 322.4 103.3 402 239.4 418.1V480h99.1v-61.5c24.3-2.7 47.6-7.4 69.4-13.9L448 480h112l-67.4-113.7c54.5-35.4 88.4-84.9 88.4-139.7zm-466.8 14.5c0-73.5 98.9-133 220.8-133s211.9 40.7 211.9 133c0 50.1-26.5 85-70.3 106.4-2.4-1.6-4.7-2.9-6.4-3.7-10.2-5.2-27.8-10.5-27.8-10.5s86.6-6.4 86.6-92.7-90.6-87.9-90.6-87.9h-199V361c-74.1-21.5-125.2-67.1-125.2-119.9zm225.1 38.3v-55.6c57.8 0 87.8-6.8 87.8 27.3 0 36.5-38.2 28.3-87.8 28.3zm-.9 72.5H365c10.8 0 18.9 11.7 24 19.2-16.1 1.9-33 2.8-50.6 2.9v-22.1z"></path></svg> for spatial data? (<svg viewBox="0 0 512 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8zm0 448c-110.5 0-200-89.5-200-200S145.5 56 256 56s200 89.5 200 200-89.5 200-200 200zm61.8-104.4l-84.9-61.7c-3.1-2.3-4.9-5.9-4.9-9.7V116c0-6.6 5.4-12 12-12h32c6.6 0 12 5.4 12 12v141.7l66.8 48.6c5.4 3.9 6.5 11.4 2.6 16.8L334.6 349c-3.9 5.3-11.4 6.5-16.8 2.6z"></path></svg>~15min) ??? If after the workshop what follows makes sense and you feel it is kind of true, it would a success. --- # Spatial Data: Vectors * Vectors are collections of points -- * Points may be grouped and ordered (lines, polygons) -- * Points are georeferenced: coordinates + CRS -- * Additional data may be associated with the different points or group of points (attribute table) -- * Commonly used to represent areas, trajectories.... --- # Spatial Data: Vectors - Examples of raster file formats: - **ESRI Shapefile** (see https://www.loc.gov/preservation/digital/formats/fdd/fdd000280.shtml) - **GeoJSON** (see https://en.wikipedia.org/wiki/GeoJSON) - **GeoPackage** (vectors and raster, see https://www.ogc.org/standards/geopackage) -- - <svg viewBox="0 0 512 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> More details : - https://gisgeography.com/gis-formats/ - https://gdal.org/drivers/vector/index.html --- # Spatial Data: Rasters * Raster are grids of equally sized (most often) cells (or pixels). -- * Every cell has one or several values (or missing values). -- * Cells are georeferenced: coordinates + CRS -- * Commonly used to represent continuous phenomena (e.g. temperature, elevation) and images (airborne or satellite imagery). --- # Spatial Data: Rasters - Examples of vector file formats: - **GeoTIFF** (see https://trac.osgeo.org/geotiff) - **NetCDF** (see https://www.unidata.ucar.edu/software/netcdf/) - **KML** (also supports vector objects, see https://www.ogc.org/standards/kml/) - **GeoPackage** (see https://www.ogc.org/standards/geopackage) -- - <svg viewBox="0 0 512 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> More details : - https://gisgeography.com/gis-formats/ - https://gdal.org/drivers/raster/index.html --- # Coding vs clicking ### 1. Programming/coding allows to automate what is frequently done! -- ### 2. Allow to make analyses/reports reproducible -- - create data pipeline - for colleagues and future you -- ### 3. Increase efficiency (spend more time thinking) -- ### 4. Feeling empowered ??? One is not better than the other do more, ok but do it in a better way --- # About <svg viewBox="0 0 581 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M581 226.6C581 119.1 450.9 32 290.5 32S0 119.1 0 226.6C0 322.4 103.3 402 239.4 418.1V480h99.1v-61.5c24.3-2.7 47.6-7.4 69.4-13.9L448 480h112l-67.4-113.7c54.5-35.4 88.4-84.9 88.4-139.7zm-466.8 14.5c0-73.5 98.9-133 220.8-133s211.9 40.7 211.9 133c0 50.1-26.5 85-70.3 106.4-2.4-1.6-4.7-2.9-6.4-3.7-10.2-5.2-27.8-10.5-27.8-10.5s86.6-6.4 86.6-92.7-90.6-87.9-90.6-87.9h-199V361c-74.1-21.5-125.2-67.1-125.2-119.9zm225.1 38.3v-55.6c57.8 0 87.8-6.8 87.8 27.3 0 36.5-38.2 28.3-87.8 28.3zm-.9 72.5H365c10.8 0 18.9 11.7 24 19.2-16.1 1.9-33 2.8-50.6 2.9v-22.1z"></path></svg> > R is a programming language and free software environment for statistical computing and graphics supported by the R Foundation for Statistical Computing. [Wikipedia](https://bit.ly/2WFv1Nu) -- .center[![:scale 50%](https://roelverbelen.netlify.app/img/hex-stickers.png)] ??? Also tons of spatial packages --- # Why <svg viewBox="0 0 581 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M581 226.6C581 119.1 450.9 32 290.5 32S0 119.1 0 226.6C0 322.4 103.3 402 239.4 418.1V480h99.1v-61.5c24.3-2.7 47.6-7.4 69.4-13.9L448 480h112l-67.4-113.7c54.5-35.4 88.4-84.9 88.4-139.7zm-466.8 14.5c0-73.5 98.9-133 220.8-133s211.9 40.7 211.9 133c0 50.1-26.5 85-70.3 106.4-2.4-1.6-4.7-2.9-6.4-3.7-10.2-5.2-27.8-10.5-27.8-10.5s86.6-6.4 86.6-92.7-90.6-87.9-90.6-87.9h-199V361c-74.1-21.5-125.2-67.1-125.2-119.9zm225.1 38.3v-55.6c57.8 0 87.8-6.8 87.8 27.3 0 36.5-38.2 28.3-87.8 28.3zm-.9 72.5H365c10.8 0 18.9 11.7 24 19.2-16.1 1.9-33 2.8-50.6 2.9v-22.1z"></path></svg>? <br> ### 1. Freeware + Open source -- ### 2. **Package ecosystem** is rich and under active development -- ### 3. Many packages connect <svg viewBox="0 0 581 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M581 226.6C581 119.1 450.9 32 290.5 32S0 119.1 0 226.6C0 322.4 103.3 402 239.4 418.1V480h99.1v-61.5c24.3-2.7 47.6-7.4 69.4-13.9L448 480h112l-67.4-113.7c54.5-35.4 88.4-84.9 88.4-139.7zm-466.8 14.5c0-73.5 98.9-133 220.8-133s211.9 40.7 211.9 133c0 50.1-26.5 85-70.3 106.4-2.4-1.6-4.7-2.9-6.4-3.7-10.2-5.2-27.8-10.5-27.8-10.5s86.6-6.4 86.6-92.7-90.6-87.9-90.6-87.9h-199V361c-74.1-21.5-125.2-67.1-125.2-119.9zm225.1 38.3v-55.6c57.8 0 87.8-6.8 87.8 27.3 0 36.5-38.2 28.3-87.8 28.3zm-.9 72.5H365c10.8 0 18.9 11.7 24 19.2-16.1 1.9-33 2.8-50.6 2.9v-22.1z"></path></svg> to other software/language -- ### 4. Community is wide, quite involved, responsive -- ### 5. You are most likely already using it! You are not starting from scratch -- ### **<svg viewBox="0 0 448 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M190.5 66.9l22.2-22.2c9.4-9.4 24.6-9.4 33.9 0L441 239c9.4 9.4 9.4 24.6 0 33.9L246.6 467.3c-9.4 9.4-24.6 9.4-33.9 0l-22.2-22.2c-9.5-9.5-9.3-25 .4-34.3L311.4 296H24c-13.3 0-24-10.7-24-24v-32c0-13.3 10.7-24 24-24h287.4L190.9 101.2c-9.8-9.3-10-24.8-.4-34.3z"></path></svg> Very little cannot be done with <svg viewBox="0 0 581 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M581 226.6C581 119.1 450.9 32 290.5 32S0 119.1 0 226.6C0 322.4 103.3 402 239.4 418.1V480h99.1v-61.5c24.3-2.7 47.6-7.4 69.4-13.9L448 480h112l-67.4-113.7c54.5-35.4 88.4-84.9 88.4-139.7zm-466.8 14.5c0-73.5 98.9-133 220.8-133s211.9 40.7 211.9 133c0 50.1-26.5 85-70.3 106.4-2.4-1.6-4.7-2.9-6.4-3.7-10.2-5.2-27.8-10.5-27.8-10.5s86.6-6.4 86.6-92.7-90.6-87.9-90.6-87.9h-199V361c-74.1-21.5-125.2-67.1-125.2-119.9zm225.1 38.3v-55.6c57.8 0 87.8-6.8 87.8 27.3 0 36.5-38.2 28.3-87.8 28.3zm-.9 72.5H365c10.8 0 18.9 11.7 24 19.2-16.1 1.9-33 2.8-50.6 2.9v-22.1z"></path></svg>** ??? Very few people look at the R source code but many people are now involved in package dev via GH or GL. R Python Julia, whatever, language itself matters less that these If you have time to learn a bit it might sometime makes sense, notably to work with other colleagues. You don't start from scratch --- # <svg viewBox="0 0 581 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M581 226.6C581 119.1 450.9 32 290.5 32S0 119.1 0 226.6C0 322.4 103.3 402 239.4 418.1V480h99.1v-61.5c24.3-2.7 47.6-7.4 69.4-13.9L448 480h112l-67.4-113.7c54.5-35.4 88.4-84.9 88.4-139.7zm-466.8 14.5c0-73.5 98.9-133 220.8-133s211.9 40.7 211.9 133c0 50.1-26.5 85-70.3 106.4-2.4-1.6-4.7-2.9-6.4-3.7-10.2-5.2-27.8-10.5-27.8-10.5s86.6-6.4 86.6-92.7-90.6-87.9-90.6-87.9h-199V361c-74.1-21.5-125.2-67.1-125.2-119.9zm225.1 38.3v-55.6c57.8 0 87.8-6.8 87.8 27.3 0 36.5-38.2 28.3-87.8 28.3zm-.9 72.5H365c10.8 0 18.9 11.7 24 19.2-16.1 1.9-33 2.8-50.6 2.9v-22.1z"></path></svg> packages ecosystem for spatial objects -- ## Classes and Methods -- ## Statistical Analyses -- ## Visualisation -- ## Data Retrieval --- # <svg viewBox="0 0 581 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M581 226.6C581 119.1 450.9 32 290.5 32S0 119.1 0 226.6C0 322.4 103.3 402 239.4 418.1V480h99.1v-61.5c24.3-2.7 47.6-7.4 69.4-13.9L448 480h112l-67.4-113.7c54.5-35.4 88.4-84.9 88.4-139.7zm-466.8 14.5c0-73.5 98.9-133 220.8-133s211.9 40.7 211.9 133c0 50.1-26.5 85-70.3 106.4-2.4-1.6-4.7-2.9-6.4-3.7-10.2-5.2-27.8-10.5-27.8-10.5s86.6-6.4 86.6-92.7-90.6-87.9-90.6-87.9h-199V361c-74.1-21.5-125.2-67.1-125.2-119.9zm225.1 38.3v-55.6c57.8 0 87.8-6.8 87.8 27.3 0 36.5-38.2 28.3-87.8 28.3zm-.9 72.5H365c10.8 0 18.9 11.7 24 19.2-16.1 1.9-33 2.8-50.6 2.9v-22.1z"></path></svg> packages ecosystem for spatial objects ## Classes and Methods -- ### Vectors - [`sp`](https://CRAN.R-project.org/package=sp) -- - [`sf`](https://CRAN.R-project.org/package=sf) <svg viewBox="0 0 512 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M173.898 439.404l-166.4-166.4c-9.997-9.997-9.997-26.206 0-36.204l36.203-36.204c9.997-9.998 26.207-9.998 36.204 0L192 312.69 432.095 72.596c9.997-9.997 26.207-9.997 36.204 0l36.203 36.204c9.997 9.997 9.997 26.206 0 36.204l-294.4 294.401c-9.998 9.997-26.207 9.997-36.204-.001z"></path></svg> -- ### **Rasters** (our main focus) - [`raster`](https://CRAN.R-project.org/package=raster) - [`stars`](https://CRAN.R-project.org/package=stars) <svg viewBox="0 0 512 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M173.898 439.404l-166.4-166.4c-9.997-9.997-9.997-26.206 0-36.204l36.203-36.204c9.997-9.998 26.207-9.998 36.204 0L192 312.69 432.095 72.596c9.997-9.997 26.207-9.997 36.204 0l36.203 36.204c9.997 9.997 9.997 26.206 0 36.204l-294.4 294.401c-9.998 9.997-26.207 9.997-36.204-.001z"></path></svg> - [`terra`](https://CRAN.R-project.org/package=terra) <svg viewBox="0 0 512 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> https://keen-swartz-3146c4.netlify.app/raster.html#package-stars <svg viewBox="0 0 512 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> https://github.com/rspatial (15 packages) ??? Not visualisation later --- # Vectors packages ### [`sp`](https://CRAN.R-project.org/package=sp) .font90[04/2005 (0.7.3) // 01/2021 (1.4.5)] -- ### [`sf`](https://CRAN.R-project.org/package=sf) .font90[10/2016 (0.9.8) // 03/2021 (3.3.7)] > A package that provides [Simple Features Access](https://en.wikipedia.org/wiki/Simple_Features) for R. -- As explained in the article ["Simple Features for R"](https://r-spatial.github.io/sf/articles/sf1.html): > R has well-supported classes for storing spatial data (sp) and interfacing to the above mentioned environments (rgdal, rgeos), but has so far lacked a complete implementation of simple features, making conversions at times convoluted, inefficient or incomplete. The package sf tries to fill this gap, and aims at succeeding sp in the long term. --- # Package [`sf`](https://CRAN.R-project.org/package=sf): Simple Features Very well documented and very popular. .center[![:scale 84%](img/sf0.png)] .right[.font80[see https://r-spatial.github.io/sf/index.html]] --- # Raster packages ### [`raster`](https://CRAN.R-project.org/package=raster) .font90[03/2010 (1.0.0) // 11/2020 (3.3.7)] <br> -- ### [`terra`](https://CRAN.R-project.org/package=terra) .font90[03/2019 (0.5-2) // 11/2020 (0.9.11)] > terra is an R package that replaces raster. It has a very similar interface, but it is simpler and much faster. <br> -- ### [`stars`](https://CRAN.R-project.org/package=stars) .font90[07/2018 (0.1-1) // 07/2020 (0.4.3)] > Reading, manipulating, writing and plotting spatiotemporal arrays (raster and vector data cubes) in 'R'. Depends on [`sf`](https://CRAN.R-project.org/package=sf). Note that `stars` and `sf` are [Tidyverse](https://www.tidyverse.org/) friendly. --- # Package [`stars`](https://CRAN.R-project.org/package=stars): SpatioTemporal ARrayS ### Extent `sf` to Raster and Vector Datacubes .center[[![:scale 62%](https://raw.githubusercontent.com/r-spatial/stars/master/images/cube3.png)](https://r-spatial.github.io/stars/index.html)] .right[.font80[see https://r-spatial.github.io/stars/index.html]] --- # Package [`stars`](https://CRAN.R-project.org/package=stars): SpatioTemporal ARrayS ### - Well documented .center[![:scale 92%](img/pkgdown_stars.png)] .right[.font80[see https://r-spatial.github.io/stars/index.html]] -- ### - 12 packages depend on/import [`stars`](https://CRAN.R-project.org/package=stars) ### - So far, less material available online, but it's getting better and better! --- class: inverse, center, middle # <svg viewBox="0 0 512 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> Introduction to `sf` ![:custom_hr]() ## <svg viewBox="0 0 512 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8zm0 448c-110.5 0-200-89.5-200-200S145.5 56 256 56s200 89.5 200 200-89.5 200-200 200zm61.8-104.4l-84.9-61.7c-3.1-2.3-4.9-5.9-4.9-9.7V116c0-6.6 5.4-12 12-12h32c6.6 0 12 5.4 12 12v141.7l66.8 48.6c5.4 3.9 6.5 11.4 2.6 16.8L334.6 349c-3.9 5.3-11.4 6.5-16.8 2.6z"></path></svg> ~20min ??? If after the workshop what follows makes sense and you feel it is kind of true, it would a success. --- # Introduction to `sf` ## `sf` objects ![](https://r-spatial.github.io/sf/articles/sf_xfig.png) --- # Introduction to `sf` ## `sf` objects .pull-left[ - Most common geometries: - `POINT`: .font80[vector] - `MULTIPOINT`: .font80[matrix] - `LINESTRING`: .font80[matrix] - `MULTILINESTRING`: .font80[list of matrices] - `POLYGON`: .font80[list of matrices] - `MULTIPOLYGON`: .font80[list of lists of matrices] - `GEOMETRYCOLLECTION`: .font80[combinations] ] .pull-right[ ![](https://r-spatial.github.io/sf/articles/sf1_files/figure-html/unnamed-chunk-20-1.png) ] -- .font90[<svg viewBox="0 0 576 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M569.517 440.013C587.975 472.007 564.806 512 527.94 512H48.054c-36.937 0-59.999-40.055-41.577-71.987L246.423 23.985c18.467-32.009 64.72-31.951 83.154 0l239.94 416.028zM288 354c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z"></path></svg> More [here](https://www.jessesadler.com/post/simple-feature-objects/) on sf objects and geometries] --- # Introduction to `sf` ## Importing: from `.csv` Acess the [Computerized Database of Québec Seabirds (CDQS)](https://open.canada.ca/data/en/dataset/9cd6f8a1-e660-4e78-89a8-6e3f781da556) data using the [`rgovcan`](https://github.com/VLucet/rgovcan) package. .font90[ ```r # Output folder if (!file.exists('output')) dir.create('output') # remotes::install_github("vlucet/rgovcan") library(rgovcan) id <- "9cd6f8a1-e660-4e78-89a8-6e3f781da556" govcan_dl_resources(id, path = 'output') #> # A tibble: 3 x 7 #> id package_id url path fmt store data #> <chr> <chr> <chr> <chr> <chr> <chr> <lgl> #> 1 578b525a-ea… 9cd6f8a1-e660-4… http://donnees.ec.g… <NA> html <NA> NA #> 2 fb9ab6f5-d8… 9cd6f8a1-e660-4… http://donnees.ec.g… <NA> html <NA> NA #> 3 ec4802ba-57… 9cd6f8a1-e660-4… http://donnees.ec.g… output/C… csv disk NA # Import csv cdqs <- read.csv('output/CDQS_BIOMQ_2017.csv') ``` ] --- # Introduction to `sf` ## Importing: from `.csv` .font90[ ```r cdqs <- st_as_sf(x = cdqs, coords = c("CentroideX", "CentroideY"), crs = 4326) ``` ] .font80[ ``` #> Simple feature collection with 10423 features and 1 field #> Geometry type: POINT #> Dimension: XY #> Bounding box: xmin: -79.9929 ymin: 45.03 xmax: -57.1923 ymax: 62.5487 #> Geodetic CRS: WGS 84 #> First 10 features: #> Numero.de.colonie.Colony.Number geometry #> 1 1 POINT (-66.3393 48.0912) #> 2 1 POINT (-66.3393 48.0912) #> 3 1 POINT (-66.3393 48.0912) #> 4 1 POINT (-66.3393 48.0912) #> 5 2 POINT (-66.2448 48.1061) #> 6 2 POINT (-66.2448 48.1061) #> 7 2 POINT (-66.2448 48.1061) #> 8 2 POINT (-66.2448 48.1061) #> 9 2 POINT (-66.2448 48.1061) #> 10 2 POINT (-66.2448 48.1061) ``` ] --- # Introduction to `sf` ## Importing: from `.csv` .font90[ ```r plot(cdqs[,1]) ``` <img src="_part1_files/figure-html/csvplot-1.png" style="display: block; margin: auto;" /> ] --- # Introduction to `sf` ## Importing: from `.shp` [Roseate Tern critical habitats in Atlantic Canada](https://open.canada.ca/data/en/dataset/0c469c0e-8afd-4c62-9420-c50af2d34f97) .font90[ ```r # Download from open canada id <- "0c469c0e-8afd-4c62-9420-c50af2d34f97" govcan_dl_resources(id, path = 'output') #> # A tibble: 9 x 7 #> id package_id url path fmt store data #> <chr> <chr> <chr> <chr> <chr> <chr> <lgl> #> 1 150795d2-f… 0c469c0e-8afd-… https://maps-carte… <NA> esri… <NA> NA #> 2 548c4f09-b… 0c469c0e-8afd-… https://maps-carte… <NA> esri… <NA> NA #> 3 2709233e-6… 0c469c0e-8afd-… https://maps-carte… <NA> wms <NA> NA #> 4 844839d8-2… 0c469c0e-8afd-… https://maps-carte… <NA> wms <NA> NA #> 5 d89521ef-d… 0c469c0e-8afd-… http://data.ec.gc.… output/Ster… zip disk NA #> 6 d1b60b56-8… 0c469c0e-8afd-… http://data.ec.gc.… <NA> html <NA> NA #> 7 5ad3f90c-6… 0c469c0e-8afd-… http://data.ec.gc.… <NA> html <NA> NA #> 8 7cf320e5-7… 0c469c0e-8afd-… https://open.canad… <NA> html <NA> NA #> 9 2de8a5a1-c… 0c469c0e-8afd-… https://ouvert.can… <NA> html <NA> NA unzip("output/Sterna_dougallii_Roseate_Tern_CH_2015.zip", exdir = 'output') ``` ] --- # Introduction to `sf` ## Importing: from `.shp` [Roseate Tern critical habitats in Atlantic Canada](https://open.canada.ca/data/en/dataset/0c469c0e-8afd-4c62-9420-c50af2d34f97) .font90[ ```r # Import shapefile roseate <- st_read("output/Sterna_dougallii_Roseate_Tern_CH_2015.shp", quiet = TRUE) roseate #> Simple feature collection with 4 features and 28 fields #> Geometry type: POLYGON #> Dimension: XY #> Bounding box: xmin: 271825.7 ymin: 4834423 xmax: 769248.4 ymax: 4995778 #> Projected CRS: NAD83 / UTM zone 20N #> OBJECTID_1 SciName CommName_E CommName_F Population #> 1 1 Sterna dougalli Roseate Tern Sterne de Dougall <NA> #> 2 2 Sterna dougalli Roseate Tern Sterne de Dougall <NA> #> 3 3 Sterna dougalli Roseate Tern Sterne de Dougall <NA> #> 4 4 Sterna dougalli Roseate Tern Sterne de Dougall <NA> #> SpeciesID SiteID SiteName CHApproach CHMethod #> 1 40 40_3 Country Island Site Critical Function Zone #> 2 40 40_2 South Brother Site Critical Function Zone #> 3 40 40_1 The Brothers Site Critical Function Zone #> 4 40 40_4 Sable Island Site Critical Function Zone #> Region ProvTerr LandTenure UTMZone Easting Northing #> 1 Guysborough Co. NS <NA> 20 614647.6 4995311 #> 2 Shelburne Co. NS Provincial 20 272068.2 4834675 #> 3 Shelburne Co. NS Provincial 20 272256.7 4835403 #> 4 <NA> NS Federal (National Park) 20 748983.0 4871222 #> Latitude Longitude Area_ha Comments DateEdited Shape_Leng CWS_Region #> 1 45.10197 -61.54278 71.83462 <NA> 2012-11-23 3067.445 Atlantic Region #> 2 43.63015 -65.82541 18.75967 <NA> 2012-11-23 1545.723 Atlantic Region #> 3 43.63676 -65.82338 18.82951 <NA> 2012-11-23 1546.267 Atlantic Region #> 4 43.95192 -59.89697 4963.63283 <NA> 2012-11-23 97283.429 Atlantic Region #> CH_Status CH_Sensiti Shape_Le_1 Shape_Area Area_ha2 #> 1 <NA> <NA> 3067.445 718346.2 71.83462 #> 2 <NA> <NA> 1545.723 187596.7 18.75967 #> 3 <NA> <NA> 1546.267 188295.1 18.82951 #> 4 <NA> <NA> 97283.429 49636328.3 4963.63283 #> geometry #> 1 POLYGON ((614154.7 4995363,... #> 2 POLYGON ((272089 4834917, 2... #> 3 POLYGON ((272251.3 4835654,... #> 4 POLYGON ((768654.3 4882185,... ``` ] --- # Introduction to `sf` ## Importing: from `.shp` [Roseate Tern critical habitats in Atlantic Canada](https://open.canada.ca/data/en/dataset/0c469c0e-8afd-4c62-9420-c50af2d34f97) .font90[ ```r plot(st_geometry(roseate[4,])) ``` <img src="_part1_files/figure-html/shpplot-1.png" style="display: block; margin: auto;" /> ] --- # Introduction to `sf` ## Importing: from `.geojson` We already have a geojson included in the workshop data: `st_laurence.geojson` .font90[ ```r stl <- st_read('data/st_laurence.geojson', quiet = TRUE) stl #> Simple feature collection with 1 feature and 10 fields #> Geometry type: POLYGON #> Dimension: XY #> Bounding box: xmin: -74.86481 ymin: 44.9585 xmax: -54.70345 ymax: 52.22242 #> Geodetic CRS: WGS 84 #> name id longitude latitude min_x min_y max_x #> 1 Gulf of St. Lawrence 24 -61.56657 48.38072 -74.86481 44.9585 -54.70345 #> max_y area mrgid geometry #> 1 52.22242 290874 4290 POLYGON ((-64.67242 47.7168... ``` ] -- .font90[<svg viewBox="0 0 576 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M569.517 440.013C587.975 472.007 564.806 512 527.94 512H48.054c-36.937 0-59.999-40.055-41.577-71.987L246.423 23.985c18.467-32.009 64.72-31.951 83.154 0l239.94 416.028zM288 354c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z"></path></svg> We strongly suggest using geojson (single file) rather than shapefiles (multiple files)] --- # Introduction to `sf` ## Importing: from `.geojson` We already have a geojson included in the workshop data: `st_laurence.geojson` .font90[ ```r plot(st_geometry(stl)) ``` <img src="_part1_files/figure-html/geojsonplot-1.png" style="display: block; margin: auto;" /> ] --- # Introduction to `sf` ## Importing: from `.gbd` [Atlas of Seabirds at Sea in Eastern Canada 2006-2016](https://open.canada.ca/data/en/dataset/f612e2b4-5c67-46dc-9a84-1154c649ab4e) .font90[ ```r id <- "f612e2b4-5c67-46dc-9a84-1154c649ab4e" govcan_dl_resources(id, path = 'output') #> # A tibble: 13 x 7 #> id package_id url path fmt store data #> <chr> <chr> <chr> <chr> <chr> <chr> <lgl> #> 1 cbc5f072-3… f612e2b4-5c67-… https://maps-car… <NA> esri… <NA> NA #> 2 c7371e87-6… f612e2b4-5c67-… https://maps-car… <NA> esri… <NA> NA #> 3 5c04ae7a-2… f612e2b4-5c67-… https://maps-car… <NA> wms <NA> NA #> 4 e3814b19-9… f612e2b4-5c67-… https://maps-car… <NA> wms <NA> NA #> 5 791dabee-f… f612e2b4-5c67-… http://data.ec.g… <NA> html <NA> NA #> 6 a73a90f7-e… f612e2b4-5c67-… http://data.ec.g… <NA> html <NA> NA #> 7 2db1dedd-2… f612e2b4-5c67-… http://data.ec.g… output/Atlas… zip disk NA #> 8 907704ab-b… f612e2b4-5c67-… http://data.ec.g… output/Seabi… pdf disk NA #> 9 ab948974-5… f612e2b4-5c67-… http://data.ec.g… output/DataD… xlsx disk NA #> 10 0adf8825-b… f612e2b4-5c67-… http://data.ec.g… output/Densi… xlsx disk NA #> 11 0caa6f2c-9… f612e2b4-5c67-… http://data.ec.g… output/Prese… pdf disk NA #> 12 1bf45da1-6… f612e2b4-5c67-… http://data.ec.g… output/ECSAS… pdf disk NA #> 13 247813e2-1… f612e2b4-5c67-… http://data.ec.g… output/SOMEC… pdf disk NA unzip("output/AtlasGrid-GrilleAtlas.gdb.zip", exdir = 'output') ``` ] --- # Introduction to `sf` ## Importing: from `.gbd` [Atlas of Seabirds at Sea in Eastern Canada 2006-2016](https://open.canada.ca/data/en/dataset/f612e2b4-5c67-46dc-9a84-1154c649ab4e) .font90[ ```r # Visualize layers available in the geodatabase st_layers("output/AtlasGrid-GrilleAtlas.gdb") #> Driver: OpenFileGDB #> Available layers: #> layer_name geometry_type features fields #> 1 AtlasGrid_GrilleAtlas Multi Polygon 514 3 ``` ] --- # Introduction to `sf` ## Importing: from `.gbd` [Atlas of Seabirds at Sea in Eastern Canada 2006-2016](https://open.canada.ca/data/en/dataset/f612e2b4-5c67-46dc-9a84-1154c649ab4e) .font90[ ```r # Load layer atlas <- st_read('output/AtlasGrid-GrilleAtlas.gdb', layer = 'AtlasGrid_GrilleAtlas') #> Reading layer `AtlasGrid_GrilleAtlas' from data source `/__w/wksh_raster_CWS/wksh_raster_CWS/output/AtlasGrid-GrilleAtlas.gdb' using driver `OpenFileGDB' #> Simple feature collection with 514 features and 3 fields #> Geometry type: MULTIPOLYGON #> Dimension: XY #> Bounding box: xmin: -1161755 ymin: -1437385 xmax: 2688245 ymax: 2921609 #> Projected CRS: Lambert_Azimuthal_Equal_Area ``` ] --- # Introduction to `sf` ## Importing: from `.gbd` [Atlas of Seabirds at Sea in Eastern Canada 2006-2016](https://open.canada.ca/data/en/dataset/f612e2b4-5c67-46dc-9a84-1154c649ab4e) .font90[ ```r plot(st_geometry(atlas)) ``` <img src="_part1_files/figure-html/gbdplot-1.png" style="display: block; margin: auto;" /> ] --- # Introduction to `sf` ## Exporting ```r st_write(roseate, "output/roseate.shp", delete_dsn = TRUE) #> Deleting source `output/roseate.shp' failed #> Writing layer `roseate' to data source `output/roseate.shp' using driver `ESRI Shapefile' #> Writing 4 features with 28 fields and geometry type Polygon. st_write(stl, "output/stl.geojson", delete_dsn = TRUE) #> Deleting source `output/stl.geojson' failed #> Writing layer `stl' to data source `output/stl.geojson' using driver `GeoJSON' #> Writing 1 features with 10 fields and geometry type Polygon. ``` --- # Introduction to `sf` ## Exporting .font60[ ```r st_drivers() #> name long_name write copy is_raster is_vector vsi #> PCIDSK PCIDSK PCIDSK Database File TRUE FALSE TRUE TRUE TRUE #> netCDF netCDF Network Common Data Format TRUE TRUE TRUE TRUE TRUE #> PDS4 PDS4 NASA Planetary Data System 4 TRUE TRUE TRUE TRUE TRUE #> JP2OpenJPEG JP2OpenJPEG JPEG-2000 driver based on OpenJPEG library FALSE TRUE TRUE TRUE TRUE #> PDF PDF Geospatial PDF TRUE TRUE TRUE TRUE TRUE #> MBTiles MBTiles MBTiles TRUE TRUE TRUE TRUE TRUE #> EEDA EEDA Earth Engine Data API FALSE FALSE FALSE TRUE FALSE #> ESRI Shapefile ESRI Shapefile ESRI Shapefile TRUE FALSE FALSE TRUE TRUE #> MapInfo File MapInfo File MapInfo File TRUE FALSE FALSE TRUE TRUE #> UK .NTF UK .NTF UK .NTF FALSE FALSE FALSE TRUE TRUE #> OGR_SDTS OGR_SDTS SDTS FALSE FALSE FALSE TRUE TRUE #> S57 S57 IHO S-57 (ENC) TRUE FALSE FALSE TRUE TRUE #> DGN DGN Microstation DGN TRUE FALSE FALSE TRUE TRUE #> OGR_VRT OGR_VRT VRT - Virtual Datasource FALSE FALSE FALSE TRUE TRUE #> REC REC EPIInfo .REC FALSE FALSE FALSE TRUE FALSE #> Memory Memory Memory TRUE FALSE FALSE TRUE FALSE #> BNA BNA Atlas BNA TRUE FALSE FALSE TRUE TRUE #> CSV CSV Comma Separated Value (.csv) TRUE FALSE FALSE TRUE TRUE #> NAS NAS NAS - ALKIS FALSE FALSE FALSE TRUE TRUE #> GML GML Geography Markup Language (GML) TRUE FALSE FALSE TRUE TRUE #> GPX GPX GPX TRUE FALSE FALSE TRUE TRUE #> LIBKML LIBKML Keyhole Markup Language (LIBKML) TRUE FALSE FALSE TRUE TRUE #> KML KML Keyhole Markup Language (KML) TRUE FALSE FALSE TRUE TRUE #> GeoJSON GeoJSON GeoJSON TRUE FALSE FALSE TRUE TRUE #> GeoJSONSeq GeoJSONSeq GeoJSON Sequence TRUE FALSE FALSE TRUE TRUE #> ESRIJSON ESRIJSON ESRIJSON FALSE FALSE FALSE TRUE TRUE #> TopoJSON TopoJSON TopoJSON FALSE FALSE FALSE TRUE TRUE #> Interlis 1 Interlis 1 Interlis 1 TRUE FALSE FALSE TRUE TRUE #> Interlis 2 Interlis 2 Interlis 2 TRUE FALSE FALSE TRUE TRUE #> OGR_GMT OGR_GMT GMT ASCII Vectors (.gmt) TRUE FALSE FALSE TRUE TRUE #> GPKG GPKG GeoPackage TRUE TRUE TRUE TRUE TRUE #> SQLite SQLite SQLite / Spatialite TRUE FALSE FALSE TRUE TRUE #> OGR_DODS OGR_DODS OGR_DODS FALSE FALSE FALSE TRUE FALSE #> ODBC ODBC ODBC TRUE FALSE FALSE TRUE FALSE #> WAsP WAsP WAsP .map format TRUE FALSE FALSE TRUE TRUE #> PGeo PGeo ESRI Personal GeoDatabase FALSE FALSE FALSE TRUE FALSE #> MSSQLSpatial MSSQLSpatial Microsoft SQL Server Spatial Database TRUE FALSE FALSE TRUE FALSE #> OGR_OGDI OGR_OGDI OGDI Vectors (VPF, VMAP, DCW) FALSE FALSE FALSE TRUE FALSE #> PostgreSQL PostgreSQL PostgreSQL/PostGIS TRUE FALSE FALSE TRUE FALSE #> MySQL MySQL MySQL TRUE FALSE FALSE TRUE FALSE #> OpenFileGDB OpenFileGDB ESRI FileGDB FALSE FALSE FALSE TRUE TRUE #> XPlane XPlane X-Plane/Flightgear aeronautical data FALSE FALSE FALSE TRUE TRUE #> DXF DXF AutoCAD DXF TRUE FALSE FALSE TRUE TRUE #> CAD CAD AutoCAD Driver FALSE FALSE TRUE TRUE TRUE #> Geoconcept Geoconcept Geoconcept TRUE FALSE FALSE TRUE TRUE #> GeoRSS GeoRSS GeoRSS TRUE FALSE FALSE TRUE TRUE #> GPSTrackMaker GPSTrackMaker GPSTrackMaker TRUE FALSE FALSE TRUE TRUE #> VFK VFK Czech Cadastral Exchange Data Format FALSE FALSE FALSE TRUE FALSE #> PGDUMP PGDUMP PostgreSQL SQL dump TRUE FALSE FALSE TRUE TRUE #> OSM OSM OpenStreetMap XML and PBF FALSE FALSE FALSE TRUE TRUE #> GPSBabel GPSBabel GPSBabel TRUE FALSE FALSE TRUE FALSE #> SUA SUA Tim Newport-Peace's Special Use Airspace Format FALSE FALSE FALSE TRUE TRUE #> OpenAir OpenAir OpenAir FALSE FALSE FALSE TRUE TRUE #> OGR_PDS OGR_PDS Planetary Data Systems TABLE FALSE FALSE FALSE TRUE TRUE #> WFS WFS OGC WFS (Web Feature Service) FALSE FALSE FALSE TRUE TRUE #> WFS3 WFS3 OGC WFS 3 client (Web Feature Service) FALSE FALSE FALSE TRUE FALSE #> SOSI SOSI Norwegian SOSI Standard FALSE FALSE FALSE TRUE FALSE #> HTF HTF Hydrographic Transfer Vector FALSE FALSE FALSE TRUE TRUE #> AeronavFAA AeronavFAA Aeronav FAA FALSE FALSE FALSE TRUE TRUE #> Geomedia Geomedia Geomedia .mdb FALSE FALSE FALSE TRUE FALSE #> EDIGEO EDIGEO French EDIGEO exchange format FALSE FALSE FALSE TRUE TRUE #> GFT GFT Google Fusion Tables TRUE FALSE FALSE TRUE FALSE #> SVG SVG Scalable Vector Graphics FALSE FALSE FALSE TRUE TRUE #> CouchDB CouchDB CouchDB / GeoCouch TRUE FALSE FALSE TRUE FALSE #> Cloudant Cloudant Cloudant / CouchDB TRUE FALSE FALSE TRUE FALSE #> Idrisi Idrisi Idrisi Vector (.vct) FALSE FALSE FALSE TRUE TRUE #> ARCGEN ARCGEN Arc/Info Generate FALSE FALSE FALSE TRUE TRUE #> SEGUKOOA SEGUKOOA SEG-P1 / UKOOA P1/90 FALSE FALSE FALSE TRUE TRUE #> SEGY SEGY SEG-Y FALSE FALSE FALSE TRUE TRUE #> XLS XLS MS Excel format FALSE FALSE FALSE TRUE FALSE #> ODS ODS Open Document/ LibreOffice / OpenOffice Spreadsheet TRUE FALSE FALSE TRUE TRUE #> XLSX XLSX MS Office Open XML spreadsheet TRUE FALSE FALSE TRUE TRUE #> ElasticSearch ElasticSearch Elastic Search TRUE FALSE FALSE TRUE FALSE #> Walk Walk Walk FALSE FALSE FALSE TRUE FALSE #> Carto Carto Carto TRUE FALSE FALSE TRUE FALSE #> AmigoCloud AmigoCloud AmigoCloud TRUE FALSE FALSE TRUE FALSE #> SXF SXF Storage and eXchange Format FALSE FALSE FALSE TRUE TRUE #> Selafin Selafin Selafin TRUE FALSE FALSE TRUE TRUE #> JML JML OpenJUMP JML TRUE FALSE FALSE TRUE TRUE #> PLSCENES PLSCENES Planet Labs Scenes API FALSE FALSE TRUE TRUE FALSE #> CSW CSW OGC CSW (Catalog Service for the Web) FALSE FALSE FALSE TRUE FALSE #> VDV VDV VDV-451/VDV-452/INTREST Data Format TRUE FALSE FALSE TRUE TRUE #> GMLAS GMLAS Geography Markup Language (GML) driven by application schemas FALSE TRUE FALSE TRUE TRUE #> MVT MVT Mapbox Vector Tiles TRUE FALSE FALSE TRUE TRUE #> TIGER TIGER U.S. Census TIGER/Line TRUE FALSE FALSE TRUE TRUE #> AVCBin AVCBin Arc/Info Binary Coverage FALSE FALSE FALSE TRUE TRUE #> AVCE00 AVCE00 Arc/Info E00 (ASCII) Coverage FALSE FALSE FALSE TRUE TRUE #> NGW NGW NextGIS Web TRUE TRUE TRUE TRUE FALSE #> HTTP HTTP HTTP Fetching Wrapper FALSE FALSE TRUE TRUE FALSE ``` ] --- # Introduction to `sf` ## Transform between `sf` and `sp` <svg viewBox="0 0 576 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M569.517 440.013C587.975 472.007 564.806 512 527.94 512H48.054c-36.937 0-59.999-40.055-41.577-71.987L246.423 23.985c18.467-32.009 64.72-31.951 83.154 0l239.94 416.028zM288 354c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z"></path></svg> You can use `st_as_sf()` to transform `sp` object to `sf` objects... .font80[ ```r # Do not run newsf <- st_as_sf(oldsp) ``` ] ... and vice-versa with `as()` .font80[ ```r # Do not run newsp <- as(oldsf, "Spatial") ``` ] --- # Introduction to `sf` ## Spatial projections ```r cdqs <- st_transform(cdqs, st_crs(atlas)) ``` <svg viewBox="0 0 576 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M569.517 440.013C587.975 472.007 564.806 512 527.94 512H48.054c-36.937 0-59.999-40.055-41.577-71.987L246.423 23.985c18.467-32.009 64.72-31.951 83.154 0l239.94 416.028zM288 354c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z"></path></svg> Covered in more details in the `stars` portion of the workshop --- # Introduction to `sf` ## Manipulating attributes - The `sf` package is built to work well with the `tidyverse` - As such, you can essentially treat an `sf` object as you would a `data.frame` --- # Introduction to `sf` ## Manipulating attributes: new variable ```r hist(cdqs$Nombre.de.nicheurs.Number.of.Breeders) ``` <img src="_part1_files/figure-html/attr_hist-1.png" style="display: block; margin: auto;" /> --- # Introduction to `sf` ## Manipulating attributes: new variable ```r cdqs$var2 <- log(cdqs$Nombre.de.nicheurs.Number.of.Breeders+1) hist(cdqs$var2) ``` <img src="_part1_files/figure-html/attr_add-1.png" style="display: block; margin: auto;" /> --- # Introduction to `sf` ## Manipulating attributes: new variable ```r cdqs <- mutate(cdqs, var3 = log(Nombre.de.nicheurs.Number.of.Breeders+1)) hist(cdqs$var3) ``` <img src="_part1_files/figure-html/attr_add2-1.png" style="display: block; margin: auto;" /> --- # Introduction to `sf` ## Manipulating attributes: remove variable ```r cdqs$var2 <- NULL cdqs <- cdqs %>% select(-var3) colnames(cdqs) #> [1] "Numero.de.colonie.Colony.Number" #> [2] "Nom.de.colonie.Colony.name" #> [3] "Espece.Species.EN" #> [4] "Espece.Species.FR" #> [5] "Jour.et.mois.Day.and.Month" #> [6] "Annee.Year" #> [7] "Nombre.de.nicheurs.Number.of.Breeders" #> [8] "Methodes" #> [9] "Remarques" #> [10] "Reference" #> [11] "geometry" ``` --- # Introduction to `sf` ## A quick word on pipes The pipe operator `%>%` is a chained method that lets you pass an intermediate result to the next function. Thus, these two code chunks provide the same output: .pull-left[ .font90[ ```r # Example with pipe operator cdqs <- cdqs %>% mutate(var3 = log(Nombre.de.nicheurs.Number.of.Breeders+1)) %>% select(-var3) 'var3' %in% colnames(cdqs) #> [1] FALSE ``` ] ] .pull-right[ .font90[ ```r cdqs <- mutate(cdqs, var3 = log(Nombre.de.nicheurs.Number.of.Breeders+1)) cdqs <- select(cdqs, -var3) 'var3' %in% colnames(cdqs) #> [1] FALSE ``` ] ] .font70[`%>%` is similar to the `+` operator used by `ggplot2` and `tmap`. More on this later] .font70[See [here](https://www.datacamp.com/community/tutorials/pipe-r-tutorial) for a full description of the history and use of pipes. Note that in the upcoming release A [simple native pipe syntax](https://cran.r-project.org/doc/manuals/r-devel/NEWS.html), `|>`, will be included in the upcoming stable release. ] --- # Introduction to `sf` ## Manipulating attributes: subsets - Working with conditional statements (use `filter()` from the tidyverse) .font90[ ```r newcdqs <- cdqs[cdqs$Annee.Year > 2000, ] plot(st_geometry(cdqs), pch = 1, col = '#2ae9ee', cex = 1) plot(st_geometry(newcdqs), pch = 20, col = '#d76060', cex = .8, add = TRUE) ``` <img src="_part1_files/figure-html/attr_sub-1.png" style="display: block; margin: auto;" /> ] --- # Introduction to `sf` ## Manipulating attributes: joins ```r atlas #> Simple feature collection with 514 features and 3 fields #> Geometry type: MULTIPOLYGON #> Dimension: XY #> Bounding box: xmin: -1161755 ymin: -1437385 xmax: 2688245 ymax: 2921609 #> Projected CRS: Lambert_Azimuthal_Equal_Area #> First 10 features: #> id Shape_Length Shape_Area Shape #> 1 g1 346410.2 8660254035 MULTIPOLYGON (((-1161755 -1... #> 2 g2 346410.2 8660254035 MULTIPOLYGON (((38245.24 -1... #> 3 g3 346410.2 8660254040 MULTIPOLYGON (((-811754.8 -... #> 4 g4 346410.2 8660254040 MULTIPOLYGON (((-711754.8 -... #> 5 g5 346410.2 8660254040 MULTIPOLYGON (((-611754.8 -... #> 6 g6 346410.2 8660254040 MULTIPOLYGON (((-511754.8 -... #> 7 g7 346410.2 8660254040 MULTIPOLYGON (((-411754.8 -... #> 8 g8 346410.2 8660254040 MULTIPOLYGON (((-311754.8 -... #> 9 g9 346410.2 8660254040 MULTIPOLYGON (((88245.24 -1... #> 10 g10 346410.2 8660254040 MULTIPOLYGON (((188245.2 -1... ``` --- # Introduction to `sf` ## Manipulating attributes: joins .font90[ ```r join_data <- data.frame(id = paste0('g', sample(nrow(atlas), nrow(atlas))), value = runif(nrow(atlas))) join_data[1:10, ] #> id value #> 1 g422 0.5840200 #> 2 g113 0.8059136 #> 3 g455 0.3869628 #> 4 g488 0.7466073 #> 5 g172 0.5781390 #> 6 g35 0.2948536 #> 7 g360 0.1334845 #> 8 g48 0.6573725 #> 9 g152 0.4371089 #> 10 g128 0.8591686 ``` ] --- # Introduction to `sf` ## Manipulating attributes: joins .font90[ ```r newatlas <- left_join(atlas, join_data, by='id') ``` ] .pull-left[ .font80[ ``` #> id value Shape #> 1 g1 0.32478119 MULTIPOLYGON (((-1161755 -1... #> 2 g2 0.97764346 MULTIPOLYGON (((38245.24 -1... #> 3 g3 0.28147281 MULTIPOLYGON (((-811754.8 -... #> 4 g4 0.63816565 MULTIPOLYGON (((-711754.8 -... #> 5 g5 0.80484863 MULTIPOLYGON (((-611754.8 -... #> 6 g6 0.09784202 MULTIPOLYGON (((-511754.8 -... #> 7 g7 0.88443160 MULTIPOLYGON (((-411754.8 -... #> 8 g8 0.35912924 MULTIPOLYGON (((-311754.8 -... #> 9 g9 0.63048634 MULTIPOLYGON (((88245.24 -1... #> 10 g10 0.92105004 MULTIPOLYGON (((188245.2 -1... ``` ] ] .pull-right[ <img src="_part1_files/figure-html/unnamed-chunk-9-1.png" style="display: block; margin: auto;" /> ] --- # Introduction to `sf` ## Geometry manipulations: area .font90[ ```r atlas$area <- st_area(atlas) atlas #> Simple feature collection with 514 features and 4 fields #> Geometry type: MULTIPOLYGON #> Dimension: XY #> Bounding box: xmin: -1161755 ymin: -1437385 xmax: 2688245 ymax: 2921609 #> Projected CRS: Lambert_Azimuthal_Equal_Area #> First 10 features: #> id Shape_Length Shape_Area Shape area #> 1 g1 346410.2 8660254035 MULTIPOLYGON (((-1161755 -1... 8660254035 [m^2] #> 2 g2 346410.2 8660254035 MULTIPOLYGON (((38245.24 -1... 8660254035 [m^2] #> 3 g3 346410.2 8660254040 MULTIPOLYGON (((-811754.8 -... 8660254040 [m^2] #> 4 g4 346410.2 8660254040 MULTIPOLYGON (((-711754.8 -... 8660254040 [m^2] #> 5 g5 346410.2 8660254040 MULTIPOLYGON (((-611754.8 -... 8660254040 [m^2] #> 6 g6 346410.2 8660254040 MULTIPOLYGON (((-511754.8 -... 8660254040 [m^2] #> 7 g7 346410.2 8660254040 MULTIPOLYGON (((-411754.8 -... 8660254040 [m^2] #> 8 g8 346410.2 8660254040 MULTIPOLYGON (((-311754.8 -... 8660254040 [m^2] #> 9 g9 346410.2 8660254040 MULTIPOLYGON (((88245.24 -1... 8660254040 [m^2] #> 10 g10 346410.2 8660254040 MULTIPOLYGON (((188245.2 -1... 8660254040 [m^2] ``` ] --- # Introduction to `sf` ## Geometry manipulations: distance ```r st_distance(atlas[1:5, ], cdqs[1:5,]) #> Units: [m] #> [,1] [,2] [,3] [,4] [,5] #> [1,] 1491183.2 1491183.2 1491183.2 1491183.2 1496921.7 #> [2,] 955993.2 955993.2 955993.2 955993.2 956136.8 #> [3,] 1071538.0 1071538.0 1071538.0 1071538.0 1076845.8 #> [4,] 1017741.4 1017741.4 1017741.4 1017741.4 1022637.4 #> [5,] 968064.9 968064.9 968064.9 968064.9 972802.3 ``` --- # Introduction to `sf` ## Geometry manipulations: logical intersect ```r st_intersects(atlas[151:160, ], cdqs) #> Sparse geometry binary predicate list of length 10, where the predicate was `intersects' #> 1: (empty) #> 2: (empty) #> 3: (empty) #> 4: (empty) #> 5: (empty) #> 6: 1645, 1646, 1647, 1648, 1649, 1650, 1651, 1652, 1653, 1654, ... #> 7: (empty) #> 8: (empty) #> 9: 6947, 6948, 6949, 6950, 6951, 6952, 6953, 6954, 6955, 6956, ... #> 10: (empty) ``` .font90[<svg viewBox="0 0 576 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M569.517 440.013C587.975 472.007 564.806 512 527.94 512H48.054c-36.937 0-59.999-40.055-41.577-71.987L246.423 23.985c18.467-32.009 64.72-31.951 83.154 0l239.94 416.028zM288 354c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z"></path></svg> Projections should be the same when doing binary manipulations (*i.e.* multiple objects)] .font90[<svg viewBox="0 0 576 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M569.517 440.013C587.975 472.007 564.806 512 527.94 512H48.054c-36.937 0-59.999-40.055-41.577-71.987L246.423 23.985c18.467-32.009 64.72-31.951 83.154 0l239.94 416.028zM288 354c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z"></path></svg> Order matters. Returns the id of the 2nd object intersecting the 1st object] --- # Introduction to `sf` ## Geometry manipulations: buffer .pull-left[ .font80[ ```r newatlas <- st_buffer(atlas[1:10, ], 30000) plot(st_geometry(atlas[1:10, ])) plot(st_geometry(newatlas), border = 'red', add = TRUE) ``` <img src="_part1_files/figure-html/buffer1-1.png" style="display: block; margin: auto;" /> ] .font90[<svg viewBox="0 0 576 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M569.517 440.013C587.975 472.007 564.806 512 527.94 512H48.054c-36.937 0-59.999-40.055-41.577-71.987L246.423 23.985c18.467-32.009 64.72-31.951 83.154 0l239.94 416.028zM288 354c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z"></path></svg> Units = units of projection] ] .pull-left[ .font80[ ```r newatlas <- st_buffer(atlas[1:10, ], -30000) plot(st_geometry(atlas[1:10, ])) plot(st_geometry(newatlas), border = 'red', add = TRUE) ``` <img src="_part1_files/figure-html/buffer2-1.png" style="display: block; margin: auto;" /> ] ] --- # Introduction to `sf` ## Geometry manipulations: union (a.k.a. dissolve) ```r newatlas <- st_union(atlas) plot(newatlas) ``` <img src="_part1_files/figure-html/union-1.png" style="display: block; margin: auto;" /> --- # Introduction to `sf` ## Geometry manipulations: convex hull ```r cdqshull <- st_convex_hull(st_union(cdqs)) plot(st_geometry(cdqshull)) plot(st_geometry(cdqs), add = TRUE) ``` <img src="_part1_files/figure-html/convex-1.png" style="display: block; margin: auto;" /> .font90[<svg viewBox="0 0 576 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M569.517 440.013C587.975 472.007 564.806 512 527.94 512H48.054c-36.937 0-59.999-40.055-41.577-71.987L246.423 23.985c18.467-32.009 64.72-31.951 83.154 0l239.94 416.028zM288 354c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z"></path></svg> Geometries should be a single feature, hence the use of `union()`] --- # Introduction to `sf` ## Geometry manipulations: zonal intersection ```r cdqs_atlas <- st_intersection(newatlas, cdqshull) plot(st_geometry(newatlas)) plot(st_geometry(cdqshull), add = TRUE) plot(st_geometry(cdqs_atlas), col = 'red', add = TRUE) ``` <img src="_part1_files/figure-html/intersection-1.png" style="display: block; margin: auto;" /> --- # Introduction to `sf` ## Geometry manipulations: difference .pull-left[ .font80[ ```r cdqs_atlas <- st_difference(newatlas, cdqshull) plot(st_geometry(newatlas)) plot(st_geometry(cdqshull), add = TRUE) plot(st_geometry(cdqs_atlas), col = 'red', add = TRUE) ``` <img src="_part1_files/figure-html/difference-1.png" style="display: block; margin: auto;" /> ] ] .pull-right[ .font80[ ```r cdqs_atlas <- st_difference(cdqshull, newatlas) plot(st_geometry(newatlas)) plot(st_geometry(cdqshull), add = TRUE) plot(st_geometry(cdqs_atlas), col = 'red', add = TRUE) ``` <img src="_part1_files/figure-html/difference2-1.png" style="display: block; margin: auto;" /> ] ] --- # Introduction to `sf` ## Geometry manipulations: spatial join .pull-left[ .font80[ ```r atlasJ <- st_join(atlas, cdqs) plot(atlasJ[,'Nombre.de.nicheurs.Number.of.Breeders']) ``` <img src="_part1_files/figure-html/spatjoin1-1.png" style="display: block; margin: auto;" /> ] ] .pull-right[ .font80[ ```r cdqsJ <- st_join(cdqs, atlas) plot(cdqsJ[,'id']) ``` <img src="_part1_files/figure-html/spatjoin2-1.png" style="display: block; margin: auto;" /> ] ] .font90[<svg viewBox="0 0 576 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M569.517 440.013C587.975 472.007 564.806 512 527.94 512H48.054c-36.937 0-59.999-40.055-41.577-71.987L246.423 23.985c18.467-32.009 64.72-31.951 83.154 0l239.94 416.028zM288 354c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z"></path></svg> use the `join` argument to select any function compatible to `st_intersects`] --- # Introduction to `sf` ## Geometry manipulations R Studio [Cheat sheets](https://github.com/rstudio/cheatsheets/blob/master/sf.pdf) for `sf` .pull-left[ <center><img src="img/sf1.png" width="100%"></img></center> ] .pull-right[ <center><img src="img/sf2.png" width="100%"></img></center> ] - This [vignette](https://cran.r-project.org/web/packages/sf/vignettes/sf3.html) for an overview of geometry manipulations using `sf` --- class: inverse, center, middle # Questions? ![:custom_hr]() ## .font150[<svg viewBox="0 0 512 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 448c-110.532 0-200-89.431-200-200 0-110.495 89.472-200 200-200 110.491 0 200 89.471 200 200 0 110.53-89.431 200-200 200zm107.244-255.2c0 67.052-72.421 68.084-72.421 92.863V300c0 6.627-5.373 12-12 12h-45.647c-6.627 0-12-5.373-12-12v-8.659c0-35.745 27.1-50.034 47.579-61.516 17.561-9.845 28.324-16.541 28.324-29.579 0-17.246-21.999-28.693-39.784-28.693-23.189 0-33.894 10.977-48.942 29.969-4.057 5.12-11.46 6.071-16.666 2.124l-27.824-21.098c-5.107-3.872-6.251-11.066-2.644-16.363C184.846 131.491 214.94 112 261.794 112c49.071 0 101.45 38.304 101.45 88.8zM298 368c0 23.159-18.841 42-42 42s-42-18.841-42-42 18.841-42 42-42 42 18.841 42 42z"></path></svg>] --- class: inverse, center, middle # <svg viewBox="0 0 512 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> Exercise ![:custom_hr]() ## <svg viewBox="0 0 512 512" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8zm0 448c-110.5 0-200-89.5-200-200S145.5 56 256 56s200 89.5 200 200-89.5 200-200 200zm61.8-104.4l-84.9-61.7c-3.1-2.3-4.9-5.9-4.9-9.7V116c0-6.6 5.4-12 12-12h32c6.6 0 12 5.4 12 12v141.7l66.8 48.6c5.4 3.9 6.5 11.4 2.6 16.8L334.6 349c-3.9 5.3-11.4 6.5-16.8 2.6z"></path></svg> ~40min --- # Exercice ## Steps 1. Use `rgovcan` to download the atlas data `ID = "f612e2b4-5c67-46dc-9a84-1154c649ab4e"` 2. Load ‘data/dat.csv’ to access the data associated with the atlas 3. Create two objects containing data for Atlantic Puffin and Razorbill 4. Join the data for the Atlantic Puffin and Razorbill with the atlas grid 5. Create two convex hulls: - Atlantic Puffin with densities > 6 birds / `\(km^2\)` - Razorbill with densities > 1 birds / `\(km^2\)` 6. Do a zonal intersection between the convex hulls 7. Measure the overlapping area between the two convex hulls --- # Exercice ## Solution .font100[ ] --- # Exercice ## Solution .font100[ ]