r-tmap / tmap

R package for thematic maps
https://r-tmap.github.io/tmap
GNU General Public License v3.0
850 stars 120 forks source link

Disable downsampling of basemap in facets? #895

Open SebKrantz opened 3 weeks ago

SebKrantz commented 3 weeks ago

I'm using tm_basemap("Esri.WorldGrayCanvas", zoom = 4) as a basemap. The resolution on large geographies is ok-isch. When using tm_facet_wrap(), the resolution of the basemap decreases, and tmap displays a message "SpatRaster object downsampled to 204 by 224 cells.". I understand that this may be a good idea for memory reasons, but in many cases it really makes the basemap unintelligible. It would be great if the resolution of the map could be increased in general, and if it could be kept for facets. Thanks!

mtennekes commented 3 weeks ago

The option raster.max.cells controls this. It is the upperbound for the total number of cells, so the total over all facets. If the number of cells is larger, then it is downsampled. This is intentionally to prevent system crashes and the reduce computation time significantly.

However, I agree that the maps should be as sharp as possible.

The current value is 1e6, which is definitely too small, also for a single map without facets. So 1e7 corresponds to roughly 3000 by 3000, and 1e8 10,000 by 10,000. What would be a good default value in your opinion, @SebKrantz (cc @Nowosad)?

You can play around with this value as follows:

tmap_options(raster.max.cells = 1e6)
tm_shape(World[169,]) +
    tm_borders(col = "red") +
    tm_basemap("Esri.WorldImagery", zoom = 4)
Nowosad commented 2 weeks ago

My general thinking is that raster.max.cells should relate to each panel separately (that would be my expectation as a user). Currently, when you have one panel, the map is fine; but when you have 10 panels, all of them do not look good -- their resolution is too low...

SebKrantz commented 2 weeks ago

Sorry for the late response. I Agree with @Nowosad. For facet plots increasing it makes a big difference. For single plots, using tm_basemap("Esri.WorldGrayCanvas", zoom = 4) in my application, I actually don't find a huge difference. The text is still not very sharp even if I set tmap_options(raster.max.cells = 1e8).

mtennekes commented 6 days ago

I see the argument, but I don't fully agree. Imho, there is a use-case balance between:

I see faceting as a powerful tool for exploration. However, if the running time for printing a basemap is say 3 seconds per map, the running time of say 10 facets is too much (let alone if there are many more facets), unless the aim is to produce print-ready HQ maps.

Therefore, setting raster.max.cells to each panel would ruin the possibility to explore spatial data via facets. On the other hand, I agree that it is easier to understand and probably more intuitive. However, ideally, the standard user should not have to bother about raster.max.cells. It should be set to a lowest resolution for which the perceived quality still (almost) as good as the original resolution. At least, for the standard user. For users who aim for HQ, it should not be downscaled at all.

The difficulty is that the sharpness not only depends on the (downsampled) resolution, but also on the screen and the source of the files. (E.g., is the text in your application still not sharp when setting tmap_options(raster.max.cells = Inf), @SebKrantz ? If not there may be an issue with the source files, or with the device).

Just an idea: we could add something like this:

tmap_prioritize("speed")      # downscaling to make sure the running time is below x seconds
tmap_prioritize("balance") # something in between
tmap_prioritize("quality") # no downscaling

What do you think?