Raymond M Hunter
Raymond M Hunter

Reputation: 1

Plotting large raster in leaflet Shiny crashes app

I am creating a shiny app called Forest Finder that allows users to explore forests (rasters) in CA by county (polygons). My app keeps crashing when hosted on the web although it works seamlessly on my local machine. There are no errors in the app logs after crashing. I have a decent amount of toggles in the UI that filter and reclassify my rasters and put a load on the server, but I think that the main issue is my rasters are too large to be plotted in leaflet on the server.

I already resampled from 30mx30m to 60mx60m, and I don't want to go any bigger as I lose a lot of resolution. Small rasters plot fine on the app and larger ones (San Bernardino, Los Angeles, Fresno, Santa Barbara) make it crash. I tested out resampling to 240mx240m and publishing and it worked well without crashing. I just cant seem to resolve this issue without compromising resolution.

How might I be able to approach this? Specifically, I have tried looking into creating tiles or trying to aggregate the raster (or have it not render) at a zoomed out scale and populate as you zoom in. However, I have been having trouble finding solutions to this. Are there any other work around with leaflet and large rasters? Most of my large rasters that are crashing the app are filled with NA values.

Shiny App

GitHub Repo

Upvotes: 0

Views: 61

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47481

Perhaps you break terra's memory-safe design by forcing all values into RAM with the terra::values method here (and perhaps elsewhere):

legend[legend$value %in% unique(values(cnty_rast())), ] |>

Omitting values might fix the problem for you, as in:

legend[legend$value %in% unique(cnty_rast()), ] |>

(precomputing these values for each county could improve performance.)

Perhaps you have similar code elsewhere that needs to be fixed.

The actual plotting of large rasters should not be a problem as a sample is used as where appropriate (see ?leaflet::addRasterImage and ?terra::plet).

Upvotes: 0

Related Questions