VeruGHub / easyclimate

Easy access to high-resolution daily climate data for Europe
https://verughub.github.io/easyclimate/
GNU General Public License v3.0
45 stars 1 forks source link

Downloading the exact shape for polygons #39

Closed VeruGHub closed 1 year ago

VeruGHub commented 1 year ago

When coords is a polygon, get_daily_climate uses the maximum range of coordinates of the polygon to download the data (i.e. it downloads a rectangle or square of values) but not the exact shape of the polygon. Limiting the downloading to the shape would reduce the requests to the server

Pakillo commented 1 year ago

I think that's probably the default with terra, I don't think we can change that easily. Can you post a small reprex to try?

For data.frame output, I think we should return just values within the polygon. And for raster output, I guess cells outside the polygon should be NA?

VeruGHub commented 1 year ago

I think we can offer a cleaner output but I am not sure this is limiting the requests to the server. When output = "df" I think it is working properly.

aus <- geodata::gadm("Austria", level = 1, path = paste(getwd(), "contour", sep = "/"))
wien <- terra::subset(x = aus, subset = aus$NAME_1=="Wien")

tmin <- easyclimate::get_daily_climate(coords = wien, 
                                       climatic_var = "Tmin",
                                       period = "2020-12-26",
                                       output = "raster")
#> 
#> Connecting to the server...
#> 
#> Downloading Tmin data... This process might take several minutes

tmindf <- terra::as.data.frame(tmin, xy=TRUE)
wiendf <- as.data.frame(terra::geom(wien))

tmin_outdf <- easyclimate::get_daily_climate(coords = wien, 
                                             climatic_var = "Tmin",
                                             period = "2020-12-26",
                                             output = "df")
#> 
#> Connecting to the server...
#> 
#> 
#> Downloading Tmin data... This process might take several minutes

c(nrow(tmindf), nrow(tmin_outdf))
#> [1] 1175  724

library(ggplot2)

ggplot() +
  geom_raster(data = tmindf, aes(x = x, y = y, fill = `2020-12-26`), alpha = 0.9) +
  geom_polygon(data = wiendf, aes(x=x, y=y, group=part), fill = NA, col = "grey30", linewidth = 1)


#vs

tmin_crop <- terra::crop(tmin, wien, mask = TRUE)
tmindf_crop <- terra::as.data.frame(tmin_crop, xy=TRUE)

ggplot() +
  geom_raster(data = tmindf_crop, aes(x = x, y = y, fill = `2020-12-26`), alpha = 0.9) +
  geom_polygon(data = wiendf, aes(x=x, y=y, group=part), fill = NA, col = "grey30", linewidth = 1)

Created on 2023-01-14 by the reprex package (v2.0.1)

We could add mask = TRUE in L229 of get_daily_climate_single

Pakillo commented 1 year ago

Fixed now.

aus <- geodata::gadm("Austria", level = 1, path = paste(getwd(), "contour", sep = "/"))
wien <- terra::subset(x = aus, subset = aus$NAME_1=="Wien")

tmin <- easyclimate::get_daily_climate(coords = wien, 
                                       climatic_var = "Tmin",
                                       period = "2020-12-26",
                                       output = "raster")
#> Connecting to the server...
#> 
#> Downloading Tmin data... This process might take several minutes

tmindf <- terra::as.data.frame(tmin, xy=TRUE)
wiendf <- as.data.frame(terra::geom(wien))

library(ggplot2)

ggplot() +
  geom_raster(data = tmindf, aes(x = x, y = y, fill = `2020-12-26`), alpha = 0.9) +
  geom_polygon(data = wiendf, aes(x=x, y=y, group=part), fill = NA, col = "grey30", linewidth = 1)

Created on 2023-05-28 with reprex v2.0.2