mapview
mapview copied to clipboard
calling mapview on a stars object with factor values produces error
Calling mapview()
on a stars
object where the values are of class factor
results in an error. Coercing the object's values to numeric can be used to view it, but it would be nice to be able to view the factor levels on the map.
Reproducible example:
library(mapview)
library(stars)
lc <- read_stars(system.file('tif/lc.tif',package='stars'))
mapview(lc)
Resulting error:
Error in results[!nas] <- f(x[!nas]) : replacement has length zero
System info:
Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)
other attached packages:
[1] stars_0.5-3 sf_0.9-8 abind_1.4-5 mapview_2.9.9 qdrmapbox_0.1.0
Thanks again for all your work on this package!
Heads-up: The layer rendering is likely best implemented in leafem::addStarsImage. The legend creation needs to happen in {mapview}. I have started on the factor layers in {leafem}, but it will take a bit more time to handle them properly. One additional thing that came up is how to handle color tables that are included in the .tif files (as e.g. lc.tif has). I am guessing a logical switch will be the easiest, but this is open to discussion. @edzer how does stars handle these? Does the user have the ability to specify their own color mapping?
Given that I've just updated {leafem} in CRAN this will take a while to be available from CRAN.
More soon.
Hi again Tim, thanks for your work on this. I was getting the colors from the color table to display correctly until I tried to run the code on a server running R 4.1.0 and using the CRAN versions of all packages. In that case the default viridis colors are displayed.
Desired output that I get on R 4.0.3:
sessioninfo:
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.7 LTS
other attached packages:
[1] stars_0.5-3 sf_1.0-0 abind_1.4-5 mapview_2.10.0
Not desired output on R 4.1.0:
sessioninfo:
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 10 (buster)
other attached packages:
[1] stars_0.5-3 sf_1.0-0 abind_1.4-5 mapview_2.10.0
What's the leafem versions in the two settings?
Both 0.1.6 (CRAN version)
My guess is that the one that works as desired is the current github version (where I forgot to bump the version after the last commit - sorry bout that!). The CRAN version very likely does not have the desired functionality, so it's worth trying to install the current github version on the server and see if that works...
Yes, that does work, thanks!
Nice!
This may be a more involved issue for later, but I did notice that the color table is reflected in the actual display of the raster on the map but the legend still uses the viridis colors (this is using R 4.1.0, CRAN version of mapview and stars but GitHub version of leafem).
Yeah, as I mentioned earlier, the legend creation will need to be implemented in mapview. So far I've only implemented the layer representation using the colorable in leafem (GitHub). Mit sure when I will find the time to include proper Legend handlich for color tables in mapview. Currently busy with updating leafgl to work the latest js updates from the upstream repo...
Found some time at the pub ;-) This now looks much better:
One thing that is missing still is the black area in the legend. It is represented by a label of ""
, which is currently not handled.
Please check this with a layer where you know the color code mapping, so we can be sure the labels in the legend are mapped correctly. Using the default plot.stars
method only helps partly, as the labels in the legend are cut off (@edzer). Though things look ok, from what I can make out: