riatelab / cartography

Thematic Cartography for R
http://riatelab.github.io/cartography/docs/
397 stars 46 forks source link

Tile Caching and barscale units #64

Closed arthurgailes closed 4 years ago

arthurgailes commented 4 years ago

Two quick feature additions - I was creating maps in a loop, and being able to cache the tiles sped up the process quite a bit.

By default, getTiles behaves as before, only placing them in a tempdir. Now if cachedir is specified to a file path, it will save the tiles to that directory, allowing the files to be re-used if the function is called again. Useful for re-running the code in different sessions.

forceDownload will overwrite existing cached tiles.

example code:

cache_directory = your_directory_here
# takes 9 minutes the first time
t <- getTiles(x = mtq, type = "CartoDB", cachedir =cache_directory, zoom=15)
# takes seven minutes the second time
t <- getTiles(x = mtq, type = "CartoDB", cachedir =cache_directory, zoom=15)

I hope to work on an efficient raster caching system if this is accepted, which would really speed things up.

Second addition is the ability to use miles and meters in the barscale; pretty straightforward. I wrote the conversion in a separate function in case you'd want to add more unit types later.

dieghernan commented 4 years ago

If I may, I would like to propose a slight change on code, regarding the default behaviour of cachedir, from an user perspective.

By doing , users wouldn't mind to type the full path and on RStudio you can navigate easily on Files, I think this would work:

if(isFALSE(cachedir)) {
  cachedir <- tempdir()
} else if (isTRUE(cachedir)) {
  cachedir <- getwd()
} else {
  # create the cachedir if it doesn't exist.
  if (!dir.exists(cachedir)) dir.create(cachedir)
}
# uses subdirectories based on src to make the directory easier for users to navigate
cachedir <- paste0(cachedir, "/", src)
if (!dir.exists(cachedir)) dir.create(cachedir)

instead of

https://github.com/arthurgailes/cartography/blob/9d849627ede7c63a95159ea279328b0d6d6cb98f/R/getTiles.R#L177-L186

arthurgailes commented 4 years ago

I agree, that's better. Small change to add "tile.chache" folder to working directory, so subfolders for different map sources won't cause clutter. Committed.

rCarto commented 4 years ago

Merged! Thank you very much @arthurgailes and @dieghernan . I just had to modify a bit barscale.R to make the automatic size work with other metrics.