diff --git a/04-raster_data.qmd b/04-raster_data.qmd new file mode 100644 index 0000000000000000000000000000000000000000..ce11f4e0e1272c098afb88151148598319e7445f --- /dev/null +++ b/04-raster_data.qmd @@ -0,0 +1,653 @@ +# Work with Raster Data + +This chapter is largely inspired by two presentation; @MmadelinSIGR and @JNowosadSIGR; carried out as part of the [SIGR2021](https://sigr2021.github.io/site/index.html) thematic school. + +## Format of objects `SpatRaster` + +The package `terra` [@terra] allows to handle vector and raster data. To manipulate this spatial data, `terra` store it in object of type `SpatVector` and `SpatRaster`. In this chapter, we focus on the manipulation of raster data (`SpatRaster`) from functions offered by this package. + +An object `SpatRaster` allows to handle vector and raster data, in one or more layers (variables). This object also stores a number of fundamental parameters that describe it (number of columns, rows, spatial extent, coordinate reference system, etc.). + +![Source : [@RasterCheatSheet]](img/raster.png){fig-align="center" width="350"} + +## Importing and exporting data + +The package `terra` allows importing and exporting raster files. It is based on the [GDAL](https://gdal.org/) library which makes it possible to read and process a very large number of geographic image formats. + +```{r terra, message=FALSE} +library(terra) +``` + +The function `rast()` allows you to create and/or import raster data. The following lines import the raster file **elevation.tif** ([*Tagged Image File Format*](https://fr.wikipedia.org/wiki/Tagged_Image_File_Format)) into an object of type `SpatRaster` (default). + +```{r import_raster, message=FALSE} + +elevation <- rast("data_cambodia/elevation.tif") +elevation +``` + +Modifying the name of the stored variable (altitude). + +```{r names_raster, message=FALSE} + +names(elevation) <- "Altitude" + +``` + +The function `writeRaster()` allow you to save an object `SpatRaster` on your machine, in the format of your choice. + +```{r export_raster, eval=FALSE} +writeRaster(x = elevation, filename = "data_cambodia/new_elevation.tif") +``` + +## Displaying a SpatRaster object + +The function `plot()` is use to display an object `SpatRaster`. + +```{r affichage_1_raster, eval=TRUE, fig.align='center', fig.width=6} + +plot(elevation) + +``` + +A raster always contains numerical data, but it can be both quantitative data and numerically coded qualitative (categorical) data (ex: type of land cover). + +Specify the type of data stored with the augment `type` (`type = "continuous"` default), to display them correctly. + +Import and display of raster containing categorical data: Phnom Penh Land Cover 2019 (land cover types) with a resolution of 1.5 meters: + +```{r import_raster_2, message=FALSE} + +lulc_2019 <- rast("data_cambodia/lulc_2019.tif") #Import Phnom Penh landcover 2019, landcover types + +``` + +The landcover data was produced from SPOT7 satellite image with 1.5 meter spatial resolution. An extraction centered on the municipality of Phnom Penh was then carried out. + +```{r affichage_raster_2, fig.align='center', fig.width=6} + +plot(lulc_2019, type = "classes") + +``` + +To display the actual tiles of landcover types, as well as the official colors of Phnom Penh Landcover nomenclature (available [here](https://www.statistiques.developpement-durable.gouv.fr/corine-land-cover-0)), you can proceed as follows. + +```{r affichage_raster_3, eval=TRUE, fig.align='center', fig.width = 12} + +class_name <- c( + "Roads", + "Built-up areas", + "Water Bodies and rivers", + "Wetlands", + "Dry bare area", + "Bare crop fields", + "Low vegetation areas", + "High vegetation areas", + "Forested areas") + +class_color <- c("#070401", "#c84639", "#1398eb","#8bc2c2", + "#dc7b34", "#a6bd5f","#e8e8e8", "#4fb040", "#35741f") +plot(lulc_2019, + type = "classes", + levels = class_name, + col = class_color, + plg = list(cex = 0.7), + mar = c(3.1, 3.1, 2.1, 10) #The margin are (bottom, left, top, right) respectively + ) +``` + +## Change to the study area + +### (Re)projections {#reprojections} + +To modify the projection system of a raster, use the function `project()`. It is then necessary to indicate the method for estimating the new cell values. + +{fig-align="center"} + +Four interpolation methods are available: + +- ***near*** : nearest neighbor, fast and default method for qualitative data;\ +- ***bilinear*** : bilinear interpolation. Default method for quantitative data;\ +- ***cubic*** : cubic interpolation;\ +- ***cubicspline*** : cubic spline interpolation. + +```{r reproj_raster, eval=TRUE, fig.align='center', fig.width=8} +# Re-project data + +elevation_utm = project(x = elevation, y = "EPSG:32648", method = "bilinear") #from WGS84(EPSG:4326) to UTM zone48N(EPSG:32648) +lulc_2019_utm = project(x = lulc_2019, y = "EPSG:32648", method = "near") #keep original projection: UTM zone48N(EPSG:32648) +``` + +```{r reproj_raster_2, eval=TRUE, echo=FALSE, fig.align='center', fig.width = 7} +par(mfrow=c(1,2)) +plot(elevation_utm, main="Projected elevation raster in UTM 48 N" ) +plot(lulc_2019_utm, type ="classes", main="CLC 2018 projected in UTM 48 N") +``` + +### Crop {#crop} + +Clipping a raster to the extent of another object `SpatVector` or `SpatRaster` is achievable with the `crop()`. + +::: {layout-ncol="2"} + + + + +Source : [@RasterCheatSheet] +::: + +Import vector data of (municipal divisions) using function `vect`. This data will be stored in an `SpatVector` object. + +```{r crop_raster, eval=TRUE} + +district <- vect("data_cambodia/cambodia.gpkg", layer="district") + +``` + +Extraction of district boundaries of Thma Bang district (ADM2_PCODE : KH0907). + +```{r crop_raster_1, eval=TRUE} +thma_bang <- subset(district, district$ADM2_PCODE == "KH0907") +``` + +Using the function `crop()`, Both data layers must be in the same projection. + +```{r crop_raster_3, eval=TRUE, fig.align='center', fig.width=6} + +crop_thma_bang <- crop(elevation_utm, thma_bang) + +plot(crop_thma_bang) +plot(thma_bang, add=TRUE) +``` + +### Mask {#mask} + +To display only the values of a raster contained in a polygon, use the function `mask()`. + +![Source : [@RasterCheatSheet]](img/mask.png){fig-align="center" width="350"} + +Creation of a mask on the **crop_thma_bang** raster to the municipal limits (polygon) of **Thma Bang district**. + +```{r mask_raster, eval=TRUE, fig.align='center', fig.width=6} + +mask_thma_bang <- mask(crop_thma_bang, thma_bang) + +plot(mask_thma_bang) +plot(thma_bang, add = TRUE) +``` + +### Aggregation and disaggregation + +Resampling a raster to a different resolution is done in two steps. + +::: {layout-ncol="3"} + + + + + + +Source : [@RasterCheatSheet] +::: + +Display the resolution of a raster with the function `res()`. + +```{r agr_raster, eval=TRUE} + +res(elevation_utm) #check cell size + +``` + +Create a grid with the same extent, then decrease the spatial resolution (larger cells). + +```{r agr_raster_1, eval=TRUE} + +elevation_LowerGrid <- elevation_utm +# elevation_HigherGrid <- elevation_utm + +res(elevation_LowerGrid) <- 1000 #cells size = 1000 meter +# res(elevation_HigherGrid) <- 10 #cells size = 10 meter + +elevation_LowerGrid +``` + +The function `resample()` allows to resample the atarting values in the new spatial resolution. Several resampling methods are available (cf. [partie 5.4.1](#reprojections)). + +```{r agr_raster_2, eval=TRUE, fig.align='center', fig.width=6} + +elevation_LowerGrid <- resample(elevation_utm, + elevation_LowerGrid, + method = "bilinear") + +plot(elevation_LowerGrid, + main="Cell size = 1000m\nBilinear resampling method") +``` + +### Raster fusion + +Merge multiple objects `SpatRaster` into one with `merge()` or `mosaic()`. + +{fig-align="center"} + +After cutting the elevation raster by the municipal boundary of Thma Bang district (cf [partie 5.4.2](#crop)), we do the same thing for the neighboring municipality of Phnum Kravanh district. + +```{r merge_raster, eval=TRUE} + +phnum_kravanh <- subset(district, district$ADM2_PCODE == "KH1504") # Extraction of the municipal boundaries of Phnum Kravanh district + +crop_phnum_kravanh <- crop(elevation_utm, phnum_kravanh) #clipping the elevation raster according to district boundaries +``` + +The **crop_thma_bang** and **crop_phnum_kravanh** elevation raster overlap spatially: + +```{r merge_raster_1, eval=TRUE, echo=FALSE, fig.align='center',fig.width=8} + +par(mfrow=c(1,2), mar=c(0,0,0,0)) +plot(crop_thma_bang, main="Crop Thma Bang") +plot(thma_bang, add=TRUE) +plot(phnum_kravanh, add=TRUE) +plot(crop_phnum_kravanh, main="Crop Phnum Kravanh") +plot(phnum_kravanh, add=TRUE) +plot(thma_bang, add=TRUE) + +``` + +The difference between the functions `merge()` and `mosiac()` relates to values of the overlapping cells. The function `mosaic()` calculate the average value while `merge()` holding the value of the object `SpaRaster` called n the function. + +```{r merge_raster_2, eval=TRUE, fig.align='center', fig.width=6} + +#in this example, merge() and mosaic() give the same result +merge_raster <- merge(crop_thma_bang, crop_phnum_kravanh) +mosaic_raster <- mosaic(crop_thma_bang, crop_phnum_kravanh) + +plot(merge_raster) +# plot(mosaic_raster) +``` + +### Segregate + +Decompose a raster by value (or modality) into different rasterlayers with the function `segregate`. + +```{r segregate, eval=TRUE, fig.align='center', fig.width=6} +lulc_2019_class <- segregate(lulc_2019, keep=TRUE, other=NA) #creating a raster layer by modality +plot(lulc_2019_class) + +``` + +## Map Algebra + +Map algebra is classified into four groups of operation [@Tomlin_1990]: + +- ***Local*** : operation by cell, on one or more layers;\ +- ***Focal*** : neighborhood operation (surrounding cells);\ +- ***Zonal*** : to summarize the matrix values for certain zones, usually irregular; +- ***Global*** : to summarize the matrix values of one or more matrices. + +![Source : [@XingongLi2009]](img/lo_fo_zo_glo.png){fig-align="center"} + +### Local operations + +![Source : [@JMennis2015]](img/op_local_2.png){fig-align="center" width="452"} + +#### Value replacement + +```{r op_local_0, eval=FALSE} + +elevation_utm[elevation_utm[[1]]== -9999] <- NA #replaces -9999 values with NA + +elevation_utm[elevation_utm < 1500] <- NA #Replace values < 1500 with NA +``` + +```{r op_local_00, eval=TRUE} + +elevation_utm[is.na(elevation_utm)] <- 0 #replace NA values with 0 + +``` + +#### Operation on each cell + +```{r op_local_1, eval=TRUE} + +elevation_1000 <- elevation_utm + 1000 # Adding 1000 to the value of each cell + +elevation_median <- elevation_utm - global(elevation_utm, median)[[1]] # Removed median elevation to each cell's value + +``` + +```{r op_local_2, eval=TRUE, echo=FALSE, fig.align='center', fig.width=8} + +par(mfrow=c(1,2), mar=c(0,0,0,0)) +plot(elevation_1000, main="elevation_1000\nelevation + 1000") +plot(elevation_median, main="elevation_median\nelevation - median value") + +``` + +#### Reclassification + +Reclassifying raster values can be used to discretize quantitative data as well as to categorize qualitative categories. + +```{r reclass_2, eval=TRUE} + +reclassif <- matrix(c(1, 2, 1, + 2, 4, 2, + 4, 6, 3, + 6, 9, 4), + ncol = 3, byrow = TRUE) +``` + +Values between 1 and 2 will be replaced by the value 1.\ +Values between 3 and 4 will be replaced by the value 2.\ +Values between 5 and 6 will be replaced by the value 3. Values between 7 and 9 will be replaced by the value 4. + +... + +```{r reclass_3, eval=TRUE} +reclassif +``` + +The function `classify()` allows you to perform the reclassification. + +```{r reclass_4, eval=TRUE} + +lulc_2019_reclass <- classify(lulc_2019, rcl = reclassif) +plot(lulc_2019_reclass, type ="classes") + +``` + +Display with the official titles and colors of the different categories. + +```{r reclass_6, eval=TRUE, fig.align='center', out.width="80%"} + +plot(lulc_2019_reclass, + type ="classes", + levels=c("Urban areas", + "Water body", + "Bare areas", + "Vegetation areas"), + col=c("#E6004D", + "#00BFFF", + "#D3D3D3", + "#32CD32"), + mar=c(3, 1.5, 1, 11)) + +``` + +#### Operation on several layers (ex: NDVI) + +It is possible to calculate the value of a cell from its values stored in different layers of an object `SpatRaster`. + +Perhaps the most common example is the calculation of the [Normalized Vegetation Index (*NDVI*)](http://www.trameverteetbleue.fr/outils-methodes/donnees-mobilisables/indice-vegetation-modis). For each cell, a value is calculated from two layers of raster from a multispectral satellite image. + +```{r NDVI, eval=TRUE} +# Import d'une image satellite multispectrale +sentinel2a <- rast("data_cambodia/Sentinel2A.tif") +``` + +This multispectral satellite image (10m resolution) dated 25/02/2020, was produced by Sentinel-2 satellite and was retrieved from [plateforme Copernicus Open Access Hub](https://scihub.copernicus.eu/dhus/#/home). An extraction of Red and near infrared spectral bands, centered on the Phnom Penh city, was then carried out. + +```{r NDVI_1, eval=TRUE, fig.align='center', fig.width=7} +plot(sentinel2a) +``` + +To lighten the code, we assign the two matrix layers in different `SpatRaster` objects. + +```{r NDVI_2, eval=TRUE} + +B04_Red <- sentinel2a[[1]] #spectral band Red + +B08_NIR <-sentinel2a[[2]] #spectral band near infrared +``` + +From these two raster objects , we can calculate the normalized vegetation index: + +$${NDVI}=\frac{\mathrm{NIR} - \mathrm{Red}} {\mathrm{NIR} + \mathrm{Red}}$$ + +```{r NDVI_3, eval=TRUE, fig.align='center', out.width="90%"} +raster_NDVI <- (B08_NIR - B04_Red ) / (B08_NIR + B04_Red ) + +plot(raster_NDVI) +``` + +The higher the values (close to 1), the denser the vegetation. + +### Focal operations + +![Source : [@JMennis2015]](img/op_focal_2.png){fig-align="center" width="415"} + +Focal analysis conisders a cell plus its direct neighbors in contiguous and symmetrical (neighborhood operations). Most often, the value of the output cell is the result of a block of 3 x 3 (odd number) input cells. + +The first step is to build a matrix that determines the block of cells that will be considered around each cell. + +```{r op_focal_1, eval=TRUE} +# 5 x 5 matrix, where each cell has the same weight +mon_focal <- matrix(1, nrow = 5, ncol = 5) +mon_focal +``` + +The function `focal()` Then allows you to perform the desired analysis. For example: calculating the average of the values of all contiguous cells, for each cell in the raster. + +```{r op_focal_3, eval=TRUE} +elevation_LowerGrid_mean <- focal(elevation_LowerGrid, + w = mon_focal, + fun = mean) +``` + +```{r op_focal_5, eval=TRUE, echo=FALSE, fig.align='center', fig.width=8} + +par(mfrow=c(1,2), mar=c(0,0,0,0)) +plot(elevation_LowerGrid, main="Elevation_LowerGrid\n(starting raster)") +plot(elevation_LowerGrid_mean, main="Elevation_LowerGrid_mean\n(focal result 5 x 5)") + +``` + +#### Focal operations for elevation rasters + +The function `terrain()` allows to perform focal analyzes specific to elevation rasters. Six operations are available: + +- ***slope*** = calculation of the slope or degree of inclination of the surface;\ +- ***aspect*** = calculate slope orientation;\ +- ***roughness*** = calculate of the variability or irregularity of the elevation;\ +- ***TPI*** = calculation of the index of topgraphic positions;\ +- ***TRI*** = elevation variability index calculation;\ +- ***flowdir*** = calculation of the water flow direction. + +Example with calculation of slopes(*slope*). + +```{r op_focal_6, eval=TRUE, fig.align='center', fig.width=6} + +#slope calculation +slope <- terrain(elevation_utm, "slope", + neighbors = 8, #8 (or 4) cells around taken into account + unit = "degrees") #Output unit + +plot(slope) #Inclination of the slopes, in degrees +``` + +### Global operations + +{fig-align="center" width="297"} + +Global operation are used to summarize the matrix values of one or more matrices. + +```{r op_global_1, eval=TRUE} + +global(elevation_utm, fun = "mean") #average values +``` + +```{r op_global_2, eval=TRUE} + +global(elevation_utm, fun = "sd") #standard deviation +``` + +```{r op_global_3, eval=TRUE} + +freq(lulc_2019_reclass) #frequency +table(lulc_2019_reclass[]) #contingency table +``` + +Statistical representations that summarize matrix information. + +```{r op_global_4, eval=TRUE, fig.align='center', fig.width=6} + +hist(elevation_utm) #histogram +density(elevation_utm) #density + +``` + +### Zonal operation + +![Source : [@JMennis2015]](img/op_zonal_2.png){fig-align="center" width="342"} + +The zonal operation make it possible to summarize the matrix values of certain zones (group of contiguous cells in space or in value). + +#### Zonal operation on an extraction + +**All global operations can be performed on an extraction of cells resulting from the functions `crop()`, `mask()`, `segregate()`...** + +Example: average elevation for the city of Thma Bang district (cf [partie 5.4.3](#mask)). + +```{r op_global_6, eval=TRUE} +# Average value of the "mask" raster over Thma Bang district +global(mask_thma_bang, fun = "mean", na.rm=TRUE) +``` + +#### Zonal operation from a vector layer + +The function `extract()` allows you to extract and manipulate the values of cells that intersect vector data. + +Example from polygons: + +```{r op_zonal_poly, eval=TRUE} +# Average elevation for each polygon (district)? +elevation_by_dist <- extract(elevation_LowerGrid, district, fun=mean) +head(elevation_by_dist, 10) +``` + +#### Zonal operation from raster + +Zonal operation can be performed by area bounded by the categorical values of a second raster. For this, the two raster must have exaclty the same extent and the same resolution. + +```{r op_zonal_1, eval=TRUE} + +#create a second raster with same resolution and extent as "elevation_clip" +elevation_clip <- rast("data_cambodia/elevation_clip.tif") +elevation_clip_utm <- project(x = elevation_clip, y = "EPSG:32648", method = "bilinear") +second_raster_CLC <- rast(elevation_clip_utm) + +#resampling of lulc_2019_reclass +second_raster_CLC <- resample(lulc_2019_reclass, second_raster_CLC, method = "near") + +#added a variable name for the second raster +names(second_raster_CLC) <- "lulc_2019_reclass_resample" + +``` + +```{r op_zonal_2, eval=TRUE, echo=FALSE, fig.align='center', fig.width=8} +par(mfrow=c(1,2), mar=c(0,0,0,0)) +plot(elevation_clip_utm, main="elevation_clip_utm") +plot(second_raster_CLC, type = "classes", main="lulc_2019_reclass_resample") +``` + +Calculation of the average elevation for the different areas of the second raster. + +```{r op_zonal_3, eval=TRUE} +#average elevation for each area of the "second_raster" +zonal(elevation_clip_utm, second_raster_CLC , "mean", na.rm=TRUE) + +``` + +## Transformation et conversion + +### Rasterisation + +Convert polygons to raster format. + +```{r Raster_vec1, eval=TRUE} + +chamkarmon = subset(district, district$ADM2_PCODE =="KH1201") +raster_district <- rasterize(x = chamkarmon, y = elevation_clip_utm) + +``` + +```{r Raster_vec11, eval=TRUE, fig.align='center', fig.width=6} + +plot(raster_district) + +``` + +Convert points to raster format + +```{r Raster_vec2, eval=TRUE} + +#rasterization of the centroids of the municipalities +raster_dist_centroid <- rasterize(x = centroids(district), + y = elevation_clip_utm, fun=sum) +plot(raster_dist_centroid, col = "red") +plot(district, add =TRUE) +``` + +Convert lines in raster format + +```{r Raster_vec3, eval=TRUE} + +#rasterization of municipal boundaries +raster_dist_line <- rasterize(x = as.lines(district), y = elevation_clip_utm, fun=sum) +``` + +```{r Raster_vec33, eval=TRUE} +plot(raster_dist_line) + +``` + +### Vectorisation + +Transform a raster to vector polygons. + +```{r Raster_vec4, eval=TRUE} + +polygon_elevation <- as.polygons(elevation_clip_utm) +``` + +```{r Raster_vec44, eval=TRUE} +plot(polygon_elevation, y = 1, border="white") +``` + +Transform a raster to vector points. + +```{r Raster_vec5, eval=TRUE} +points_elevation <- as.points(elevation_clip_utm) +``` + +```{r Raster_vec55, eval=TRUE} +plot(points_elevation, y = 1, cex = 0.3) +``` + +Transform a raster into vector lines. + +```{r Raster_vec6, eval=TRUE} +lines_elevation <- as.lines(elevation_clip_utm) +``` + +```{r Raster_vec66, eval=TRUE} +plot(lines_elevation) +``` + +### terra, raster, sf, stars... + +Reference packages for manipulating spatial data all rely o their own object class. It is sometimes necessary to convert these objects from one class to another class to take advance of all the features offered by these different packages. + +Conversion functions for raster data: + +| FROM/TO | raster | terra | stars | +|---------|----------|--------------------------|---------------| +| raster | | rast() | st_as_stars() | +| terra | raster() | | st_as_stars() | +| stars | raster() | as(x, 'Raster') + rast() | | + +Conversion functions for vector data: + +| FROM/TO | sf | sp | terra | +|---------|------------|------------------|--------| +| sf | | as(x, 'Spatial') | vect() | +| sp | st_as_sf() | | vect() | +| terra | st_as_sf() | as(x, 'Spatial') | | diff --git a/_quarto.yml b/_quarto.yml index 597acb70df25a203b4326cfa4d8d8796390c6c94..96637df6b85abb18e95e55c0303a9d7ac2105911 100644 --- a/_quarto.yml +++ b/_quarto.yml @@ -15,6 +15,7 @@ book: - 01-introduction.qmd - 02-data_acquisition.qmd - 03-vector_data.qmd + - 04-raster_data.qmd - 05-mapping_with_r.qmd - references.qmd diff --git a/public/04-raster_data.html b/public/04-raster_data.html new file mode 100644 index 0000000000000000000000000000000000000000..6cd00dea9d0fd9aec04f6e1db5df3e3b8ee42b39 --- /dev/null +++ b/public/04-raster_data.html @@ -0,0 +1,1256 @@ +<!DOCTYPE html> +<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head> + +<meta charset="utf-8"> +<meta name="generator" content="quarto-0.9.617"> + +<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> + + +<title>Mapping and spatial analyses in R for One Health studies - 4 Work with Raster Data</title> +<style> +code{white-space: pre-wrap;} +span.smallcaps{font-variant: small-caps;} +span.underline{text-decoration: underline;} +div.column{display: inline-block; vertical-align: top; width: 50%;} +div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} +ul.task-list{list-style: none;} +pre > code.sourceCode { white-space: pre; position: relative; } +pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } +pre > code.sourceCode > span:empty { height: 1.2em; } +.sourceCode { overflow: visible; } +code.sourceCode > span { color: inherit; text-decoration: inherit; } +div.sourceCode { margin: 1em 0; } +pre.sourceCode { margin: 0; } +@media screen { +div.sourceCode { overflow: auto; } +} +@media print { +pre > code.sourceCode { white-space: pre-wrap; } +pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; } +} +pre.numberSource code + { counter-reset: source-line 0; } +pre.numberSource code > span + { position: relative; left: -4em; counter-increment: source-line; } +pre.numberSource code > span > a:first-child::before + { content: counter(source-line); + position: relative; left: -1em; text-align: right; vertical-align: baseline; + border: none; display: inline-block; + -webkit-touch-callout: none; -webkit-user-select: none; + -khtml-user-select: none; -moz-user-select: none; + -ms-user-select: none; user-select: none; + padding: 0 4px; width: 4em; + color: #aaaaaa; + } +pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; } +div.sourceCode + { } +@media screen { +pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } +} +code span.al { color: #ff0000; font-weight: bold; } /* Alert */ +code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */ +code span.at { color: #7d9029; } /* Attribute */ +code span.bn { color: #40a070; } /* BaseN */ +code span.bu { } /* BuiltIn */ +code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */ +code span.ch { color: #4070a0; } /* Char */ +code span.cn { color: #880000; } /* Constant */ +code span.co { color: #60a0b0; font-style: italic; } /* Comment */ +code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */ +code span.do { color: #ba2121; font-style: italic; } /* Documentation */ +code span.dt { color: #902000; } /* DataType */ +code span.dv { color: #40a070; } /* DecVal */ +code span.er { color: #ff0000; font-weight: bold; } /* Error */ +code span.ex { } /* Extension */ +code span.fl { color: #40a070; } /* Float */ +code span.fu { color: #06287e; } /* Function */ +code span.im { } /* Import */ +code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */ +code span.kw { color: #007020; font-weight: bold; } /* Keyword */ +code span.op { color: #666666; } /* Operator */ +code span.ot { color: #007020; } /* Other */ +code span.pp { color: #bc7a00; } /* Preprocessor */ +code span.sc { color: #4070a0; } /* SpecialChar */ +code span.ss { color: #bb6688; } /* SpecialString */ +code span.st { color: #4070a0; } /* String */ +code span.va { color: #19177c; } /* Variable */ +code span.vs { color: #4070a0; } /* VerbatimString */ +code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */ +div.csl-bib-body { } +div.csl-entry { + clear: both; +} +.hanging div.csl-entry { + margin-left:2em; + text-indent:-2em; +} +div.csl-left-margin { + min-width:2em; + float:left; +} +div.csl-right-inline { + margin-left:2em; + padding-left:1em; +} +div.csl-indent { + margin-left: 2em; +} +</style> + + +<script src="site_libs/quarto-nav/quarto-nav.js"></script> +<script src="site_libs/quarto-nav/headroom.min.js"></script> +<script src="site_libs/clipboard/clipboard.min.js"></script> +<script src="site_libs/quarto-search/autocomplete.umd.js"></script> +<script src="site_libs/quarto-search/fuse.min.js"></script> +<script src="site_libs/quarto-search/quarto-search.js"></script> +<meta name="quarto:offset" content="./"> +<link href="./05-mapping_with_r.html" rel="next"> +<link href="./03-vector_data.html" rel="prev"> +<script src="site_libs/quarto-html/quarto.js"></script> +<script src="site_libs/quarto-html/popper.min.js"></script> +<script src="site_libs/quarto-html/tippy.umd.min.js"></script> +<script src="site_libs/quarto-html/anchor.min.js"></script> +<link href="site_libs/quarto-html/tippy.css" rel="stylesheet"> +<link href="site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles"> +<script src="site_libs/bootstrap/bootstrap.min.js"></script> +<link href="site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet"> +<link href="site_libs/bootstrap/bootstrap.min.css" rel="stylesheet"> +<script id="quarto-search-options" type="application/json">{ + "location": "sidebar", + "copy-button": false, + "collapse-after": 3, + "panel-placement": "start", + "type": "textbox", + "limit": 20, + "language": { + "search-no-results-text": "No results", + "search-matching-documents-text": "matching documents", + "search-copy-link-title": "Copy link to search", + "search-hide-matches-text": "Hide additional matches", + "search-more-match-text": "more match in this document", + "search-more-matches-text": "more matches in this document", + "search-clear-button-title": "Clear", + "search-detached-cancel-button-title": "Cancel", + "search-submit-button-title": "Submit" + } +}</script> + + <script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script> + +<link rel="stylesheet" href="styles.css"> +</head> + +<body class="nav-sidebar floating"> + +<div id="quarto-search-results"></div> + <header id="quarto-header" class="headroom fixed-top"> + <nav class="quarto-secondary-nav" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }"> + <div class="container-fluid d-flex justify-content-between"> + <h1 class="quarto-secondary-nav-title"><span class="chapter-number">4</span> <span class="chapter-title">Work with Raster Data</span></h1> + <button type="button" class="quarto-btn-toggle btn" aria-label="Show secondary navigation"> + <i class="bi bi-chevron-right"></i> + </button> + </div> + </nav> +</header> +<!-- content --> +<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article"> +<!-- sidebar --> + <nav id="quarto-sidebar" class="sidebar collapse sidebar-navigation floating overflow-auto"> + <div class="pt-lg-2 mt-2 text-left sidebar-header"> + <div class="sidebar-title mb-0 py-0"> + <a href="./">Mapping and spatial analyses in R for One Health studies</a> + <div class="sidebar-tools-main"> + <a href="https://forge.ird.fr/espace-dev/personnels/longour/rspatial-for-onehealth" title="Source Code" class="sidebar-tool px-1"><i class="bi bi-git"></i></a> +</div> + </div> + </div> + <div class="mt-2 flex-shrink-0 align-items-center"> + <div class="sidebar-search"> + <div id="quarto-search" class="" title="Search"></div> + </div> + </div> + <div class="sidebar-menu-container"> + <ul class="list-unstyled mt-1"> + <li class="sidebar-item"> + <div class="sidebar-item-container"> + <a href="./index.html" class="sidebar-item-text sidebar-link">Preface</a> + </div> +</li> + <li class="sidebar-item"> + <div class="sidebar-item-container"> + <a href="./01-introduction.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">1</span> <span class="chapter-title">Introduction</span></a> + </div> +</li> + <li class="sidebar-item"> + <div class="sidebar-item-container"> + <a href="./02-data_acquisition.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">2</span> <span class="chapter-title">Data Acquisition</span></a> + </div> +</li> + <li class="sidebar-item"> + <div class="sidebar-item-container"> + <a href="./03-vector_data.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">3</span> <span class="chapter-title">Vector Data</span></a> + </div> +</li> + <li class="sidebar-item"> + <div class="sidebar-item-container"> + <a href="./04-raster_data.html" class="sidebar-item-text sidebar-link active"><span class="chapter-number">4</span> <span class="chapter-title">Work with Raster Data</span></a> + </div> +</li> + <li class="sidebar-item"> + <div class="sidebar-item-container"> + <a href="./05-mapping_with_r.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">5</span> <span class="chapter-title">Mapping With R</span></a> + </div> +</li> + <li class="sidebar-item"> + <div class="sidebar-item-container"> + <a href="./references.html" class="sidebar-item-text sidebar-link">References</a> + </div> +</li> + </ul> + </div> +</nav> +<!-- margin-sidebar --> + <div id="quarto-margin-sidebar" class="sidebar margin-sidebar"> + <nav id="TOC" role="doc-toc"> + <h2 id="toc-title">Table of contents</h2> + + <ul> + <li><a href="#format-of-objects-spatraster" id="toc-format-of-objects-spatraster" class="nav-link active" data-scroll-target="#format-of-objects-spatraster"> <span class="header-section-number">4.1</span> Format of objects <code>SpatRaster</code></a></li> + <li><a href="#importing-and-exporting-data" id="toc-importing-and-exporting-data" class="nav-link" data-scroll-target="#importing-and-exporting-data"> <span class="header-section-number">4.2</span> Importing and exporting data</a></li> + <li><a href="#displaying-a-spatraster-object" id="toc-displaying-a-spatraster-object" class="nav-link" data-scroll-target="#displaying-a-spatraster-object"> <span class="header-section-number">4.3</span> Displaying a SpatRaster object</a></li> + <li><a href="#change-to-the-study-area" id="toc-change-to-the-study-area" class="nav-link" data-scroll-target="#change-to-the-study-area"> <span class="header-section-number">4.4</span> Change to the study area</a> + <ul class="collapse"> + <li><a href="#reprojections" id="toc-reprojections" class="nav-link" data-scroll-target="#reprojections"> <span class="header-section-number">4.4.1</span> (Re)projections</a></li> + <li><a href="#crop" id="toc-crop" class="nav-link" data-scroll-target="#crop"> <span class="header-section-number">4.4.2</span> Crop</a></li> + <li><a href="#mask" id="toc-mask" class="nav-link" data-scroll-target="#mask"> <span class="header-section-number">4.4.3</span> Mask</a></li> + <li><a href="#aggregation-and-disaggregation" id="toc-aggregation-and-disaggregation" class="nav-link" data-scroll-target="#aggregation-and-disaggregation"> <span class="header-section-number">4.4.4</span> Aggregation and disaggregation</a></li> + <li><a href="#raster-fusion" id="toc-raster-fusion" class="nav-link" data-scroll-target="#raster-fusion"> <span class="header-section-number">4.4.5</span> Raster fusion</a></li> + <li><a href="#segregate" id="toc-segregate" class="nav-link" data-scroll-target="#segregate"> <span class="header-section-number">4.4.6</span> Segregate</a></li> + </ul></li> + <li><a href="#map-algebra" id="toc-map-algebra" class="nav-link" data-scroll-target="#map-algebra"> <span class="header-section-number">4.5</span> Map Algebra</a> + <ul class="collapse"> + <li><a href="#local-operations" id="toc-local-operations" class="nav-link" data-scroll-target="#local-operations"> <span class="header-section-number">4.5.1</span> Local operations</a></li> + <li><a href="#focal-operations" id="toc-focal-operations" class="nav-link" data-scroll-target="#focal-operations"> <span class="header-section-number">4.5.2</span> Focal operations</a></li> + <li><a href="#global-operations" id="toc-global-operations" class="nav-link" data-scroll-target="#global-operations"> <span class="header-section-number">4.5.3</span> Global operations</a></li> + <li><a href="#zonal-operation" id="toc-zonal-operation" class="nav-link" data-scroll-target="#zonal-operation"> <span class="header-section-number">4.5.4</span> Zonal operation</a></li> + </ul></li> + <li><a href="#transformation-et-conversion" id="toc-transformation-et-conversion" class="nav-link" data-scroll-target="#transformation-et-conversion"> <span class="header-section-number">4.6</span> Transformation et conversion</a> + <ul class="collapse"> + <li><a href="#rasterisation" id="toc-rasterisation" class="nav-link" data-scroll-target="#rasterisation"> <span class="header-section-number">4.6.1</span> Rasterisation</a></li> + <li><a href="#vectorisation" id="toc-vectorisation" class="nav-link" data-scroll-target="#vectorisation"> <span class="header-section-number">4.6.2</span> Vectorisation</a></li> + <li><a href="#terra-raster-sf-stars" id="toc-terra-raster-sf-stars" class="nav-link" data-scroll-target="#terra-raster-sf-stars"> <span class="header-section-number">4.6.3</span> terra, raster, sf, stars…</a></li> + </ul></li> + </ul> +</nav> + </div> +<!-- main --> +<main class="content" id="quarto-document-content"> + +<header id="title-block-header" class="quarto-title-block default"> +<div class="quarto-title"> +<h1 class="title d-none d-lg-block"><span class="chapter-number">4</span> <span class="chapter-title">Work with Raster Data</span></h1> +</div> + + + +<div class="quarto-title-meta"> + + + + </div> + + +</header> + +<p>This chapter is largely inspired by two presentation; <span class="citation" data-cites="MmadelinSIGR">Madelin (<a href="references.html#ref-MmadelinSIGR" role="doc-biblioref">2021</a>)</span> and <span class="citation" data-cites="JNowosadSIGR">Nowosad (<a href="references.html#ref-JNowosadSIGR" role="doc-biblioref">2021</a>)</span>; carried out as part of the <a href="https://sigr2021.github.io/site/index.html">SIGR2021</a> thematic school.</p> +<section id="format-of-objects-spatraster" class="level2" data-number="4.1"> +<h2 data-number="4.1" class="anchored" data-anchor-id="format-of-objects-spatraster"><span class="header-section-number">4.1</span> Format of objects <code>SpatRaster</code></h2> +<p>The package <code>terra</code> <span class="citation" data-cites="terra">(<a href="references.html#ref-terra" role="doc-biblioref">Hijmans 2022</a>)</span> allows to handle vector and raster data. To manipulate this spatial data, <code>terra</code> store it in object of type <code>SpatVector</code> and <code>SpatRaster</code>. In this chapter, we focus on the manipulation of raster data (<code>SpatRaster</code>) from functions offered by this package.</p> +<p>An object <code>SpatRaster</code> allows to handle vector and raster data, in one or more layers (variables). This object also stores a number of fundamental parameters that describe it (number of columns, rows, spatial extent, coordinate reference system, etc.).</p> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="img/raster.png" class="img-fluid figure-img" width="350"></p> +<p></p><figcaption aria-hidden="true" class="figure-caption">Source : <span class="citation" data-cites="RasterCheatSheet">(<a href="references.html#ref-RasterCheatSheet" role="doc-biblioref">Racine 2016</a>)</span></figcaption><p></p> +</figure> +</div> +</section> +<section id="importing-and-exporting-data" class="level2" data-number="4.2"> +<h2 data-number="4.2" class="anchored" data-anchor-id="importing-and-exporting-data"><span class="header-section-number">4.2</span> Importing and exporting data</h2> +<p>The package <code>terra</code> allows importing and exporting raster files. It is based on the <a href="https://gdal.org/">GDAL</a> library which makes it possible to read and process a very large number of geographic image formats.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(terra)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<p>The function <code>rast()</code> allows you to create and/or import raster data. The following lines import the raster file <strong>elevation.tif</strong> (<a href="https://fr.wikipedia.org/wiki/Tagged_Image_File_Format"><em>Tagged Image File Format</em></a>) into an object of type <code>SpatRaster</code> (default).</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>elevation <span class="ot"><-</span> <span class="fu">rast</span>(<span class="st">"data_cambodia/elevation.tif"</span>) </span> +<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>elevation</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output cell-output-stdout"> +<pre><code>class : SpatRaster +dimensions : 5235, 6458, 1 (nrow, ncol, nlyr) +resolution : 0.0008333394, 0.0008332568 (x, y) +extent : 102.2935, 107.6752, 10.33984, 14.70194 (xmin, xmax, ymin, ymax) +coord. ref. : lon/lat WGS 84 (EPSG:4326) +source : elevation.tif +name : elevation </code></pre> +</div> +</div> +<p>Modifying the name of the stored variable (altitude).</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(elevation) <span class="ot"><-</span> <span class="st">"Altitude"</span> </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<p>The function <code>writeRaster()</code> allow you to save an object <code>SpatRaster</code> on your machine, in the format of your choice.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">writeRaster</span>(<span class="at">x =</span> elevation, <span class="at">filename =</span> <span class="st">"data_cambodia/new_elevation.tif"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +</section> +<section id="displaying-a-spatraster-object" class="level2" data-number="4.3"> +<h2 data-number="4.3" class="anchored" data-anchor-id="displaying-a-spatraster-object"><span class="header-section-number">4.3</span> Displaying a SpatRaster object</h2> +<p>The function <code>plot()</code> is use to display an object <code>SpatRaster</code>.</p> +<div class="cell" data-layout-align="center"> +<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(elevation)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/affichage_1_raster-1.png" class="img-fluid figure-img" width="576"></p> +</figure> +</div> +</div> +</div> +<p>A raster always contains numerical data, but it can be both quantitative data and numerically coded qualitative (categorical) data (ex: type of land cover).</p> +<p>Specify the type of data stored with the augment <code>type</code> (<code>type = "continuous"</code> default), to display them correctly.</p> +<p>Import and display of raster containing categorical data: Phnom Penh Land Cover 2019 (land cover types) with a resolution of 1.5 meters:</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>lulc_2019 <span class="ot"><-</span> <span class="fu">rast</span>(<span class="st">"data_cambodia/lulc_2019.tif"</span>) <span class="co">#Import Phnom Penh landcover 2019, landcover types</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<p>The landcover data was produced from SPOT7 satellite image with 1.5 meter spatial resolution. An extraction centered on the municipality of Phnom Penh was then carried out.</p> +<div class="cell" data-layout-align="center"> +<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(lulc_2019, <span class="at">type =</span> <span class="st">"classes"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/affichage_raster_2-1.png" class="img-fluid figure-img" width="576"></p> +</figure> +</div> +</div> +</div> +<p>To display the actual tiles of landcover types, as well as the official colors of Phnom Penh Landcover nomenclature (available <a href="https://www.statistiques.developpement-durable.gouv.fr/corine-land-cover-0">here</a>), you can proceed as follows.</p> +<div class="cell" data-layout-align="center"> +<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>class_name <span class="ot"><-</span> <span class="fu">c</span>(</span> +<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="st">"Roads"</span>,</span> +<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="st">"Built-up areas"</span>,</span> +<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a> <span class="st">"Water Bodies and rivers"</span>,</span> +<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"Wetlands"</span>,</span> +<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"Dry bare area"</span>,</span> +<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a> <span class="st">"Bare crop fields"</span>,</span> +<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a> <span class="st">"Low vegetation areas"</span>,</span> +<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a> <span class="st">"High vegetation areas"</span>,</span> +<span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a> <span class="st">"Forested areas"</span>)</span> +<span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a>class_color <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"#070401"</span>, <span class="st">"#c84639"</span>, <span class="st">"#1398eb"</span>,<span class="st">"#8bc2c2"</span>,</span> +<span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a> <span class="st">"#dc7b34"</span>, <span class="st">"#a6bd5f"</span>,<span class="st">"#e8e8e8"</span>, <span class="st">"#4fb040"</span>, <span class="st">"#35741f"</span>)</span> +<span id="cb9-14"><a href="#cb9-14" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(lulc_2019,</span> +<span id="cb9-15"><a href="#cb9-15" aria-hidden="true" tabindex="-1"></a> <span class="at">type =</span> <span class="st">"classes"</span>,</span> +<span id="cb9-16"><a href="#cb9-16" aria-hidden="true" tabindex="-1"></a> <span class="at">levels =</span> class_name,</span> +<span id="cb9-17"><a href="#cb9-17" aria-hidden="true" tabindex="-1"></a> <span class="at">col =</span> class_color,</span> +<span id="cb9-18"><a href="#cb9-18" aria-hidden="true" tabindex="-1"></a> <span class="at">plg =</span> <span class="fu">list</span>(<span class="at">cex =</span> <span class="fl">0.7</span>),</span> +<span id="cb9-19"><a href="#cb9-19" aria-hidden="true" tabindex="-1"></a> <span class="at">mar =</span> <span class="fu">c</span>(<span class="fl">3.1</span>, <span class="fl">3.1</span>, <span class="fl">2.1</span>, <span class="dv">10</span>) <span class="co">#The margin are (bottom, left, top, right) respectively</span></span> +<span id="cb9-20"><a href="#cb9-20" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/affichage_raster_3-1.png" class="img-fluid figure-img" width="1152"></p> +</figure> +</div> +</div> +</div> +</section> +<section id="change-to-the-study-area" class="level2" data-number="4.4"> +<h2 data-number="4.4" class="anchored" data-anchor-id="change-to-the-study-area"><span class="header-section-number">4.4</span> Change to the study area</h2> +<section id="reprojections" class="level3" data-number="4.4.1"> +<h3 data-number="4.4.1" class="anchored" data-anchor-id="reprojections"><span class="header-section-number">4.4.1</span> (Re)projections</h3> +<p>To modify the projection system of a raster, use the function <code>project()</code>. It is then necessary to indicate the method for estimating the new cell values.</p> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="img/project_raster.png" class="img-fluid figure-img"></p> +<p></p><figcaption aria-hidden="true" class="figure-caption">Source : Centre Canadien de Télédétection</figcaption><p></p> +</figure> +</div> +<p>Four interpolation methods are available:</p> +<ul> +<li><strong><em>near</em></strong> : nearest neighbor, fast and default method for qualitative data;<br> +</li> +<li><strong><em>bilinear</em></strong> : bilinear interpolation. Default method for quantitative data;<br> +</li> +<li><strong><em>cubic</em></strong> : cubic interpolation;<br> +</li> +<li><strong><em>cubicspline</em></strong> : cubic spline interpolation.</li> +</ul> +<div class="cell" data-layout-align="center"> +<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Re-project data </span></span> +<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a>elevation_utm <span class="ot">=</span> <span class="fu">project</span>(<span class="at">x =</span> elevation, <span class="at">y =</span> <span class="st">"EPSG:32648"</span>, <span class="at">method =</span> <span class="st">"bilinear"</span>) <span class="co">#from WGS84(EPSG:4326) to UTM zone48N(EPSG:32648) </span></span> +<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a>lulc_2019_utm <span class="ot">=</span> <span class="fu">project</span>(<span class="at">x =</span> lulc_2019, <span class="at">y =</span> <span class="st">"EPSG:32648"</span>, <span class="at">method =</span> <span class="st">"near"</span>) <span class="co">#keep original projection: UTM zone48N(EPSG:32648)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<div class="cell" data-layout-align="center"> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/reproj_raster_2-1.png" class="img-fluid figure-img" width="672"></p> +</figure> +</div> +</div> +</div> +</section> +<section id="crop" class="level3" data-number="4.4.2"> +<h3 data-number="4.4.2" class="anchored" data-anchor-id="crop"><span class="header-section-number">4.4.2</span> Crop</h3> +<p>Clipping a raster to the extent of another object <code>SpatVector</code> or <code>SpatRaster</code> is achievable with the <code>crop()</code>.</p> +<div class="quarto-layout-panel"> +<div class="quarto-layout-row quarto-layout-valign-top"> +<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;"> +<p><img src="img/crop.png" class="img-fluid"></p> +</div> +<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;"> +<p><img src="img/crop2.png" class="img-fluid"></p> +</div> +</div> +<div class="quarto-layout-row quarto-layout-valign-top"> +<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;"> +<p>Source : <span class="citation" data-cites="RasterCheatSheet">(<a href="references.html#ref-RasterCheatSheet" role="doc-biblioref">Racine 2016</a>)</span></p> +</div> +</div> +</div> +<p>Import vector data of (municipal divisions) using function <code>vect</code>. This data will be stored in an <code>SpatVector</code> object.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>district <span class="ot"><-</span> <span class="fu">vect</span>(<span class="st">"data_cambodia/cambodia.gpkg"</span>, <span class="at">layer=</span><span class="st">"district"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<p>Extraction of district boundaries of Thma Bang district (ADM2_PCODE : KH0907).</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>thma_bang <span class="ot"><-</span> <span class="fu">subset</span>(district, district<span class="sc">$</span>ADM2_PCODE <span class="sc">==</span> <span class="st">"KH0907"</span>) </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<p>Using the function <code>crop()</code>, Both data layers must be in the same projection.</p> +<div class="cell" data-layout-align="center"> +<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>crop_thma_bang <span class="ot"><-</span> <span class="fu">crop</span>(elevation_utm, thma_bang)</span> +<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(crop_thma_bang)</span> +<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(thma_bang, <span class="at">add=</span><span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/crop_raster_3-1.png" class="img-fluid figure-img" width="576"></p> +</figure> +</div> +</div> +</div> +</section> +<section id="mask" class="level3" data-number="4.4.3"> +<h3 data-number="4.4.3" class="anchored" data-anchor-id="mask"><span class="header-section-number">4.4.3</span> Mask</h3> +<p>To display only the values of a raster contained in a polygon, use the function <code>mask()</code>.</p> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="img/mask.png" class="img-fluid figure-img" width="350"></p> +<p></p><figcaption aria-hidden="true" class="figure-caption">Source : <span class="citation" data-cites="RasterCheatSheet">(<a href="references.html#ref-RasterCheatSheet" role="doc-biblioref">Racine 2016</a>)</span></figcaption><p></p> +</figure> +</div> +<p>Creation of a mask on the <strong>crop_thma_bang</strong> raster to the municipal limits (polygon) of <strong>Thma Bang district</strong>.</p> +<div class="cell" data-layout-align="center"> +<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>mask_thma_bang <span class="ot"><-</span> <span class="fu">mask</span>(crop_thma_bang, thma_bang)</span> +<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(mask_thma_bang)</span> +<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(thma_bang, <span class="at">add =</span> <span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/mask_raster-1.png" class="img-fluid figure-img" width="576"></p> +</figure> +</div> +</div> +</div> +</section> +<section id="aggregation-and-disaggregation" class="level3" data-number="4.4.4"> +<h3 data-number="4.4.4" class="anchored" data-anchor-id="aggregation-and-disaggregation"><span class="header-section-number">4.4.4</span> Aggregation and disaggregation</h3> +<p>Resampling a raster to a different resolution is done in two steps.</p> +<div class="quarto-layout-panel"> +<div class="quarto-layout-row quarto-layout-valign-top"> +<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="img/raster.png" class="img-fluid figure-img"></p> +<p></p><figcaption aria-hidden="true" class="figure-caption">1</figcaption><p></p> +</figure> +</div> +</div> +<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="img/agr_raster.png" class="img-fluid figure-img"></p> +<p></p><figcaption aria-hidden="true" class="figure-caption">2</figcaption><p></p> +</figure> +</div> +</div> +<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="img/agr_raster_2.png" class="img-fluid figure-img"></p> +<p></p><figcaption aria-hidden="true" class="figure-caption">3</figcaption><p></p> +</figure> +</div> +</div> +</div> +<div class="quarto-layout-row quarto-layout-valign-top"> +<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;"> +<p>Source : <span class="citation" data-cites="RasterCheatSheet">(<a href="references.html#ref-RasterCheatSheet" role="doc-biblioref">Racine 2016</a>)</span></p> +</div> +</div> +</div> +<p>Display the resolution of a raster with the function <code>res()</code>.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">res</span>(elevation_utm) <span class="co">#check cell size</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output cell-output-stdout"> +<pre><code>[1] 91.19475 91.19475</code></pre> +</div> +</div> +<p>Create a grid with the same extent, then decrease the spatial resolution (larger cells).</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>elevation_LowerGrid <span class="ot"><-</span> elevation_utm</span> +<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a><span class="co"># elevation_HigherGrid <- elevation_utm</span></span> +<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a><span class="fu">res</span>(elevation_LowerGrid) <span class="ot"><-</span> <span class="dv">1000</span> <span class="co">#cells size = 1000 meter</span></span> +<span id="cb17-5"><a href="#cb17-5" aria-hidden="true" tabindex="-1"></a><span class="co"># res(elevation_HigherGrid) <- 10 #cells size = 10 meter</span></span> +<span id="cb17-6"><a href="#cb17-6" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb17-7"><a href="#cb17-7" aria-hidden="true" tabindex="-1"></a>elevation_LowerGrid</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output cell-output-stdout"> +<pre><code>class : SpatRaster +dimensions : 484, 589, 1 (nrow, ncol, nlyr) +resolution : 1000, 1000 (x, y) +extent : 203586.3, 792586.3, 1142954, 1626954 (xmin, xmax, ymin, ymax) +coord. ref. : WGS 84 / UTM zone 48N (EPSG:32648) </code></pre> +</div> +</div> +<p>The function <code>resample()</code> allows to resample the atarting values in the new spatial resolution. Several resampling methods are available (cf. <a href="#reprojections">partie 5.4.1</a>).</p> +<div class="cell" data-layout-align="center"> +<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>elevation_LowerGrid <span class="ot"><-</span> <span class="fu">resample</span>(elevation_utm, </span> +<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a> elevation_LowerGrid, </span> +<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a> <span class="at">method =</span> <span class="st">"bilinear"</span>) </span> +<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(elevation_LowerGrid, </span> +<span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></a> <span class="at">main=</span><span class="st">"Cell size = 1000m</span><span class="sc">\n</span><span class="st">Bilinear resampling method"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/agr_raster_2-1.png" class="img-fluid figure-img" width="576"></p> +</figure> +</div> +</div> +</div> +</section> +<section id="raster-fusion" class="level3" data-number="4.4.5"> +<h3 data-number="4.4.5" class="anchored" data-anchor-id="raster-fusion"><span class="header-section-number">4.4.5</span> Raster fusion</h3> +<p>Merge multiple objects <code>SpatRaster</code> into one with <code>merge()</code> or <code>mosaic()</code>.</p> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="img/mosaic.png" class="img-fluid figure-img"></p> +<p></p><figcaption aria-hidden="true" class="figure-caption">Source : <a href="https://desktop.arcgis.com/fr/arcmap/10.3/manage-data/raster-and-images/what-is-a-mosaic.htm" class="uri">https://desktop.arcgis.com/fr/arcmap/10.3/manage-data/raster-and-images/what-is-a-mosaic.htm</a></figcaption><p></p> +</figure> +</div> +<p>After cutting the elevation raster by the municipal boundary of Thma Bang district (cf <a href="#crop">partie 5.4.2</a>), we do the same thing for the neighboring municipality of Phnum Kravanh district.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>phnum_kravanh <span class="ot"><-</span> <span class="fu">subset</span>(district, district<span class="sc">$</span>ADM2_PCODE <span class="sc">==</span> <span class="st">"KH1504"</span>) <span class="co"># Extraction of the municipal boundaries of Phnum Kravanh district</span></span> +<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a>crop_phnum_kravanh <span class="ot"><-</span> <span class="fu">crop</span>(elevation_utm, phnum_kravanh) <span class="co">#clipping the elevation raster according to district boundaries</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<p>The <strong>crop_thma_bang</strong> and <strong>crop_phnum_kravanh</strong> elevation raster overlap spatially:</p> +<div class="cell" data-layout-align="center"> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/merge_raster_1-1.png" class="img-fluid figure-img" width="768"></p> +</figure> +</div> +</div> +</div> +<p>The difference between the functions <code>merge()</code> and <code>mosiac()</code> relates to values of the overlapping cells. The function <code>mosaic()</code> calculate the average value while <code>merge()</code> holding the value of the object <code>SpaRaster</code> called n the function.</p> +<div class="cell" data-layout-align="center"> +<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="co">#in this example, merge() and mosaic() give the same result</span></span> +<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a>merge_raster <span class="ot"><-</span> <span class="fu">merge</span>(crop_thma_bang, crop_phnum_kravanh) </span> +<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a>mosaic_raster <span class="ot"><-</span> <span class="fu">mosaic</span>(crop_thma_bang, crop_phnum_kravanh)</span> +<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(merge_raster)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/merge_raster_2-1.png" class="img-fluid figure-img" width="576"></p> +</figure> +</div> +</div> +<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="co"># plot(mosaic_raster)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +</section> +<section id="segregate" class="level3" data-number="4.4.6"> +<h3 data-number="4.4.6" class="anchored" data-anchor-id="segregate"><span class="header-section-number">4.4.6</span> Segregate</h3> +<p>Decompose a raster by value (or modality) into different rasterlayers with the function <code>segregate</code>.</p> +<div class="cell" data-layout-align="center"> +<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>lulc_2019_class <span class="ot"><-</span> <span class="fu">segregate</span>(lulc_2019, <span class="at">keep=</span><span class="cn">TRUE</span>, <span class="at">other=</span><span class="cn">NA</span>) <span class="co">#creating a raster layer by modality</span></span> +<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(lulc_2019_class)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/segregate-1.png" class="img-fluid figure-img" width="576"></p> +</figure> +</div> +</div> +</div> +</section> +</section> +<section id="map-algebra" class="level2" data-number="4.5"> +<h2 data-number="4.5" class="anchored" data-anchor-id="map-algebra"><span class="header-section-number">4.5</span> Map Algebra</h2> +<p>Map algebra is classified into four groups of operation <span class="citation" data-cites="Tomlin_1990">(<a href="references.html#ref-Tomlin_1990" role="doc-biblioref"><strong>Tomlin_1990?</strong></a>)</span>:</p> +<ul> +<li><strong><em>Local</em></strong> : operation by cell, on one or more layers;<br> +</li> +<li><strong><em>Focal</em></strong> : neighborhood operation (surrounding cells);<br> +</li> +<li><strong><em>Zonal</em></strong> : to summarize the matrix values for certain zones, usually irregular;</li> +<li><strong><em>Global</em></strong> : to summarize the matrix values of one or more matrices.</li> +</ul> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="img/lo_fo_zo_glo.png" class="img-fluid figure-img"></p> +<p></p><figcaption aria-hidden="true" class="figure-caption">Source : <span class="citation" data-cites="XingongLi2009">(<a href="references.html#ref-XingongLi2009" role="doc-biblioref">Li 2009</a>)</span></figcaption><p></p> +</figure> +</div> +<section id="local-operations" class="level3" data-number="4.5.1"> +<h3 data-number="4.5.1" class="anchored" data-anchor-id="local-operations"><span class="header-section-number">4.5.1</span> Local operations</h3> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="img/op_local_2.png" class="img-fluid figure-img" width="452"></p> +<p></p><figcaption aria-hidden="true" class="figure-caption">Source : <span class="citation" data-cites="JMennis2015">(<a href="references.html#ref-JMennis2015" role="doc-biblioref">Mennis 2015</a>)</span></figcaption><p></p> +</figure> +</div> +<section id="value-replacement" class="level4" data-number="4.5.1.1"> +<h4 data-number="4.5.1.1" class="anchored" data-anchor-id="value-replacement"><span class="header-section-number">4.5.1.1</span> Value replacement</h4> +<div class="cell"> +<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a>elevation_utm[elevation_utm[[<span class="dv">1</span>]]<span class="sc">==</span> <span class="sc">-</span><span class="dv">9999</span>] <span class="ot"><-</span> <span class="cn">NA</span> <span class="co">#replaces -9999 values with NA</span></span> +<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a>elevation_utm[elevation_utm <span class="sc"><</span> <span class="dv">1500</span>] <span class="ot"><-</span> <span class="cn">NA</span> <span class="co">#Replace values < 1500 with NA</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<div class="cell"> +<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>elevation_utm[<span class="fu">is.na</span>(elevation_utm)] <span class="ot"><-</span> <span class="dv">0</span> <span class="co">#replace NA values with 0</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +</section> +<section id="operation-on-each-cell" class="level4" data-number="4.5.1.2"> +<h4 data-number="4.5.1.2" class="anchored" data-anchor-id="operation-on-each-cell"><span class="header-section-number">4.5.1.2</span> Operation on each cell</h4> +<div class="cell"> +<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a>elevation_1000 <span class="ot"><-</span> elevation_utm <span class="sc">+</span> <span class="dv">1000</span> <span class="co"># Adding 1000 to the value of each cell</span></span> +<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a>elevation_median <span class="ot"><-</span> elevation_utm <span class="sc">-</span> <span class="fu">global</span>(elevation_utm, median)[[<span class="dv">1</span>]] <span class="co"># Removed median elevation to each cell's value</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<div class="cell" data-layout-align="center"> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/op_local_2-1.png" class="img-fluid figure-img" width="768"></p> +</figure> +</div> +</div> +</div> +</section> +<section id="reclassification" class="level4" data-number="4.5.1.3"> +<h4 data-number="4.5.1.3" class="anchored" data-anchor-id="reclassification"><span class="header-section-number">4.5.1.3</span> Reclassification</h4> +<p>Reclassifying raster values can be used to discretize quantitative data as well as to categorize qualitative categories.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a>reclassif <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">1</span>, </span> +<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a> <span class="dv">2</span>, <span class="dv">4</span>, <span class="dv">2</span>,</span> +<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a> <span class="dv">4</span>, <span class="dv">6</span>, <span class="dv">3</span>,</span> +<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a> <span class="dv">6</span>, <span class="dv">9</span>, <span class="dv">4</span>), </span> +<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a> <span class="at">ncol =</span> <span class="dv">3</span>, <span class="at">byrow =</span> <span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<p>Values between 1 and 2 will be replaced by the value 1.<br> +Values between 3 and 4 will be replaced by the value 2.<br> +Values between 5 and 6 will be replaced by the value 3. Values between 7 and 9 will be replaced by the value 4.</p> +<p>…</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a>reclassif</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output cell-output-stdout"> +<pre><code> [,1] [,2] [,3] +[1,] 1 2 1 +[2,] 2 4 2 +[3,] 4 6 3 +[4,] 6 9 4</code></pre> +</div> +</div> +<p>The function <code>classify()</code> allows you to perform the reclassification.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb30"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>lulc_2019_reclass <span class="ot"><-</span> <span class="fu">classify</span>(lulc_2019, <span class="at">rcl =</span> reclassif)</span> +<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(lulc_2019_reclass, <span class="at">type =</span><span class="st">"classes"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<p><img src="04-raster_data_files/figure-html/reclass_4-1.png" class="img-fluid" width="672"></p> +</div> +</div> +<p>Display with the official titles and colors of the different categories.</p> +<div class="cell" data-layout-align="center"> +<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(lulc_2019_reclass, </span> +<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a> <span class="at">type =</span><span class="st">"classes"</span>, </span> +<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a> <span class="at">levels=</span><span class="fu">c</span>(<span class="st">"Urban areas"</span>,</span> +<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a> <span class="st">"Water body"</span>,</span> +<span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"Bare areas"</span>,</span> +<span id="cb31-6"><a href="#cb31-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"Vegetation areas"</span>),</span> +<span id="cb31-7"><a href="#cb31-7" aria-hidden="true" tabindex="-1"></a> <span class="at">col=</span><span class="fu">c</span>(<span class="st">"#E6004D"</span>,</span> +<span id="cb31-8"><a href="#cb31-8" aria-hidden="true" tabindex="-1"></a> <span class="st">"#00BFFF"</span>,</span> +<span id="cb31-9"><a href="#cb31-9" aria-hidden="true" tabindex="-1"></a> <span class="st">"#D3D3D3"</span>, </span> +<span id="cb31-10"><a href="#cb31-10" aria-hidden="true" tabindex="-1"></a> <span class="st">"#32CD32"</span>),</span> +<span id="cb31-11"><a href="#cb31-11" aria-hidden="true" tabindex="-1"></a> <span class="at">mar=</span><span class="fu">c</span>(<span class="dv">3</span>, <span class="fl">1.5</span>, <span class="dv">1</span>, <span class="dv">11</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/reclass_6-1.png" class="img-fluid figure-img" style="width:80.0%"></p> +</figure> +</div> +</div> +</div> +</section> +<section id="operation-on-several-layers-ex-ndvi" class="level4" data-number="4.5.1.4"> +<h4 data-number="4.5.1.4" class="anchored" data-anchor-id="operation-on-several-layers-ex-ndvi"><span class="header-section-number">4.5.1.4</span> Operation on several layers (ex: NDVI)</h4> +<p>It is possible to calculate the value of a cell from its values stored in different layers of an object <code>SpatRaster</code>.</p> +<p>Perhaps the most common example is the calculation of the <a href="http://www.trameverteetbleue.fr/outils-methodes/donnees-mobilisables/indice-vegetation-modis">Normalized Vegetation Index (<em>NDVI</em>)</a>. For each cell, a value is calculated from two layers of raster from a multispectral satellite image.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Import d'une image satellite multispectrale</span></span> +<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a>sentinel2a <span class="ot"><-</span> <span class="fu">rast</span>(<span class="st">"data_cambodia/Sentinel2A.tif"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<p>This multispectral satellite image (10m resolution) dated 25/02/2020, was produced by Sentinel-2 satellite and was retrieved from <a href="https://scihub.copernicus.eu/dhus/#/home">plateforme Copernicus Open Access Hub</a>. An extraction of Red and near infrared spectral bands, centered on the Phnom Penh city, was then carried out.</p> +<div class="cell" data-layout-align="center"> +<div class="sourceCode cell-code" id="cb33"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(sentinel2a)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/NDVI_1-1.png" class="img-fluid figure-img" width="672"></p> +</figure> +</div> +</div> +</div> +<p>To lighten the code, we assign the two matrix layers in different <code>SpatRaster</code> objects.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb34"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a>B04_Red <span class="ot"><-</span> sentinel2a[[<span class="dv">1</span>]] <span class="co">#spectral band Red</span></span> +<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a>B08_NIR <span class="ot"><-</span>sentinel2a[[<span class="dv">2</span>]] <span class="co">#spectral band near infrared</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<p>From these two raster objects , we can calculate the normalized vegetation index:</p> +<p><span class="math display">\[{NDVI}=\frac{\mathrm{NIR} - \mathrm{Red}} {\mathrm{NIR} + \mathrm{Red}}\]</span></p> +<div class="cell" data-layout-align="center"> +<div class="sourceCode cell-code" id="cb35"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a>raster_NDVI <span class="ot"><-</span> (B08_NIR <span class="sc">-</span> B04_Red ) <span class="sc">/</span> (B08_NIR <span class="sc">+</span> B04_Red )</span> +<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(raster_NDVI)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/NDVI_3-1.png" class="img-fluid figure-img" style="width:90.0%"></p> +</figure> +</div> +</div> +</div> +<p>The higher the values (close to 1), the denser the vegetation.</p> +</section> +</section> +<section id="focal-operations" class="level3" data-number="4.5.2"> +<h3 data-number="4.5.2" class="anchored" data-anchor-id="focal-operations"><span class="header-section-number">4.5.2</span> Focal operations</h3> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="img/op_focal_2.png" class="img-fluid figure-img" width="415"></p> +<p></p><figcaption aria-hidden="true" class="figure-caption">Source : <span class="citation" data-cites="JMennis2015">(<a href="references.html#ref-JMennis2015" role="doc-biblioref">Mennis 2015</a>)</span></figcaption><p></p> +</figure> +</div> +<p>Focal analysis conisders a cell plus its direct neighbors in contiguous and symmetrical (neighborhood operations). Most often, the value of the output cell is the result of a block of 3 x 3 (odd number) input cells.</p> +<p>The first step is to build a matrix that determines the block of cells that will be considered around each cell.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb36"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 5 x 5 matrix, where each cell has the same weight</span></span> +<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a>mon_focal <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="dv">1</span>, <span class="at">nrow =</span> <span class="dv">5</span>, <span class="at">ncol =</span> <span class="dv">5</span>)</span> +<span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a>mon_focal</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output cell-output-stdout"> +<pre><code> [,1] [,2] [,3] [,4] [,5] +[1,] 1 1 1 1 1 +[2,] 1 1 1 1 1 +[3,] 1 1 1 1 1 +[4,] 1 1 1 1 1 +[5,] 1 1 1 1 1</code></pre> +</div> +</div> +<p>The function <code>focal()</code> Then allows you to perform the desired analysis. For example: calculating the average of the values of all contiguous cells, for each cell in the raster.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb38"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a>elevation_LowerGrid_mean <span class="ot"><-</span> <span class="fu">focal</span>(elevation_LowerGrid, </span> +<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a> <span class="at">w =</span> mon_focal, </span> +<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a> <span class="at">fun =</span> mean)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<div class="cell" data-layout-align="center"> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/op_focal_5-1.png" class="img-fluid figure-img" width="768"></p> +</figure> +</div> +</div> +</div> +<section id="focal-operations-for-elevation-rasters" class="level4" data-number="4.5.2.1"> +<h4 data-number="4.5.2.1" class="anchored" data-anchor-id="focal-operations-for-elevation-rasters"><span class="header-section-number">4.5.2.1</span> Focal operations for elevation rasters</h4> +<p>The function <code>terrain()</code> allows to perform focal analyzes specific to elevation rasters. Six operations are available:</p> +<ul> +<li><strong><em>slope</em></strong> = calculation of the slope or degree of inclination of the surface;<br> +</li> +<li><strong><em>aspect</em></strong> = calculate slope orientation;<br> +</li> +<li><strong><em>roughness</em></strong> = calculate of the variability or irregularity of the elevation;<br> +</li> +<li><strong><em>TPI</em></strong> = calculation of the index of topgraphic positions;<br> +</li> +<li><strong><em>TRI</em></strong> = elevation variability index calculation;<br> +</li> +<li><strong><em>flowdir</em></strong> = calculation of the water flow direction.</li> +</ul> +<p>Example with calculation of slopes(<em>slope</em>).</p> +<div class="cell" data-layout-align="center"> +<div class="sourceCode cell-code" id="cb39"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="co">#slope calculation</span></span> +<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a>slope <span class="ot"><-</span> <span class="fu">terrain</span>(elevation_utm, <span class="st">"slope"</span>, </span> +<span id="cb39-3"><a href="#cb39-3" aria-hidden="true" tabindex="-1"></a> <span class="at">neighbors =</span> <span class="dv">8</span>, <span class="co">#8 (or 4) cells around taken into account</span></span> +<span id="cb39-4"><a href="#cb39-4" aria-hidden="true" tabindex="-1"></a> <span class="at">unit =</span> <span class="st">"degrees"</span>) <span class="co">#Output unit</span></span> +<span id="cb39-5"><a href="#cb39-5" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb39-6"><a href="#cb39-6" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(slope) <span class="co">#Inclination of the slopes, in degrees</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/op_focal_6-1.png" class="img-fluid figure-img" width="576"></p> +</figure> +</div> +</div> +</div> +</section> +</section> +<section id="global-operations" class="level3" data-number="4.5.3"> +<h3 data-number="4.5.3" class="anchored" data-anchor-id="global-operations"><span class="header-section-number">4.5.3</span> Global operations</h3> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="img/op_global.png" class="img-fluid figure-img" width="297"></p> +<p></p><figcaption aria-hidden="true" class="figure-caption">Source : <a href="https://gisgeography.com/map-algebra-global-zonal-focal-local" class="uri">https://gisgeography.com/map-algebra-global-zonal-focal-local</a></figcaption><p></p> +</figure> +</div> +<p>Global operation are used to summarize the matrix values of one or more matrices.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb40"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a><span class="fu">global</span>(elevation_utm, <span class="at">fun =</span> <span class="st">"mean"</span>) <span class="co">#average values</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output cell-output-stdout"> +<pre><code> mean +Altitude 80.01082</code></pre> +</div> +</div> +<div class="cell"> +<div class="sourceCode cell-code" id="cb42"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a><span class="fu">global</span>(elevation_utm, <span class="at">fun =</span> <span class="st">"sd"</span>) <span class="co">#standard deviation</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output cell-output-stdout"> +<pre><code> sd +Altitude 155.885</code></pre> +</div> +</div> +<div class="cell"> +<div class="sourceCode cell-code" id="cb44"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a><span class="fu">freq</span>(lulc_2019_reclass) <span class="co">#frequency</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output cell-output-stdout"> +<pre><code> layer value count +[1,] 1 1 47485325 +[2,] 1 2 13656289 +[3,] 1 3 14880961 +[4,] 1 4 37194979</code></pre> +</div> +<div class="sourceCode cell-code" id="cb46"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a><span class="fu">table</span>(lulc_2019_reclass[]) <span class="co">#contingency table</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output cell-output-stdout"> +<pre><code> + 1 2 3 4 +47485325 13656289 14880961 37194979 </code></pre> +</div> +</div> +<p>Statistical representations that summarize matrix information.</p> +<div class="cell" data-layout-align="center"> +<div class="sourceCode cell-code" id="cb48"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1"><a href="#cb48-1" aria-hidden="true" tabindex="-1"></a><span class="fu">hist</span>(elevation_utm) <span class="co">#histogram</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output cell-output-stderr"> +<pre><code>Warning: [hist] a sample of3% of the cells was used</code></pre> +</div> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/op_global_4-1.png" class="img-fluid figure-img" width="576"></p> +</figure> +</div> +</div> +<div class="sourceCode cell-code" id="cb50"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a><span class="fu">density</span>(elevation_utm) <span class="co">#density</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/op_global_4-2.png" class="img-fluid figure-img" width="576"></p> +</figure> +</div> +</div> +</div> +</section> +<section id="zonal-operation" class="level3" data-number="4.5.4"> +<h3 data-number="4.5.4" class="anchored" data-anchor-id="zonal-operation"><span class="header-section-number">4.5.4</span> Zonal operation</h3> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="img/op_zonal_2.png" class="img-fluid figure-img" width="342"></p> +<p></p><figcaption aria-hidden="true" class="figure-caption">Source : <span class="citation" data-cites="JMennis2015">(<a href="references.html#ref-JMennis2015" role="doc-biblioref">Mennis 2015</a>)</span></figcaption><p></p> +</figure> +</div> +<p>The zonal operation make it possible to summarize the matrix values of certain zones (group of contiguous cells in space or in value).</p> +<section id="zonal-operation-on-an-extraction" class="level4" data-number="4.5.4.1"> +<h4 data-number="4.5.4.1" class="anchored" data-anchor-id="zonal-operation-on-an-extraction"><span class="header-section-number">4.5.4.1</span> Zonal operation on an extraction</h4> +<p><strong>All global operations can be performed on an extraction of cells resulting from the functions <code>crop()</code>, <code>mask()</code>, <code>segregate()</code>…</strong></p> +<p>Example: average elevation for the city of Thma Bang district (cf <a href="#mask">partie 5.4.3</a>).</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb51"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Average value of the "mask" raster over Thma Bang district</span></span> +<span id="cb51-2"><a href="#cb51-2" aria-hidden="true" tabindex="-1"></a><span class="fu">global</span>(mask_thma_bang, <span class="at">fun =</span> <span class="st">"mean"</span>, <span class="at">na.rm=</span><span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output cell-output-stdout"> +<pre><code> mean +Altitude 584.7703</code></pre> +</div> +</div> +</section> +<section id="zonal-operation-from-a-vector-layer" class="level4" data-number="4.5.4.2"> +<h4 data-number="4.5.4.2" class="anchored" data-anchor-id="zonal-operation-from-a-vector-layer"><span class="header-section-number">4.5.4.2</span> Zonal operation from a vector layer</h4> +<p>The function <code>extract()</code> allows you to extract and manipulate the values of cells that intersect vector data.</p> +<p>Example from polygons:</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb53"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Average elevation for each polygon (district)?</span></span> +<span id="cb53-2"><a href="#cb53-2" aria-hidden="true" tabindex="-1"></a>elevation_by_dist <span class="ot"><-</span> <span class="fu">extract</span>(elevation_LowerGrid, district, <span class="at">fun=</span>mean)</span> +<span id="cb53-3"><a href="#cb53-3" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(elevation_by_dist, <span class="dv">10</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output cell-output-stdout"> +<pre><code> ID Altitude +1 1 8.953352 +2 2 196.422240 +3 3 23.453937 +4 4 3.973118 +5 5 29.545801 +6 6 41.579593 +7 7 50.162749 +8 8 85.128777 +9 9 269.068091 +10 10 8.439041</code></pre> +</div> +</div> +</section> +<section id="zonal-operation-from-raster" class="level4" data-number="4.5.4.3"> +<h4 data-number="4.5.4.3" class="anchored" data-anchor-id="zonal-operation-from-raster"><span class="header-section-number">4.5.4.3</span> Zonal operation from raster</h4> +<p>Zonal operation can be performed by area bounded by the categorical values of a second raster. For this, the two raster must have exaclty the same extent and the same resolution.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb55"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a><span class="co">#create a second raster with same resolution and extent as "elevation_clip"</span></span> +<span id="cb55-2"><a href="#cb55-2" aria-hidden="true" tabindex="-1"></a>elevation_clip <span class="ot"><-</span> <span class="fu">rast</span>(<span class="st">"data_cambodia/elevation_clip.tif"</span>)</span> +<span id="cb55-3"><a href="#cb55-3" aria-hidden="true" tabindex="-1"></a>elevation_clip_utm <span class="ot"><-</span> <span class="fu">project</span>(<span class="at">x =</span> elevation_clip, <span class="at">y =</span> <span class="st">"EPSG:32648"</span>, <span class="at">method =</span> <span class="st">"bilinear"</span>)</span> +<span id="cb55-4"><a href="#cb55-4" aria-hidden="true" tabindex="-1"></a>second_raster_CLC <span class="ot"><-</span> <span class="fu">rast</span>(elevation_clip_utm)</span> +<span id="cb55-5"><a href="#cb55-5" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb55-6"><a href="#cb55-6" aria-hidden="true" tabindex="-1"></a><span class="co">#resampling of lulc_2019_reclass </span></span> +<span id="cb55-7"><a href="#cb55-7" aria-hidden="true" tabindex="-1"></a>second_raster_CLC <span class="ot"><-</span> <span class="fu">resample</span>(lulc_2019_reclass, second_raster_CLC, <span class="at">method =</span> <span class="st">"near"</span>) </span> +<span id="cb55-8"><a href="#cb55-8" aria-hidden="true" tabindex="-1"></a> </span> +<span id="cb55-9"><a href="#cb55-9" aria-hidden="true" tabindex="-1"></a><span class="co">#added a variable name for the second raster</span></span> +<span id="cb55-10"><a href="#cb55-10" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(second_raster_CLC) <span class="ot"><-</span> <span class="st">"lulc_2019_reclass_resample"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<div class="cell" data-layout-align="center"> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/op_zonal_2-1.png" class="img-fluid figure-img" width="768"></p> +</figure> +</div> +</div> +</div> +<p>Calculation of the average elevation for the different areas of the second raster.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb56"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a><span class="co">#average elevation for each area of the "second_raster"</span></span> +<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a><span class="fu">zonal</span>(elevation_clip_utm, second_raster_CLC , <span class="st">"mean"</span>, <span class="at">na.rm=</span><span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output cell-output-stdout"> +<pre><code> lulc_2019_reclass_resample elevation_clip +1 1 12.83846 +2 2 8.31809 +3 3 11.41178 +4 4 11.93546</code></pre> +</div> +</div> +</section> +</section> +</section> +<section id="transformation-et-conversion" class="level2" data-number="4.6"> +<h2 data-number="4.6" class="anchored" data-anchor-id="transformation-et-conversion"><span class="header-section-number">4.6</span> Transformation et conversion</h2> +<section id="rasterisation" class="level3" data-number="4.6.1"> +<h3 data-number="4.6.1" class="anchored" data-anchor-id="rasterisation"><span class="header-section-number">4.6.1</span> Rasterisation</h3> +<p>Convert polygons to raster format.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb58"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb58-1"><a href="#cb58-1" aria-hidden="true" tabindex="-1"></a>chamkarmon <span class="ot">=</span> <span class="fu">subset</span>(district, district<span class="sc">$</span>ADM2_PCODE <span class="sc">==</span><span class="st">"KH1201"</span>) </span> +<span id="cb58-2"><a href="#cb58-2" aria-hidden="true" tabindex="-1"></a>raster_district <span class="ot"><-</span> <span class="fu">rasterize</span>(<span class="at">x =</span> chamkarmon, <span class="at">y =</span> elevation_clip_utm)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<div class="cell" data-layout-align="center"> +<div class="sourceCode cell-code" id="cb59"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb59-1"><a href="#cb59-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(raster_district)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<div class="quarto-figure quarto-figure-center"> +<figure class="figure"> +<p><img src="04-raster_data_files/figure-html/Raster_vec11-1.png" class="img-fluid figure-img" width="576"></p> +</figure> +</div> +</div> +</div> +<p>Convert points to raster format</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb60"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a><span class="co">#rasterization of the centroids of the municipalities</span></span> +<span id="cb60-2"><a href="#cb60-2" aria-hidden="true" tabindex="-1"></a>raster_dist_centroid <span class="ot"><-</span> <span class="fu">rasterize</span>(<span class="at">x =</span> <span class="fu">centroids</span>(district), </span> +<span id="cb60-3"><a href="#cb60-3" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> elevation_clip_utm, <span class="at">fun=</span>sum)</span> +<span id="cb60-4"><a href="#cb60-4" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(raster_dist_centroid, <span class="at">col =</span> <span class="st">"red"</span>)</span> +<span id="cb60-5"><a href="#cb60-5" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(district, <span class="at">add =</span><span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<p><img src="04-raster_data_files/figure-html/Raster_vec2-1.png" class="img-fluid" width="672"></p> +</div> +</div> +<p>Convert lines in raster format</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb61"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a><span class="co">#rasterization of municipal boundaries</span></span> +<span id="cb61-2"><a href="#cb61-2" aria-hidden="true" tabindex="-1"></a>raster_dist_line <span class="ot"><-</span> <span class="fu">rasterize</span>(<span class="at">x =</span> <span class="fu">as.lines</span>(district), <span class="at">y =</span> elevation_clip_utm, <span class="at">fun=</span>sum)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<div class="cell"> +<div class="sourceCode cell-code" id="cb62"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(raster_dist_line)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<p><img src="04-raster_data_files/figure-html/Raster_vec33-1.png" class="img-fluid" width="672"></p> +</div> +</div> +</section> +<section id="vectorisation" class="level3" data-number="4.6.2"> +<h3 data-number="4.6.2" class="anchored" data-anchor-id="vectorisation"><span class="header-section-number">4.6.2</span> Vectorisation</h3> +<p>Transform a raster to vector polygons.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb63"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb63-1"><a href="#cb63-1" aria-hidden="true" tabindex="-1"></a>polygon_elevation <span class="ot"><-</span> <span class="fu">as.polygons</span>(elevation_clip_utm)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<div class="cell"> +<div class="sourceCode cell-code" id="cb64"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb64-1"><a href="#cb64-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(polygon_elevation, <span class="at">y =</span> <span class="dv">1</span>, <span class="at">border=</span><span class="st">"white"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<p><img src="04-raster_data_files/figure-html/Raster_vec44-1.png" class="img-fluid" width="672"></p> +</div> +</div> +<p>Transform a raster to vector points.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb65"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb65-1"><a href="#cb65-1" aria-hidden="true" tabindex="-1"></a>points_elevation <span class="ot"><-</span> <span class="fu">as.points</span>(elevation_clip_utm)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<div class="cell"> +<div class="sourceCode cell-code" id="cb66"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb66-1"><a href="#cb66-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(points_elevation, <span class="at">y =</span> <span class="dv">1</span>, <span class="at">cex =</span> <span class="fl">0.3</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<p><img src="04-raster_data_files/figure-html/Raster_vec55-1.png" class="img-fluid" width="672"></p> +</div> +</div> +<p>Transform a raster into vector lines.</p> +<div class="cell"> +<div class="sourceCode cell-code" id="cb67"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb67-1"><a href="#cb67-1" aria-hidden="true" tabindex="-1"></a>lines_elevation <span class="ot"><-</span> <span class="fu">as.lines</span>(elevation_clip_utm)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +</div> +<div class="cell"> +<div class="sourceCode cell-code" id="cb68"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb68-1"><a href="#cb68-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(lines_elevation)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> +<div class="cell-output-display"> +<p><img src="04-raster_data_files/figure-html/Raster_vec66-1.png" class="img-fluid" width="672"></p> +</div> +</div> +</section> +<section id="terra-raster-sf-stars" class="level3" data-number="4.6.3"> +<h3 data-number="4.6.3" class="anchored" data-anchor-id="terra-raster-sf-stars"><span class="header-section-number">4.6.3</span> terra, raster, sf, stars…</h3> +<p>Reference packages for manipulating spatial data all rely o their own object class. It is sometimes necessary to convert these objects from one class to another class to take advance of all the features offered by these different packages.</p> +<p>Conversion functions for raster data:</p> +<table class="table"> +<thead> +<tr class="header"> +<th>FROM/TO</th> +<th>raster</th> +<th>terra</th> +<th>stars</th> +</tr> +</thead> +<tbody> +<tr class="odd"> +<td>raster</td> +<td></td> +<td>rast()</td> +<td>st_as_stars()</td> +</tr> +<tr class="even"> +<td>terra</td> +<td>raster()</td> +<td></td> +<td>st_as_stars()</td> +</tr> +<tr class="odd"> +<td>stars</td> +<td>raster()</td> +<td>as(x, ‘Raster’) + rast()</td> +<td></td> +</tr> +</tbody> +</table> +<p>Conversion functions for vector data:</p> +<table class="table"> +<thead> +<tr class="header"> +<th>FROM/TO</th> +<th>sf</th> +<th>sp</th> +<th>terra</th> +</tr> +</thead> +<tbody> +<tr class="odd"> +<td>sf</td> +<td></td> +<td>as(x, ‘Spatial’)</td> +<td>vect()</td> +</tr> +<tr class="even"> +<td>sp</td> +<td>st_as_sf()</td> +<td></td> +<td>vect()</td> +</tr> +<tr class="odd"> +<td>terra</td> +<td>st_as_sf()</td> +<td>as(x, ‘Spatial’)</td> +<td></td> +</tr> +</tbody> +</table> + + +<div id="refs" class="references csl-bib-body hanging-indent" role="doc-bibliography" style="display: none"> +<div id="ref-terra" class="csl-entry" role="doc-biblioentry"> +Hijmans, Robert J. 2022. <span>“Terra: Spatial Data Analysis.â€</span> <a href="https://CRAN.R-project.org/package=terra">https://CRAN.R-project.org/package=terra</a>. +</div> +<div id="ref-XingongLi2009" class="csl-entry" role="doc-biblioentry"> +Li, Xingong. 2009. <span>“Map Algebra and Beyond : 1. Map Algebra for Scalar Fields.â€</span> <a href="https://slideplayer.com/slide/5822638/" class="uri">https://slideplayer.com/slide/5822638/</a>. +</div> +<div id="ref-MmadelinSIGR" class="csl-entry" role="doc-biblioentry"> +Madelin, Malika. 2021. <span>“Analyse d’images Raster (Et Télédétection).â€</span> <a href="https://mmadelin.github.io/sigr2021/SIGR2021_raster_MM.html" class="uri">https://mmadelin.github.io/sigr2021/SIGR2021_raster_MM.html</a>. +</div> +<div id="ref-JMennis2015" class="csl-entry" role="doc-biblioentry"> +Mennis, Jeremy. 2015. <span>“Fundamentals of GIS : Raster Operations.â€</span> <a href="https://cupdf.com/document/gus-0262-fundamentals-of-gis-lecture-presentation-7-raster-operations-jeremy.html" class="uri">https://cupdf.com/document/gus-0262-fundamentals-of-gis-lecture-presentation-7-raster-operations-jeremy.html</a>. +</div> +<div id="ref-JNowosadSIGR" class="csl-entry" role="doc-biblioentry"> +Nowosad, Jakub. 2021. <span>“Image Processing and All Things Raster.â€</span> <a href="https://nowosad.github.io/SIGR2021/workshop2/workshop2.html" class="uri">https://nowosad.github.io/SIGR2021/workshop2/workshop2.html</a>. +</div> +<div id="ref-RasterCheatSheet" class="csl-entry" role="doc-biblioentry"> +Racine, Etienne B. 2016. <span>“The Visual Raster Cheat Sheet.â€</span> <a href="https://rpubs.com/etiennebr/visualraster" class="uri">https://rpubs.com/etiennebr/visualraster</a>. +</div> +</div> +</section> +</section> + +</main> <!-- /main --> +<script id="quarto-html-after-body" type="application/javascript"> +window.document.addEventListener("DOMContentLoaded", function (event) { + const icon = ""; + const anchorJS = new window.AnchorJS(); + anchorJS.options = { + placement: 'right', + icon: icon + }; + anchorJS.add('.anchored'); + const clipboard = new window.ClipboardJS('.code-copy-button', { + target: function(trigger) { + return trigger.previousElementSibling; + } + }); + clipboard.on('success', function(e) { + // button target + const button = e.trigger; + // don't keep focus + button.blur(); + // flash "checked" + button.classList.add('code-copy-button-checked'); + var currentTitle = button.getAttribute("title"); + button.setAttribute("title", "Copied!"); + setTimeout(function() { + button.setAttribute("title", currentTitle); + button.classList.remove('code-copy-button-checked'); + }, 1000); + // clear code selection + e.clearSelection(); + }); + function tippyHover(el, contentFn) { + const config = { + allowHTML: true, + content: contentFn, + maxWidth: 500, + delay: 100, + arrow: false, + appendTo: function(el) { + return el.parentElement; + }, + interactive: true, + interactiveBorder: 10, + theme: 'quarto', + placement: 'bottom-start' + }; + window.tippy(el, config); + } + const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]'); + for (var i=0; i<noterefs.length; i++) { + const ref = noterefs[i]; + tippyHover(ref, function() { + let href = ref.getAttribute('href'); + try { href = new URL(href).hash; } catch {} + const id = href.replace(/^#\/?/, ""); + const note = window.document.getElementById(id); + return note.innerHTML; + }); + } + var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]'); + for (var i=0; i<bibliorefs.length; i++) { + const ref = bibliorefs[i]; + const cites = ref.parentNode.getAttribute('data-cites').split(' '); + tippyHover(ref, function() { + var popup = window.document.createElement('div'); + cites.forEach(function(cite) { + var citeDiv = window.document.createElement('div'); + citeDiv.classList.add('hanging-indent'); + citeDiv.classList.add('csl-entry'); + var biblioDiv = window.document.getElementById('ref-' + cite); + if (biblioDiv) { + citeDiv.innerHTML = biblioDiv.innerHTML; + } + popup.appendChild(citeDiv); + }); + return popup.innerHTML; + }); + } + var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//); + var filterRegex = new RegExp('/' + window.location.host + '/'); + var isInternal = (href) => { + return filterRegex.test(href) || localhostRegex.test(href); + } + // Inspect non-navigation links and adorn them if external + var links = window.document.querySelectorAll('a:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external)'); + for (var i=0; i<links.length; i++) { + const link = links[i]; + if (!isInternal(link.href)) { + // target, if specified + link.setAttribute("target", "_blank"); + } + } +}); +</script> +<nav class="page-navigation"> + <div class="nav-page nav-page-previous"> + <a href="./03-vector_data.html" class="pagination-link"> + <i class="bi bi-arrow-left-short"></i> <span class="nav-page-text"><span class="chapter-number">3</span> <span class="chapter-title">Vector Data</span></span> + </a> + </div> + <div class="nav-page nav-page-next"> + <a href="./05-mapping_with_r.html" class="pagination-link"> + <span class="nav-page-text"><span class="chapter-number">5</span> <span class="chapter-title">Mapping With R</span></span> <i class="bi bi-arrow-right-short"></i> + </a> + </div> +</nav> +</div> <!-- /content --> +<footer class="footer"> + <div class="nav-footer"> + <div class="nav-footer-left">UMR 228 ESPACE-DEV</div> + <div class="nav-footer-right"><img src="img/ird_footer.png" height="50"></div> + </div> +</footer> + + + +</body></html> \ No newline at end of file diff --git a/public/04-raster_data_files/figure-html/NDVI_1-1.png b/public/04-raster_data_files/figure-html/NDVI_1-1.png new file mode 100644 index 0000000000000000000000000000000000000000..363b9e7d099c58e5e21cbd0a4a9f8eb52cbd51d2 Binary files /dev/null and b/public/04-raster_data_files/figure-html/NDVI_1-1.png differ diff --git a/public/04-raster_data_files/figure-html/NDVI_3-1.png b/public/04-raster_data_files/figure-html/NDVI_3-1.png new file mode 100644 index 0000000000000000000000000000000000000000..dccbcb6b2395d22889ed63e3755e0ea39998bbbf Binary files /dev/null and b/public/04-raster_data_files/figure-html/NDVI_3-1.png differ diff --git a/public/04-raster_data_files/figure-html/Raster_vec11-1.png b/public/04-raster_data_files/figure-html/Raster_vec11-1.png new file mode 100644 index 0000000000000000000000000000000000000000..05f4bd0ae621130342885d3fdd98b2fb421a737e Binary files /dev/null and b/public/04-raster_data_files/figure-html/Raster_vec11-1.png differ diff --git a/public/04-raster_data_files/figure-html/Raster_vec2-1.png b/public/04-raster_data_files/figure-html/Raster_vec2-1.png new file mode 100644 index 0000000000000000000000000000000000000000..76406eb124a18d342766100ddca9a28bd8267c41 Binary files /dev/null and b/public/04-raster_data_files/figure-html/Raster_vec2-1.png differ diff --git a/public/04-raster_data_files/figure-html/Raster_vec33-1.png b/public/04-raster_data_files/figure-html/Raster_vec33-1.png new file mode 100644 index 0000000000000000000000000000000000000000..dd1c2fd03080739f291edc89d973d5a1ea421961 Binary files /dev/null and b/public/04-raster_data_files/figure-html/Raster_vec33-1.png differ diff --git a/public/04-raster_data_files/figure-html/Raster_vec44-1.png b/public/04-raster_data_files/figure-html/Raster_vec44-1.png new file mode 100644 index 0000000000000000000000000000000000000000..9c5b4638e3964ab5d7ac07f6285172680c724196 Binary files /dev/null and b/public/04-raster_data_files/figure-html/Raster_vec44-1.png differ diff --git a/public/04-raster_data_files/figure-html/Raster_vec55-1.png b/public/04-raster_data_files/figure-html/Raster_vec55-1.png new file mode 100644 index 0000000000000000000000000000000000000000..b7588e46f8b4987cdb715d9f99bccd8b10f0bb9c Binary files /dev/null and b/public/04-raster_data_files/figure-html/Raster_vec55-1.png differ diff --git a/public/04-raster_data_files/figure-html/Raster_vec66-1.png b/public/04-raster_data_files/figure-html/Raster_vec66-1.png new file mode 100644 index 0000000000000000000000000000000000000000..e623f91babf30cbe15608831fd4048e6362d0852 Binary files /dev/null and b/public/04-raster_data_files/figure-html/Raster_vec66-1.png differ diff --git a/public/04-raster_data_files/figure-html/affichage_1_raster-1.png b/public/04-raster_data_files/figure-html/affichage_1_raster-1.png new file mode 100644 index 0000000000000000000000000000000000000000..1ea5c12a47c2c26355a898238aed2c0cb670e0c5 Binary files /dev/null and b/public/04-raster_data_files/figure-html/affichage_1_raster-1.png differ diff --git a/public/04-raster_data_files/figure-html/affichage_raster_2-1.png b/public/04-raster_data_files/figure-html/affichage_raster_2-1.png new file mode 100644 index 0000000000000000000000000000000000000000..26040052560d5145c409e9a8cd4a8e40421ecff6 Binary files /dev/null and b/public/04-raster_data_files/figure-html/affichage_raster_2-1.png differ diff --git a/public/04-raster_data_files/figure-html/affichage_raster_3-1.png b/public/04-raster_data_files/figure-html/affichage_raster_3-1.png new file mode 100644 index 0000000000000000000000000000000000000000..e24e954c97f3dddba568c245b547161cf1463266 Binary files /dev/null and b/public/04-raster_data_files/figure-html/affichage_raster_3-1.png differ diff --git a/public/04-raster_data_files/figure-html/agr_raster_2-1.png b/public/04-raster_data_files/figure-html/agr_raster_2-1.png new file mode 100644 index 0000000000000000000000000000000000000000..1d6fe21c05443eeb997373248d54a6c9a2225abd Binary files /dev/null and b/public/04-raster_data_files/figure-html/agr_raster_2-1.png differ diff --git a/public/04-raster_data_files/figure-html/crop_raster_3-1.png b/public/04-raster_data_files/figure-html/crop_raster_3-1.png new file mode 100644 index 0000000000000000000000000000000000000000..82da0d58aaf42728497036b3d5cae605ad94122f Binary files /dev/null and b/public/04-raster_data_files/figure-html/crop_raster_3-1.png differ diff --git a/public/04-raster_data_files/figure-html/mask_raster-1.png b/public/04-raster_data_files/figure-html/mask_raster-1.png new file mode 100644 index 0000000000000000000000000000000000000000..c9143ea235101db07497ce799828986908a9f26b Binary files /dev/null and b/public/04-raster_data_files/figure-html/mask_raster-1.png differ diff --git a/public/04-raster_data_files/figure-html/merge_raster_1-1.png b/public/04-raster_data_files/figure-html/merge_raster_1-1.png new file mode 100644 index 0000000000000000000000000000000000000000..e49fdfda0c8c18f888b441fa10905c232acfee73 Binary files /dev/null and b/public/04-raster_data_files/figure-html/merge_raster_1-1.png differ diff --git a/public/04-raster_data_files/figure-html/merge_raster_2-1.png b/public/04-raster_data_files/figure-html/merge_raster_2-1.png new file mode 100644 index 0000000000000000000000000000000000000000..e89586f34d88c6c4c9d4d1b715e900afbdca1944 Binary files /dev/null and b/public/04-raster_data_files/figure-html/merge_raster_2-1.png differ diff --git a/public/04-raster_data_files/figure-html/op_focal_5-1.png b/public/04-raster_data_files/figure-html/op_focal_5-1.png new file mode 100644 index 0000000000000000000000000000000000000000..f335a688be032f6f023774ba5a80c48e3a3c61a4 Binary files /dev/null and b/public/04-raster_data_files/figure-html/op_focal_5-1.png differ diff --git a/public/04-raster_data_files/figure-html/op_focal_6-1.png b/public/04-raster_data_files/figure-html/op_focal_6-1.png new file mode 100644 index 0000000000000000000000000000000000000000..9d75457e2c4d307f76f99fca5e6d93f0cfa32c7e Binary files /dev/null and b/public/04-raster_data_files/figure-html/op_focal_6-1.png differ diff --git a/public/04-raster_data_files/figure-html/op_global_4-1.png b/public/04-raster_data_files/figure-html/op_global_4-1.png new file mode 100644 index 0000000000000000000000000000000000000000..719ad904a4f790ff344410cf8b0ecd9aff711bb7 Binary files /dev/null and b/public/04-raster_data_files/figure-html/op_global_4-1.png differ diff --git a/public/04-raster_data_files/figure-html/op_global_4-2.png b/public/04-raster_data_files/figure-html/op_global_4-2.png new file mode 100644 index 0000000000000000000000000000000000000000..21f5efeb4a5a68c4847cc7d18a63ae0ff8f3ca73 Binary files /dev/null and b/public/04-raster_data_files/figure-html/op_global_4-2.png differ diff --git a/public/04-raster_data_files/figure-html/op_local_2-1.png b/public/04-raster_data_files/figure-html/op_local_2-1.png new file mode 100644 index 0000000000000000000000000000000000000000..b76e22aa3f6cd6a08ace08c8d833888ea2a2b5e2 Binary files /dev/null and b/public/04-raster_data_files/figure-html/op_local_2-1.png differ diff --git a/public/04-raster_data_files/figure-html/op_zonal_2-1.png b/public/04-raster_data_files/figure-html/op_zonal_2-1.png new file mode 100644 index 0000000000000000000000000000000000000000..ef5c8c4476214d0f65eb24cde66f8fab75fa05f8 Binary files /dev/null and b/public/04-raster_data_files/figure-html/op_zonal_2-1.png differ diff --git a/public/04-raster_data_files/figure-html/reclass_4-1.png b/public/04-raster_data_files/figure-html/reclass_4-1.png new file mode 100644 index 0000000000000000000000000000000000000000..7c56d285b7aa4553b54522ef144c069ed6eb2707 Binary files /dev/null and b/public/04-raster_data_files/figure-html/reclass_4-1.png differ diff --git a/public/04-raster_data_files/figure-html/reclass_6-1.png b/public/04-raster_data_files/figure-html/reclass_6-1.png new file mode 100644 index 0000000000000000000000000000000000000000..edc537b16687f0420be2139f6de00952a0e5cdc0 Binary files /dev/null and b/public/04-raster_data_files/figure-html/reclass_6-1.png differ diff --git a/public/04-raster_data_files/figure-html/reproj_raster_2-1.png b/public/04-raster_data_files/figure-html/reproj_raster_2-1.png new file mode 100644 index 0000000000000000000000000000000000000000..3f396ce87d029eb9d09806db62569dee481c439f Binary files /dev/null and b/public/04-raster_data_files/figure-html/reproj_raster_2-1.png differ diff --git a/public/04-raster_data_files/figure-html/segregate-1.png b/public/04-raster_data_files/figure-html/segregate-1.png new file mode 100644 index 0000000000000000000000000000000000000000..f5a79a19ec8640777deeb735ffff6a3e69ad5bf1 Binary files /dev/null and b/public/04-raster_data_files/figure-html/segregate-1.png differ diff --git a/public/05-mapping_with_r.html b/public/05-mapping_with_r.html index 2702c8af8ba76ff55ad607c0a42c046b71e594ff..75c73e59485d1c72e89e51c88c66cc2a32ddee8f 100644 --- a/public/05-mapping_with_r.html +++ b/public/05-mapping_with_r.html @@ -7,7 +7,7 @@ <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> -<title>Mapping and spatial analyses in R for One Health studies - 4 Mapping With R</title> +<title>Mapping and spatial analyses in R for One Health studies - 5 Mapping With R</title> <style> code{white-space: pre-wrap;} span.smallcaps{font-variant: small-caps;} @@ -108,7 +108,7 @@ div.csl-indent { <script src="site_libs/quarto-search/quarto-search.js"></script> <meta name="quarto:offset" content="./"> <link href="./references.html" rel="next"> -<link href="./03-vector_data.html" rel="prev"> +<link href="./04-raster_data.html" rel="prev"> <script src="site_libs/quarto-html/quarto.js"></script> <script src="site_libs/quarto-html/popper.min.js"></script> <script src="site_libs/quarto-html/tippy.umd.min.js"></script> @@ -148,7 +148,7 @@ div.csl-indent { <header id="quarto-header" class="headroom fixed-top"> <nav class="quarto-secondary-nav" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }"> <div class="container-fluid d-flex justify-content-between"> - <h1 class="quarto-secondary-nav-title"><span class="chapter-number">4</span> <span class="chapter-title">Mapping With R</span></h1> + <h1 class="quarto-secondary-nav-title"><span class="chapter-number">5</span> <span class="chapter-title">Mapping With R</span></h1> <button type="button" class="quarto-btn-toggle btn" aria-label="Show secondary navigation"> <i class="bi bi-chevron-right"></i> </button> @@ -196,7 +196,12 @@ div.csl-indent { </li> <li class="sidebar-item"> <div class="sidebar-item-container"> - <a href="./05-mapping_with_r.html" class="sidebar-item-text sidebar-link active"><span class="chapter-number">4</span> <span class="chapter-title">Mapping With R</span></a> + <a href="./04-raster_data.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">4</span> <span class="chapter-title">Work with Raster Data</span></a> + </div> +</li> + <li class="sidebar-item"> + <div class="sidebar-item-container"> + <a href="./05-mapping_with_r.html" class="sidebar-item-text sidebar-link active"><span class="chapter-number">5</span> <span class="chapter-title">Mapping With R</span></a> </div> </li> <li class="sidebar-item"> @@ -213,47 +218,47 @@ div.csl-indent { <h2 id="toc-title">Table of contents</h2> <ul> - <li><a href="#types-of-maps" id="toc-types-of-maps" class="nav-link active" data-scroll-target="#types-of-maps"> <span class="header-section-number">4.1</span> Types of maps</a> + <li><a href="#types-of-maps" id="toc-types-of-maps" class="nav-link active" data-scroll-target="#types-of-maps"> <span class="header-section-number">5.1</span> Types of maps</a> <ul class="collapse"> - <li><a href="#example-data" id="toc-example-data" class="nav-link" data-scroll-target="#example-data"> <span class="header-section-number">4.1.1</span> Example data</a></li> - <li><a href="#displaying-a-background-map" id="toc-displaying-a-background-map" class="nav-link" data-scroll-target="#displaying-a-background-map"> <span class="header-section-number">4.1.2</span> Displaying a background map</a></li> - <li><a href="#proportional-symbol-map" id="toc-proportional-symbol-map" class="nav-link" data-scroll-target="#proportional-symbol-map"> <span class="header-section-number">4.1.3</span> Proportional symbol map</a></li> - <li><a href="#choropleth-map" id="toc-choropleth-map" class="nav-link" data-scroll-target="#choropleth-map"> <span class="header-section-number">4.1.4</span> Choropleth map</a></li> - <li><a href="#typology-map" id="toc-typology-map" class="nav-link" data-scroll-target="#typology-map"> <span class="header-section-number">4.1.5</span> Typology map</a></li> - <li><a href="#map-of-stocks-and-ratios" id="toc-map-of-stocks-and-ratios" class="nav-link" data-scroll-target="#map-of-stocks-and-ratios"> <span class="header-section-number">4.1.6</span> Map of stocks and ratios</a></li> - <li><a href="#map-of-stocks-and-categories" id="toc-map-of-stocks-and-categories" class="nav-link" data-scroll-target="#map-of-stocks-and-categories"> <span class="header-section-number">4.1.7</span> Map of stocks and categories</a></li> + <li><a href="#example-data" id="toc-example-data" class="nav-link" data-scroll-target="#example-data"> <span class="header-section-number">5.1.1</span> Example data</a></li> + <li><a href="#displaying-a-background-map" id="toc-displaying-a-background-map" class="nav-link" data-scroll-target="#displaying-a-background-map"> <span class="header-section-number">5.1.2</span> Displaying a background map</a></li> + <li><a href="#proportional-symbol-map" id="toc-proportional-symbol-map" class="nav-link" data-scroll-target="#proportional-symbol-map"> <span class="header-section-number">5.1.3</span> Proportional symbol map</a></li> + <li><a href="#choropleth-map" id="toc-choropleth-map" class="nav-link" data-scroll-target="#choropleth-map"> <span class="header-section-number">5.1.4</span> Choropleth map</a></li> + <li><a href="#typology-map" id="toc-typology-map" class="nav-link" data-scroll-target="#typology-map"> <span class="header-section-number">5.1.5</span> Typology map</a></li> + <li><a href="#map-of-stocks-and-ratios" id="toc-map-of-stocks-and-ratios" class="nav-link" data-scroll-target="#map-of-stocks-and-ratios"> <span class="header-section-number">5.1.6</span> Map of stocks and ratios</a></li> + <li><a href="#map-of-stocks-and-categories" id="toc-map-of-stocks-and-categories" class="nav-link" data-scroll-target="#map-of-stocks-and-categories"> <span class="header-section-number">5.1.7</span> Map of stocks and categories</a></li> </ul></li> - <li><a href="#layout" id="toc-layout" class="nav-link" data-scroll-target="#layout"> <span class="header-section-number">4.2</span> Layout</a> + <li><a href="#layout" id="toc-layout" class="nav-link" data-scroll-target="#layout"> <span class="header-section-number">5.2</span> Layout</a> <ul class="collapse"> - <li><a href="#example-data-1" id="toc-example-data-1" class="nav-link" data-scroll-target="#example-data-1"> <span class="header-section-number">4.2.1</span> Example data</a></li> - <li><a href="#themes" id="toc-themes" class="nav-link" data-scroll-target="#themes"> <span class="header-section-number">4.2.2</span> Themes</a></li> - <li><a href="#titles" id="toc-titles" class="nav-link" data-scroll-target="#titles"> <span class="header-section-number">4.2.3</span> Titles</a></li> - <li><a href="#arrow-orientation" id="toc-arrow-orientation" class="nav-link" data-scroll-target="#arrow-orientation"> <span class="header-section-number">4.2.4</span> Arrow orientation</a></li> - <li><a href="#scale" id="toc-scale" class="nav-link" data-scroll-target="#scale"> <span class="header-section-number">4.2.5</span> Scale</a></li> - <li><a href="#credits" id="toc-credits" class="nav-link" data-scroll-target="#credits"> <span class="header-section-number">4.2.6</span> Credits</a></li> - <li><a href="#complete-dressing" id="toc-complete-dressing" class="nav-link" data-scroll-target="#complete-dressing"> <span class="header-section-number">4.2.7</span> Complete dressing</a></li> - <li><a href="#annotations" id="toc-annotations" class="nav-link" data-scroll-target="#annotations"> <span class="header-section-number">4.2.8</span> Annotations</a></li> - <li><a href="#legends" id="toc-legends" class="nav-link" data-scroll-target="#legends"> <span class="header-section-number">4.2.9</span> Legends</a></li> - <li><a href="#labels" id="toc-labels" class="nav-link" data-scroll-target="#labels"> <span class="header-section-number">4.2.10</span> Labels</a></li> - <li><a href="#center-the-map-on-a-region" id="toc-center-the-map-on-a-region" class="nav-link" data-scroll-target="#center-the-map-on-a-region"> <span class="header-section-number">4.2.11</span> Center the map on a region</a></li> - <li><a href="#displaying-several-maps-on-the-sam-figure" id="toc-displaying-several-maps-on-the-sam-figure" class="nav-link" data-scroll-target="#displaying-several-maps-on-the-sam-figure"> <span class="header-section-number">4.2.12</span> Displaying several maps on the sam figure</a></li> - <li><a href="#exporting-maps" id="toc-exporting-maps" class="nav-link" data-scroll-target="#exporting-maps"> <span class="header-section-number">4.2.13</span> Exporting maps</a></li> - <li><a href="#adding-an-image-to-a-map" id="toc-adding-an-image-to-a-map" class="nav-link" data-scroll-target="#adding-an-image-to-a-map"> <span class="header-section-number">4.2.14</span> Adding an image to a map</a></li> - <li><a href="#place-an-item-precisely-on-the-map" id="toc-place-an-item-precisely-on-the-map" class="nav-link" data-scroll-target="#place-an-item-precisely-on-the-map"> <span class="header-section-number">4.2.15</span> Place an item precisely on the map</a></li> - <li><a href="#add-shading-to-a-layer" id="toc-add-shading-to-a-layer" class="nav-link" data-scroll-target="#add-shading-to-a-layer"> <span class="header-section-number">4.2.16</span> Add shading to a layer</a></li> - <li><a href="#creating-boxes" id="toc-creating-boxes" class="nav-link" data-scroll-target="#creating-boxes"> <span class="header-section-number">4.2.17</span> Creating Boxes</a></li> + <li><a href="#example-data-1" id="toc-example-data-1" class="nav-link" data-scroll-target="#example-data-1"> <span class="header-section-number">5.2.1</span> Example data</a></li> + <li><a href="#themes" id="toc-themes" class="nav-link" data-scroll-target="#themes"> <span class="header-section-number">5.2.2</span> Themes</a></li> + <li><a href="#titles" id="toc-titles" class="nav-link" data-scroll-target="#titles"> <span class="header-section-number">5.2.3</span> Titles</a></li> + <li><a href="#arrow-orientation" id="toc-arrow-orientation" class="nav-link" data-scroll-target="#arrow-orientation"> <span class="header-section-number">5.2.4</span> Arrow orientation</a></li> + <li><a href="#scale" id="toc-scale" class="nav-link" data-scroll-target="#scale"> <span class="header-section-number">5.2.5</span> Scale</a></li> + <li><a href="#credits" id="toc-credits" class="nav-link" data-scroll-target="#credits"> <span class="header-section-number">5.2.6</span> Credits</a></li> + <li><a href="#complete-dressing" id="toc-complete-dressing" class="nav-link" data-scroll-target="#complete-dressing"> <span class="header-section-number">5.2.7</span> Complete dressing</a></li> + <li><a href="#annotations" id="toc-annotations" class="nav-link" data-scroll-target="#annotations"> <span class="header-section-number">5.2.8</span> Annotations</a></li> + <li><a href="#legends" id="toc-legends" class="nav-link" data-scroll-target="#legends"> <span class="header-section-number">5.2.9</span> Legends</a></li> + <li><a href="#labels" id="toc-labels" class="nav-link" data-scroll-target="#labels"> <span class="header-section-number">5.2.10</span> Labels</a></li> + <li><a href="#center-the-map-on-a-region" id="toc-center-the-map-on-a-region" class="nav-link" data-scroll-target="#center-the-map-on-a-region"> <span class="header-section-number">5.2.11</span> Center the map on a region</a></li> + <li><a href="#displaying-several-maps-on-the-sam-figure" id="toc-displaying-several-maps-on-the-sam-figure" class="nav-link" data-scroll-target="#displaying-several-maps-on-the-sam-figure"> <span class="header-section-number">5.2.12</span> Displaying several maps on the sam figure</a></li> + <li><a href="#exporting-maps" id="toc-exporting-maps" class="nav-link" data-scroll-target="#exporting-maps"> <span class="header-section-number">5.2.13</span> Exporting maps</a></li> + <li><a href="#adding-an-image-to-a-map" id="toc-adding-an-image-to-a-map" class="nav-link" data-scroll-target="#adding-an-image-to-a-map"> <span class="header-section-number">5.2.14</span> Adding an image to a map</a></li> + <li><a href="#place-an-item-precisely-on-the-map" id="toc-place-an-item-precisely-on-the-map" class="nav-link" data-scroll-target="#place-an-item-precisely-on-the-map"> <span class="header-section-number">5.2.15</span> Place an item precisely on the map</a></li> + <li><a href="#add-shading-to-a-layer" id="toc-add-shading-to-a-layer" class="nav-link" data-scroll-target="#add-shading-to-a-layer"> <span class="header-section-number">5.2.16</span> Add shading to a layer</a></li> + <li><a href="#creating-boxes" id="toc-creating-boxes" class="nav-link" data-scroll-target="#creating-boxes"> <span class="header-section-number">5.2.17</span> Creating Boxes</a></li> </ul></li> - <li><a href="#d-maps" id="toc-d-maps" class="nav-link" data-scroll-target="#d-maps"> <span class="header-section-number">4.3</span> 3D maps</a> + <li><a href="#d-maps" id="toc-d-maps" class="nav-link" data-scroll-target="#d-maps"> <span class="header-section-number">5.3</span> 3D maps</a> <ul class="collapse"> - <li><a href="#linemap" id="toc-linemap" class="nav-link" data-scroll-target="#linemap"> <span class="header-section-number">4.3.1</span> linemap</a></li> - <li><a href="#relief-tanaka" id="toc-relief-tanaka" class="nav-link" data-scroll-target="#relief-tanaka"> <span class="header-section-number">4.3.2</span> Relief Tanaka</a></li> + <li><a href="#linemap" id="toc-linemap" class="nav-link" data-scroll-target="#linemap"> <span class="header-section-number">5.3.1</span> linemap</a></li> + <li><a href="#relief-tanaka" id="toc-relief-tanaka" class="nav-link" data-scroll-target="#relief-tanaka"> <span class="header-section-number">5.3.2</span> Relief Tanaka</a></li> </ul></li> - <li><a href="#cartographic-transformation" id="toc-cartographic-transformation" class="nav-link" data-scroll-target="#cartographic-transformation"> <span class="header-section-number">4.4</span> Cartographic Transformation</a> + <li><a href="#cartographic-transformation" id="toc-cartographic-transformation" class="nav-link" data-scroll-target="#cartographic-transformation"> <span class="header-section-number">5.4</span> Cartographic Transformation</a> <ul class="collapse"> - <li><a href="#dorlings-cartograms" id="toc-dorlings-cartograms" class="nav-link" data-scroll-target="#dorlings-cartograms"> <span class="header-section-number">4.4.1</span> Dorling’s cartograms</a></li> - <li><a href="#non-continuous-cartograms" id="toc-non-continuous-cartograms" class="nav-link" data-scroll-target="#non-continuous-cartograms"> <span class="header-section-number">4.4.2</span> Non-continuous cartograms</a></li> - <li><a href="#continuous-cartograms" id="toc-continuous-cartograms" class="nav-link" data-scroll-target="#continuous-cartograms"> <span class="header-section-number">4.4.3</span> Continuous cartograms</a></li> - <li><a href="#stengths-and-weaknessses-of-cartograms" id="toc-stengths-and-weaknessses-of-cartograms" class="nav-link" data-scroll-target="#stengths-and-weaknessses-of-cartograms"> <span class="header-section-number">4.4.4</span> Stengths and weaknessses of cartograms</a></li> + <li><a href="#dorlings-cartograms" id="toc-dorlings-cartograms" class="nav-link" data-scroll-target="#dorlings-cartograms"> <span class="header-section-number">5.4.1</span> Dorling’s cartograms</a></li> + <li><a href="#non-continuous-cartograms" id="toc-non-continuous-cartograms" class="nav-link" data-scroll-target="#non-continuous-cartograms"> <span class="header-section-number">5.4.2</span> Non-continuous cartograms</a></li> + <li><a href="#continuous-cartograms" id="toc-continuous-cartograms" class="nav-link" data-scroll-target="#continuous-cartograms"> <span class="header-section-number">5.4.3</span> Continuous cartograms</a></li> + <li><a href="#stengths-and-weaknessses-of-cartograms" id="toc-stengths-and-weaknessses-of-cartograms" class="nav-link" data-scroll-target="#stengths-and-weaknessses-of-cartograms"> <span class="header-section-number">5.4.4</span> Stengths and weaknessses of cartograms</a></li> </ul></li> </ul> </nav> @@ -263,7 +268,7 @@ div.csl-indent { <header id="title-block-header" class="quarto-title-block default"> <div class="quarto-title"> -<h1 class="title d-none d-lg-block"><span class="chapter-number">4</span> <span class="chapter-title">Mapping With R</span></h1> +<h1 class="title d-none d-lg-block"><span class="chapter-number">5</span> <span class="chapter-title">Mapping With R</span></h1> </div> @@ -277,16 +282,16 @@ div.csl-indent { </header> -<section id="types-of-maps" class="level2" data-number="4.1"> -<h2 data-number="4.1" class="anchored" data-anchor-id="types-of-maps"><span class="header-section-number">4.1</span> Types of maps</h2> +<section id="types-of-maps" class="level2" data-number="5.1"> +<h2 data-number="5.1" class="anchored" data-anchor-id="types-of-maps"><span class="header-section-number">5.1</span> Types of maps</h2> <p>The fonction <code>mf_map()</code> is the central function of the package <code>mapsf</code> <span class="citation" data-cites="mapsf">(<a href="references.html#ref-mapsf" role="doc-biblioref">Giraud 2022a</a>)</span>. It makes it possible to carry out most of the usual representations in cartography. These main arguments are:</p> <ul> <li><code>x</code>, an sf object ;</li> <li><code>var</code>, the name of variable to present ;</li> <li><code>type</code>, the type of presentation.</li> </ul> -<section id="example-data" class="level3" data-number="4.1.1"> -<h3 data-number="4.1.1" class="anchored" data-anchor-id="example-data"><span class="header-section-number">4.1.1</span> Example data</h3> +<section id="example-data" class="level3" data-number="5.1.1"> +<h3 data-number="5.1.1" class="anchored" data-anchor-id="example-data"><span class="header-section-number">5.1.1</span> Example data</h3> <p>The following lines import the spatial information layers located in the <a href="https://www.geopackage.org/">geopackage</a> <strong>cambodia.gpkg</strong> file.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(sf)</span> @@ -303,8 +308,8 @@ div.csl-indent { <span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a>hospital <span class="ot">=</span> <span class="fu">st_read</span>(<span class="st">"data_cambodia/cambodia.gpkg"</span>, <span class="at">layer =</span> <span class="st">"hospital"</span>, <span class="at">quiet =</span> <span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> </div> </section> -<section id="displaying-a-background-map" class="level3" data-number="4.1.2"> -<h3 data-number="4.1.2" class="anchored" data-anchor-id="displaying-a-background-map"><span class="header-section-number">4.1.2</span> Displaying a background map</h3> +<section id="displaying-a-background-map" class="level3" data-number="5.1.2"> +<h3 data-number="5.1.2" class="anchored" data-anchor-id="displaying-a-background-map"><span class="header-section-number">5.1.2</span> Displaying a background map</h3> <p>Without using types specification, the function <code>mf_map()</code> simply display the background map.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(mapsf)</span> @@ -318,8 +323,8 @@ div.csl-indent { </div> </div> </section> -<section id="proportional-symbol-map" class="level3" data-number="4.1.3"> -<h3 data-number="4.1.3" class="anchored" data-anchor-id="proportional-symbol-map"><span class="header-section-number">4.1.3</span> Proportional symbol map</h3> +<section id="proportional-symbol-map" class="level3" data-number="5.1.3"> +<h3 data-number="5.1.3" class="anchored" data-anchor-id="proportional-symbol-map"><span class="header-section-number">5.1.3</span> Proportional symbol map</h3> <p>Proportional symbol maps are used to represent inventory variables (absolute quantitative variables, sum and average make sense). The function <code>mf_map(..., type = "prop")</code> proposes this representation.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co">#District</span></span> @@ -341,8 +346,8 @@ div.csl-indent { <p><img src="05-mapping_with_r_files/figure-html/proportional_symbols-1.png" class="img-fluid" width="672"></p> </div> </div> -<section id="compare-multiple-map" class="level4" data-number="4.1.3.1"> -<h4 data-number="4.1.3.1" class="anchored" data-anchor-id="compare-multiple-map"><span class="header-section-number">4.1.3.1</span> Compare multiple map</h4> +<section id="compare-multiple-map" class="level4" data-number="5.1.3.1"> +<h4 data-number="5.1.3.1" class="anchored" data-anchor-id="compare-multiple-map"><span class="header-section-number">5.1.3.1</span> Compare multiple map</h4> <p>It is possible to fix the dimensions of the largest symbol corresponding to a certain value with the arguments <code>inches</code> and <code>val_max</code>. We can use construct maps with comparable proportional symbols.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">2</span>)) <span class="co">#Displaying two maps facing each other</span></span> @@ -383,8 +388,8 @@ div.csl-indent { <p>Here we have displayed two maps facing each other, see the point <a href="#afficher-plusieurs-cartes-sur-la-même-figure">Displaying several maps on the same figure</a> for more details.</p> </section> </section> -<section id="choropleth-map" class="level3" data-number="4.1.4"> -<h3 data-number="4.1.4" class="anchored" data-anchor-id="choropleth-map"><span class="header-section-number">4.1.4</span> Choropleth map</h3> +<section id="choropleth-map" class="level3" data-number="5.1.4"> +<h3 data-number="5.1.4" class="anchored" data-anchor-id="choropleth-map"><span class="header-section-number">5.1.4</span> Choropleth map</h3> <p>Choropleth maps are used to represent ratio variables (relative quantitative variables, mean has meaning, sum has no meaning).</p> <p>For this type of representation, you must first:</p> <ul> @@ -411,8 +416,8 @@ div.csl-indent { <p><img src="05-mapping_with_r_files/figure-html/choro-1.png" class="img-fluid" width="672"></p> </div> </div> -<section id="discretisation" class="level4" data-number="4.1.4.1"> -<h4 data-number="4.1.4.1" class="anchored" data-anchor-id="discretisation"><span class="header-section-number">4.1.4.1</span> Discretisation</h4> +<section id="discretisation" class="level4" data-number="5.1.4.1"> +<h4 data-number="5.1.4.1" class="anchored" data-anchor-id="discretisation"><span class="header-section-number">5.1.4.1</span> Discretisation</h4> <p>The fonction <code>mf_get_breaks()</code> provides the methods of discretization of classic variables: quantiles, average/standard deviation, equal amplitudes, nested averages, Fisher-Jenks, geometric, etc.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>education<span class="sc">$</span>enrol_g_pct <span class="ot">=</span> <span class="dv">100</span> <span class="sc">*</span> education<span class="sc">$</span>enrol_girl<span class="sc">/</span>education<span class="sc">$</span>t_enrol <span class="co">#Calculate percentage of enrolled girl student</span></span> @@ -428,8 +433,8 @@ div.csl-indent { </div> </div> </section> -<section id="palettes" class="level4" data-number="4.1.4.2"> -<h4 data-number="4.1.4.2" class="anchored" data-anchor-id="palettes"><span class="header-section-number">4.1.4.2</span> Color palettes</h4> +<section id="palettes" class="level4" data-number="5.1.4.2"> +<h4 data-number="5.1.4.2" class="anchored" data-anchor-id="palettes"><span class="header-section-number">5.1.4.2</span> Color palettes</h4> <p>The argument <code>pal</code> de <code>mf_map()</code> is dedicated to choosing a color palette. The palettes provided by the function <code>hcl.colors()</code> can be used directly.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_map</span>(<span class="at">x =</span> education, <span class="at">var =</span> <span class="st">"enrol_g_pct"</span>, <span class="at">type =</span> <span class="st">"choro"</span>,</span> @@ -453,8 +458,8 @@ div.csl-indent { </div> </div> </section> -<section id="for-a-point-layer" class="level4" data-number="4.1.4.3"> -<h4 data-number="4.1.4.3" class="anchored" data-anchor-id="for-a-point-layer"><span class="header-section-number">4.1.4.3</span> For a point layer</h4> +<section id="for-a-point-layer" class="level4" data-number="5.1.4.3"> +<h4 data-number="5.1.4.3" class="anchored" data-anchor-id="for-a-point-layer"><span class="header-section-number">5.1.4.3</span> For a point layer</h4> <p>It is possible to use this mode of presentation in specific implementation also.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>dist_c <span class="ot"><-</span> <span class="fu">st_centroid</span>(district)</span> @@ -482,8 +487,8 @@ div.csl-indent { </div> </section> </section> -<section id="typology-map" class="level3" data-number="4.1.5"> -<h3 data-number="4.1.5" class="anchored" data-anchor-id="typology-map"><span class="header-section-number">4.1.5</span> Typology map</h3> +<section id="typology-map" class="level3" data-number="5.1.5"> +<h3 data-number="5.1.5" class="anchored" data-anchor-id="typology-map"><span class="header-section-number">5.1.5</span> Typology map</h3> <p>Typology maps are used to represent qualitative variables. The function <code>mf_map(..., type = "typo")</code> proposes this representation.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_map</span>(</span> @@ -499,8 +504,8 @@ div.csl-indent { <p><img src="05-mapping_with_r_files/figure-html/typo_simple-1.png" class="img-fluid" width="672"></p> </div> </div> -<section id="ordering-value-in-the-legend" class="level4" data-number="4.1.5.1"> -<h4 data-number="4.1.5.1" class="anchored" data-anchor-id="ordering-value-in-the-legend"><span class="header-section-number">4.1.5.1</span> Ordering value in the legend</h4> +<section id="ordering-value-in-the-legend" class="level4" data-number="5.1.5.1"> +<h4 data-number="5.1.5.1" class="anchored" data-anchor-id="ordering-value-in-the-legend"><span class="header-section-number">5.1.5.1</span> Ordering value in the legend</h4> <p>The argument <code>val_order</code> is used to order the categories in the</p> <div class="cell"> <div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_map</span>(</span> @@ -518,8 +523,8 @@ div.csl-indent { </div> </div> </section> -<section id="map-of-point" class="level4" data-number="4.1.5.2"> -<h4 data-number="4.1.5.2" class="anchored" data-anchor-id="map-of-point"><span class="header-section-number">4.1.5.2</span> Map of point</h4> +<section id="map-of-point" class="level4" data-number="5.1.5.2"> +<h4 data-number="5.1.5.2" class="anchored" data-anchor-id="map-of-point"><span class="header-section-number">5.1.5.2</span> Map of point</h4> <p>When the implantation of the layer is punctual, symbols are used to carry the colors of the typology.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="co">#extract centroid point of the district</span></span> @@ -542,8 +547,8 @@ div.csl-indent { </div> </div> </section> -<section id="map-of-lines" class="level4" data-number="4.1.5.3"> -<h4 data-number="4.1.5.3" class="anchored" data-anchor-id="map-of-lines"><span class="header-section-number">4.1.5.3</span> Map of lines</h4> +<section id="map-of-lines" class="level4" data-number="5.1.5.3"> +<h4 data-number="5.1.5.3" class="anchored" data-anchor-id="map-of-lines"><span class="header-section-number">5.1.5.3</span> Map of lines</h4> <div class="cell"> <div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="co">#Selection of roads that intersect the city of Siem Reap</span></span> <span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a>pp <span class="ot"><-</span> district[district<span class="sc">$</span>ADM1_EN <span class="sc">==</span> <span class="st">"Phnom Penh"</span>, ]</span> @@ -567,8 +572,8 @@ div.csl-indent { </div> </section> </section> -<section id="map-of-stocks-and-ratios" class="level3" data-number="4.1.6"> -<h3 data-number="4.1.6" class="anchored" data-anchor-id="map-of-stocks-and-ratios"><span class="header-section-number">4.1.6</span> Map of stocks and ratios</h3> +<section id="map-of-stocks-and-ratios" class="level3" data-number="5.1.6"> +<h3 data-number="5.1.6" class="anchored" data-anchor-id="map-of-stocks-and-ratios"><span class="header-section-number">5.1.6</span> Map of stocks and ratios</h3> <p>The function <code>mf_map(..., var = c("var1", "var2"), type = "prop_choro")</code> represents proportional symbols whose areas are proportional to the values of one variable and whose color is based on the discretization of a second variable. The function uses the arguments of the functions <code>mf_map(..., type = "prop")</code> and <code>mf_map(..., type = "choro")</code>.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_map</span>(<span class="at">x =</span> district)</span> @@ -590,8 +595,8 @@ div.csl-indent { </div> </div> </section> -<section id="map-of-stocks-and-categories" class="level3" data-number="4.1.7"> -<h3 data-number="4.1.7" class="anchored" data-anchor-id="map-of-stocks-and-categories"><span class="header-section-number">4.1.7</span> Map of stocks and categories</h3> +<section id="map-of-stocks-and-categories" class="level3" data-number="5.1.7"> +<h3 data-number="5.1.7" class="anchored" data-anchor-id="map-of-stocks-and-categories"><span class="header-section-number">5.1.7</span> Map of stocks and categories</h3> <p>The function <code>mf_map(..., var = c("var1", "var2"), type = "prop_typo")</code> represents proportional symbols whose areas are proportional to the values of one variable and whose color is based on the discretization of a second variable. The function uses the arguments of the <code>mf_map(..., type = "prop")</code> and function <code>mf_map(..., type = "typo")</code>.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_map</span>(<span class="at">x =</span> district)</span> @@ -612,11 +617,11 @@ div.csl-indent { </div> </section> </section> -<section id="layout" class="level2" data-number="4.2"> -<h2 data-number="4.2" class="anchored" data-anchor-id="layout"><span class="header-section-number">4.2</span> Layout</h2> +<section id="layout" class="level2" data-number="5.2"> +<h2 data-number="5.2" class="anchored" data-anchor-id="layout"><span class="header-section-number">5.2</span> Layout</h2> <p>To be finalized, a thematic map must contain certain additional elements such as: title, author, source, scale, orientation…</p> -<section id="example-data-1" class="level3" data-number="4.2.1"> -<h3 data-number="4.2.1" class="anchored" data-anchor-id="example-data-1"><span class="header-section-number">4.2.1</span> Example data</h3> +<section id="example-data-1" class="level3" data-number="5.2.1"> +<h3 data-number="5.2.1" class="anchored" data-anchor-id="example-data-1"><span class="header-section-number">5.2.1</span> Example data</h3> <p>The following lines import the spatial information layers located in the <a href="https://www.geopackage.org/">geopackage</a> <strong>lot46.gpkg</strong> file.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(sf)</span> @@ -628,11 +633,11 @@ div.csl-indent { <span id="cb16-7"><a href="#cb16-7" aria-hidden="true" tabindex="-1"></a>cases <span class="ot">=</span> <span class="fu">st_read</span>(<span class="st">"data_cambodia/cambodia.gpkg"</span>, <span class="at">layer =</span> <span class="st">"cases"</span>, <span class="at">quiet =</span> <span class="cn">TRUE</span>) <span class="co">#Import example data of fever_cases in Cambodia</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> </div> </section> -<section id="themes" class="level3" data-number="4.2.2"> -<h3 data-number="4.2.2" class="anchored" data-anchor-id="themes"><span class="header-section-number">4.2.2</span> Themes</h3> +<section id="themes" class="level3" data-number="5.2.2"> +<h3 data-number="5.2.2" class="anchored" data-anchor-id="themes"><span class="header-section-number">5.2.2</span> Themes</h3> <p>The function <code>mf_theme()</code> defines a cartographic theme. Using a theme allows you to define several graphic parameters which are then applied to the maps created with <code>mapsf</code>. These parameters are: the map margins, the main color, the background color, the position and the aspect of the title. A theme can also be defined with the <code>mf_init()</code> and function <code>mf_export()</code>.</p> -<section id="use-a-predefined-theme" class="level4" data-number="4.2.2.1"> -<h4 data-number="4.2.2.1" class="anchored" data-anchor-id="use-a-predefined-theme"><span class="header-section-number">4.2.2.1</span> Use a predefined theme</h4> +<section id="use-a-predefined-theme" class="level4" data-number="5.2.2.1"> +<h4 data-number="5.2.2.1" class="anchored" data-anchor-id="use-a-predefined-theme"><span class="header-section-number">5.2.2.1</span> Use a predefined theme</h4> <p>A series of predefined themes are available by default (see <code>?mf_theme</code>).</p> <div class="cell"> <div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(mapsf)</span> @@ -660,8 +665,8 @@ div.csl-indent { </div> </div> </section> -<section id="modify-an-existing-theme" class="level4" data-number="4.2.2.2"> -<h4 data-number="4.2.2.2" class="anchored" data-anchor-id="modify-an-existing-theme"><span class="header-section-number">4.2.2.2</span> Modify an existing theme</h4> +<section id="modify-an-existing-theme" class="level4" data-number="5.2.2.2"> +<h4 data-number="5.2.2.2" class="anchored" data-anchor-id="modify-an-existing-theme"><span class="header-section-number">5.2.2.2</span> Modify an existing theme</h4> <p>It is possible to modify an existing theme. In this example, we are using the “default†theme and modifying a few settings.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(mapsf)</span> @@ -679,8 +684,8 @@ div.csl-indent { </div> </div> </section> -<section id="create-a-theme" class="level4" data-number="4.2.2.3"> -<h4 data-number="4.2.2.3" class="anchored" data-anchor-id="create-a-theme"><span class="header-section-number">4.2.2.3</span> Create a theme</h4> +<section id="create-a-theme" class="level4" data-number="5.2.2.3"> +<h4 data-number="5.2.2.3" class="anchored" data-anchor-id="create-a-theme"><span class="header-section-number">5.2.2.3</span> Create a theme</h4> <p>It is also possible to create a theme.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_theme</span>(</span> @@ -702,8 +707,8 @@ div.csl-indent { </div> </section> </section> -<section id="titles" class="level3" data-number="4.2.3"> -<h3 data-number="4.2.3" class="anchored" data-anchor-id="titles"><span class="header-section-number">4.2.3</span> Titles</h3> +<section id="titles" class="level3" data-number="5.2.3"> +<h3 data-number="5.2.3" class="anchored" data-anchor-id="titles"><span class="header-section-number">5.2.3</span> Titles</h3> <p>The function <code>mf_title()</code> adds a title to a map.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_theme</span>(<span class="st">"default"</span>)</span> @@ -732,8 +737,8 @@ div.csl-indent { </div> </div> </section> -<section id="arrow-orientation" class="level3" data-number="4.2.4"> -<h3 data-number="4.2.4" class="anchored" data-anchor-id="arrow-orientation"><span class="header-section-number">4.2.4</span> Arrow orientation</h3> +<section id="arrow-orientation" class="level3" data-number="5.2.4"> +<h3 data-number="5.2.4" class="anchored" data-anchor-id="arrow-orientation"><span class="header-section-number">5.2.4</span> Arrow orientation</h3> <p>The function <code>mf_arrow()</code> allows you to choose the position and aspect of orientation arrow.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_map</span>(district)</span> @@ -743,8 +748,8 @@ div.csl-indent { </div> </div> </section> -<section id="scale" class="level3" data-number="4.2.5"> -<h3 data-number="4.2.5" class="anchored" data-anchor-id="scale"><span class="header-section-number">4.2.5</span> Scale</h3> +<section id="scale" class="level3" data-number="5.2.5"> +<h3 data-number="5.2.5" class="anchored" data-anchor-id="scale"><span class="header-section-number">5.2.5</span> Scale</h3> <p>The function <code>mf_scale()</code> allows you to choose the position and the aspect of the scale.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_map</span>(district)</span> @@ -758,8 +763,8 @@ div.csl-indent { </div> </div> </section> -<section id="credits" class="level3" data-number="4.2.6"> -<h3 data-number="4.2.6" class="anchored" data-anchor-id="credits"><span class="header-section-number">4.2.6</span> Credits</h3> +<section id="credits" class="level3" data-number="5.2.6"> +<h3 data-number="5.2.6" class="anchored" data-anchor-id="credits"><span class="header-section-number">5.2.6</span> Credits</h3> <p>The function <code>mf_credits()</code> displays a line of credits (sources, author, etc.).</p> <div class="cell"> <div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_map</span>(district)</span> @@ -769,8 +774,8 @@ div.csl-indent { </div> </div> </section> -<section id="complete-dressing" class="level3" data-number="4.2.7"> -<h3 data-number="4.2.7" class="anchored" data-anchor-id="complete-dressing"><span class="header-section-number">4.2.7</span> Complete dressing</h3> +<section id="complete-dressing" class="level3" data-number="5.2.7"> +<h3 data-number="5.2.7" class="anchored" data-anchor-id="complete-dressing"><span class="header-section-number">5.2.7</span> Complete dressing</h3> <p>The function <code>mf_layout()</code> displays all these elements.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_map</span>(district)</span> @@ -784,8 +789,8 @@ div.csl-indent { </div> </div> </section> -<section id="annotations" class="level3" data-number="4.2.8"> -<h3 data-number="4.2.8" class="anchored" data-anchor-id="annotations"><span class="header-section-number">4.2.8</span> Annotations</h3> +<section id="annotations" class="level3" data-number="5.2.8"> +<h3 data-number="5.2.8" class="anchored" data-anchor-id="annotations"><span class="header-section-number">5.2.8</span> Annotations</h3> <div class="cell"> <div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_map</span>(district)</span> <span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_annotation</span>(district[district<span class="sc">$</span>ADM2_EN <span class="sc">==</span> <span class="st">"Bakan"</span>,], <span class="at">txt =</span> <span class="st">"Bakan"</span>, <span class="at">col_txt =</span> <span class="st">"darkred"</span>, <span class="at">halo =</span> <span class="cn">TRUE</span>, <span class="at">cex =</span> <span class="fl">1.5</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> @@ -794,8 +799,8 @@ div.csl-indent { </div> </div> </section> -<section id="legends" class="level3" data-number="4.2.9"> -<h3 data-number="4.2.9" class="anchored" data-anchor-id="legends"><span class="header-section-number">4.2.9</span> Legends</h3> +<section id="legends" class="level3" data-number="5.2.9"> +<h3 data-number="5.2.9" class="anchored" data-anchor-id="legends"><span class="header-section-number">5.2.9</span> Legends</h3> <div class="cell"> <div class="sourceCode cell-code" id="cb27"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_map</span>(district)</span> <span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_legend</span>(</span> @@ -817,8 +822,8 @@ div.csl-indent { </div> </div> </section> -<section id="labels" class="level3" data-number="4.2.10"> -<h3 data-number="4.2.10" class="anchored" data-anchor-id="labels"><span class="header-section-number">4.2.10</span> Labels</h3> +<section id="labels" class="level3" data-number="5.2.10"> +<h3 data-number="5.2.10" class="anchored" data-anchor-id="labels"><span class="header-section-number">5.2.10</span> Labels</h3> <p>The function <code>mf_label()</code> is dedicated to displaying labels.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb28"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a>dist_selected <span class="ot"><-</span> district[<span class="fu">st_intersects</span>(district, district[district<span class="sc">$</span>ADM2_EN <span class="sc">==</span> <span class="st">"Bakan"</span>, ], <span class="at">sparse =</span> F), ]</span> @@ -839,8 +844,8 @@ div.csl-indent { </div> <p>The argument <code>halo = TRUE</code> allows to display a slight halo around the labels and the argument <code>overlap = FALSE</code> allows to create non-overlapping labels.</p> </section> -<section id="center-the-map-on-a-region" class="level3" data-number="4.2.11"> -<h3 data-number="4.2.11" class="anchored" data-anchor-id="center-the-map-on-a-region"><span class="header-section-number">4.2.11</span> Center the map on a region</h3> +<section id="center-the-map-on-a-region" class="level3" data-number="5.2.11"> +<h3 data-number="5.2.11" class="anchored" data-anchor-id="center-the-map-on-a-region"><span class="header-section-number">5.2.11</span> Center the map on a region</h3> <p>The function <code>mf_init()</code> allows you to initialize a map by centering it on a spatial object.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb29"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_init</span>(<span class="at">x =</span> dist_selected)</span> @@ -851,8 +856,8 @@ div.csl-indent { </div> </div> </section> -<section id="displaying-several-maps-on-the-sam-figure" class="level3" data-number="4.2.12"> -<h3 data-number="4.2.12" class="anchored" data-anchor-id="displaying-several-maps-on-the-sam-figure"><span class="header-section-number">4.2.12</span> Displaying several maps on the sam figure</h3> +<section id="displaying-several-maps-on-the-sam-figure" class="level3" data-number="5.2.12"> +<h3 data-number="5.2.12" class="anchored" data-anchor-id="displaying-several-maps-on-the-sam-figure"><span class="header-section-number">5.2.12</span> Displaying several maps on the sam figure</h3> <p>Here you have to use <code>mfrow</code> of the function <code>par()</code>. The first digit represents the number of of rows and second the number of columns.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb30"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="co"># define the figure layout (1 row, 2 columns)</span></span> @@ -872,8 +877,8 @@ div.csl-indent { </div> </div> </section> -<section id="exporting-maps" class="level3" data-number="4.2.13"> -<h3 data-number="4.2.13" class="anchored" data-anchor-id="exporting-maps"><span class="header-section-number">4.2.13</span> Exporting maps</h3> +<section id="exporting-maps" class="level3" data-number="5.2.13"> +<h3 data-number="5.2.13" class="anchored" data-anchor-id="exporting-maps"><span class="header-section-number">5.2.13</span> Exporting maps</h3> <p>It is quite difficult to export figures (maps or others) whose height/width ratio is satisfactory. The default ratio of figures in png format is 1 (480x480 pixels):</p> <div class="cell"> <div class="sourceCode cell-code" id="cb31"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a>dist_filter <span class="ot"><-</span> district[district<span class="sc">$</span>ADM2_PCODE <span class="sc">==</span> <span class="st">"KH0808"</span>, ]</span> @@ -902,8 +907,8 @@ div.csl-indent { </div> <p>The extent of this map is exactly that of the displayed region.</p> </section> -<section id="adding-an-image-to-a-map" class="level3" data-number="4.2.14"> -<h3 data-number="4.2.14" class="anchored" data-anchor-id="adding-an-image-to-a-map"><span class="header-section-number">4.2.14</span> Adding an image to a map</h3> +<section id="adding-an-image-to-a-map" class="level3" data-number="5.2.14"> +<h3 data-number="5.2.14" class="anchored" data-anchor-id="adding-an-image-to-a-map"><span class="header-section-number">5.2.14</span> Adding an image to a map</h3> <p>This can be useful for adding a logo, a pictograph. The function <code>readPNG()</code> of package <code>png</code> allows the additional images on the figure.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb33"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_theme</span>(<span class="st">"default"</span>, <span class="at">mar =</span> <span class="fu">c</span>(<span class="dv">0</span>,<span class="dv">0</span>,<span class="dv">0</span>,<span class="dv">0</span>))</span> @@ -927,8 +932,8 @@ div.csl-indent { </div> </div> </section> -<section id="place-an-item-precisely-on-the-map" class="level3" data-number="4.2.15"> -<h3 data-number="4.2.15" class="anchored" data-anchor-id="place-an-item-precisely-on-the-map"><span class="header-section-number">4.2.15</span> Place an item precisely on the map</h3> +<section id="place-an-item-precisely-on-the-map" class="level3" data-number="5.2.15"> +<h3 data-number="5.2.15" class="anchored" data-anchor-id="place-an-item-precisely-on-the-map"><span class="header-section-number">5.2.15</span> Place an item precisely on the map</h3> <p>The function <code>locator()</code> allows clicking on the figure and obtaining the coordinate of a point in the coordinate system of the figure (of the map).</p> <div class="cell"> <pre class="place cell-code"><code># locator(1) # click to get coordinate on map @@ -951,8 +956,8 @@ div.csl-indent { </div> </div> </section> -<section id="add-shading-to-a-layer" class="level3" data-number="4.2.16"> -<h3 data-number="4.2.16" class="anchored" data-anchor-id="add-shading-to-a-layer"><span class="header-section-number">4.2.16</span> Add shading to a layer</h3> +<section id="add-shading-to-a-layer" class="level3" data-number="5.2.16"> +<h3 data-number="5.2.16" class="anchored" data-anchor-id="add-shading-to-a-layer"><span class="header-section-number">5.2.16</span> Add shading to a layer</h3> <p>The function <code>mf_shadow()</code> allows to create a shadow to a layer of polygons.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb35"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_shadow</span>(district)</span> @@ -962,8 +967,8 @@ div.csl-indent { </div> </div> </section> -<section id="creating-boxes" class="level3" data-number="4.2.17"> -<h3 data-number="4.2.17" class="anchored" data-anchor-id="creating-boxes"><span class="header-section-number">4.2.17</span> Creating Boxes</h3> +<section id="creating-boxes" class="level3" data-number="5.2.17"> +<h3 data-number="5.2.17" class="anchored" data-anchor-id="creating-boxes"><span class="header-section-number">5.2.17</span> Creating Boxes</h3> <p>The function <code>mf_inset_on()</code> allows to start creation a box. You must then “close†the box with <code>mf_inset_off()</code>.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb36"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mf_init</span>(<span class="at">x =</span> dist_selected, <span class="at">theme =</span> <span class="st">"agolalight"</span>, <span class="at">expandBB =</span> <span class="fu">c</span>(<span class="dv">0</span>,.<span class="dv">1</span>,<span class="dv">0</span>,.<span class="dv">5</span>)) </span> @@ -998,10 +1003,10 @@ div.csl-indent { </div> </section> </section> -<section id="d-maps" class="level2" data-number="4.3"> -<h2 data-number="4.3" class="anchored" data-anchor-id="d-maps"><span class="header-section-number">4.3</span> 3D maps</h2> -<section id="linemap" class="level3" data-number="4.3.1"> -<h3 data-number="4.3.1" class="anchored" data-anchor-id="linemap"><span class="header-section-number">4.3.1</span> linemap</h3> +<section id="d-maps" class="level2" data-number="5.3"> +<h2 data-number="5.3" class="anchored" data-anchor-id="d-maps"><span class="header-section-number">5.3</span> 3D maps</h2> +<section id="linemap" class="level3" data-number="5.3.1"> +<h3 data-number="5.3.1" class="anchored" data-anchor-id="linemap"><span class="header-section-number">5.3.1</span> linemap</h3> <p>The package <code>linemap</code> <span class="citation" data-cites="linemap">(<a href="references.html#ref-linemap" role="doc-biblioref">Giraud 2021</a>)</span> allows you to make maps made up of lines.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb37"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(linemap)</span> @@ -1036,8 +1041,8 @@ div.csl-indent { <span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a><span class="co"># list.files(path="Health")</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> </div> </section> -<section id="relief-tanaka" class="level3" data-number="4.3.2"> -<h3 data-number="4.3.2" class="anchored" data-anchor-id="relief-tanaka"><span class="header-section-number">4.3.2</span> Relief Tanaka</h3> +<section id="relief-tanaka" class="level3" data-number="5.3.2"> +<h3 data-number="5.3.2" class="anchored" data-anchor-id="relief-tanaka"><span class="header-section-number">5.3.2</span> Relief Tanaka</h3> <p>We use the <code>tanaka</code> package <span class="citation" data-cites="tanaka">(<a href="references.html#ref-tanaka" role="doc-biblioref">Giraud 2022b</a>)</span> which provides a method <span class="citation" data-cites="tanaka1950">(<a href="references.html#ref-tanaka1950" role="doc-biblioref">Tanaka 1950</a>)</span> used to improve the perception of relief.</p> <div class="cell"> <div class="sourceCode cell-code" id="cb39"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tanaka)</span> @@ -1079,8 +1084,8 @@ div.csl-indent { </div> </section> </section> -<section id="cartographic-transformation" class="level2" data-number="4.4"> -<h2 data-number="4.4" class="anchored" data-anchor-id="cartographic-transformation"><span class="header-section-number">4.4</span> Cartographic Transformation</h2> +<section id="cartographic-transformation" class="level2" data-number="5.4"> +<h2 data-number="5.4" class="anchored" data-anchor-id="cartographic-transformation"><span class="header-section-number">5.4</span> Cartographic Transformation</h2> <blockquote class="blockquote"> <p>classical anamorphosis is a representation of States(or any cells) by <strong>rectangle or any polygons</strong> according to a <strong>quantities</strong> attached to them. (…) We strive to <strong>keep the general arrangement</strong> of meshes or the silhouette of the continent.â€<br> <span class="citation" data-cites="Brunet93">Brunet, Ferras, and Théry (<a href="references.html#ref-Brunet93" role="doc-biblioref">1993</a>)</span></p> @@ -1112,8 +1117,8 @@ div.csl-indent { </div> </div> <p>To make the cartograms we use the package <code>cartogram</code> <span class="citation" data-cites="cartogram">(<a href="references.html#ref-cartogram" role="doc-biblioref">Jeworutzki 2020</a>)</span>.</p> -<section id="dorlings-cartograms" class="level3" data-number="4.4.1"> -<h3 data-number="4.4.1" class="anchored" data-anchor-id="dorlings-cartograms"><span class="header-section-number">4.4.1</span> Dorling’s cartograms</h3> +<section id="dorlings-cartograms" class="level3" data-number="5.4.1"> +<h3 data-number="5.4.1" class="anchored" data-anchor-id="dorlings-cartograms"><span class="header-section-number">5.4.1</span> Dorling’s cartograms</h3> <p>The territories are represented by figures (circles, squares or rectangles) which do not overlap, the surface of which are proportional to a variable. The proportion of the figures are defined according to the starting positions.</p> <div class="quarto-figure quarto-figure-center"> <figure class="figure"> @@ -1154,8 +1159,8 @@ The perception of quantities is very good. The circle sizes are really comarable </div> <p>The parameter <code>k</code> allows to vary the expansion factor of the circles.</p> </section> -<section id="non-continuous-cartograms" class="level3" data-number="4.4.2"> -<h3 data-number="4.4.2" class="anchored" data-anchor-id="non-continuous-cartograms"><span class="header-section-number">4.4.2</span> Non-continuous cartograms</h3> +<section id="non-continuous-cartograms" class="level3" data-number="5.4.2"> +<h3 data-number="5.4.2" class="anchored" data-anchor-id="non-continuous-cartograms"><span class="header-section-number">5.4.2</span> Non-continuous cartograms</h3> <p>The size of the polygons is proportional to a variable. The arrangement of the polygons relative to each other is preserved. The shape of the polygons is similar.</p> <div class="quarto-figure quarto-figure-center"> <figure class="figure"> @@ -1186,8 +1191,8 @@ The converstion of the polygons shape is optimal. </div> <p>The parameter <code>k</code> allows to vary the expansion of the polygons.</p> </section> -<section id="continuous-cartograms" class="level3" data-number="4.4.3"> -<h3 data-number="4.4.3" class="anchored" data-anchor-id="continuous-cartograms"><span class="header-section-number">4.4.3</span> Continuous cartograms</h3> +<section id="continuous-cartograms" class="level3" data-number="5.4.3"> +<h3 data-number="5.4.3" class="anchored" data-anchor-id="continuous-cartograms"><span class="header-section-number">5.4.3</span> Continuous cartograms</h3> <p>The size of the polygons is proportional to variable. The arrangement of the polygons relative to each other is preserved. To maintain contiguity, the sape of the polygons is heavily transformed.</p> <div class="quarto-figure quarto-figure-center"> <figure class="figure"> @@ -1243,8 +1248,8 @@ The shape of the polygond is strongly distorted. </div> </div> </section> -<section id="stengths-and-weaknessses-of-cartograms" class="level3" data-number="4.4.4"> -<h3 data-number="4.4.4" class="anchored" data-anchor-id="stengths-and-weaknessses-of-cartograms"><span class="header-section-number">4.4.4</span> Stengths and weaknessses of cartograms</h3> +<section id="stengths-and-weaknessses-of-cartograms" class="level3" data-number="5.4.4"> +<h3 data-number="5.4.4" class="anchored" data-anchor-id="stengths-and-weaknessses-of-cartograms"><span class="header-section-number">5.4.4</span> Stengths and weaknessses of cartograms</h3> <p>cartograms are cartographic representations perceived as <strong>innovative</strong> (although the method is 40 years old). These very generalize images capture <strong>quantities</strong> and <strong>gradients</strong> well. These are real <strong>communication</strong> images that <strong>provoke</strong>, arouse <strong>interest</strong>, convey a strong <strong>message</strong>, <strong>challenge</strong>.</p> <p>But cartograms induce a loss of <strong>visual cues</strong> (difficult to find one’s country or region on the map), require a <strong>reading effort</strong> which can be significant and do not make it possible to manage <strong>missing data</strong>.</p> @@ -1386,8 +1391,8 @@ window.document.addEventListener("DOMContentLoaded", function (event) { </script> <nav class="page-navigation"> <div class="nav-page nav-page-previous"> - <a href="./03-vector_data.html" class="pagination-link"> - <i class="bi bi-arrow-left-short"></i> <span class="nav-page-text"><span class="chapter-number">3</span> <span class="chapter-title">Vector Data</span></span> + <a href="./04-raster_data.html" class="pagination-link"> + <i class="bi bi-arrow-left-short"></i> <span class="nav-page-text"><span class="chapter-number">4</span> <span class="chapter-title">Work with Raster Data</span></span> </a> </div> <div class="nav-page nav-page-next"> diff --git a/public/img/agr_raster.png b/public/img/agr_raster.png new file mode 100644 index 0000000000000000000000000000000000000000..ad880ae0bf892f240ab0019dfb5b508fe682b04e Binary files /dev/null and b/public/img/agr_raster.png differ diff --git a/public/img/agr_raster_2.png b/public/img/agr_raster_2.png new file mode 100644 index 0000000000000000000000000000000000000000..bcc9677ab8736e62f29799392a183af3cd17ba1b Binary files /dev/null and b/public/img/agr_raster_2.png differ diff --git a/public/img/crop.png b/public/img/crop.png new file mode 100644 index 0000000000000000000000000000000000000000..1c7eedbc3d5218eae404951d562cb3301219f7c9 Binary files /dev/null and b/public/img/crop.png differ diff --git a/public/img/crop2.png b/public/img/crop2.png new file mode 100644 index 0000000000000000000000000000000000000000..fb441a794963a6cddb7406af7ef636ccf87a86d6 Binary files /dev/null and b/public/img/crop2.png differ diff --git a/public/img/lo_fo_zo_glo.png b/public/img/lo_fo_zo_glo.png new file mode 100644 index 0000000000000000000000000000000000000000..eff51fe7434c4be897ed874c1f1accd08ded07b8 Binary files /dev/null and b/public/img/lo_fo_zo_glo.png differ diff --git a/public/img/mask.png b/public/img/mask.png new file mode 100644 index 0000000000000000000000000000000000000000..e41a2007e6e68eab9ee08bbfa54681468504559f Binary files /dev/null and b/public/img/mask.png differ diff --git a/public/img/mosaic.png b/public/img/mosaic.png new file mode 100644 index 0000000000000000000000000000000000000000..2f5932b88fbc2bef5d9e6d62c0612c9e15547720 Binary files /dev/null and b/public/img/mosaic.png differ diff --git a/public/img/op_focal_2.png b/public/img/op_focal_2.png new file mode 100644 index 0000000000000000000000000000000000000000..0fd23d2c478af43edcf54eadb78e12a9923d7e0e Binary files /dev/null and b/public/img/op_focal_2.png differ diff --git a/public/img/op_global.png b/public/img/op_global.png new file mode 100644 index 0000000000000000000000000000000000000000..3899b99608c7dfa1ca58e10e06a02a1350f91f54 Binary files /dev/null and b/public/img/op_global.png differ diff --git a/public/img/op_local_2.png b/public/img/op_local_2.png new file mode 100644 index 0000000000000000000000000000000000000000..8f5958c29c2756f2c2fa9ef665f5ce1a31bea9ad Binary files /dev/null and b/public/img/op_local_2.png differ diff --git a/public/img/op_zonal_2.png b/public/img/op_zonal_2.png new file mode 100644 index 0000000000000000000000000000000000000000..d83cad775ecd38dcd71850a6e5fc63a4751caabf Binary files /dev/null and b/public/img/op_zonal_2.png differ diff --git a/public/img/project_raster.png b/public/img/project_raster.png new file mode 100644 index 0000000000000000000000000000000000000000..34944cdae935f035f1e43a911034e03a128e6c16 Binary files /dev/null and b/public/img/project_raster.png differ diff --git a/public/img/raster.png b/public/img/raster.png new file mode 100644 index 0000000000000000000000000000000000000000..ac9215bf901ae8b64b3cc1368a4d5c8678839f4a Binary files /dev/null and b/public/img/raster.png differ diff --git a/public/index.html b/public/index.html index d837b96076d7468f2b849afc849676ac4c4d4937..51d201cdb1ecb2dce282fe7a7a13054a62daedfb 100644 --- a/public/index.html +++ b/public/index.html @@ -113,7 +113,12 @@ ul.task-list{list-style: none;} </li> <li class="sidebar-item"> <div class="sidebar-item-container"> - <a href="./05-mapping_with_r.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">4</span> <span class="chapter-title">Mapping With R</span></a> + <a href="./04-raster_data.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">4</span> <span class="chapter-title">Work with Raster Data</span></a> + </div> +</li> + <li class="sidebar-item"> + <div class="sidebar-item-container"> + <a href="./05-mapping_with_r.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">5</span> <span class="chapter-title">Mapping With R</span></a> </div> </li> <li class="sidebar-item"> diff --git a/public/search.json b/public/search.json index 375e9ab370c661775fd20329a3dc646a5c1ac235..b1dca80759a4f5f3b54f89b328cd0f320a1828da 100644 --- a/public/search.json +++ b/public/search.json @@ -114,30 +114,30 @@ { "objectID": "05-mapping_with_r.html", "href": "05-mapping_with_r.html", - "title": "4 Mapping With R", + "title": "5 Mapping With R", "section": "", "text": "The fonction mf_map() is the central function of the package mapsf (Giraud 2022a). It makes it possible to carry out most of the usual representations in cartography. These main arguments are:\n\nx, an sf object ;\nvar, the name of variable to present ;\ntype, the type of presentation.\n\n\n\nThe following lines import the spatial information layers located in the geopackage cambodia.gpkg file.\n\nlibrary(sf)\n\n#Import Cambodia country border\ncountry = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"country\", quiet = TRUE)\n#Import provincial administrative border of Cambodia\neducation = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"education\", quiet = TRUE)\n#Import district administrative border of Cambodia\ndistrict = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"district\", quiet = TRUE)\n#Import roads data in Cambodia\nroad = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"road\", quiet = TRUE)\n#Import health center data in Cambodia\nhospital = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"hospital\", quiet = TRUE)\n\n\n\n\nWithout using types specification, the function mf_map() simply display the background map.\n\nlibrary(mapsf)\n\nmf_map(x = district, border = \"white\")\nmf_map(x = country,lwd = 2, col = NA, add = TRUE)\nmf_map(x = road, lwd = .5, col = \"ivory4\", add = TRUE)\nmf_map(x = hospital, pch = 20, cex = 1, col = \"#FE9A2E\", add = TRUE) \n\n\n\n\n\n\n\nProportional symbol maps are used to represent inventory variables (absolute quantitative variables, sum and average make sense). The function mf_map(..., type = \"prop\") proposes this representation.\n\n#District\nmf_map(x = district) \n\n# Proportional symbol \nmf_map(\n x = district, \n var = \"T_POP\",\n val_max = 700000,\n type = \"prop\",\n col = \"#148F77\", \n leg_title = \"Population 2019\"\n)\n\n# Title\nmf_title(\"Distribution of population in provincial level\")\n\n\n\n\n\n\nIt is possible to fix the dimensions of the largest symbol corresponding to a certain value with the arguments inches and val_max. We can use construct maps with comparable proportional symbols.\n\npar(mfrow = c(1,2)) #Displaying two maps facing each other\n\n#district\nmf_map(x = district, border = \"grey90\", lwd = .5) \n# Add male Population\nmf_map(\n x = district, \n var = \"Male\", \n type = \"prop\",\n col = \"#1F618D\",\n inches = 0.2, \n val_max = 300000, \n leg_title = \"Male\", \n leg_val_cex = 0.5,\n)\nmf_title(\"Male Population by Distict\") #Adding map title\n\n#district\nmf_map(x = district, border = \"grey90\", lwd = .5) \n# Add female Population\nmf_map(\n x = district, \n var = \"Female\", \n type = \"prop\",\n col = \"#E74C3C\",\n inches = 0.2, \n val_max = 300000, \n leg_title =\"Female\", \n leg_val_cex = 0.5\n)\nmf_title(\"Female Population by Distict\") #Adding map title\n\n\n\n\nHere we have displayed two maps facing each other, see the point Displaying several maps on the same figure for more details.\n\n\n\n\nChoropleth maps are used to represent ratio variables (relative quantitative variables, mean has meaning, sum has no meaning).\nFor this type of representation, you must first:\n\nchoose a discretization method to transform a continuous statistical series into classes defined by intervals,\nchoose a number of classes,\nchoose a color palette.\n\nThe function mf_map(…, type = “choroâ€)makes it possible to create choroplete maps. The arguments nbreaks and breaks are used to parameterize the discretizations, and the function mf_get_breaks() makes it possible to work on the discretizations outside the function mf_map(). Similarly, the argument palis used to fill in a color palette, but several functions can be used to set the palettes apart from the (mf_get_pal…) function.\n\n# Population density (inhabitants/km2) using the sf::st_area() function\ndistrict$DENS <- 1e6 * district$T_POP / as.numeric(st_area(district)) #Calculate population density \nmf_map(\n x = district,\n var = \"DENS\",\n type = \"choro\",\n breaks = \"quantile\",\n pal = \"BuGn\",\n lwd = 1,\n leg_title = \"Distribution of population\\n(inhabitants per km2)\", \n leg_val_rnd = 0\n)\nmf_title(\"Distribution of the population in (2019)\")\n\n\n\n\n\n\nThe fonction mf_get_breaks() provides the methods of discretization of classic variables: quantiles, average/standard deviation, equal amplitudes, nested averages, Fisher-Jenks, geometric, etc.\n\neducation$enrol_g_pct = 100 * education$enrol_girl/education$t_enrol #Calculate percentage of enrolled girl student\n\nd1 = mf_get_breaks(education$enrol_g_pct, nbreaks = 6, breaks = \"equal\", freq = TRUE)\nd2 = mf_get_breaks(education$enrol_g_pct, nbreaks = 6, breaks = \"quantile\")\nd3 = mf_get_breaks(education$enrol_g_pct, nbreaks = 6, breaks = \"geom\")\nd4 = mf_get_breaks(education$enrol_g_pct, breaks = \"msd\", central = FALSE)\n\n\n\n\n\n\n\n\n\nThe argument pal de mf_map() is dedicated to choosing a color palette. The palettes provided by the function hcl.colors() can be used directly.\n\nmf_map(x = education, var = \"enrol_g_pct\", type = \"choro\",\n breaks = d3, pal = \"Reds 3\")\n\n\n\n\n\n\n\n\n\nThe fonction mf_get_pal() allows you to build a color palette. This function is especially useful for creating balanced asymmetrical diverging palettes.\n\nmypal <- mf_get_pal(n = c(4,6), palette = c(\"Burg\", \"Teal\"))\nimage(1:10, 1, as.matrix(1:10), col=mypal, xlab = \"\", ylab = \"\", xaxt = \"n\",\n yaxt = \"n\",bty = \"n\")\n\n\n\n\n\n\n\nIt is possible to use this mode of presentation in specific implementation also.\n\ndist_c <- st_centroid(district)\nmf_map(district)\nmf_map(\n x = dist_c,\n var = \"DENS\",\n type = \"choro\",\n breaks = \"quantile\",\n nbreaks = 5,\n pal = \"PuRd\",\n pch = 23,\n cex = 1.5,\n border = \"white\",\n lwd = .7,\n leg_pos = \"topleft\",\n leg_title = \"Distribution of population\\n(inhabitants per km2)\", \n leg_val_rnd = 0, \n add = TRUE\n)\nmf_title(\"Distribution of population in (2019)\")\n\n\n\n\n\n\n\n\nTypology maps are used to represent qualitative variables. The function mf_map(..., type = \"typo\") proposes this representation.\n\nmf_map(\n x = district,\n var=\"Status\",\n type = \"typo\",\n pal = c('#E8F9FD','#FF7396','#E4BAD4','#FFE3FE'),\n lwd = .7,\n leg_title = \"\"\n)\nmf_title(\"Administrative status by size of area\")\n\n\n\n\n\n\nThe argument val_order is used to order the categories in the\n\nmf_map(\n x = district,\n var=\"Status\",\n type = \"typo\",\n pal = c('#E8F9FD','#FF7396','#E4BAD4','#FFE3FE'),\n val_order = c(\"1st largest district\", \"2nd largest district\", \"3rd largest district\",\"<4500km2\"),\n lwd = .7,\n leg_title = \"\"\n)\nmf_title(\"Administrative status by size of area\")\n\n\n\n\n\n\n\nWhen the implantation of the layer is punctual, symbols are used to carry the colors of the typology.\n\n#extract centroid point of the district\ndist_ctr <- st_centroid(district[district$Status != \"<4500km2\", ])\nmf_map(district)\nmf_map(\n x = dist_ctr,\n var = \"Status\",\n type = \"typo\",\n cex = 2,\n pch = 22,\n pal = c('#FF7396','#E4BAD4','#FFE3FE'),\n leg_title = \"\",\n leg_pos = \"bottomright\",\n add = TRUE\n)\nmf_title(\"Administrative status by size of area\")\n\n\n\n\n\n\n\n\n#Selection of roads that intersect the city of Siem Reap\npp <- district[district$ADM1_EN == \"Phnom Penh\", ]\nroad_pp <- road[st_intersects(x = road, y = pp, sparse = FALSE), ]\nmf_map(pp)\nmf_map(\n x = road_pp,\n var = \"fclass\",\n type = \"typo\",\n lwd = 1.2,\n pal = mf_get_pal(n = 6, \"Tropic\"),\n leg_title = \"Types of road\",\n leg_pos = \"topright\",\n leg_frame = T,\n add = TRUE\n)\nmf_title(\"Administrative status\")\n\n\n\n\n\n\n\n\nThe function mf_map(..., var = c(\"var1\", \"var2\"), type = \"prop_choro\") represents proportional symbols whose areas are proportional to the values of one variable and whose color is based on the discretization of a second variable. The function uses the arguments of the functions mf_map(..., type = \"prop\") and mf_map(..., type = \"choro\").\n\nmf_map(x = district)\nmf_map(\n x = district,\n var = c(\"T_POP\", \"DENS\"),\n val_max = 500000,\n type = \"prop_choro\",\n border = \"grey60\",\n lwd = 0.5,\n leg_pos = c(\"bottomright\", \"bottomleft\"),\n leg_title = c(\"Population\", \"Density of\\n population\\n(inhabitants per km2)\"),\n breaks = \"q6\",\n pal = \"Blues 3\",\n leg_val_rnd = c(0,1))\nmf_title(\"Population\")\n\n\n\n\n\n\n\nThe function mf_map(..., var = c(\"var1\", \"var2\"), type = \"prop_typo\") represents proportional symbols whose areas are proportional to the values of one variable and whose color is based on the discretization of a second variable. The function uses the arguments of the mf_map(..., type = \"prop\") and function mf_map(..., type = \"typo\").\n\nmf_map(x = district)\nmf_map(\n x = district,\n var = c(\"Area.Km2.\", \"Status\"),\n type = \"prop_typo\",\n pal = c('#E8F9FD','#FF7396','#E4BAD4','#FFE3FE'),\n val_order = c(\"<4500km2\",\"1st largest district\", \"2nd largest district\", \"3rd largest district\"),\n leg_pos = c(\"bottomleft\",\"topleft\"),\n leg_title = c(\"Population\\n(2019)\",\n \"Statut administratif\"),\n)\nmf_title(\"Population\")" }, { "objectID": "05-mapping_with_r.html#layout", "href": "05-mapping_with_r.html#layout", - "title": "4 Mapping With R", - "section": "4.2 Layout", - "text": "4.2 Layout\nTo be finalized, a thematic map must contain certain additional elements such as: title, author, source, scale, orientation…\n\n4.2.1 Example data\nThe following lines import the spatial information layers located in the geopackage lot46.gpkg file.\n\nlibrary(sf)\ncountry = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"country\", quiet = TRUE) #Import Cambodia country border\neducation = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"education\", quiet = TRUE) #Import provincial administrative border of Cambodia\ndistrict = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"district\", quiet = TRUE) #Import district administrative border of Cambodia\nroad = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"road\", quiet = TRUE) #Import roads data in Cambodia\nhospital = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"hospital\", quiet = TRUE) #Import hospital data in Cambodia\ncases = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"cases\", quiet = TRUE) #Import example data of fever_cases in Cambodia\n\n\n\n4.2.2 Themes\nThe function mf_theme() defines a cartographic theme. Using a theme allows you to define several graphic parameters which are then applied to the maps created with mapsf. These parameters are: the map margins, the main color, the background color, the position and the aspect of the title. A theme can also be defined with the mf_init() and function mf_export().\n\n4.2.2.1 Use a predefined theme\nA series of predefined themes are available by default (see ?mf_theme).\n\nlibrary(mapsf)\n# use of a background color for the figure, to see the use of margin\nopar <- par(mfrow = c(2,2))\n# Using a predefined theme\nmf_theme(\"default\")\nmf_map(district)\nmf_title(\"Theme : 'default'\")\n\nmf_theme(\"darkula\")\nmf_map(district)\nmf_title(\"Theme : 'darkula'\")\n\nmf_theme(\"candy\")\nmf_map(district)\nmf_title(\"Theme : 'candy'\")\n\nmf_theme(\"nevermind\")\nmf_map(district)\nmf_title(\"Theme : 'nevermind'\")\npar(opar)\n\n\n\n\n\n\n4.2.2.2 Modify an existing theme\nIt is possible to modify an existing theme. In this example, we are using the “default†theme and modifying a few settings.\n\nlibrary(mapsf)\nopar <- par(mfrow = c(1,2))\nmf_theme(\"default\")\nmf_map(district)\nmf_title(\"default\")\n\nmf_theme(\"default\", tab = FALSE, font = 4, bg = \"grey60\", pos = \"center\")\nmf_map(district)\nmf_title(\"modified default\")\npar(opar)\n\n\n\n\n\n\n4.2.2.3 Create a theme\nIt is also possible to create a theme.\n\nmf_theme(\n bg = \"lightblue\", # background color\n fg = \"tomato1\", # main color\n mar = c(1,0,1.5,0), # margin\n tab = FALSE, # \"tab\" style for the title\n inner = FALSE, # title inside or outside of map area\n line = 1.5, # space dedicated to title\n pos = \"center\", # heading position\n cex = 1.5, # title size\n font = 2 # font types for title\n)\nmf_map(district)\nmf_title(\"New theme\")\n\n\n\n\n\n\n\n4.2.3 Titles\nThe function mf_title() adds a title to a map.\n\nmf_theme(\"default\")\nmf_map(district)\nmf_title(\"Map title\")\n\n\n\n\nIt is possible to customize the appearance of the title\n\nmf_map(district)\nmf_title(\n txt = \"Map title\", \n pos = \"center\", \n tab = FALSE, \n bg = \"tomato3\", \n fg = \"lightblue\", \n cex = 1.5, \n line = 1.7, \n font = 1, \n inner = FALSE\n)\n\n\n\n\n\n\n4.2.4 Arrow orientation\nThe function mf_arrow() allows you to choose the position and aspect of orientation arrow.\n\nmf_map(district)\nmf_arrow()\n\n\n\n\n\n\n4.2.5 Scale\nThe function mf_scale() allows you to choose the position and the aspect of the scale.\n\nmf_map(district)\nmf_scale(\n size = 60,\n lwd = 1,\n cex = 0.7\n)\n\n\n\n\n\n\n4.2.6 Credits\nThe function mf_credits() displays a line of credits (sources, author, etc.).\n\nmf_map(district)\nmf_credits(\"IRD\\nInstitut Pasteur du Cambodge, 2022\")\n\n\n\n\n\n\n4.2.7 Complete dressing\nThe function mf_layout() displays all these elements.\n\nmf_map(district)\nmf_layout(\n title = \"Cambodia\",\n credits = \"IRD\\nInstitut Pasteur du Cambodge, 2022\",\n arrow = TRUE\n)\n\n\n\n\n\n\n4.2.8 Annotations\n\nmf_map(district)\nmf_annotation(district[district$ADM2_EN == \"Bakan\",], txt = \"Bakan\", col_txt = \"darkred\", halo = TRUE, cex = 1.5)\n\n\n\n\n\n\n4.2.9 Legends\n\nmf_map(district)\nmf_legend(\n type = \"prop\", \n val = c(1000,500,200,10), \n inches = .2, \n title = \"Population\", \n pos = \"topleft\"\n)\nmf_legend(\n type = \"choro\", \n val = c(0,10,20,30,40),\n pal = \"Greens\", \n pos = \"bottomright\", \n val_rnd = 0\n)\n\n\n\n\n\n\n4.2.10 Labels\nThe function mf_label() is dedicated to displaying labels.\n\ndist_selected <- district[st_intersects(district, district[district$ADM2_EN == \"Bakan\", ], sparse = F), ]\n\nmf_map(dist_selected)\nmf_label(\n x = dist_selected,\n var = \"ADM2_EN\",\n col= \"darkgreen\",\n halo = TRUE,\n overlap = FALSE, \n lines = FALSE\n)\nmf_scale()\n\n\n\n\nThe argument halo = TRUE allows to display a slight halo around the labels and the argument overlap = FALSE allows to create non-overlapping labels.\n\n\n4.2.11 Center the map on a region\nThe function mf_init() allows you to initialize a map by centering it on a spatial object.\n\nmf_init(x = dist_selected)\nmf_map(district, add = TRUE)\nmf_map(dist_selected, col = NA, border = \"#29a3a3\", lwd = 2, add = TRUE)\n\n\n\n\n\n\n4.2.12 Displaying several maps on the sam figure\nHere you have to use mfrow of the function par(). The first digit represents the number of of rows and second the number of columns.\n\n# define the figure layout (1 row, 2 columns)\npar(mfrow = c(1, 2))\n\n# first map\nmf_map(district)\nmf_map(district, \"Male\", \"prop\", val_max = 300000)\nmf_title(\"Population, male\")\n\n# second map\nmf_map(district)\nmf_map(district, \"Female\", \"prop\", val_max = 300000)\nmf_title(\"Population, female\")\n\n\n\n\n\n\n4.2.13 Exporting maps\nIt is quite difficult to export figures (maps or others) whose height/width ratio is satisfactory. The default ratio of figures in png format is 1 (480x480 pixels):\n\ndist_filter <- district[district$ADM2_PCODE == \"KH0808\", ]\npng(\"img/dist_filter_1.png\")\nmf_map(dist_filter)\nmf_title(\"Filtered district\")\ndev.off()\n\n\n\n\n\n\nOn this map a lot of space is lost to the left and right of the district.\nThe function mf_export() allows exports of maps whose height/width ratio is controlled and corresponds to that of a spatial object.\n\nmf_export(dist_filter, \"img/dist_filter_2.png\", width = 480)\nmf_map(dist_filter)\nmf_title(\"Filtered district\")\ndev.off()\n\n\n\n\n\n\nThe extent of this map is exactly that of the displayed region.\n\n\n4.2.14 Adding an image to a map\nThis can be useful for adding a logo, a pictograph. The function readPNG() of package png allows the additional images on the figure.\n\nmf_theme(\"default\", mar = c(0,0,0,0))\nlibrary(png)\n\nlogo <- readPNG(\"img/ird_logo.png\") #Import image\npp <- dim(logo)[2:1]*200 #Image dimension in map unit (width and height of the original image)\n\n#The upper left corner of the department's bounding box\nxy <- st_bbox(district)[c(1,4)]\nmf_map(district, col = \"#D1914D\", border = \"white\")\nrasterImage(\n image = logo,\n xleft = xy[1] ,\n ybottom = xy[2] - pp[2],\n xright = xy[1] + pp[1],\n ytop = xy[2]\n)\n\n\n\n\n\n\n4.2.15 Place an item precisely on the map\nThe function locator() allows clicking on the figure and obtaining the coordinate of a point in the coordinate system of the figure (of the map).\n\n# locator(1) # click to get coordinate on map\n# points(locator(1)) # click to plot point on map\n# text(locator(1), # click to place the item on map\n# labels =\"Located any texts on map\", \n# adj = c(0,0))\n\n\nVideo\nlocator()peut être utilisée sur la plupart des graphiques (pas ceux produits avec ggplot2).\n\n\n\n\n\n\nHow to interactively position legends and layout elements on a map with cartography\n\n\n\n\n\n4.2.16 Add shading to a layer\nThe function mf_shadow() allows to create a shadow to a layer of polygons.\n\nmf_shadow(district)\nmf_map(district, add=TRUE)\n\n\n\n\n\n\n4.2.17 Creating Boxes\nThe function mf_inset_on() allows to start creation a box. You must then “close†the box with mf_inset_off().\n\nmf_init(x = dist_selected, theme = \"agolalight\", expandBB = c(0,.1,0,.5)) \nmf_map(district, add = TRUE)\nmf_map(dist_selected, col = \"tomato4\", border = \"tomato1\", lwd = 2, add = TRUE)\n\n# Cambodia inset box\nmf_inset_on(x = country, pos = \"topright\", cex = .3)\nmf_map(country, lwd = .5, border= \"grey90\")\nmf_map(dist_selected, col = \"tomato4\", border = \"tomato1\", lwd = .5, add = TRUE)\nmf_scale(size = 100, pos = \"bottomleft\", cex = .6, lwd = .5)\nmf_inset_off()\n\n# District inset box\nmf_inset_on(x = district, pos = \"bottomright\", cex = .3)\nmf_map(district, lwd = 0.5, border= \"grey90\")\nmf_map(dist_selected, col = \"tomato4\", border = \"tomato1\", lwd = .5, add = TRUE)\nmf_scale(size = 100, pos = \"bottomright\", cex = .6, lwd = .5)\nmf_inset_off()\n\n# World inset box\nmf_inset_on(x = \"worldmap\", pos = \"topleft\")\nmf_worldmap(dist_selected, land_col = \"#cccccc\", border_col = NA, \n water_col = \"#e3e3e3\", col = \"tomato4\")\n\nmf_inset_off()\nmf_title(\"Bakan district and its surroundings\")\nmf_scale(10, pos = 'bottomleft')" + "title": "5 Mapping With R", + "section": "5.2 Layout", + "text": "5.2 Layout\nTo be finalized, a thematic map must contain certain additional elements such as: title, author, source, scale, orientation…\n\n5.2.1 Example data\nThe following lines import the spatial information layers located in the geopackage lot46.gpkg file.\n\nlibrary(sf)\ncountry = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"country\", quiet = TRUE) #Import Cambodia country border\neducation = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"education\", quiet = TRUE) #Import provincial administrative border of Cambodia\ndistrict = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"district\", quiet = TRUE) #Import district administrative border of Cambodia\nroad = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"road\", quiet = TRUE) #Import roads data in Cambodia\nhospital = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"hospital\", quiet = TRUE) #Import hospital data in Cambodia\ncases = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"cases\", quiet = TRUE) #Import example data of fever_cases in Cambodia\n\n\n\n5.2.2 Themes\nThe function mf_theme() defines a cartographic theme. Using a theme allows you to define several graphic parameters which are then applied to the maps created with mapsf. These parameters are: the map margins, the main color, the background color, the position and the aspect of the title. A theme can also be defined with the mf_init() and function mf_export().\n\n5.2.2.1 Use a predefined theme\nA series of predefined themes are available by default (see ?mf_theme).\n\nlibrary(mapsf)\n# use of a background color for the figure, to see the use of margin\nopar <- par(mfrow = c(2,2))\n# Using a predefined theme\nmf_theme(\"default\")\nmf_map(district)\nmf_title(\"Theme : 'default'\")\n\nmf_theme(\"darkula\")\nmf_map(district)\nmf_title(\"Theme : 'darkula'\")\n\nmf_theme(\"candy\")\nmf_map(district)\nmf_title(\"Theme : 'candy'\")\n\nmf_theme(\"nevermind\")\nmf_map(district)\nmf_title(\"Theme : 'nevermind'\")\npar(opar)\n\n\n\n\n\n\n5.2.2.2 Modify an existing theme\nIt is possible to modify an existing theme. In this example, we are using the “default†theme and modifying a few settings.\n\nlibrary(mapsf)\nopar <- par(mfrow = c(1,2))\nmf_theme(\"default\")\nmf_map(district)\nmf_title(\"default\")\n\nmf_theme(\"default\", tab = FALSE, font = 4, bg = \"grey60\", pos = \"center\")\nmf_map(district)\nmf_title(\"modified default\")\npar(opar)\n\n\n\n\n\n\n5.2.2.3 Create a theme\nIt is also possible to create a theme.\n\nmf_theme(\n bg = \"lightblue\", # background color\n fg = \"tomato1\", # main color\n mar = c(1,0,1.5,0), # margin\n tab = FALSE, # \"tab\" style for the title\n inner = FALSE, # title inside or outside of map area\n line = 1.5, # space dedicated to title\n pos = \"center\", # heading position\n cex = 1.5, # title size\n font = 2 # font types for title\n)\nmf_map(district)\nmf_title(\"New theme\")\n\n\n\n\n\n\n\n5.2.3 Titles\nThe function mf_title() adds a title to a map.\n\nmf_theme(\"default\")\nmf_map(district)\nmf_title(\"Map title\")\n\n\n\n\nIt is possible to customize the appearance of the title\n\nmf_map(district)\nmf_title(\n txt = \"Map title\", \n pos = \"center\", \n tab = FALSE, \n bg = \"tomato3\", \n fg = \"lightblue\", \n cex = 1.5, \n line = 1.7, \n font = 1, \n inner = FALSE\n)\n\n\n\n\n\n\n5.2.4 Arrow orientation\nThe function mf_arrow() allows you to choose the position and aspect of orientation arrow.\n\nmf_map(district)\nmf_arrow()\n\n\n\n\n\n\n5.2.5 Scale\nThe function mf_scale() allows you to choose the position and the aspect of the scale.\n\nmf_map(district)\nmf_scale(\n size = 60,\n lwd = 1,\n cex = 0.7\n)\n\n\n\n\n\n\n5.2.6 Credits\nThe function mf_credits() displays a line of credits (sources, author, etc.).\n\nmf_map(district)\nmf_credits(\"IRD\\nInstitut Pasteur du Cambodge, 2022\")\n\n\n\n\n\n\n5.2.7 Complete dressing\nThe function mf_layout() displays all these elements.\n\nmf_map(district)\nmf_layout(\n title = \"Cambodia\",\n credits = \"IRD\\nInstitut Pasteur du Cambodge, 2022\",\n arrow = TRUE\n)\n\n\n\n\n\n\n5.2.8 Annotations\n\nmf_map(district)\nmf_annotation(district[district$ADM2_EN == \"Bakan\",], txt = \"Bakan\", col_txt = \"darkred\", halo = TRUE, cex = 1.5)\n\n\n\n\n\n\n5.2.9 Legends\n\nmf_map(district)\nmf_legend(\n type = \"prop\", \n val = c(1000,500,200,10), \n inches = .2, \n title = \"Population\", \n pos = \"topleft\"\n)\nmf_legend(\n type = \"choro\", \n val = c(0,10,20,30,40),\n pal = \"Greens\", \n pos = \"bottomright\", \n val_rnd = 0\n)\n\n\n\n\n\n\n5.2.10 Labels\nThe function mf_label() is dedicated to displaying labels.\n\ndist_selected <- district[st_intersects(district, district[district$ADM2_EN == \"Bakan\", ], sparse = F), ]\n\nmf_map(dist_selected)\nmf_label(\n x = dist_selected,\n var = \"ADM2_EN\",\n col= \"darkgreen\",\n halo = TRUE,\n overlap = FALSE, \n lines = FALSE\n)\nmf_scale()\n\n\n\n\nThe argument halo = TRUE allows to display a slight halo around the labels and the argument overlap = FALSE allows to create non-overlapping labels.\n\n\n5.2.11 Center the map on a region\nThe function mf_init() allows you to initialize a map by centering it on a spatial object.\n\nmf_init(x = dist_selected)\nmf_map(district, add = TRUE)\nmf_map(dist_selected, col = NA, border = \"#29a3a3\", lwd = 2, add = TRUE)\n\n\n\n\n\n\n5.2.12 Displaying several maps on the sam figure\nHere you have to use mfrow of the function par(). The first digit represents the number of of rows and second the number of columns.\n\n# define the figure layout (1 row, 2 columns)\npar(mfrow = c(1, 2))\n\n# first map\nmf_map(district)\nmf_map(district, \"Male\", \"prop\", val_max = 300000)\nmf_title(\"Population, male\")\n\n# second map\nmf_map(district)\nmf_map(district, \"Female\", \"prop\", val_max = 300000)\nmf_title(\"Population, female\")\n\n\n\n\n\n\n5.2.13 Exporting maps\nIt is quite difficult to export figures (maps or others) whose height/width ratio is satisfactory. The default ratio of figures in png format is 1 (480x480 pixels):\n\ndist_filter <- district[district$ADM2_PCODE == \"KH0808\", ]\npng(\"img/dist_filter_1.png\")\nmf_map(dist_filter)\nmf_title(\"Filtered district\")\ndev.off()\n\n\n\n\n\n\nOn this map a lot of space is lost to the left and right of the district.\nThe function mf_export() allows exports of maps whose height/width ratio is controlled and corresponds to that of a spatial object.\n\nmf_export(dist_filter, \"img/dist_filter_2.png\", width = 480)\nmf_map(dist_filter)\nmf_title(\"Filtered district\")\ndev.off()\n\n\n\n\n\n\nThe extent of this map is exactly that of the displayed region.\n\n\n5.2.14 Adding an image to a map\nThis can be useful for adding a logo, a pictograph. The function readPNG() of package png allows the additional images on the figure.\n\nmf_theme(\"default\", mar = c(0,0,0,0))\nlibrary(png)\n\nlogo <- readPNG(\"img/ird_logo.png\") #Import image\npp <- dim(logo)[2:1]*200 #Image dimension in map unit (width and height of the original image)\n\n#The upper left corner of the department's bounding box\nxy <- st_bbox(district)[c(1,4)]\nmf_map(district, col = \"#D1914D\", border = \"white\")\nrasterImage(\n image = logo,\n xleft = xy[1] ,\n ybottom = xy[2] - pp[2],\n xright = xy[1] + pp[1],\n ytop = xy[2]\n)\n\n\n\n\n\n\n5.2.15 Place an item precisely on the map\nThe function locator() allows clicking on the figure and obtaining the coordinate of a point in the coordinate system of the figure (of the map).\n\n# locator(1) # click to get coordinate on map\n# points(locator(1)) # click to plot point on map\n# text(locator(1), # click to place the item on map\n# labels =\"Located any texts on map\", \n# adj = c(0,0))\n\n\nVideo\nlocator()peut être utilisée sur la plupart des graphiques (pas ceux produits avec ggplot2).\n\n\n\n\n\n\nHow to interactively position legends and layout elements on a map with cartography\n\n\n\n\n\n5.2.16 Add shading to a layer\nThe function mf_shadow() allows to create a shadow to a layer of polygons.\n\nmf_shadow(district)\nmf_map(district, add=TRUE)\n\n\n\n\n\n\n5.2.17 Creating Boxes\nThe function mf_inset_on() allows to start creation a box. You must then “close†the box with mf_inset_off().\n\nmf_init(x = dist_selected, theme = \"agolalight\", expandBB = c(0,.1,0,.5)) \nmf_map(district, add = TRUE)\nmf_map(dist_selected, col = \"tomato4\", border = \"tomato1\", lwd = 2, add = TRUE)\n\n# Cambodia inset box\nmf_inset_on(x = country, pos = \"topright\", cex = .3)\nmf_map(country, lwd = .5, border= \"grey90\")\nmf_map(dist_selected, col = \"tomato4\", border = \"tomato1\", lwd = .5, add = TRUE)\nmf_scale(size = 100, pos = \"bottomleft\", cex = .6, lwd = .5)\nmf_inset_off()\n\n# District inset box\nmf_inset_on(x = district, pos = \"bottomright\", cex = .3)\nmf_map(district, lwd = 0.5, border= \"grey90\")\nmf_map(dist_selected, col = \"tomato4\", border = \"tomato1\", lwd = .5, add = TRUE)\nmf_scale(size = 100, pos = \"bottomright\", cex = .6, lwd = .5)\nmf_inset_off()\n\n# World inset box\nmf_inset_on(x = \"worldmap\", pos = \"topleft\")\nmf_worldmap(dist_selected, land_col = \"#cccccc\", border_col = NA, \n water_col = \"#e3e3e3\", col = \"tomato4\")\n\nmf_inset_off()\nmf_title(\"Bakan district and its surroundings\")\nmf_scale(10, pos = 'bottomleft')" }, { "objectID": "05-mapping_with_r.html#d-maps", "href": "05-mapping_with_r.html#d-maps", - "title": "4 Mapping With R", - "section": "4.3 3D maps", - "text": "4.3 3D maps\n\n4.3.1 linemap\nThe package linemap (Giraud 2021) allows you to make maps made up of lines.\n\nlibrary(linemap)\nlibrary(mapsf)\nlibrary(sf)\nlibrary(dplyr)\n\npp = st_read(\"data_cambodia/PP.gpkg\", quiet = TRUE) # import Phnom Penh administrative border\npp_pop_dens <- getgrid(x = pp, cellsize =1000, var = \"DENs\") # create population density in grid format (pop density/1km)\n\nmf_init(pp)\n\nlinemap(\n x = pp_pop_dens, \n var = \"DENs\",\n k = 1,\n threshold = 5, \n lwd = 1,\n col = \"ivory1\",\n border = \"ivory4\",\n add = T)\n\nmf_title(\"Phnom Penh Population Density, 2019\")\nmf_credits(\"Humanitarian Data Exchange, 2022\\nunit data:km2\")\n\n\n\n# url = \"https://data.humdata.org/dataset/1803994d-6218-4b98-ac3a-30c7f85c6dbc/resource/f30b0f4b-1c40-45f3-986d-2820375ea8dd/download/health_facility.zip\"\n# health_facility.zip = \"health_facility.zip\"\n# download.file(url, destfile = health_facility.zip)\n# unzip(health_facility.zip) # Unzipped files are in a new folder named Health\n# list.files(path=\"Health\")\n\n\n\n4.3.2 Relief Tanaka\nWe use the tanaka package (Giraud 2022b) which provides a method (Tanaka 1950) used to improve the perception of relief.\n\nlibrary(tanaka)\nlibrary(terra)\n\nrpop <- rast(\"data_cambodia/khm_pd_2019_1km_utm.tif\") # Import population raster data (in UTM)\ndistrict = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"district\", quiet = TRUE) # Import Cambodian districts layer\ndistrict <- st_transform(district, st_crs(rpop)) # Transform data into the same coordinate system\n\nmat <- focalMat(x = rpop, d = c(1500), type = \"Gauss\") # Raster smoothing\nrpopl <- focal(x = rpop, w = mat, fun = sum, na.rm = TRUE)\n\n# Mapping\ncols <- hcl.colors(8, \"Reds\", alpha = 1, rev = T)[-1]\nmf_theme(\"agolalight\")\nmf_init(district)\ntanaka(x = rpop, breaks = c(0,10,25,50,100,250,500,64265),\n col = cols, add = T, mask = district, legend.pos = \"n\")\nmf_legend(type = \"choro\", pos = \"bottomright\", \n val = c(0,10,25,50,100,250,500,64265), pal = cols,\n bg = \"#EDF4F5\", fg = NA, frame = T, val_rnd = 0,\n title = \"Population\\nper km2\")\nmf_title(\"Population density of Cambodia, 2019\")\nmf_credits(\"Humanitarian Data Exchange, 2022\",\n bg = \"#EDF4F5\")\n\n\n\n\n\n\n\n\n\n\nThe tanaka package" + "title": "5 Mapping With R", + "section": "5.3 3D maps", + "text": "5.3 3D maps\n\n5.3.1 linemap\nThe package linemap (Giraud 2021) allows you to make maps made up of lines.\n\nlibrary(linemap)\nlibrary(mapsf)\nlibrary(sf)\nlibrary(dplyr)\n\npp = st_read(\"data_cambodia/PP.gpkg\", quiet = TRUE) # import Phnom Penh administrative border\npp_pop_dens <- getgrid(x = pp, cellsize =1000, var = \"DENs\") # create population density in grid format (pop density/1km)\n\nmf_init(pp)\n\nlinemap(\n x = pp_pop_dens, \n var = \"DENs\",\n k = 1,\n threshold = 5, \n lwd = 1,\n col = \"ivory1\",\n border = \"ivory4\",\n add = T)\n\nmf_title(\"Phnom Penh Population Density, 2019\")\nmf_credits(\"Humanitarian Data Exchange, 2022\\nunit data:km2\")\n\n\n\n# url = \"https://data.humdata.org/dataset/1803994d-6218-4b98-ac3a-30c7f85c6dbc/resource/f30b0f4b-1c40-45f3-986d-2820375ea8dd/download/health_facility.zip\"\n# health_facility.zip = \"health_facility.zip\"\n# download.file(url, destfile = health_facility.zip)\n# unzip(health_facility.zip) # Unzipped files are in a new folder named Health\n# list.files(path=\"Health\")\n\n\n\n5.3.2 Relief Tanaka\nWe use the tanaka package (Giraud 2022b) which provides a method (Tanaka 1950) used to improve the perception of relief.\n\nlibrary(tanaka)\nlibrary(terra)\n\nrpop <- rast(\"data_cambodia/khm_pd_2019_1km_utm.tif\") # Import population raster data (in UTM)\ndistrict = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"district\", quiet = TRUE) # Import Cambodian districts layer\ndistrict <- st_transform(district, st_crs(rpop)) # Transform data into the same coordinate system\n\nmat <- focalMat(x = rpop, d = c(1500), type = \"Gauss\") # Raster smoothing\nrpopl <- focal(x = rpop, w = mat, fun = sum, na.rm = TRUE)\n\n# Mapping\ncols <- hcl.colors(8, \"Reds\", alpha = 1, rev = T)[-1]\nmf_theme(\"agolalight\")\nmf_init(district)\ntanaka(x = rpop, breaks = c(0,10,25,50,100,250,500,64265),\n col = cols, add = T, mask = district, legend.pos = \"n\")\nmf_legend(type = \"choro\", pos = \"bottomright\", \n val = c(0,10,25,50,100,250,500,64265), pal = cols,\n bg = \"#EDF4F5\", fg = NA, frame = T, val_rnd = 0,\n title = \"Population\\nper km2\")\nmf_title(\"Population density of Cambodia, 2019\")\nmf_credits(\"Humanitarian Data Exchange, 2022\",\n bg = \"#EDF4F5\")\n\n\n\n\n\n\n\n\n\n\nThe tanaka package" }, { "objectID": "05-mapping_with_r.html#cartographic-transformation", "href": "05-mapping_with_r.html#cartographic-transformation", - "title": "4 Mapping With R", - "section": "4.4 Cartographic Transformation", - "text": "4.4 Cartographic Transformation\n\nclassical anamorphosis is a representation of States(or any cells) by rectangle or any polygons according to a quantities attached to them. (…) We strive to keep the general arrangement of meshes or the silhouette of the continent.â€\nBrunet, Ferras, and Théry (1993)\n\n3 types of anamorphoses or cartograms are presented here:\n\nDorling’s cartograms (Dorling 1996)\nNon-contiguous cartograms (Olson 1976)\nContiguous cartograms (Dougenik, Chrisman, and Niemeyer 1985)\n\n\n\n\n\n\n\nA comprehensive course on anamorphoses : Les anamorphoses cartographiques (Lambert 2015).\n\n\n\n\n\n\n\n\n\nMake cartograms with R\n\n\n\nTo make the cartograms we use the package cartogram (Jeworutzki 2020).\n\n4.4.1 Dorling’s cartograms\nThe territories are represented by figures (circles, squares or rectangles) which do not overlap, the surface of which are proportional to a variable. The proportion of the figures are defined according to the starting positions.\n\n\n\n\n\n\n\n\nSpace is quite poorly identified.\nYou can name the circles to get your bearings and/or use the color to make clusters appear and better identify the geographical blocks.\n\n\n\n\n\nThe perception of quantities is very good. The circle sizes are really comarable.\n\n\n\nlibrary(mapsf)\nlibrary(cartogram)\ndistrict <- st_read(\"data_cambodia/cambodia.gpkg\", layer = \"district\" , quiet = TRUE)\ndist_dorling <- cartogram_dorling(x = district, weight = \"T_POP\", k = 0.7)\nmf_map(dist_dorling, col = \"#40E0D0\", border= \"white\")\nmf_label(\n x = dist_dorling[order(dist_dorling$T_POP, decreasing = TRUE), ][1:10,], \n var = \"ADM2_EN\",\n overlap = FALSE, \n # show.lines = FALSE,\n halo = TRUE, \n r = 0.15\n)\nmf_title(\"Population of District - Dorling Cartogram\")\n\n\n\n\nThe parameter k allows to vary the expansion factor of the circles.\n\n\n4.4.2 Non-continuous cartograms\nThe size of the polygons is proportional to a variable. The arrangement of the polygons relative to each other is preserved. The shape of the polygons is similar.\n\n\n\n\n\n(Cauvin, Escobar, and Serradj 2013)\n\n\n\nThe topology of the regions is lost.\n\n\n\n\n\nThe converstion of the polygons shape is optimal.\n\n\n\ndist_ncont <- cartogram_ncont(x = district, weight = \"T_POP\", k = 1.2)\nmf_map(district, col = NA, border = \"#FDFEFE\", lwd = 1.5)\nmf_map(dist_ncont, col = \"#20B2AA\", border= \"white\", add = TRUE)\nmf_title(\"Population of District - Non-continuous cartograms\")\n\n\n\n\nThe parameter k allows to vary the expansion of the polygons.\n\n\n4.4.3 Continuous cartograms\nThe size of the polygons is proportional to variable. The arrangement of the polygons relative to each other is preserved. To maintain contiguity, the sape of the polygons is heavily transformed.\n\n\n\n\n\n(Paull and Hennig 2016)\n\n\n\nThe shape of the polygond is strongly distorted.\n\n\n\n\n\nIt is a “real geographical mapâ€: topology and contiguity are preserved.\n\n\n\ndist_ncont <- cartogram_cont(x = district, weight = \"DENs\", maxSizeError = 6)\n\nMean size error for iteration 1: 15.8686749410166\n\n\nMean size error for iteration 2: 12.1107731631101\n\n\nMean size error for iteration 3: 9.98940057337996\n\n\nMean size error for iteration 4: 8.62323208787643\n\n\nMean size error for iteration 5: 7.60706404894655\n\n\nMean size error for iteration 6: 6.83561617758241\n\n\nMean size error for iteration 7: 10.1399490743501\n\n\nMean size error for iteration 8: 5.79418495291592\n\nmf_map(dist_ncont, col = \"#66CDAA\", border= \"white\", add = FALSE)\nmf_title(\"Population of District - Continuous cartograms\")\nmf_inset_on(district, cex = .2, pos = \"bottomleft\")\nmf_map(district, lwd = .5)\nmf_inset_off()\n\n\n\n\n\n\n4.4.4 Stengths and weaknessses of cartograms\ncartograms are cartographic representations perceived as innovative (although the method is 40 years old). These very generalize images capture quantities and gradients well. These are real communication images that provoke, arouse interest, convey a strong message, challenge.\nBut cartograms induce a loss of visual cues (difficult to find one’s country or region on the map), require a reading effort which can be significant and do not make it possible to manage missing data.\n\n\n\n\nBrunet, Roger, Robert Ferras, and Hervé Théry. 1993. Les Mots de La géographie: Dictionnaire Critique. 03) 911 BRU.\n\n\nCauvin, Colette, Francisco Escobar, and Aziz Serradj. 2013. Thematic Cartography, Cartography and the Impact of the Quantitative Revolution. Vol. 2. John Wiley & Sons.\n\n\nDorling, Daniel. 1996. Area Cartograms: Their Use and Creation, Concepts and Techniques in Modern Geography. Vol. 59. CATMOG: Concepts and Techniques in Modern Geography. Institute of British Geographers.\n\n\nDougenik, James A, Nicholas R Chrisman, and Duane R Niemeyer. 1985. “An Algorithm to Construct Continuous Area Cartograms.†The Professional Geographer 37 (1): 75–81.\n\n\nGiraud, Timothée. 2021. “Linemap: Line Maps.†https://CRAN.R-project.org/package=linemap.\n\n\n———. 2022a. “Mapsf: Thematic Cartography.†https://CRAN.R-project.org/package=mapsf.\n\n\n———. 2022b. “Tanaka: Design Shaded Contour Lines (or Tanaka) Maps.†https://CRAN.R-project.org/package=tanaka.\n\n\nJeworutzki, Sebastian. 2020. “Cartogram: Create Cartograms with r.†https://CRAN.R-project.org/package=cartogram.\n\n\nLambert, Nicolas. 2015. “Les Anamorphoses Cartographiques.†Blog. Carnet Néocartographique. https://neocarto.hypotheses.org/366.\n\n\nOlson, Judy M. 1976. “Noncontiguous Area Cartograms.†The Professional Geographer 28 (4): 371–80.\n\n\nPaull, John, and Benjamin Hennig. 2016. “Atlas of Organics: Four Maps of the World of Organic Agriculture.†Journal of Organics 3 (1): 25–32.\n\n\nTanaka, Kitiro. 1950. “The Relief Contour Method of Representing Topography on Maps.†Geographical Review 40 (3): 444. https://doi.org/10.2307/211219." + "title": "5 Mapping With R", + "section": "5.4 Cartographic Transformation", + "text": "5.4 Cartographic Transformation\n\nclassical anamorphosis is a representation of States(or any cells) by rectangle or any polygons according to a quantities attached to them. (…) We strive to keep the general arrangement of meshes or the silhouette of the continent.â€\nBrunet, Ferras, and Théry (1993)\n\n3 types of anamorphoses or cartograms are presented here:\n\nDorling’s cartograms (Dorling 1996)\nNon-contiguous cartograms (Olson 1976)\nContiguous cartograms (Dougenik, Chrisman, and Niemeyer 1985)\n\n\n\n\n\n\n\nA comprehensive course on anamorphoses : Les anamorphoses cartographiques (Lambert 2015).\n\n\n\n\n\n\n\n\n\nMake cartograms with R\n\n\n\nTo make the cartograms we use the package cartogram (Jeworutzki 2020).\n\n5.4.1 Dorling’s cartograms\nThe territories are represented by figures (circles, squares or rectangles) which do not overlap, the surface of which are proportional to a variable. The proportion of the figures are defined according to the starting positions.\n\n\n\n\n\n\n\n\nSpace is quite poorly identified.\nYou can name the circles to get your bearings and/or use the color to make clusters appear and better identify the geographical blocks.\n\n\n\n\n\nThe perception of quantities is very good. The circle sizes are really comarable.\n\n\n\nlibrary(mapsf)\nlibrary(cartogram)\ndistrict <- st_read(\"data_cambodia/cambodia.gpkg\", layer = \"district\" , quiet = TRUE)\ndist_dorling <- cartogram_dorling(x = district, weight = \"T_POP\", k = 0.7)\nmf_map(dist_dorling, col = \"#40E0D0\", border= \"white\")\nmf_label(\n x = dist_dorling[order(dist_dorling$T_POP, decreasing = TRUE), ][1:10,], \n var = \"ADM2_EN\",\n overlap = FALSE, \n # show.lines = FALSE,\n halo = TRUE, \n r = 0.15\n)\nmf_title(\"Population of District - Dorling Cartogram\")\n\n\n\n\nThe parameter k allows to vary the expansion factor of the circles.\n\n\n5.4.2 Non-continuous cartograms\nThe size of the polygons is proportional to a variable. The arrangement of the polygons relative to each other is preserved. The shape of the polygons is similar.\n\n\n\n\n\n(Cauvin, Escobar, and Serradj 2013)\n\n\n\nThe topology of the regions is lost.\n\n\n\n\n\nThe converstion of the polygons shape is optimal.\n\n\n\ndist_ncont <- cartogram_ncont(x = district, weight = \"T_POP\", k = 1.2)\nmf_map(district, col = NA, border = \"#FDFEFE\", lwd = 1.5)\nmf_map(dist_ncont, col = \"#20B2AA\", border= \"white\", add = TRUE)\nmf_title(\"Population of District - Non-continuous cartograms\")\n\n\n\n\nThe parameter k allows to vary the expansion of the polygons.\n\n\n5.4.3 Continuous cartograms\nThe size of the polygons is proportional to variable. The arrangement of the polygons relative to each other is preserved. To maintain contiguity, the sape of the polygons is heavily transformed.\n\n\n\n\n\n(Paull and Hennig 2016)\n\n\n\nThe shape of the polygond is strongly distorted.\n\n\n\n\n\nIt is a “real geographical mapâ€: topology and contiguity are preserved.\n\n\n\ndist_ncont <- cartogram_cont(x = district, weight = \"DENs\", maxSizeError = 6)\n\nMean size error for iteration 1: 15.8686749410166\n\n\nMean size error for iteration 2: 12.1107731631101\n\n\nMean size error for iteration 3: 9.98940057337996\n\n\nMean size error for iteration 4: 8.62323208787643\n\n\nMean size error for iteration 5: 7.60706404894655\n\n\nMean size error for iteration 6: 6.83561617758241\n\n\nMean size error for iteration 7: 10.1399490743501\n\n\nMean size error for iteration 8: 5.79418495291592\n\nmf_map(dist_ncont, col = \"#66CDAA\", border= \"white\", add = FALSE)\nmf_title(\"Population of District - Continuous cartograms\")\nmf_inset_on(district, cex = .2, pos = \"bottomleft\")\nmf_map(district, lwd = .5)\nmf_inset_off()\n\n\n\n\n\n\n5.4.4 Stengths and weaknessses of cartograms\ncartograms are cartographic representations perceived as innovative (although the method is 40 years old). These very generalize images capture quantities and gradients well. These are real communication images that provoke, arouse interest, convey a strong message, challenge.\nBut cartograms induce a loss of visual cues (difficult to find one’s country or region on the map), require a reading effort which can be significant and do not make it possible to manage missing data.\n\n\n\n\nBrunet, Roger, Robert Ferras, and Hervé Théry. 1993. Les Mots de La géographie: Dictionnaire Critique. 03) 911 BRU.\n\n\nCauvin, Colette, Francisco Escobar, and Aziz Serradj. 2013. Thematic Cartography, Cartography and the Impact of the Quantitative Revolution. Vol. 2. John Wiley & Sons.\n\n\nDorling, Daniel. 1996. Area Cartograms: Their Use and Creation, Concepts and Techniques in Modern Geography. Vol. 59. CATMOG: Concepts and Techniques in Modern Geography. Institute of British Geographers.\n\n\nDougenik, James A, Nicholas R Chrisman, and Duane R Niemeyer. 1985. “An Algorithm to Construct Continuous Area Cartograms.†The Professional Geographer 37 (1): 75–81.\n\n\nGiraud, Timothée. 2021. “Linemap: Line Maps.†https://CRAN.R-project.org/package=linemap.\n\n\n———. 2022a. “Mapsf: Thematic Cartography.†https://CRAN.R-project.org/package=mapsf.\n\n\n———. 2022b. “Tanaka: Design Shaded Contour Lines (or Tanaka) Maps.†https://CRAN.R-project.org/package=tanaka.\n\n\nJeworutzki, Sebastian. 2020. “Cartogram: Create Cartograms with r.†https://CRAN.R-project.org/package=cartogram.\n\n\nLambert, Nicolas. 2015. “Les Anamorphoses Cartographiques.†Blog. Carnet Néocartographique. https://neocarto.hypotheses.org/366.\n\n\nOlson, Judy M. 1976. “Noncontiguous Area Cartograms.†The Professional Geographer 28 (4): 371–80.\n\n\nPaull, John, and Benjamin Hennig. 2016. “Atlas of Organics: Four Maps of the World of Organic Agriculture.†Journal of Organics 3 (1): 25–32.\n\n\nTanaka, Kitiro. 1950. “The Relief Contour Method of Representing Topography on Maps.†Geographical Review 40 (3): 444. https://doi.org/10.2307/211219." }, { "objectID": "references.html", @@ -145,5 +145,54 @@ "title": "References", "section": "", "text": "Agafonkin, Vladimir. 2015. “Leaflet Javascript Libary.â€\n\n\nAppelhans, Tim, Florian Detsch, Christoph Reudenbach, and Stefan\nWoellauer. 2022. “Mapview: Interactive Viewing of Spatial Data in\nr.†https://CRAN.R-project.org/package=mapview.\n\n\nAppelhans, Tim, Kenton Russell, and Lorenzo Busetto. 2020.\n“Mapedit: Interactive Editing of Spatial Data in r.†https://CRAN.R-project.org/package=mapedit.\n\n\nBivand, Roger, Tim Keitt, and Barry Rowlingson. 2022. “Rgdal:\nBindings for the ’Geospatial’ Data Abstraction Library.†https://CRAN.R-project.org/package=rgdal.\n\n\nBivand, Roger, and Colin Rundel. 2021. “Rgeos: Interface to\nGeometry Engine - Open Source (’GEOS’).†https://CRAN.R-project.org/package=rgeos.\n\n\nBrunet, Roger, Robert Ferras, and Hervé Théry. 1993. Les Mots de La\ngéographie: Dictionnaire Critique. 03) 911 BRU.\n\n\nCambon, Jesse, Diego Hernangómez, Christopher Belanger, and Daniel\nPossenriede. 2021. “Tidygeocoder: An r Package for\nGeocoding†6: 3544. https://doi.org/10.21105/joss.03544.\n\n\nCauvin, Colette, Francisco Escobar, and Aziz Serradj. 2013. Thematic\nCartography, Cartography and the Impact of the Quantitative\nRevolution. Vol. 2. John Wiley & Sons.\n\n\nCheng, Joe, Bhaskar Karambelkar, and Yihui Xie. 2022. “Leaflet:\nCreate Interactive Web Maps with the JavaScript ’Leaflet’\nLibrary.†https://CRAN.R-project.org/package=leaflet.\n\n\nDorling, Daniel. 1996. Area Cartograms: Their Use and Creation,\nConcepts and Techniques in Modern Geography. Vol. 59. CATMOG:\nConcepts and Techniques in Modern Geography. Institute of British\nGeographers.\n\n\nDougenik, James A, Nicholas R Chrisman, and Duane R Niemeyer. 1985.\n“An Algorithm to Construct Continuous Area Cartograms.â€\nThe Professional Geographer 37 (1): 75–81.\n\n\nDunnington, Dewey. 2021. “Ggspatial: Spatial Data Framework for\nGgplot2.†https://CRAN.R-project.org/package=ggspatial.\n\n\nGDAL/OGR contributors. n.d. GDAL/OGR Geospatial Data\nAbstraction Software Library. Open Source Geospatial Foundation. https://gdal.org.\n\n\nGilardi, Andrea, and Robin Lovelace. 2021. “Osmextract: Download\nand Import Open Street Map Data Extracts.†https://CRAN.R-project.org/package=osmextract.\n\n\nGiraud, Timothée. 2021a. “Linemap: Line Maps.†https://CRAN.R-project.org/package=linemap.\n\n\n———. 2021b. “Maptiles: Download and Display Map Tiles.†https://CRAN.R-project.org/package=maptiles.\n\n\n———. 2022a. “Mapsf: Thematic Cartography.†https://CRAN.R-project.org/package=mapsf.\n\n\n———. 2022b. “Tanaka: Design Shaded Contour Lines (or Tanaka)\nMaps.†https://CRAN.R-project.org/package=tanaka.\n\n\nGiraud, Timothée, and Nicolas Lambert. 2016. “Cartography: Create\nand Integrate Maps in Your r Workflow†1. https://doi.org/10.21105/joss.00054.\n\n\nGombin, Joel, and Paul-Antoine Chevalier. 2022. “banR: R Client\nfor the BAN API.â€\n\n\nHijmans, Robert J. 2022a. “Raster: Geographic Data Analysis and\nModeling.†https://CRAN.R-project.org/package=raster.\n\n\n———. 2022b. “Terra: Spatial Data Analysis.†https://CRAN.R-project.org/package=terra.\n\n\nJeworutzki, Sebastian. 2020. “Cartogram: Create Cartograms with\nr.†https://CRAN.R-project.org/package=cartogram.\n\n\nLambert, Nicolas. 2015. “Les Anamorphoses Cartographiques.â€\nBlog. Carnet Néocartographique. https://neocarto.hypotheses.org/366.\n\n\nOlson, Judy M. 1976. “Noncontiguous Area Cartograms.â€\nThe Professional Geographer 28 (4): 371–80.\n\n\nPadgham, Mark, Bob Rudis, Robin Lovelace, and Maëlle Salmon. 2017.\n“Osmdata†2. https://doi.org/10.21105/joss.00305.\n\n\nPaull, John, and Benjamin Hennig. 2016. “Atlas of Organics: Four\nMaps of the World of Organic Agriculture.†Journal of\nOrganics 3 (1): 25–32.\n\n\nPebesma, Edzer. 2018b. “Simple Features for r:\nStandardized Support for Spatial Vector Data†10. https://doi.org/10.32614/RJ-2018-009.\n\n\n———. 2018a. “Simple Features for R: Standardized Support for\nSpatial Vector Data.†The R Journal 10 (1): 439. https://doi.org/10.32614/rj-2018-009.\n\n\n———. 2021. “Stars: Spatiotemporal Arrays, Raster and Vector Data\nCubes.†https://CRAN.R-project.org/package=stars.\n\n\nPebesma, Edzer J., and Roger S. Bivand. 2005. “Classes and Methods\nfor Spatial Data in r†5. https://CRAN.R-project.org/doc/Rnews/.\n\n\nPROJ contributors. 2021. PROJ Coordinate Transformation\nSoftware Library. Open Source Geospatial Foundation. https://proj.org/.\n\n\nTanaka, Kitiro. 1950. “The Relief Contour Method of Representing\nTopography on Maps.†Geographical Review 40 (3): 444. https://doi.org/10.2307/211219.\n\n\nTennekes, Martijn. 2018. “Tmap: Thematic\nMaps in r†84. https://doi.org/10.18637/jss.v084.i06.\n\n\nWickham, Hadley. 2016. “Ggplot2: Elegant Graphics for Data\nAnalysis.†https://ggplot2.tidyverse.org." + }, + { + "objectID": "04-raster_data.html", + "href": "04-raster_data.html", + "title": "4 Work with Raster Data", + "section": "", + "text": "This chapter is largely inspired by two presentation; Madelin (2021) and Nowosad (2021); carried out as part of the SIGR2021 thematic school." + }, + { + "objectID": "04-raster_data.html#format-of-objects-spatraster", + "href": "04-raster_data.html#format-of-objects-spatraster", + "title": "4 Work with Raster Data", + "section": "4.1 Format of objects SpatRaster", + "text": "4.1 Format of objects SpatRaster\nThe package terra (Hijmans 2022) allows to handle vector and raster data. To manipulate this spatial data, terra store it in object of type SpatVector and SpatRaster. In this chapter, we focus on the manipulation of raster data (SpatRaster) from functions offered by this package.\nAn object SpatRaster allows to handle vector and raster data, in one or more layers (variables). This object also stores a number of fundamental parameters that describe it (number of columns, rows, spatial extent, coordinate reference system, etc.).\n\n\n\nSource : (Racine 2016)" + }, + { + "objectID": "04-raster_data.html#importing-and-exporting-data", + "href": "04-raster_data.html#importing-and-exporting-data", + "title": "4 Work with Raster Data", + "section": "4.2 Importing and exporting data", + "text": "4.2 Importing and exporting data\nThe package terra allows importing and exporting raster files. It is based on the GDAL library which makes it possible to read and process a very large number of geographic image formats.\n\nlibrary(terra)\n\nThe function rast() allows you to create and/or import raster data. The following lines import the raster file elevation.tif (Tagged Image File Format) into an object of type SpatRaster (default).\n\nelevation <- rast(\"data_cambodia/elevation.tif\") \nelevation\n\nclass : SpatRaster \ndimensions : 5235, 6458, 1 (nrow, ncol, nlyr)\nresolution : 0.0008333394, 0.0008332568 (x, y)\nextent : 102.2935, 107.6752, 10.33984, 14.70194 (xmin, xmax, ymin, ymax)\ncoord. ref. : lon/lat WGS 84 (EPSG:4326) \nsource : elevation.tif \nname : elevation \n\n\nModifying the name of the stored variable (altitude).\n\nnames(elevation) <- \"Altitude\" \n\nThe function writeRaster() allow you to save an object SpatRaster on your machine, in the format of your choice.\n\nwriteRaster(x = elevation, filename = \"data_cambodia/new_elevation.tif\")" + }, + { + "objectID": "04-raster_data.html#displaying-a-spatraster-object", + "href": "04-raster_data.html#displaying-a-spatraster-object", + "title": "4 Work with Raster Data", + "section": "4.3 Displaying a SpatRaster object", + "text": "4.3 Displaying a SpatRaster object\nThe function plot() is use to display an object SpatRaster.\n\nplot(elevation)\n\n\n\n\n\n\n\n\nA raster always contains numerical data, but it can be both quantitative data and numerically coded qualitative (categorical) data (ex: type of land cover).\nSpecify the type of data stored with the augment type (type = \"continuous\" default), to display them correctly.\nImport and display of raster containing categorical data: Phnom Penh Land Cover 2019 (land cover types) with a resolution of 1.5 meters:\n\nlulc_2019 <- rast(\"data_cambodia/lulc_2019.tif\") #Import Phnom Penh landcover 2019, landcover types\n\nThe landcover data was produced from SPOT7 satellite image with 1.5 meter spatial resolution. An extraction centered on the municipality of Phnom Penh was then carried out.\n\nplot(lulc_2019, type = \"classes\")\n\n\n\n\n\n\n\n\nTo display the actual tiles of landcover types, as well as the official colors of Phnom Penh Landcover nomenclature (available here), you can proceed as follows.\n\nclass_name <- c(\n \"Roads\",\n \"Built-up areas\",\n \"Water Bodies and rivers\",\n \"Wetlands\",\n \"Dry bare area\",\n \"Bare crop fields\",\n \"Low vegetation areas\",\n \"High vegetation areas\",\n \"Forested areas\")\n\nclass_color <- c(\"#070401\", \"#c84639\", \"#1398eb\",\"#8bc2c2\",\n \"#dc7b34\", \"#a6bd5f\",\"#e8e8e8\", \"#4fb040\", \"#35741f\")\nplot(lulc_2019,\n type = \"classes\",\n levels = class_name,\n col = class_color,\n plg = list(cex = 0.7),\n mar = c(3.1, 3.1, 2.1, 10) #The margin are (bottom, left, top, right) respectively\n )" + }, + { + "objectID": "04-raster_data.html#change-to-the-study-area", + "href": "04-raster_data.html#change-to-the-study-area", + "title": "4 Work with Raster Data", + "section": "4.4 Change to the study area", + "text": "4.4 Change to the study area\n\n4.4.1 (Re)projections\nTo modify the projection system of a raster, use the function project(). It is then necessary to indicate the method for estimating the new cell values.\n\n\n\nSource : Centre Canadien de Télédétection\n\n\nFour interpolation methods are available:\n\nnear : nearest neighbor, fast and default method for qualitative data;\n\nbilinear : bilinear interpolation. Default method for quantitative data;\n\ncubic : cubic interpolation;\n\ncubicspline : cubic spline interpolation.\n\n\n# Re-project data \n\nelevation_utm = project(x = elevation, y = \"EPSG:32648\", method = \"bilinear\") #from WGS84(EPSG:4326) to UTM zone48N(EPSG:32648) \nlulc_2019_utm = project(x = lulc_2019, y = \"EPSG:32648\", method = \"near\") #keep original projection: UTM zone48N(EPSG:32648)\n\n\n\n\n\n\n\n\n\n\n\n\n4.4.2 Crop\nClipping a raster to the extent of another object SpatVector or SpatRaster is achievable with the crop().\n\n\n\n\n\n\n\n\n\n\n\nSource : (Racine 2016)\n\n\n\nImport vector data of (municipal divisions) using function vect. This data will be stored in an SpatVector object.\n\ndistrict <- vect(\"data_cambodia/cambodia.gpkg\", layer=\"district\")\n\nExtraction of district boundaries of Thma Bang district (ADM2_PCODE : KH0907).\n\nthma_bang <- subset(district, district$ADM2_PCODE == \"KH0907\") \n\nUsing the function crop(), Both data layers must be in the same projection.\n\ncrop_thma_bang <- crop(elevation_utm, thma_bang)\n\nplot(crop_thma_bang)\nplot(thma_bang, add=TRUE)\n\n\n\n\n\n\n\n\n\n\n4.4.3 Mask\nTo display only the values of a raster contained in a polygon, use the function mask().\n\n\n\nSource : (Racine 2016)\n\n\nCreation of a mask on the crop_thma_bang raster to the municipal limits (polygon) of Thma Bang district.\n\nmask_thma_bang <- mask(crop_thma_bang, thma_bang)\n\nplot(mask_thma_bang)\nplot(thma_bang, add = TRUE)\n\n\n\n\n\n\n\n\n\n\n4.4.4 Aggregation and disaggregation\nResampling a raster to a different resolution is done in two steps.\n\n\n\n\n\n\n1\n\n\n\n\n\n\n\n2\n\n\n\n\n\n\n\n3\n\n\n\n\n\n\nSource : (Racine 2016)\n\n\n\nDisplay the resolution of a raster with the function res().\n\nres(elevation_utm) #check cell size\n\n[1] 91.19475 91.19475\n\n\nCreate a grid with the same extent, then decrease the spatial resolution (larger cells).\n\nelevation_LowerGrid <- elevation_utm\n# elevation_HigherGrid <- elevation_utm\n\nres(elevation_LowerGrid) <- 1000 #cells size = 1000 meter\n# res(elevation_HigherGrid) <- 10 #cells size = 10 meter\n\nelevation_LowerGrid\n\nclass : SpatRaster \ndimensions : 484, 589, 1 (nrow, ncol, nlyr)\nresolution : 1000, 1000 (x, y)\nextent : 203586.3, 792586.3, 1142954, 1626954 (xmin, xmax, ymin, ymax)\ncoord. ref. : WGS 84 / UTM zone 48N (EPSG:32648) \n\n\nThe function resample() allows to resample the atarting values in the new spatial resolution. Several resampling methods are available (cf. partie 5.4.1).\n\nelevation_LowerGrid <- resample(elevation_utm, \n elevation_LowerGrid, \n method = \"bilinear\") \n\nplot(elevation_LowerGrid, \n main=\"Cell size = 1000m\\nBilinear resampling method\")\n\n\n\n\n\n\n\n\n\n\n4.4.5 Raster fusion\nMerge multiple objects SpatRaster into one with merge() or mosaic().\n\n\n\nSource : https://desktop.arcgis.com/fr/arcmap/10.3/manage-data/raster-and-images/what-is-a-mosaic.htm\n\n\nAfter cutting the elevation raster by the municipal boundary of Thma Bang district (cf partie 5.4.2), we do the same thing for the neighboring municipality of Phnum Kravanh district.\n\nphnum_kravanh <- subset(district, district$ADM2_PCODE == \"KH1504\") # Extraction of the municipal boundaries of Phnum Kravanh district\n\ncrop_phnum_kravanh <- crop(elevation_utm, phnum_kravanh) #clipping the elevation raster according to district boundaries\n\nThe crop_thma_bang and crop_phnum_kravanh elevation raster overlap spatially:\n\n\n\n\n\n\n\n\n\nThe difference between the functions merge() and mosiac() relates to values of the overlapping cells. The function mosaic() calculate the average value while merge() holding the value of the object SpaRaster called n the function.\n\n#in this example, merge() and mosaic() give the same result\nmerge_raster <- merge(crop_thma_bang, crop_phnum_kravanh) \nmosaic_raster <- mosaic(crop_thma_bang, crop_phnum_kravanh)\n\nplot(merge_raster)\n\n\n\n\n\n\n\n# plot(mosaic_raster)\n\n\n\n4.4.6 Segregate\nDecompose a raster by value (or modality) into different rasterlayers with the function segregate.\n\nlulc_2019_class <- segregate(lulc_2019, keep=TRUE, other=NA) #creating a raster layer by modality\nplot(lulc_2019_class)" + }, + { + "objectID": "04-raster_data.html#map-algebra", + "href": "04-raster_data.html#map-algebra", + "title": "4 Work with Raster Data", + "section": "4.5 Map Algebra", + "text": "4.5 Map Algebra\nMap algebra is classified into four groups of operation (Tomlin_1990?):\n\nLocal : operation by cell, on one or more layers;\n\nFocal : neighborhood operation (surrounding cells);\n\nZonal : to summarize the matrix values for certain zones, usually irregular;\nGlobal : to summarize the matrix values of one or more matrices.\n\n\n\n\nSource : (Li 2009)\n\n\n\n4.5.1 Local operations\n\n\n\nSource : (Mennis 2015)\n\n\n\n4.5.1.1 Value replacement\n\nelevation_utm[elevation_utm[[1]]== -9999] <- NA #replaces -9999 values with NA\n\nelevation_utm[elevation_utm < 1500] <- NA #Replace values < 1500 with NA\n\n\nelevation_utm[is.na(elevation_utm)] <- 0 #replace NA values with 0\n\n\n\n4.5.1.2 Operation on each cell\n\nelevation_1000 <- elevation_utm + 1000 # Adding 1000 to the value of each cell\n\nelevation_median <- elevation_utm - global(elevation_utm, median)[[1]] # Removed median elevation to each cell's value\n\n\n\n\n\n\n\n\n\n\n\n\n4.5.1.3 Reclassification\nReclassifying raster values can be used to discretize quantitative data as well as to categorize qualitative categories.\n\nreclassif <- matrix(c(1, 2, 1, \n 2, 4, 2,\n 4, 6, 3,\n 6, 9, 4), \n ncol = 3, byrow = TRUE)\n\nValues between 1 and 2 will be replaced by the value 1.\nValues between 3 and 4 will be replaced by the value 2.\nValues between 5 and 6 will be replaced by the value 3. Values between 7 and 9 will be replaced by the value 4.\n…\n\nreclassif\n\n [,1] [,2] [,3]\n[1,] 1 2 1\n[2,] 2 4 2\n[3,] 4 6 3\n[4,] 6 9 4\n\n\nThe function classify() allows you to perform the reclassification.\n\nlulc_2019_reclass <- classify(lulc_2019, rcl = reclassif)\nplot(lulc_2019_reclass, type =\"classes\")\n\n\n\n\nDisplay with the official titles and colors of the different categories.\n\nplot(lulc_2019_reclass, \n type =\"classes\", \n levels=c(\"Urban areas\",\n \"Water body\",\n \"Bare areas\",\n \"Vegetation areas\"),\n col=c(\"#E6004D\",\n \"#00BFFF\",\n \"#D3D3D3\", \n \"#32CD32\"),\n mar=c(3, 1.5, 1, 11))\n\n\n\n\n\n\n\n\n\n\n4.5.1.4 Operation on several layers (ex: NDVI)\nIt is possible to calculate the value of a cell from its values stored in different layers of an object SpatRaster.\nPerhaps the most common example is the calculation of the Normalized Vegetation Index (NDVI). For each cell, a value is calculated from two layers of raster from a multispectral satellite image.\n\n# Import d'une image satellite multispectrale\nsentinel2a <- rast(\"data_cambodia/Sentinel2A.tif\")\n\nThis multispectral satellite image (10m resolution) dated 25/02/2020, was produced by Sentinel-2 satellite and was retrieved from plateforme Copernicus Open Access Hub. An extraction of Red and near infrared spectral bands, centered on the Phnom Penh city, was then carried out.\n\nplot(sentinel2a)\n\n\n\n\n\n\n\n\nTo lighten the code, we assign the two matrix layers in different SpatRaster objects.\n\nB04_Red <- sentinel2a[[1]] #spectral band Red\n\nB08_NIR <-sentinel2a[[2]] #spectral band near infrared\n\nFrom these two raster objects , we can calculate the normalized vegetation index:\n\\[{NDVI}=\\frac{\\mathrm{NIR} - \\mathrm{Red}} {\\mathrm{NIR} + \\mathrm{Red}}\\]\n\nraster_NDVI <- (B08_NIR - B04_Red ) / (B08_NIR + B04_Red )\n\nplot(raster_NDVI)\n\n\n\n\n\n\n\n\nThe higher the values (close to 1), the denser the vegetation.\n\n\n\n4.5.2 Focal operations\n\n\n\nSource : (Mennis 2015)\n\n\nFocal analysis conisders a cell plus its direct neighbors in contiguous and symmetrical (neighborhood operations). Most often, the value of the output cell is the result of a block of 3 x 3 (odd number) input cells.\nThe first step is to build a matrix that determines the block of cells that will be considered around each cell.\n\n# 5 x 5 matrix, where each cell has the same weight\nmon_focal <- matrix(1, nrow = 5, ncol = 5)\nmon_focal\n\n [,1] [,2] [,3] [,4] [,5]\n[1,] 1 1 1 1 1\n[2,] 1 1 1 1 1\n[3,] 1 1 1 1 1\n[4,] 1 1 1 1 1\n[5,] 1 1 1 1 1\n\n\nThe function focal() Then allows you to perform the desired analysis. For example: calculating the average of the values of all contiguous cells, for each cell in the raster.\n\nelevation_LowerGrid_mean <- focal(elevation_LowerGrid, \n w = mon_focal, \n fun = mean)\n\n\n\n\n\n\n\n\n\n\n\n4.5.2.1 Focal operations for elevation rasters\nThe function terrain() allows to perform focal analyzes specific to elevation rasters. Six operations are available:\n\nslope = calculation of the slope or degree of inclination of the surface;\n\naspect = calculate slope orientation;\n\nroughness = calculate of the variability or irregularity of the elevation;\n\nTPI = calculation of the index of topgraphic positions;\n\nTRI = elevation variability index calculation;\n\nflowdir = calculation of the water flow direction.\n\nExample with calculation of slopes(slope).\n\n#slope calculation\nslope <- terrain(elevation_utm, \"slope\", \n neighbors = 8, #8 (or 4) cells around taken into account\n unit = \"degrees\") #Output unit\n\nplot(slope) #Inclination of the slopes, in degrees\n\n\n\n\n\n\n\n\n\n\n\n4.5.3 Global operations\n\n\n\nSource : https://gisgeography.com/map-algebra-global-zonal-focal-local\n\n\nGlobal operation are used to summarize the matrix values of one or more matrices.\n\nglobal(elevation_utm, fun = \"mean\") #average values\n\n mean\nAltitude 80.01082\n\n\n\nglobal(elevation_utm, fun = \"sd\") #standard deviation\n\n sd\nAltitude 155.885\n\n\n\nfreq(lulc_2019_reclass) #frequency\n\n layer value count\n[1,] 1 1 47485325\n[2,] 1 2 13656289\n[3,] 1 3 14880961\n[4,] 1 4 37194979\n\ntable(lulc_2019_reclass[]) #contingency table\n\n\n 1 2 3 4 \n47485325 13656289 14880961 37194979 \n\n\nStatistical representations that summarize matrix information.\n\nhist(elevation_utm) #histogram\n\nWarning: [hist] a sample of3% of the cells was used\n\n\n\n\n\n\n\n\ndensity(elevation_utm) #density\n\n\n\n\n\n\n\n\n\n\n4.5.4 Zonal operation\n\n\n\nSource : (Mennis 2015)\n\n\nThe zonal operation make it possible to summarize the matrix values of certain zones (group of contiguous cells in space or in value).\n\n4.5.4.1 Zonal operation on an extraction\nAll global operations can be performed on an extraction of cells resulting from the functions crop(), mask(), segregate()…\nExample: average elevation for the city of Thma Bang district (cf partie 5.4.3).\n\n# Average value of the \"mask\" raster over Thma Bang district\nglobal(mask_thma_bang, fun = \"mean\", na.rm=TRUE)\n\n mean\nAltitude 584.7703\n\n\n\n\n4.5.4.2 Zonal operation from a vector layer\nThe function extract() allows you to extract and manipulate the values of cells that intersect vector data.\nExample from polygons:\n\n# Average elevation for each polygon (district)?\nelevation_by_dist <- extract(elevation_LowerGrid, district, fun=mean)\nhead(elevation_by_dist, 10)\n\n ID Altitude\n1 1 8.953352\n2 2 196.422240\n3 3 23.453937\n4 4 3.973118\n5 5 29.545801\n6 6 41.579593\n7 7 50.162749\n8 8 85.128777\n9 9 269.068091\n10 10 8.439041\n\n\n\n\n4.5.4.3 Zonal operation from raster\nZonal operation can be performed by area bounded by the categorical values of a second raster. For this, the two raster must have exaclty the same extent and the same resolution.\n\n#create a second raster with same resolution and extent as \"elevation_clip\"\nelevation_clip <- rast(\"data_cambodia/elevation_clip.tif\")\nelevation_clip_utm <- project(x = elevation_clip, y = \"EPSG:32648\", method = \"bilinear\")\nsecond_raster_CLC <- rast(elevation_clip_utm)\n\n#resampling of lulc_2019_reclass \nsecond_raster_CLC <- resample(lulc_2019_reclass, second_raster_CLC, method = \"near\") \n \n#added a variable name for the second raster\nnames(second_raster_CLC) <- \"lulc_2019_reclass_resample\"\n\n\n\n\n\n\n\n\n\n\nCalculation of the average elevation for the different areas of the second raster.\n\n#average elevation for each area of the \"second_raster\"\nzonal(elevation_clip_utm, second_raster_CLC , \"mean\", na.rm=TRUE)\n\n lulc_2019_reclass_resample elevation_clip\n1 1 12.83846\n2 2 8.31809\n3 3 11.41178\n4 4 11.93546" + }, + { + "objectID": "04-raster_data.html#transformation-et-conversion", + "href": "04-raster_data.html#transformation-et-conversion", + "title": "4 Work with Raster Data", + "section": "4.6 Transformation et conversion", + "text": "4.6 Transformation et conversion\n\n4.6.1 Rasterisation\nConvert polygons to raster format.\n\nchamkarmon = subset(district, district$ADM2_PCODE ==\"KH1201\") \nraster_district <- rasterize(x = chamkarmon, y = elevation_clip_utm)\n\n\nplot(raster_district)\n\n\n\n\n\n\n\n\nConvert points to raster format\n\n#rasterization of the centroids of the municipalities\nraster_dist_centroid <- rasterize(x = centroids(district), \n y = elevation_clip_utm, fun=sum)\nplot(raster_dist_centroid, col = \"red\")\nplot(district, add =TRUE)\n\n\n\n\nConvert lines in raster format\n\n#rasterization of municipal boundaries\nraster_dist_line <- rasterize(x = as.lines(district), y = elevation_clip_utm, fun=sum)\n\n\nplot(raster_dist_line)\n\n\n\n\n\n\n4.6.2 Vectorisation\nTransform a raster to vector polygons.\n\npolygon_elevation <- as.polygons(elevation_clip_utm)\n\n\nplot(polygon_elevation, y = 1, border=\"white\")\n\n\n\n\nTransform a raster to vector points.\n\npoints_elevation <- as.points(elevation_clip_utm)\n\n\nplot(points_elevation, y = 1, cex = 0.3)\n\n\n\n\nTransform a raster into vector lines.\n\nlines_elevation <- as.lines(elevation_clip_utm)\n\n\nplot(lines_elevation)\n\n\n\n\n\n\n4.6.3 terra, raster, sf, stars…\nReference packages for manipulating spatial data all rely o their own object class. It is sometimes necessary to convert these objects from one class to another class to take advance of all the features offered by these different packages.\nConversion functions for raster data:\n\n\n\nFROM/TO\nraster\nterra\nstars\n\n\n\n\nraster\n\nrast()\nst_as_stars()\n\n\nterra\nraster()\n\nst_as_stars()\n\n\nstars\nraster()\nas(x, ‘Raster’) + rast()\n\n\n\n\nConversion functions for vector data:\n\n\n\nFROM/TO\nsf\nsp\nterra\n\n\n\n\nsf\n\nas(x, ‘Spatial’)\nvect()\n\n\nsp\nst_as_sf()\n\nvect()\n\n\nterra\nst_as_sf()\nas(x, ‘Spatial’)\n\n\n\n\n\n\n\n\nHijmans, Robert J. 2022. “Terra: Spatial Data Analysis.†https://CRAN.R-project.org/package=terra.\n\n\nLi, Xingong. 2009. “Map Algebra and Beyond : 1. Map Algebra for Scalar Fields.†https://slideplayer.com/slide/5822638/.\n\n\nMadelin, Malika. 2021. “Analyse d’images Raster (Et Télédétection).†https://mmadelin.github.io/sigr2021/SIGR2021_raster_MM.html.\n\n\nMennis, Jeremy. 2015. “Fundamentals of GIS : Raster Operations.†https://cupdf.com/document/gus-0262-fundamentals-of-gis-lecture-presentation-7-raster-operations-jeremy.html.\n\n\nNowosad, Jakub. 2021. “Image Processing and All Things Raster.†https://nowosad.github.io/SIGR2021/workshop2/workshop2.html.\n\n\nRacine, Etienne B. 2016. “The Visual Raster Cheat Sheet.†https://rpubs.com/etiennebr/visualraster." } ] \ No newline at end of file diff --git a/references.bib b/references.bib index 5001dad8f7478b1f46ba8cb6e1e8339e3095d920..13f362da5fe52001eec8045b6a92093a30cdf3be 100644 --- a/references.bib +++ b/references.bib @@ -331,3 +331,43 @@ Library}, date = {2020}, url = {https://CRAN.R-project.org/package=cartogram} } + +@misc{MmadelinSIGR, + author = {Malika Madelin}, + title = {Analyse d’images raster (et télédétection)}, + howpublished = {\url{https://mmadelin.github.io/sigr2021/SIGR2021_raster_MM.html}}, + year = {2021}, + note = {Accessed: {2021–01-03}} +} + +@misc{JNowosadSIGR, + author = {Jakub Nowosad}, + title = {Image processing and all things raster}, + howpublished = {\url{https://nowosad.github.io/SIGR2021/workshop2/workshop2.html}}, + year = {2021}, + note = {Accessed: {2021–01-03}} +} + +@misc{RasterCheatSheet, + author = {Etienne B. Racine}, + title = {The Visual Raster Cheat Sheet}, + howpublished = {\url{https://rpubs.com/etiennebr/visualraster}}, + year = {2016}, + note = {Accessed: {2021–01-03}} +} + +@misc{XingongLi2009, + author = {Xingong Li}, + title = {Map algebra and beyond : 1. Map algebra for scalar fields}, + howpublished = {\url{https://slideplayer.com/slide/5822638/}}, + year = {2009}, + note = {Accessed: {2021–01-03}} +} + +@misc{JMennis2015, + author = {Jeremy Mennis}, + title = {Fundamentals of GIS : raster operations}, + howpublished = {\url{https://cupdf.com/document/gus-0262-fundamentals-of-gis-lecture-presentation-7-raster-operations-jeremy.html}}, + year = {2015}, + note = {Accessed: {2021–01-03}} +} \ No newline at end of file