I work with satellite data organized on an irregular two-dimensional grid whose dimensions are scanline (along track dimension) and ground pixel (across track dimension). Latitu
I think data.table
might be helpful here. The merging is almost instantly.
"5839200 rows merged in 1.24507117271423 seconds"
library(data.table)
pixel_cornersDT <- as.data.table(pixel_corners)
so2dfDT <- as.data.table(so2df)
setkey(pixel_cornersDT, id)
setkey(so2dfDT, id)
so2dfDT <- merge(pixel_cornersDT, so2dfDT, by=c("id"), all = TRUE)
Having the data in a data.table
, the subsetting in the plotting function is also gonna be considerably faster.
I dont think the process would be faster with raster
or sf
but you can experiment with the functions rasterFromXYZ()
or st_make_grid()
. But most of the time will be spent on the conversion to raster/sf objects, as you would have to transform the whole dataset.
I would suggest doing all the data processing with data.table
including the cropping, and from there you could switch to raster/sf objects for plotting purposes.
The google plot shows correctly, but you have specified a black/white map, and its overlayed with the "raster", so you won't see a lot. You could change the basemap to a satellite-background.
base_map <- get_map(location = c(lon = (latlon[4]+latlon[3])/2, lat = (latlon[1]+latlon[2])/2),
zoom = 7, maptype="satellite")
You could use the rescale
function from the scales
package. I included two options below.
The first (uncommented) takes the quantiles as breaks and the other breaks are individually-defined. I wouldn't use the log transformation (trans
- argument) as you would create NA values, since you have negative values aswell.
ggplot(df_sub) +
geom_polygon(aes(y=lat_bounds, x=lon_bounds, fill=so2tc, group=id), alpha=0.8) +
borders('world', xlim=range(df_sub$lon), ylim=range(df_sub$lat),
colour='gray20', size=.2) +
theme_light() +
theme(panel.ontop=TRUE, panel.background=element_blank()) +
# scale_fill_distiller(palette='Spectral', type="seq", trans = "log2") +
scale_fill_distiller(palette = "Spectral",
# values = scales::rescale(quantile(df_sub$so2tc), c(0,1))) +
values = scales::rescale(c(-3,0,1,5), c(0,1))) +
coord_quickmap(xlim=c(latlon[3], latlon[4]), ylim=c(latlon[1], latlon[2])) +
labs(title=title, subtitle=subtitle,
x="Longitude", y="Latitude",
fill=expression(DU))
The whole process takes now about 8 seconds for me, including the plotting without background-map, although the map rendering will also take additional 1-2 seconds.