#call the workspace you've been working in so far
load('E:/Documents/R Studio is here/R Studio files projects/gis_coding/.RData')
This part is simply the end parts of a long and tiresome journey where ‘trophies’ collected along the way are displayed on the wall. Our wall is the web, where all our exploits will be brazenly displayed to the world. Talk of hanging our clean linen in public, literally.
Let’s create a webmap with some of the classification rasters we have dealt with in the tutorial.
#call the necessary packages
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.1.3
library(raster)
## Warning: package 'raster' was built under R version 4.1.3
## Loading required package: sp
library(rgdal)
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
##
## rgdal: version: 1.5-28, (SVN revision 1158)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: C:/Users/User/Documents/R/win-library/4.1/rgdal/gdal
## GDAL binary built with GEOS: TRUE
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/User/Documents/R/win-library/4.1/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-6
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
## Overwritten PROJ_LIB was C:/Users/User/Documents/R/win-library/4.1/rgdal/proj
library(rgeos)
## rgeos version: 0.5-9, (SVN revision 684)
## GEOS runtime version: 3.9.1-CAPI-1.14.2
## Please note that rgeos will be retired by the end of 2023,
## plan transition to sf functions using GEOS at your earliest convenience.
## GEOS using OverlayNG
## Linking to sp version: 1.4-6
## Polygon checking: TRUE
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(sp)
library(htmltools)
#this is the package that creates the opacity sliders, it fades and unfades rasters and vectors
library(leaflet.opacity)
## Warning: package 'leaflet.opacity' was built under R version 4.1.3
#call the landcover raster layers. the years should provide a clue on their acquisition date
rclass2016 <- raster('r.classification2016.tif')
rclass2019 <- raster('r.classification2019.tif')
We will use the data called county_hs_assets used
elsewhere. (Errors cropped up simply trying to display the first 6
rows!!!)
head(county_hs_assets@data)
## ADM1_EN Shape_Leng Shape_Area ADM1_PCODE ADM1_REF ADM1ALT1EN ADM1ALT2EN
## 1 Bungoma 3.062486 0.2450581 KE039 <NA> <NA> <NA>
## 2 Kakamega 4.125304 0.2443814 KE037 <NA> <NA> <NA>
## 3 Kiambu 3.193175 0.2066285 KE022 <NA> <NA> <NA>
## 4 Kiambu 3.193175 0.2066285 KE022 <NA> <NA> <NA>
## 5 Nairobi 1.658061 0.0574956 KE047 <NA> <NA> <NA>
## 6 Nakuru 10.598716 1.2836187 KE032 <NA> <NA> <NA>
## ADM0_EN ADM0_PCODE date validOn validTo County...Sub.County
## 1 Kenya KE 2017/11/03 2019/10/31 <NA> BUNGOMA
## 2 Kenya KE 2017/11/03 2019/10/31 <NA> KAKAMEGA
## 3 Kenya KE 2017/11/03 2019/10/31 <NA> KIAMBU
## 4 Kenya KE 2017/11/03 2019/10/31 <NA> KIAMBU
## 5 Kenya KE 2017/11/03 2019/10/31 <NA> NAIROBI CITY
## 6 Kenya KE 2017/11/03 2019/10/31 <NA> NAKURU
## Conventional.Households Stand...alone.Radio Desk.Top.Computer..Laptop..Tablet
## 1 357,714 62.1 4.1
## 2 432,284 62.0 4.3
## 3 46,816 61.2 28.4
## 4 792,333 61.4 19.6
## 5 1,494,676 53.4 22.7
## 6 598,237 61.1 9.6
## Functional.Television Analogue.Television Internet Bicycle Motor.Cycle
## 1 25.4 3.6 7.2 26.2 11.9
## 2 29.2 4.3 8.6 24.4 11.5
## 3 73.8 7.0 49.7 19.2 5.0
## 4 70.0 6.3 39.1 16.3 6.9
## 5 68.7 7.5 42.1 12.5 4.3
## 6 52.5 5.7 20.9 19.1 10.9
## Refrigerator Car Truck..Lorry..Bus..Three.Wheeler.truck Tuk.Tuk names
## 1 2.8 3.1 0.6 0.4 bungoma
## 2 3.3 3.3 0.6 0.4 kakamega
## 3 32.2 21.1 2.3 0.5 kiambu
## 4 19.6 12.4 1.5 0.5 kiambu
## 5 23.5 12.9 1.2 0.4 nairobi city
## 6 8.9 7.1 1.0 0.4 nakuru
#the colors we will use to display the counties, dependend on no. of conventional households
county_color <- colorFactor(topo.colors(5), county_hs_assets@data$Conventional.Households)
#draw the webmap
leaflet() %>%
addTiles(group = 'OSM') %>%
addProviderTiles(providers$Esri.WorldImagery, group = 'ESRI world') %>%
addProviderTiles(providers$Stamen.Toner, group = 'Stamen') %>%
addPolygons(data = county_hs_assets, fill = T, color = ~county_color(county_hs_assets@data$Conventional.Households), popup = paste("County: ", county_hs_assets@data$ADM1_EN, "<br>", "Households: ", county_hs_assets@data$Conventional.Households),#hard to learn labelling and popups,
highlightOptions = highlightOptions(stroke = T, color = 'white', bringToFront = T), group = 'counties') %>%
addRasterImage(x= rclass2016,
colors = c('yellow', 'snow4', 'yellowgreen', 'darkgreen', 'gray1', 'darkblue'),
method = 'ngb',
group = 'landcover 2016', layerId = 'landcover2016') %>%
addRasterImage(x = rclass2019, colors = c('yellow', 'gray85', 'darkolivegreen3', 'darkgreen', 'dimgrey', 'darkblue'),
method = 'ngb',
group = 'landcover 2019', layerId = 'landcover2019') %>%
addLayersControl(baseGroups = c('ESRI world', 'Stamen', 'OSM'), overlayGroups = c('counties', 'landcover 2016', 'landcover 2019')) %>%
addLegend(data = rclass2016, position = 'bottomleft',
colors = c('yellow', 'snow4', 'yellowgreen', 'darkgreen', 'dimgrey', 'darkblue'), opacity = 0.9,
labels =c('Agriculture', 'Built-up', 'Bushland', 'Forest', 'Road', 'Water'), title = 'Landcover classes') %>% #legend can be tricky to master
addOpacitySlider(layerId = 'landcover2016') %>%
addOpacitySlider(layerId = 'landcover2019')
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
Surprisingly, no errors popped up, but some improvement on popups and labels is highly needed. Please check if the opacity sliders work on my behalf. Thanks for taking the ride with me, the ups and downs. It was a long journey indeed, but not boring to say the least.