Once again, let’s do the honours by loading our R workspace so as to avoid cases of R not recognizing our files.
#load the R workspace again
load('E:/Documents/R Studio is here/R Studio files projects/gis_coding/.RData')
So far, we have been looking at a map of landcover classes, the countiesland_datum2 dataset. Even after consulting StackOverflow, we did not manage to make the values appear distinct on the legend. Me thinks, there is a problem with the raster that ain’t figured out yet. No hard feelings anyway.
Let’s forge ahead. The table containing the description of the landcover values is in the pdf file of the extracted zipped folder of the landcover raster. To make matters easier, here it is.
landclasses_excel <- read.csv(file = 'landclasses_excel.csv', header = T, sep = ',')
landclasses_excel
## VALUE LABEL R G B
## 1 0 No data 0 0 0
## 2 1 Trees cover areas 0 160 0
## 3 2 Shrubs cover areas 150 100 0
## 4 3 Grassland 255 180 0
## 5 4 Cropland 255 255 100
## 6 5 Vegetation aquatic or regularly flooded 0 220 130
## 7 6 Lichen Mosses / Sparse vegetation 255 235 175
## 8 7 Bare areas 255 245 215
## 9 8 Built up areas 195 20 0
## 10 9 Snow and/or Ice 255 255 255
## 11 10 Open water 0 70 200
library(tmap)
Now lets view our map of landcovers one more time. It’s getting irritating, I know, but its good to have all the tools on the table before putting them to work.
tm_shape(countiesland_datum) + tm_raster(palette = terrain.colors(10, 0.7, rev = F), n= 10, legend.show = T, legend.is.portrait = T, colorNA = NULL) +
tm_shape(county_hs_assets) + tm_fill(col = NA, alpha = 0) + tm_polygons(col = 'MAP_COLORS', alpha = 0, border.col = 'black', lwd = 0.3, lty = 'solid') + tm_text('ADM1_EN', size = 'AREA', col = 'red', legend.size.show = F, legend.col.show = F) +
tm_layout(title = 'Landcover types of top 5 counties by population', legend.position = c('left', 'bottom'))
## stars object downsampled to 1082 by 924 cells. See tm_shape manual (argument raster.downsample)
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).
Having treecover areas (1) and shrub cover areas (2) blending together is understandable. But built up areas (8) to snow/ice (9)? Not really.
If you get a solution of making the legend values distinct, kindly inbox me.
Lets try to see the kind of landcovers existing within each county.
#load the functions witha tonguetwister name
library(exactextractr)
## Warning: package 'exactextractr' was built under R version 4.1.3
#get the landcover that covers the most area in each county
landcov_mode <- exact_extract(countiesland_datum2, county_hs_assets, 'mode', append_cols = 'names')
## Warning in .exact_extract(x, sf::st_as_sf(y), ...): Polygons transformed to
## raster CRS (EPSG:32737)
##
|
| | 0%
|
|============ | 17%
|
|======================= | 33%
|
|=================================== | 50%
|
|=============================================== | 67%
|
|========================================================== | 83%
|
|======================================================================| 100%
landcov_mode
## names mode
## 1 bungoma 4
## 2 kakamega 4
## 3 kiambu 4
## 4 kiambu 4
## 5 nairobi city 8
## 6 nakuru 4
For all counties except Nairobi, the major landcover type is category 4, otherwise known as ‘cropland’ in the landclasses_excel table. We can understand Nairobi being different by having category 8 - built up areas because it is nothing but a concrete and metal jungle. Even in the bad areas.
head(adm_subset)
## class : SpatialPolygonsDataFrame
## features : 5
## extent : 34.34341, 37.3626, -1.442148, 1.150185 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs
## variables : 12
## names : Shape_Leng, Shape_Area, ADM1_EN, ADM1_PCODE, ADM1_REF, ADM1ALT1EN, ADM1ALT2EN, ADM0_EN, ADM0_PCODE, date, validOn, validTo
## min values : 1.65806105822, 0.0574955973101, Bungoma, KE022, NA, NA, NA, Kenya, KE, 2017/11/03, 2019/10/31, NA
## max values : 10.5987160624, 1.28361868572, Nakuru, KE047, NA, NA, NA, Kenya, KE, 2017/11/03, 2019/10/31, NA
library(raster)
## Warning: package 'raster' was built under R version 4.1.3
## Loading required package: sp
landcov_mode2 <- extract(countiesland_datum2, adm_subset)
## Warning in .local(x, y, ...): Transforming SpatialPolygons to the crs of the
## Raster
OOps. I just ran into a messy problem when calling landcov_mode2. Incase you are curious, R returned a very long list of insensible values. Anyway, I will call it a day here and refer to you to the ‘Get Spatial with R’ web page created by Michael T. Hallworth. I intended to replicate the procedures under the ‘Moving Beyond the Basics’ chapter but customized to the Kenyan context. However, with problems like the one belwo, you may just as well learn it from the source himself.
Here is the webpage: https://mhallwor.github.io/_pages/activities_ExtractingRasterValues Here is the zipped file for the data used: https://github.com/mhallwor/mhallwor.github.io/blob/master/dataDownloads/Spatial_Layers.zip?raw=true
If you call landcov_mode2 you will be printed a long list of values, which if you do not know how to summarize them, it will be nothing but useless crap.