Skip to content

Commit d8510ed

Browse files
committed
add link to data creation script
1 parent c275faf commit d8510ed

3 files changed

Lines changed: 58 additions & 11 deletions

File tree

README.Rmd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ tm_legend(show = FALSE)
194194

195195
For learning resources using these data see our [afrilearnr interactive tutorials](https://github.com/afrimapr/afrilearnr), resources in English & French for a [4 hour entry level tutorial](https://github.com/afrimapr/r-maps-tutorial-fr-eng) and the in-progress [afrimapr book](https://github.com/afrimapr/afrimapr-book).
196196

197+
## How were the datasets created ?
198+
199+
Here is the [code (somewhat untidy)](https://github.com/afrimapr/afrilearndata/blob/master/data-raw/afrilearndata-data-creation.R) to create the datasets in the package.
200+
197201
## Related
198202

199203
For other and larger spatial datasets see the [spData package](https://github.com/Nowosad/spData) which was part of the inspiration for afrilearndata.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ tutorials](https://github.com/afrimapr/afrilearnr), resources in English
192192
tutorial](https://github.com/afrimapr/r-maps-tutorial-fr-eng) and the
193193
in-progress [afrimapr book](https://github.com/afrimapr/afrimapr-book).
194194

195+
## How were the datasets created ?
196+
197+
Here is the [code (somewhat
198+
untidy)](https://github.com/afrimapr/afrilearndata/blob/master/data-raw/afrilearndata-data-creation.R)
199+
to create the datasets in the package.
200+
195201
## Related
196202

197203
For other and larger spatial datasets see the [spData

data-raw/afrilearndata-data-creation.R

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -484,13 +484,18 @@ tm_shape(afrilandcover) + tm_raster("landcover")
484484
#fails w or w/out quotes
485485
#tm_shape(afrilandcover) + tm_raster("landcover", palette="colour")
486486
tm_shape(afrilandcover) + tm_raster("landcover", palette=levels(afrilandcover)[[1]]$colour)
487+
#can't quite get it to display both the colours & the labels
488+
tm_shape(afrilandcover) + tm_raster("landcover", style="cat",palette=levels(afrilandcover)[[1]]$colour, labels=levels(afrilandcover)[[1]]$landcover)
489+
490+
# maybe I should try stars for afrilandcover instead ?
491+
# can I get a stars object to save the rastr attribute table with th colours ?
492+
# or terra ?
493+
# terra can save raster attribute tables incl. colour palette as indicated here :
494+
# https://geocompr.robinlovelace.net/attr.html
495+
496+
library(terra)
497+
afrilandcoverterra <- terra::rast(filename)
487498

488-
#now I see that tmap has a global landcover as a stars object
489-
#but doesn't have the colour palette saved in the object
490-
# data(land)
491-
# pal8 <- c("#33A02C", "#B2DF8A", "#FDBF6F", "#1F78B4", "#999999", "#E31A1C", "#E6E6E6", "#A6CEE3")
492-
# tm_shape(land, ylim = c(-88,88)) +
493-
# tm_raster("cover_cls", palette = pal8, title = "Global Land Cover")
494499

495500
# write file to package, as grd file to preserve raster attribute table
496501
filename <- r"(inst/extdata/afrilandcover.grd)" #windows safe paths
@@ -506,27 +511,59 @@ filename <- system.file("extdata","afrilandcover.grd", package="afrilearndata",
506511
testlc <- raster::raster(filename)
507512
mapview(testlc, att="landcover", col.regions=levels(testlc)[[1]]$colour)
508513

514+
515+
#now I see that tmap has a global landcover as a stars object
516+
#but doesn't have the colour palette saved in the object
517+
data(land)
518+
pal8 <- c("#33A02C", "#B2DF8A", "#FDBF6F", "#1F78B4", "#999999", "#E31A1C", "#E6E6E6", "#A6CEE3")
519+
tm_shape(land, ylim = c(-88,88)) +
520+
tm_raster("cover_cls", palette = pal8, title = "Global Land Cover")
521+
509522
# test reading in as a stars object
510523

511524
#proxy=FALSE ensures data are loaded but probably not necessary for this small grid
512525
afrilandstars <- stars::read_stars(filename, proxy=FALSE)
513526

527+
# it gives Warning message: ignoring unrecognized unit: class number
528+
# which is from here in stars :
529+
# https://github.com/r-spatial/stars/blob/ed1e36ff9ea317fd9d9c2fc9d360c06cb7258555/R/dimensions.R
530+
531+
#using RAT should get it to create a categorical stars object but not working ??
532+
afrilandstars <- stars::read_stars(filename, proxy=FALSE, RAT=rownames(igbp)[rat$ID+1])
533+
534+
#TODO can I get stars to store & plot the colours ?
535+
# nearly working but not quite ...
536+
537+
#seems from here that a colour attribute can be set
538+
#https://github.com/r-spatial/stars/issues/392#issuecomment-788812130
539+
#attr(r$f, "colors") = c("#b41614", "#3cfa96")
540+
afrilandstars$landcover <- rownames(igbp)[rat$ID+1]
541+
attr(afrilandstars$landcover, "colors") = igbp$IGBP[rat$ID+1]
542+
514543
#stars plots the categories well, but does not seem to retain the colours ?
515-
#and the labels get trun
544+
#and the labels get truncated
516545
plot(afrilandstars)
517546
#mapview likewise
518547
mapview(afrilandstars)
519548

520-
#TODO can I get stars to store & plot the colours ?
549+
#or just try converting the raster object
550+
afrilandstars2 <- st_as_stars(afrilandcover)
551+
552+
tm_shape(afrilandstars2) + tm_raster("landcover")
553+
554+
tm_shape(afrilandstars2) + tm_raster("landcover", style="cat",palette=levels(afrilandcover)[[1]]$colour, labels=levels(afrilandcover)[[1]]$landcover)
521555

522-
#seems from here that a colour attribute can be set
523-
#https://github.com/r-spatial/stars/issues/392#issuecomment-788812130
524-
attr(r$f, "colors") = c("#b41614", "#3cfa96")
525556

526557
#but how can it be read from the grd file ?
527558
#or maybe I should save as a tiff like above too ?
528559

529560

561+
# TODO I think stars is way to go - like in tmap
562+
# but just need to work out how to get it to create the categorical raster
563+
# how does tmap create land ?
564+
# https://github.com/r-tmap/tmap/blob/bab938b2bffb040638b635ffd99e7bce8143e7f3/build/create_land.R
565+
566+
530567

531568
# potential different approach to get the same data
532569
# https://rspatialdata.github.io/land_cover.html

0 commit comments

Comments
 (0)