geocompr
geocompr copied to clipboard
Ch 9 edits/comments
From an outsider perspective unfamiliar with tmap, the interactive mapping capabilities in R, or webapp dev with shiny, this chapter is great. The sections on shiny and the interactive mode in tmap are very helpful to suggest new ways to visualize and communicate a lot of research I'm doing.
Going through I had a couple of code issues/noticed a couple of things. Some are related to the use of raster instead of terra. Checking through the version history I hadn't seen major updates on this chapter yet tagged for the 2nd Ed. I am submitting an associated PR to fix some of these, but haven't done a thorough review of where raster is being explicitly called instead of terra so perhaps some of these may just be noted for a future chapter rewrite, and not necessarily accepted now.
- Should use terra instead of raster explicitly in a few locations (library statements, plotting in 9.6, when importing nz_elev, and nlcd data in examples) but this will cause downstream issues (that may necessitate some chapter edits so not sure the book should be rebuilt using this).
-
mapview doesn't support terra objects so changing to terra will cause issues. See code snippet below. Something came up in Chapter 8 when using
mapshot()
at the end of the chapter as well but didn't think much of it as I had just installed mapview and needed to add things to the windows PATH etc.
> nlcd = rast(system.file("raster/nlcd.tif", package = "spDataLarge"))
> mapview::mapview(nlcd)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘mapView’ for signature ‘"SpatRaster"’
- Not very familiar with mapview but got the following issue on Fig 9.21 in section 9.4. It seems to arise from
burst = TRUE
. Perhaps deprecated behavior?
> trails |>
+ st_transform(st_crs(franconia)) |>
+ st_intersection(franconia[franconia$district == "Oberfranken", ]) |>
+ st_collection_extract("LINE") |>
+ mapview( color = "red", lwd = 3, layer.name = "trails") +
+ mapview(franconia, zcol = "district", burst = TRUE) +
+ breweries
Error in if (unique(sapply(x, nrow)) == length(popup)) { :
the condition has length > 1
In addition: Warning message:
attribute variables are assumed to be spatially constant throughout all geometries
> trails |>
+ st_transform(st_crs(franconia)) |>
+ st_intersection(franconia[franconia$district == "Oberfranken", ]) |>
+ st_collection_extract("LINE") |>
+ mapview( color = "red", lwd = 3, layer.name = "trails") +
+ mapview(franconia, zcol = "district", burst = F) +
+ breweries
Warning message:
attribute variables are assumed to be spatially constant throughout all geometries
- Last paragraph before 9.7 seems to be written in present tense from 2018 as mapdeck is referred to here as a released package, but was already described above, while the other packages haven't been described at all. Someone more familiar with the geospatial ecosystem could extend the paragraph to say "in 2018..." "then in 2022..." or just rephrase it to be contemporary to the 2nd ed.
- I couldn't find the solutions for this chapter so putting this here: the
africa
data throws a topology error during aggregation. Addingst_make_valid()
call during its creation fixes this, although maybe a fix that should be for spData?
> african_regions = aggregate(africa['name'], by = africa$subregion,
+ FUN = sum)
Error in CPL_geos_union(st_geometry(x), by_feature, is_coverage) :
Evaluation error: TopologyException: side location conflict at 933409.4797448538 1114516.2485827128.
- Lines 970 I'm not sure how ggplot2 handles terra
- The default plotting options for tmap on my laptop (1920 x 1080 display, RStudio 2022.02.3 Build 492, Windows 11 Version 10.0.22000 Build 22000) is leading to legend display issues and the compass is very oversized. The code seems optimized for use in markdown. Here's 2 example of Fig 9.8 rendered in the RStudio viewer rather than in RMarkdown or for beginnings of a solution to Ex. 4. It is making completing the rest of the exercises with tmaps default plotting mode unideal to work with in the base viewer. @Nowosad perhaps you have some insight on this? From my experience with R plots, plotting parameters optimized for writing figures aren't visually appealing in the inline generator but are great when writing the file (in base R and plotly).
> map_nz +
+ tm_compass(type = "8star", position = c("left", "top")) +
+ tm_scale_bar(breaks = c(0, 100, 200), text.size = 1)
> nlcd_zion = crop(nlcd, vect(zion)) |>
+ mask(vect(zion))
> zi_map0 = tm_shape(nlcd_zion) +
+ tm_raster(title = "Land Cover Type")
> zi_map = zi_map0 +
+ tm_shape(zion) +
+ tm_borders(lwd = 3, col = 'black')
> zi_map +
+ tm_layout(main.title = "Zion National Park") +
+ tm_scale_bar(breaks = c(0, 10, 20), position = c("RIGHT", "BOTTOM"))
stars object downsampled to 882 by 1133 cells. See tm_shape manual (argument raster.downsample)
Some legend labels were too wide. These labels have been resized to 0.22, 0.13, 0.20, 0.21, 0.13, 0.12, 0.14, 0.15. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.
filename = "C9_E4_zion.png"
png(file = filename, width = 400, height = 600, pointsize = 18)
par(mar = c(5.1, 5.1, 4.1, 2.1), mfrow = c(1, 1))
zi_map +
tm_layout(main.title = "Zion National Park") +
tm_scale_bar(breaks = c(0, 10, 20), position = c("RIGHT", "BOTTOM"))
dev.off()
Hi @Lvulis many thanks for this detailed issue, a strong and up-to-date review of Chapter 9.
First-up I'm tagging @mtennekes who may find some of your feedback on tmap useful.
Second, @Nowosad is working on a branch that contains updated code ready for tmap v. 4: https://github.com/robinlovelace/geocompr/tree/tmapv4
Not sure how much that changes things, if at all. Grateful for all the feedback and will check out the PR next...
Looking through that branch I think a lot of this already addressed! Would also add that I would contest it is comprehensive, see many edits from @Nowosad that incorporate this and many other changes :)
Hi @Lvulis -- the 9th chapter is waiting for the new developments in the {tmap} package. I will look at your comments for this chapter in the next several months...
Hi @Lvulis,
Thanks a lot!
I've been updating the mapping chapter for the last few weeks. You can see my current progress at https://github.com/geocompx/geocompr/pull/953.
In short:
- Most importantly -- we are switching to tmap version 4 (it is still a work in progress, but should be ready for the general audience soon)
- We moved the content from raster to terra
- mapview supports terra objects now.
- I removed the use of burst in mapview (however, it should now be fixed in the dev version)
- I added an example of how to use terra objects in ggplot2
- I was unable to reproduce this issue:
african_regions = aggregate(africa['name'], by = africa$subregion, FUN = sum)