Hi,
we have come across some questions while experimenting with the function filter_spatial. This is a reprex of the code we used:
library(openeo)
library(stars)
collections = "SENTINEL2_L2A_SENTINELHUB"
bands = c("B02", "B03", "B04", "B08", "SCL")
ext = list(11.14079 , 49.75891, 11.1432, 49.76039)
period = c("2021-04-01", "2021-06-30")
names(ext) = c("west", "south", "east", "north")
dir = tempdir()
# change to login with your credentials
# con = openeo::connect("https://openeo.cloud")
# login(
# login_type = "oidc"
# , provider = "egi"
# , config = list(
# client_id = "<client_id>"
# , secret = "<secret>"
# )
# )
procs = processes()
cube = procs$load_collection(
id = collections
, spatial_extent = ext
, temporal_extent = period
, bands = bands
)
# geoJSON in right format
gmj = '{"type":"Polygon","coordinates":[[[11.142508,49.759305999999998],[11.143205,49.759535],[11.142304,49.760394000000008],[11.140790999999999,49.759847],[11.141800000000002,49.758911999999998],[11.142508,49.759305999999998]]]} '
class(gmj) = "geojson"
gms = jsonlite::fromJSON(gmj)
cube_proj = procs$resample_spatial(
data = cube
, projection = 4326
, method = "near"
)
cube_crop = procs$filter_spatial(
cube_proj
, geometries = gms
)
## create and start job
job = openeo::create_job(cube_crop)
openeo::start_job(job)
id = as.character(job$id)
jobs = openeo::list_jobs()
jobs[[id]]
## download files
openeo::download_results(
job = jobs[[id]]$id
, folder = dir
)
## create stars object
fls = list.files(dir, pattern = "^openEO_", full.names = TRUE)
stars = stars::read_stars(fls, along = "time")
stars = stats::setNames(stars, nm = "value")
# map
mapview::mapviewOptions(fgb = FALSE)
sf = geojsonsf::geojson_sf(gmj)
m1 = mapview::mapview(sf)
m2 = stars[,,,,6] |> abind::adrop() |> split() |> mapview::mapview()
m1 + m2
Some things we noticed:
-
From the function description it seemed that the function is supposed to crop to the Polygon extent, however it looks more like it crops to the bounding box
-
In order for the function to work the GeoJSON string has to be manipulated to exclude the header if the polygon was part of a feature collection
-
What about projection? For us it worked when both the polygon and the cube were in WGS 84 (requires the cube to be converted to WGS84 first!). Is this the only option seeing as the GeoJSON does not include CRS information?