Commit dee78d58 authored by Facundo Muñoz's avatar Facundo Muñoz ®️
Browse files

Remove compiled vignettes from source

- they don't get installed as proper vignettes by install_github anyway

- better build the pkgdown website and browse online
parent fb2e6bb6
## ----setup, include = FALSE----------------------------------------------
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
## ----packages------------------------------------------------------------
require(raster)
require(rgdal)
if(!require(mapview, quietly = TRUE))
cat("We suggest installing the pacakge mapview for interactive visualisation",
"of cartography from within R")
## ----admin-borders-------------------------------------------------------
cmr_admin3 <- getData('GADM', country = "CMR", level=3)
# mapview(cmr_admin3, zcol = "NAME_3")
## ----water-bodies, eval = FALSE------------------------------------------
#
# ## This only works locally.
# prodel_path <- "/home/facu/CmisSync/Cirad/Sites/PRODEL/documentLibrary/carto"
# water_bodies <- readOGR(prodel_path, layer = "wb_cam.shp")
#
## ----national-parks, eval = FALSE----------------------------------------
# national_parks <- readOGR(
# file.path(prodel_path, "WDPA_Mar2018_CMR-shapefile"),
# "WDPA_Mar2018_CMR-shapefile-polygons"
# )
#
## ----production-systems, eval = FALSE------------------------------------
# # Not using this for the moment
# ps_cam <- raster("ps_cam.tif")
#
## ----animal-density, eval = FALSE----------------------------------------
# animal_density_world <- raster(file.path(prodel_path, "glw", "WdCt8k_vf_Mn_Rw_To.tif"))
# animal_density <- mask(crop(animal_density_world, extent(cmr_admin3)), cmr_admin3)
#
# # plot(animal_density)
# # summary(animal_density$WdCt8k_vf_Mn_Rw_To)
#
## ----save-package-carto, eval = FALSE------------------------------------
# cmr_dir <- "./inst/cartography/CMR"
# dir.create(cmr_dir, recursive = TRUE)
#
# writeOGR(cmr_admin3, file.path(cmr_dir, "cmr_admin3.gpkg"), layer = "cmr_admin3", driver = "GPKG")
# writeOGR(water_bodies, file.path(cmr_dir, "water_bodies.gpkg"), layer = "water_bodies", driver = "GPKG")
# writeOGR(national_parks, file.path(cmr_dir, "national_parks.gpkg"), layer = "national_parks", driver = "GPKG")
#
# writeRaster(animal_density, file.path(cmr_dir, "animal.density.tif"))
#
#
---
title: "Collection and pre-processing of cartographic information for Cameroon"
author: "Facundo Muñoz"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Collection and pre-processing of cartographic information for Cameroon}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
Here we demonstrate how we have downloaded and pre-processed the cartographic
information for Cameroon which is included in the package.
```{r packages}
require(raster)
require(rgdal)
if(!require(mapview, quietly = TRUE))
cat("We suggest installing the pacakge mapview for interactive visualisation",
"of cartography from within R")
```
## Administrative borders
Download cartography from the Global Administrative Borders Database (GADM, https://gadm.org/)
directly from within R.
```{r admin-borders}
cmr_admin3 <- getData('GADM', country = "CMR", level=3)
# mapview(cmr_admin3, zcol = "NAME_3")
```
```{r water-bodies, eval = FALSE}
## This only works locally.
prodel_path <- "/home/facu/CmisSync/Cirad/Sites/PRODEL/documentLibrary/carto"
water_bodies <- readOGR(prodel_path, layer = "wb_cam.shp")
```
```{r national-parks, eval = FALSE}
national_parks <- readOGR(
file.path(prodel_path, "WDPA_Mar2018_CMR-shapefile"),
"WDPA_Mar2018_CMR-shapefile-polygons"
)
```
```{r production-systems, eval = FALSE}
# Not using this for the moment
ps_cam <- raster("ps_cam.tif")
```
```{r animal-density, eval = FALSE}
animal_density_world <- raster(file.path(prodel_path, "glw", "WdCt8k_vf_Mn_Rw_To.tif"))
animal_density <- mask(crop(animal_density_world, extent(cmr_admin3)), cmr_admin3)
# plot(animal_density)
# summary(animal_density$WdCt8k_vf_Mn_Rw_To)
```
## Save pre-processed cartography for use within the package
Prefer standard and modern Open Geospatial Consortium ([OGC](http://www.opengeospatial.org/))
formats: GeoPackage for vector maps and GeoTiff for raster images.
```{r save-package-carto, eval = FALSE}
cmr_dir <- "./inst/cartography/CMR"
dir.create(cmr_dir, recursive = TRUE)
writeOGR(cmr_admin3, file.path(cmr_dir, "cmr_admin3.gpkg"), layer = "cmr_admin3", driver = "GPKG")
writeOGR(water_bodies, file.path(cmr_dir, "water_bodies.gpkg"), layer = "water_bodies", driver = "GPKG")
writeOGR(national_parks, file.path(cmr_dir, "national_parks.gpkg"), layer = "national_parks", driver = "GPKG")
writeRaster(animal_density, file.path(cmr_dir, "animal.density.tif"))
```
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Facundo Muñoz" />
<meta name="date" content="2019-03-20" />
<title>Collection and pre-processing of cartographic information for Cameroon</title>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css" data-origin="pandoc">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
for (var j = 0; j < rules.length; j++) {
var rule = rules[j];
// check if there is a div.sourceCode rule
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") continue;
var style = rule.style.cssText;
// check if color or background-color is set
if (rule.style.color === '' || rule.style.backgroundColor === '') continue;
// replace div.sourceCode by a pre.sourceCode rule
sheets[i].deleteRule(j);
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
}
}
})();
</script>
<link href="data:text/css;charset=utf-8,body%20%7B%0Abackground%2Dcolor%3A%20%23fff%3B%0Amargin%3A%201em%20auto%3B%0Amax%2Dwidth%3A%20700px%3B%0Aoverflow%3A%20visible%3B%0Apadding%2Dleft%3A%202em%3B%0Apadding%2Dright%3A%202em%3B%0Afont%2Dfamily%3A%20%22Open%20Sans%22%2C%20%22Helvetica%20Neue%22%2C%20Helvetica%2C%20Arial%2C%20sans%2Dserif%3B%0Afont%2Dsize%3A%2014px%3B%0Aline%2Dheight%3A%201%2E35%3B%0A%7D%0A%23header%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0A%23TOC%20%7B%0Aclear%3A%20both%3B%0Amargin%3A%200%200%2010px%2010px%3B%0Apadding%3A%204px%3B%0Awidth%3A%20400px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Aborder%2Dradius%3A%205px%3B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Afont%2Dsize%3A%2013px%3B%0Aline%2Dheight%3A%201%2E3%3B%0A%7D%0A%23TOC%20%2Etoctitle%20%7B%0Afont%2Dweight%3A%20bold%3B%0Afont%2Dsize%3A%2015px%3B%0Amargin%2Dleft%3A%205px%3B%0A%7D%0A%23TOC%20ul%20%7B%0Apadding%2Dleft%3A%2040px%3B%0Amargin%2Dleft%3A%20%2D1%2E5em%3B%0Amargin%2Dtop%3A%205px%3B%0Amargin%2Dbottom%3A%205px%3B%0A%7D%0A%23TOC%20ul%20ul%20%7B%0Amargin%2Dleft%3A%20%2D2em%3B%0A%7D%0A%23TOC%20li%20%7B%0Aline%2Dheight%3A%2016px%3B%0A%7D%0Atable%20%7B%0Amargin%3A%201em%20auto%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20%23DDDDDD%3B%0Aborder%2Dstyle%3A%20outset%3B%0Aborder%2Dcollapse%3A%20collapse%3B%0A%7D%0Atable%20th%20%7B%0Aborder%2Dwidth%3A%202px%3B%0Apadding%3A%205px%3B%0Aborder%2Dstyle%3A%20inset%3B%0A%7D%0Atable%20td%20%7B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dstyle%3A%20inset%3B%0Aline%2Dheight%3A%2018px%3B%0Apadding%3A%205px%205px%3B%0A%7D%0Atable%2C%20table%20th%2C%20table%20td%20%7B%0Aborder%2Dleft%2Dstyle%3A%20none%3B%0Aborder%2Dright%2Dstyle%3A%20none%3B%0A%7D%0Atable%20thead%2C%20table%20tr%2Eeven%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Ap%20%7B%0Amargin%3A%200%2E5em%200%3B%0A%7D%0Ablockquote%20%7B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Apadding%3A%200%2E25em%200%2E75em%3B%0A%7D%0Ahr%20%7B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%3A%20none%3B%0Aborder%2Dtop%3A%201px%20solid%20%23777%3B%0Amargin%3A%2028px%200%3B%0A%7D%0Adl%20%7B%0Amargin%2Dleft%3A%200%3B%0A%7D%0Adl%20dd%20%7B%0Amargin%2Dbottom%3A%2013px%3B%0Amargin%2Dleft%3A%2013px%3B%0A%7D%0Adl%20dt%20%7B%0Afont%2Dweight%3A%20bold%3B%0A%7D%0Aul%20%7B%0Amargin%2Dtop%3A%200%3B%0A%7D%0Aul%20li%20%7B%0Alist%2Dstyle%3A%20circle%20outside%3B%0A%7D%0Aul%20ul%20%7B%0Amargin%2Dbottom%3A%200%3B%0A%7D%0Apre%2C%20code%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0Aborder%2Dradius%3A%203px%3B%0Acolor%3A%20%23333%3B%0Awhite%2Dspace%3A%20pre%2Dwrap%3B%20%0A%7D%0Apre%20%7B%0Aborder%2Dradius%3A%203px%3B%0Amargin%3A%205px%200px%2010px%200px%3B%0Apadding%3A%2010px%3B%0A%7D%0Apre%3Anot%28%5Bclass%5D%29%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Acode%20%7B%0Afont%2Dfamily%3A%20Consolas%2C%20Monaco%2C%20%27Courier%20New%27%2C%20monospace%3B%0Afont%2Dsize%3A%2085%25%3B%0A%7D%0Ap%20%3E%20code%2C%20li%20%3E%20code%20%7B%0Apadding%3A%202px%200px%3B%0A%7D%0Adiv%2Efigure%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0Aimg%20%7B%0Abackground%2Dcolor%3A%20%23FFFFFF%3B%0Apadding%3A%202px%3B%0Aborder%3A%201px%20solid%20%23DDDDDD%3B%0Aborder%2Dradius%3A%203px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Amargin%3A%200%205px%3B%0A%7D%0Ah1%20%7B%0Amargin%2Dtop%3A%200%3B%0Afont%2Dsize%3A%2035px%3B%0Aline%2Dheight%3A%2040px%3B%0A%7D%0Ah2%20%7B%0Aborder%2Dbottom%3A%204px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Apadding%2Dbottom%3A%202px%3B%0Afont%2Dsize%3A%20145%25%3B%0A%7D%0Ah3%20%7B%0Aborder%2Dbottom%3A%202px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Afont%2Dsize%3A%20120%25%3B%0A%7D%0Ah4%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23f7f7f7%3B%0Amargin%2Dleft%3A%208px%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Ah5%2C%20h6%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23ccc%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Aa%20%7B%0Acolor%3A%20%230033dd%3B%0Atext%2Ddecoration%3A%20none%3B%0A%7D%0Aa%3Ahover%20%7B%0Acolor%3A%20%236666ff%3B%20%7D%0Aa%3Avisited%20%7B%0Acolor%3A%20%23800080%3B%20%7D%0Aa%3Avisited%3Ahover%20%7B%0Acolor%3A%20%23BB00BB%3B%20%7D%0Aa%5Bhref%5E%3D%22http%3A%22%5D%20%7B%0Atext%2Ddecoration%3A%20underline%3B%20%7D%0Aa%5Bhref%5E%3D%22https%3A%22%5D%20%7B%0Atext%2Ddecoration%3A%20underline%3B%20%7D%0A%0Acode%20%3E%20span%2Ekw%20%7B%20color%3A%20%23555%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%0Acode%20%3E%20span%2Edt%20%7B%20color%3A%20%23902000%3B%20%7D%20%0Acode%20%3E%20span%2Edv%20%7B%20color%3A%20%2340a070%3B%20%7D%20%0Acode%20%3E%20span%2Ebn%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Efl%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Ech%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Est%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Eco%20%7B%20color%3A%20%23888888%3B%20font%2Dstyle%3A%20italic%3B%20%7D%20%0Acode%20%3E%20span%2Eot%20%7B%20color%3A%20%23007020%3B%20%7D%20%0Acode%20%3E%20span%2Eal%20%7B%20color%3A%20%23ff0000%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%0Acode%20%3E%20span%2Efu%20%7B%20color%3A%20%23900%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%20code%20%3E%20span%2Eer%20%7B%20color%3A%20%23a61717%3B%20background%2Dcolor%3A%20%23e3d2d2%3B%20%7D%20%0A" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 class="title toc-ignore">Collection and pre-processing of cartographic information for Cameroon</h1>
<h4 class="author">Facundo Muñoz</h4>
<h4 class="date">2019-03-20</h4>
<p>Here we demonstrate how we have downloaded and pre-processed the cartographic information for Cameroon which is included in the package.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">require</span>(raster)
<span class="co">#&gt; Loading required package: raster</span>
<span class="co">#&gt; Loading required package: sp</span>
<span class="kw">require</span>(rgdal)
<span class="co">#&gt; Loading required package: rgdal</span>
<span class="co">#&gt; rgdal: version: 1.4-3, (SVN revision 828)</span>
<span class="co">#&gt; Geospatial Data Abstraction Library extensions to R successfully loaded</span>
<span class="co">#&gt; Loaded GDAL runtime: GDAL 2.1.3, released 2017/20/01</span>
<span class="co">#&gt; Path to GDAL shared files: /usr/share/gdal/2.1</span>
<span class="co">#&gt; GDAL binary built with GEOS: TRUE </span>
<span class="co">#&gt; Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]</span>
<span class="co">#&gt; Path to PROJ.4 shared files: (autodetected)</span>
<span class="co">#&gt; Linking to sp version: 1.3-1</span>
<span class="cf">if</span>(<span class="op">!</span><span class="kw">require</span>(mapview, <span class="dt">quietly =</span> <span class="ot">TRUE</span>))
<span class="kw">cat</span>(<span class="st">&quot;We suggest installing the pacakge mapview for interactive visualisation&quot;</span>,
<span class="st">&quot;of cartography from within R&quot;</span>)</code></pre></div>
<div id="administrative-borders" class="section level2">
<h2>Administrative borders</h2>
<p>Download cartography from the Global Administrative Borders Database (GADM, <a href="https://gadm.org/" class="uri">https://gadm.org/</a>) directly from within R.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">
cmr_admin3 &lt;-<span class="st"> </span><span class="kw">getData</span>(<span class="st">'GADM'</span>, <span class="dt">country =</span> <span class="st">&quot;CMR&quot;</span>, <span class="dt">level=</span><span class="dv">3</span>)
<span class="co"># mapview(cmr_admin3, zcol = &quot;NAME_3&quot;)</span></code></pre></div>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">
## This only works locally.
prodel_path &lt;-<span class="st"> &quot;/home/facu/CmisSync/Cirad/Sites/PRODEL/documentLibrary/carto&quot;</span>
water_bodies &lt;-<span class="st"> </span><span class="kw">readOGR</span>(prodel_path, <span class="dt">layer =</span> <span class="st">&quot;wb_cam.shp&quot;</span>)</code></pre></div>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">national_parks &lt;-<span class="st"> </span><span class="kw">readOGR</span>(
<span class="kw">file.path</span>(prodel_path, <span class="st">&quot;WDPA_Mar2018_CMR-shapefile&quot;</span>),
<span class="st">&quot;WDPA_Mar2018_CMR-shapefile-polygons&quot;</span>
)</code></pre></div>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Not using this for the moment</span>
ps_cam &lt;-<span class="st"> </span><span class="kw">raster</span>(<span class="st">&quot;ps_cam.tif&quot;</span>)</code></pre></div>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">animal_density_world &lt;-<span class="st"> </span><span class="kw">raster</span>(<span class="kw">file.path</span>(prodel_path, <span class="st">&quot;glw&quot;</span>, <span class="st">&quot;WdCt8k_vf_Mn_Rw_To.tif&quot;</span>))
animal_density &lt;-<span class="st"> </span><span class="kw">mask</span>(<span class="kw">crop</span>(animal_density_world, <span class="kw">extent</span>(cmr_admin3)), cmr_admin3)
<span class="co"># plot(animal_density)</span>
<span class="co"># summary(animal_density$WdCt8k_vf_Mn_Rw_To)</span></code></pre></div>
</div>
<div id="save-pre-processed-cartography-for-use-within-the-package" class="section level2">
<h2>Save pre-processed cartography for use within the package</h2>
<p>Prefer standard and modern Open Geospatial Consortium (<a href="http://www.opengeospatial.org/">OGC</a>) formats: GeoPackage for vector maps and GeoTiff for raster images.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">cmr_dir &lt;-<span class="st"> &quot;./inst/cartography/CMR&quot;</span>
<span class="kw">dir.create</span>(cmr_dir, <span class="dt">recursive =</span> <span class="ot">TRUE</span>)
<span class="kw">writeOGR</span>(cmr_admin3, <span class="kw">file.path</span>(cmr_dir, <span class="st">&quot;cmr_admin3.gpkg&quot;</span>), <span class="dt">layer =</span> <span class="st">&quot;cmr_admin3&quot;</span>, <span class="dt">driver =</span> <span class="st">&quot;GPKG&quot;</span>)
<span class="kw">writeOGR</span>(water_bodies, <span class="kw">file.path</span>(cmr_dir, <span class="st">&quot;water_bodies.gpkg&quot;</span>), <span class="dt">layer =</span> <span class="st">&quot;water_bodies&quot;</span>, <span class="dt">driver =</span> <span class="st">&quot;GPKG&quot;</span>)
<span class="kw">writeOGR</span>(national_parks, <span class="kw">file.path</span>(cmr_dir, <span class="st">&quot;national_parks.gpkg&quot;</span>), <span class="dt">layer =</span> <span class="st">&quot;national_parks&quot;</span>, <span class="dt">driver =</span> <span class="st">&quot;GPKG&quot;</span>)
<span class="kw">writeRaster</span>(animal_density, <span class="kw">file.path</span>(cmr_dir, <span class="st">&quot;animal.density.tif&quot;</span>))</code></pre></div>
</div>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>
## ----packages------------------------------------------------------------
require(sp)
require(raster)
require(rasterVis)
require(mapMCDA)
## ----facteurs-risque-----------------------------------------------------
cmr <- mapMCDA_datasets()
# layers <- list.files(
# system.file("cartography/CMR", package = "mapMCDA"),
# full.names = TRUE
# )
# cmr <- lapply(layers, load_layer)
# names(cmr) <- rmext(basename(layers)))
## ----unite-epidemiologique, fig.cap = "Unités épidémiologiques d'exemple pour le Cameroun."----
unites_epi <- cmr$cmr_admin3
par(mar = c(0, 0, 0, 0))
plot(unites_epi)
## ----mise-echelle, fig.width=4, fig.cap = "Mis en échelle directe ou inverse."----
plot(
data.frame(x = c(0, 100), y = c(0, 100)),
type = 'l',
xaxs = "i",
yaxs = "i",
xaxt = "n",
lab = c(1, 1, 7),
xlab = c("Échelle originale"),
ylab = c("Échelle de risque")
)
abline(100, -1)
## ----risk-layers---------------------------------------------------------
risques <- list(
dens_animale = risk_layer(
cmr$animal.density,
boundaries = unites_epi
# , scale_target = c(0, 100) # échelle directe par défault
),
points_eau = risk_layer(
cmr$water_bodies,
boundaries = unites_epi,
scale_target = c(100, 0) # échelle renversée
),
parcs = risk_layer(
cmr$national_parks,
boundaries = unites_epi,
scale_target = c(100, 0) # échelle renversée
)
)
## ----align-layers, fig.width = 6, fig.cap = "Niveaux de risque associé à chaque facteur."----
risques_alignes <- align_layers(risques)
levelplot(stack(risques_alignes))
## ----matrice-relations, echo = -1----------------------------------------
M <- matrix(c(
1, 6, 4,
1/6, 1, 3,
1/4, 1/3, 1
), byrow = TRUE, 3, 3)
colnames(M) <- rownames(M) <- names(risques)
knitr::kable(M, digits = 2)
## ----compute-weights, fig.width=4, echo = 1, fig.cap = "Pondération des facteurs de risque."----
w <- compute_weights(M)
mapMCDA:::plot_weights(w, rownames(M))
## ----wlc, fig.width = 6, fig.height = 6, fig.cap = "Carte de risque combiné."----
risque_combine <- wlc(risques, w)
levelplot(risque_combine)
## ----risk-plot, fig.width = 6, fig.height = 6, fig.cap = "Carte de niveaux de risque par unité épidémiologique."----
risk_plot(unites_epi, risk_unit(risque_combine, unites_epi), n = 5)
---
title: "mapMCDA: aperçu du package"
date: "`r Sys.Date()`"
output:
rmarkdown::html_vignette:
toc: true
toc_depth: 2
vignette: >
%\VignetteIndexEntry{Overview}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r packages}
require(sp)
require(raster)
require(rasterVis)
require(mapMCDA)
```
# Introduction
Le package `mapMCDA` facilite la ponderation de plusieurs facteurs de risque
pour produîre une carte de risque épidémiologique.
Néanmoins, l'expertise de l'utilisateur est cruciale. Elle s'exprime à trois
niveaux :
1. Choix des facteurs de risque rélevants
2. Pour chaque facteur, mise en échelle commune de risque (e.g. entre 0 et 100)
3. Évaluation deux-à-deux de l'importance rélative des facteurs de risque
À continuation, on utilise des cartes fournis par le package pour produîre une
carte de risque d'exemple.
__Cette carte n'a aucun valeur épidémiologique.__
# 1. Facteurs de risque
La fonction `mapMCDA_datasets()` permet de charger en mémoire toutes les cartes
disponibles dans le package.
L'objet `cmr` est une simple liste de cartes : objets de type `Spatial*` pour
les cartes vectoriels et de type `RasterLayer` pour les cartes raster.
Pour utiliser d'autre cartographie, utiliser les fonctions `rgdal::readOGR()` et
`raster::raster()` respectivement pour des cartes vectoriels ou raster.
On peut aussi bénéficier de la fonction `layer_type()` pour charger tout type
des cartes automatiquement, comme démontré dans le code commenté.
```{r facteurs-risque}
cmr <- mapMCDA_datasets()
# layers <- list.files(
# system.file("cartography/CMR", package = "mapMCDA"),
# full.names = TRUE
# )
# cmr <- lapply(layers, load_layer)
# names(cmr) <- rmext(basename(layers)))
```
L'une de ces cartes est celle des unités épidémiologiques, utilisée pour établir
le cadre de travail.
```{r unite-epidemiologique, fig.cap = "Unités épidémiologiques d'exemple pour le Cameroun."}
unites_epi <- cmr$cmr_admin3
par(mar = c(0, 0, 0, 0))
plot(unites_epi)
```
# 2. Mise en échelle
Chaque facteur de risque varie dans une échelle qui lui est propre.
La densité animale, par exemple, varie entre 0 et presque 5500 têtes par $km^2$.
Pour les cartes _vectorielles_, qui representent la localisation des entités
spatiales telles que lacs ou forêts, on considère la __distance__ à dites
entités.
Ce package utilise pour l'instant une fonction linèaire pour la mise en échelle.
Cependant, la rélation peut être directe ou inverse.
```{r mise-echelle, fig.width=4, fig.cap = "Mis en échelle directe ou inverse."}
plot(
data.frame(x = c(0, 100), y = c(0, 100)),
type = 'l',
xaxs = "i",
yaxs = "i",
xaxt = "n",
lab = c(1, 1, 7),
xlab = c("Échelle originale"),
ylab = c("Échelle de risque")
)
abline(100, -1)
```
La fonction `risk_layer()` calcule la carte de risque associé à un facteur
concret dans une échelle donnée. Par défault c'est entre 0 et 100. Pour
inverser la rélation il suffit de passer les limits dans l'ordre inverse, e.g.
`c(100, 0)`.
D'ailleurs, elle utilise la carte d'unités épidémiologiques pour établir
les limits de calcul de risque.
```{r risk-layers}
risques <- list(
dens_animale = risk_layer(
cmr$animal.density,
boundaries = unites_epi
# , scale_target = c(0, 100) # échelle directe par défault
),
points_eau = risk_layer(
cmr$water_bodies,
boundaries = unites_epi,
scale_target = c(100, 0) # échelle renversée
),
parcs = risk_layer(
cmr$national_parks,
boundaries = unites_epi,
scale_target = c(100, 0) # échelle renversée
)
)
```
On voudrait examiner les cartes de risque ainsi calculées.
Mais pour cela, il faut qu'elles soient _alignées_.
C'est à dire, qu'elles aient les mêmes _extents_, _résolutions_ et _projections_.
On peut se servir de la fonction `align_layers()` qui arrange tout ça pour nous.
Noter que ce pas n'est pas nécessaire pour continuer, car cette fonction est
automatiquement executée si besoin.
```{r align-layers, fig.width = 6, fig.cap = "Niveaux de risque associé à chaque facteur."}
risques_alignes <- align_layers(risques)
levelplot(stack(risques_alignes))
```
# 3. Pondération des facteurs de risque
Il y a 3 facteurs à considérer.
Il faut comparer 2-à-2 leurs importances relatives en une échelle de 1 à 9 et
representer ces rélations en une matrice qui doit avoir des 1 dans son diagonale.
Noter que les élements symmetriques doivent être réciproques.
```{r matrice-relations, echo = -1}
M <- matrix(c(
1, 6, 4,
1/6, 1, 3,
1/4, 1/3, 1
), byrow = TRUE, 3, 3)
colnames(M) <- rownames(M) <- names(risques)
knitr::kable(M, digits = 2)
```
Dans cette exemple, on considère que la densité animale est 6 fois plus importante
que la distance aux points d'eau, et 4 fois plus importante que la distance aux
parcs.
Au même temps, que la distance aux points d'eau est 3 fois plus importante que
celle aux parcs.
Le système calcule les coefficients de pondération les plus consistents avec
ces valorations par paires, avec la fonction `compute_weights()`.
```{r compute-weights, fig.width=4, echo = 1, fig.cap = "Pondération des facteurs de risque."}
w <- compute_weights(M)
mapMCDA:::plot_weights(w, rownames(M))
```
# 4. Calcul de la carte de risque
La fonction `wlc()` (pour weighted linear combination) combine tous les facteurs
de risque en utilisant les poids calculés précedament, et produise une carte
de risque qui couvre toute la région.
```{r wlc, fig.width = 6, fig.height = 6, fig.cap = "Carte de risque combiné."}
risque_combine <- wlc(risques, w)
levelplot(risque_combine)
```
Enfin, on peut produîre una carte par unité épidémiologique et dans une échelle
de quelques niveaux de risque.
```{r risk-plot, fig.width = 6, fig.height = 6, fig.cap = "Carte de niveaux de risque par unité épidémiologique."}
risk_plot(unites_epi, risk_unit(risque_combine, unites_epi), n = 5)
```
This source diff could not be displayed because it is too large. You can view the blob instead.