mapview icon indicating copy to clipboard operation
mapview copied to clipboard

calling mapview on a stars object with factor values produces error

Open qdread opened this issue 3 years ago • 10 comments

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!

qdread avatar May 28 '21 14:05 qdread

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.

tim-salabim avatar May 31 '21 06:05 tim-salabim

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:

image

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:

image

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

qdread avatar Jun 25 '21 12:06 qdread

What's the leafem versions in the two settings?

tim-salabim avatar Jun 25 '21 13:06 tim-salabim

Both 0.1.6 (CRAN version)

qdread avatar Jun 25 '21 13:06 qdread

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...

tim-salabim avatar Jun 25 '21 14:06 tim-salabim

Yes, that does work, thanks!

qdread avatar Jun 25 '21 15:06 qdread

Nice!

tim-salabim avatar Jun 25 '21 15:06 tim-salabim

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).

image

qdread avatar Jun 25 '21 16:06 qdread

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...

tim-salabim avatar Jun 25 '21 16:06 tim-salabim

Found some time at the pub ;-) This now looks much better:

image

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:

image

tim-salabim avatar Jun 25 '21 17:06 tim-salabim