8

Disclaimer: I have very little GIS experience

I've been working on a project in R that involves displaying large raster maps in an R Shiny interface. I am using the leaflet package for display. The displaying process (grabbing the raster from the stack, removing the old raster and adding the new one to the map) takes 4 or 5 seconds, and I'd like to bring that down as much as possible. The data is quite large, but it comes to me in small pieces, maybe keeping it in small pieces could be helpful.

I've already taken a few steps that speed up the display:

  • I aggregate the data ahead of time so that I'm not aggregating when I display it
  • I project the rasters for leaflet ahead of time so leaflet doesn't need to reproject when it displays them
  • I set my <= 0 values to NA ahead of time (my values should be strictly positive, NA values should be transparent on the map)

This means there's no actual processing when the map is displayed, it's just the displaying itself that takes time.

People often ask why I don't "tile the data like google maps" and truthfully that's because I don't quite know what that means and couldn't figure it out online. To the best of my knowledge it lowers the resolution as you zoom out? That would be perfect for me, my data is large but you only need the high resolution once you're zoomed in to a specific area.

1

1 Answer 1

3

You can tile a raster with GDAL itself, using gdal_translate -co TILED=YES and other options, or you can pass those creation options from R with writeRaster. From the command line you can also add "pyramids" to the file with gdaladdo (for "overviews") which adds copies of the data at a number of levels of resolution. But, there's no seamless i/o model for a tiled raster and requests from R, you would either use the low level functions in rgdal or carefully limit your raster/crop calls to match the underlying tiles and overviews. Not simple I'm afraid, and may be best to build a tile set with gdal2tiles.py and find out how to point leaflet at that instead.

Unfortunately this kind of "developer-level" workflow is not well supported in the R community, most of the tools and examples are written for high-level ops, though the low levels are readily available.

(There are wrappers in R for the GDAL command line tools, but they only add another layer, it's best to work with the utilities yourself for this kind of investigation, in my opinion.)

2
  • Thanks for the response! I think raster pyramids were what I was thinking of when I was saying tiles this whole time. If I read your answer correctly, there's no native support for raster pyramids in R then? That's unfortunate. I'm certainly not experienced enough to try and write a low level version of that myself, and it's not really such a necessity for me, just something I hoped was available. In your experience, what is the go-to for displaying GIS maps quickly in webapps? Would it be leaflet and javascript?
    – zach
    Commented Jul 31, 2017 at 23:48
  • mapview package
    – mdsumner
    Commented Aug 1, 2017 at 5:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.