rheatmapr-rasterr-leaflet

How can you create heatmaps in R with leaflet from multiple csvs? Merging/resampling issue


I have been using this as an inspiration. I have c.400 GPS files with GPS positions in csv's, each csv has a file property that I want to retain to create heatmaps based on that property.

https://gis.stackexchange.com/questions/168886/r-how-to-build-heatmap-with-the-leaflet-package

My code is fine for a single csv and generates a heat map from the raster image. My code for multiple csv's generates a new raster for each csv however I am struggling to merge rasters successfully. To enable this I have tried resampling with res/ext/crs constant, but I get what seems to be weird behaviour (or more likely my misunderstanding).

The processed_data_list includes the rasters and the file properties:

raster_layers<-lapply(processed_data_list, function(item) item$raster_data)

merged_raster_stack<- do.call(merge,raster_layers)

If I don't resample then I get errors relating to different resolutions/origin when merging. If I do resample, even when res, ext and crs are set to the original raster (KernelDensityRaster), and using the sample data, (https://data.cityofchicago.org/api/views/22s8-eq8h/rows.csv?accessType=DOWNLOAD) the resampled raster just has NAs.

resampled_raster<- KernelDensityRaster # results in merging error
#or
#resampled_raster<- raster(res = res(KernelDensityRaster), ext = extent(KernelDensityRaster), crs = crs(KernelDensityRaster)) #results in NA data values

print(summary(values(KernelDensityRaster))) Min. 1st Qu. Median Mean 3rd Qu. Max. 0.00000 0.00000 0.04892 6.14610 9.73696 56.01518

print(summary(values(resampled_raster))) Mode NA's logical 10000


Solution

  • The issue here was that I thought I was resampling just by rerunning through raster with changed ext/res/crs properties. However, I should have used the resample {raster} function:

        resampled_raster<- resample(KernelDensityRaster, FirstRaster, method = "bilinear") #resample so the new raster matchs the first raster (in terms of origin and resolution)