R4EPI / sitrep

Report templates and helper functions for applied epidemiology
https://r4epi.github.io/sitrep/
GNU General Public License v3.0
39 stars 14 forks source link

Spatial analysis issues #20

Open aspina7 opened 5 years ago

aspina7 commented 5 years ago

I have added a basic section to spatial analysis using the "tmap" package (with "tmaptools" too). I chose this because it allows both sf and sp files and has similar syntax to ggplot2 but simpler for manipulating spatial data. The reason I decided against ggmap - is that it really didnt work well in the field on poor internet connection (would often just get kicked out and it wouldnt download at all) - and the style options arent great either. In general tmap is quite a nice package, allowing for static and interactive maps. That comes with issues though as it has a lot of dependencies!

The biggest issue is that to get static tiles it uses the "openstreetmap" package which is rjava dependent and produces quite poor resolution rasters. For interactive maps it uses leaflet package. Which has great tile backgrounds but only interactive ones. Leaflet also has more server options, and particularly important is that it supports openstreetmap black&white as well as humanitarian openstreetmap (no other packages seem to provide these and they are the most important ones!!).

It would be really awesome if we could somehow manage to pull leaflet backgrounds in to static tiles. Alternatively "openstreetmap" package allows to specify server destinations to pull tiles from - so maybe theres a possible work around in there?? (just need to find a stable server with an api?)

The second thing I couldnt seem to get to work is creating kernel density (or heatmaps) - using the smooth_map function from the "tmaptools" package - based on points data.

If we manage a work around for getting tiles in (and dont want all the dependencies of tmap) then using the sf package with ggplot2 is equally simple and functional!

aspina7 commented 5 years ago

note to self: spatial random sample workaround fails occasionaly, - change to sp version?

dirkschumacher commented 5 years ago

I think we should have two options:

aspina7 commented 5 years ago

so in the current template there is the option to download in advance and save/send as a .rda, from hq to field for example. Shapefile also in there, but not geojson (though i wouldve thought geojson more rare...)

dirkschumacher commented 5 years ago

Ok nice. Yeah whatever is more common. Maybe it is just my bubble where nobody uses shapefiles ;)

aspina7 commented 5 years ago

Possible work around

https://cran.r-project.org/web/packages/ggspatial/vignettes/ggspatial.html

aspina7 commented 5 years ago

chris - see zhian's point about rjava and dependencies - we definitely need a better solution for basemaps. Thinking more and more that a simple wrapper with osmdata to pull land water and roads may just do enough (with option for pulling buildings in smaller areas.

zkamvar commented 5 years ago

The rosm package may be a solution, but don't count on it being updated anymore, only maintained: https://github.com/paleolimbot/rosm#readme

zkamvar commented 5 years ago

Just realized that ggspatial imports functions from rosm.

I think it may be possible to pre-download tiles for a given region with their API, and store it in a folder, then use file:/// construct to pass the folder's path as a URL to ggspatial/rosm

aspina7 commented 5 years ago

yep, rosm only available for baseplot though. ggspatial same functionality but makes available for ggplot. Its possible to predownload and save as a .rda - the other additional benefit of ggspatial is that it makes tiles from humanitarian osm as well as black/white osm. So the solution is to use ggspatial for tiles and SF for shapefiles etc.... all of these can be passed to ggplot.

zkamvar commented 5 years ago

After I polish the template a bit, I'll work on trying to get a reasonable workflow for getting the tiles.

aspina7 commented 5 years ago

pretty sure chris is on it

zkamvar commented 5 years ago

Then I will hold off

jarvisc1 commented 5 years ago

Have been looking at this today. As far as I can tell ggspatial only downloads the tiles when you add a spatial geom after annotation_map_tile, it doesn't appear to allow you to just specify that you want to download the tiles.

If you add a cache directory then it will create the folder and store the tiles there. These tiles can be used offline. It's works but the tiles don't look great with the ggplot maps. I'll see if can improve them but speaking to Alex perhaps we can look at the internals of annotation_map_tile and change it so we can just download the tiles we want without having to create a map each time.

aspina7 commented 4 years ago

There might be an easier work around in here than in our current setup.... Will look in to eventually https://github.com/hypertidy/ceramic/blob/master/README.md

kdoyle514 commented 4 years ago

I associate tiles with raster files -- will downloading tiles allow for admin-2 or admin-3 level divisions? or is this just to provide a base map? Ceramic uses NE to define the tile extent, but then we'd also need to load NE.

Also, a word of caution that health districts in many countries are different from admin levels...in both Chad and DRC, this was the case.

aspina7 commented 4 years ago

Tiles is just basemaps. I.e. a picture. You don't want to switch to ceramics. It's not on cran yet and switching tile provider is still complicated. Gadm is the package for getting admin level packages. All the spatial stuff has been relatively sorted the old generic outbreak template. Zhian can dig it out but focus on the other stuff before getting to this.

On Tue, 6 Aug 2019, 20:19 Kate Doyle, notifications@github.com wrote:

I associate tiles with raster files -- will downloading tiles allow for admin-2 or admin-3 level divisions? or is this just to provide a base map? Ceramic uses NE to define the tile extent, but then we'd also need to load NE.

Also, a word of caution that health districts in many countries are different from admin levels...in both Chad and DRC, this was the case.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/R4EPI/sitrep/issues/20?email_source=notifications&email_token=AEL7IJYFQ5AS436AK2FVTH3QDG6EZA5CNFSM4GPFKLYKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3WAVOI#issuecomment-518785721, or mute the thread https://github.com/notifications/unsubscribe-auth/AEL7IJ5PTB7DIYTKXRGO423QDG6EZANCNFSM4GPFKLYA .

zkamvar commented 4 years ago

I believe the last commit where the outbreak report existed was here: 322623ed146342ed368dfab6cac406f592e0448

zkamvar commented 4 years ago

Here we go: https://github.com/R4EPI/sitrep/blob/322623ed146342ed368dfab6cac406f592e0448e/inst/rmarkdown/templates/outbreak/skeleton/skeleton.Rmd

aspina7 commented 4 years ago

Note to self: see latest carto release and slippymap package to improve image res https://t.co/6OvNpqLoSC?amp=1

alternatively use {maptiles} (which uses {terra}) and then plot with tidyterra::geom_spatraster_rgb - or potentially with terrainr::geom_spatial_rgb