Skip to content

Commit

Permalink
explicit specification of function package
Browse files Browse the repository at this point in the history
explicit specification of function package
  • Loading branch information
PondiB authored Oct 25, 2023
2 parents b74fbc2 + 977f3e5 commit 71313e7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
8 changes: 4 additions & 4 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,21 @@ NULL

if (class(format) == "list") {
if (format$title == "Network Common Data Form") {
file = write_ncdf(job$results)
file = gdalcubes::write_ncdf(job$results)
}
else if (format$title == "GeoTiff") {
file = write_tif(job$results)
file = gdalcubes::write_tif(job$results)
}
else {
throwError("FormatUnsupported")
}
}
else {
if (format == "NetCDF") {
file = write_ncdf(job$results)
file = gdalcubes::write_ncdf(job$results)
}
else if (format == "GTiff") {
file = write_tif(job$results)
file = gdalcubes::write_tif(job$results)
}
else {
throwError("FormatUnsupported")
Expand Down
50 changes: 25 additions & 25 deletions R/processes.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ load_collection = Process$new(
ymax = as.numeric(spatial_extent$north)
message("...After Spatial extent")

# spatial extent for stac call
# spatial extent for stac API call
xmin_stac = xmin
ymin_stac = ymin
xmax_stac = xmax
Expand All @@ -173,9 +173,9 @@ load_collection = Process$new(
message("....transformed to 4326")
}

# Connect to STAC API and get satellite data
# Connect to STAC API using rstac and get satellite data
message("STAC API call.....")
stac_object <- stac("https://earth-search.aws.element84.com/v0")
stac_object <- rstac::stac("https://earth-search.aws.element84.com/v0")
items <- stac_object %>%
stac_search(
collections = id,
Expand All @@ -186,21 +186,21 @@ load_collection = Process$new(
post_request() %>%
items_fetch()
# create image collection from stac items features
img.col <- stac_image_collection(items$features, property_filter =
img.col <- gdalcubes::stac_image_collection(items$features, property_filter =
function(x) {x[["eo:cloud_cover"]] < 30})
# Define cube view with monthly aggregation
crs <- c("EPSG", crs)
crs <- paste(crs, collapse=":")
v.overview <- cube_view(srs=crs, dx=pixels_size, dy=pixels_size, dt=time_aggregation,
v.overview <- gdalcubes::cube_view(srs=crs, dx=pixels_size, dy=pixels_size, dt=time_aggregation,
aggregation="median", resampling = "average",
extent=list(t0 = t0, t1 = t1,
left=xmin, right=xmax,
top=ymax, bottom=ymin))
# gdalcubes creation
cube <- raster_cube(img.col, v.overview)
cube <- gdalcubes::raster_cube(img.col, v.overview)

if(! is.null(bands)) {
cube = select_bands(cube, bands)
cube = gdalcubes::select_bands(cube, bands)
}
message("Data Cube is created....")
message(as_json(cube))
Expand Down Expand Up @@ -291,7 +291,7 @@ filter_bbox = Process$new(
p = list(rbind(nw, sw, se, ne, nw))
pol = sf::st_polygon(p)

cube = filter_geom(data, pol, srs = crs)
cube = gdalcubes::filter_geom(data, pol, srs = crs)

return(cube)
}
Expand Down Expand Up @@ -322,12 +322,12 @@ filter_spatial = Process$new(
returns = eo_datacube,
operation = function(data, geometries, job) {
# read geojson url and convert to geometry
geo.data = read_sf(geometries)
geo.data = sf::read_sf(geometries)
geo.data = geo.data$geometry
geo.data = st_transform(geo.data, 3857)
geo.data = sf::st_transform(geo.data, 3857)
# filter using geom
cube = filter_geom(data_cube, geo.data)
return (cube)
cube = gdalcubes::filter_geom(data_cube, geo.data)
return(cube)
}
)

Expand Down Expand Up @@ -358,7 +358,7 @@ filter_temporal = Process$new(
returns = eo_datacube,
operation = function(data, extent, dimension = NULL, job) {
if(! is.null(extent)) {
cube = select_time(data, c(extent[1], extent[2]))
cube = gdalcubes::select_time(data, c(extent[1], extent[2]))
}
return(cube)
}
Expand Down Expand Up @@ -404,17 +404,17 @@ ndvi = Process$new(
returns = eo_datacube,
operation = function(data, nir= "nir", red = "red",target_band = NULL, job) {
if((toString(nir) =="B08") && (toString(red) == "B04")){
cube = apply_pixel(data,"(B08-B04)/(B08+B04)", names = "NDVI", keep_bands=FALSE)
cube = gdalcubes::apply_pixel(data,"(B08-B04)/(B08+B04)", names = "NDVI", keep_bands=FALSE)
message("ndvi calculated ....")
message(as_json(cube))
return(cube)
}else if((toString(nir) =="B05") && (toString(red) == "B04")){
cube = apply_pixel(data,"(B05-B04)/(B05+B04)", names = "NDVI", keep_bands=FALSE)
cube = gdalcubes::apply_pixel(data,"(B05-B04)/(B05+B04)", names = "NDVI", keep_bands=FALSE)
message("ndvi calculated ....")
message(as_json(cube))
return(cube)
}else{
cube = apply_pixel(data,"(nir-red)/(nir+red)", names = "NDVI", keep_bands=FALSE)
cube = gdalcubes::apply_pixel(data,"(nir-red)/(nir+red)", names = "NDVI", keep_bands=FALSE)
message("ndvi calculated ....")
message(as_json(cube))
return(cube)
Expand Down Expand Up @@ -528,12 +528,12 @@ reduce_dimension = Process$new(
bandStr = append(bandStr, sprintf("%s(%s)", reducer, bands[i]))
}

cube = reduce_time(data, bandStr)
cube = gdalcubes::reduce_time(data, bandStr)
return(cube)
}
else if (dimension == "bands") {

cube = apply_pixel(data, reducer, keep_bands = FALSE)
cube = gdalcubes::apply_pixel(data, reducer, keep_bands = FALSE)
return(cube)
}
else {
Expand Down Expand Up @@ -581,7 +581,7 @@ merge_cubes = Process$new(
stop("Dimensions of datacubes are not equal")
}
else {
cube = join_bands(c(data1, data2))
cube = gdalcubes::join_bands(c(data1, data2))
return(cube)
}
}
Expand Down Expand Up @@ -689,18 +689,18 @@ rename_labels = Process$new(
if (! is.null(source)) {
if(class(source) == "number" || class(source) == "integer") {
band = as.character(bands(data)$name[source])
cube = apply_pixel(data, band, names = target)
cube = gdalcubes::apply_pixel(data, band, names = target)
}
else if (class(source) == "string" || class(source) == "character") {
cube = apply_pixel(data, source, names = target)
cube = gdalcubes::apply_pixel(data, source, names = target)
}
else {
stop("Source is not a number or string")
}
}
else {
band = as.character(bands(data)$name[1])
cube = apply_pixel(data, band, names = target)
cube = gdalcubes::apply_pixel(data, band, names = target)
}
return(cube)
}
Expand Down Expand Up @@ -768,20 +768,20 @@ run_udf = Process$new(
user.function = eval(func.parse)
# reducer udf
message("reducer function -> time")
data = reduce_time(data, names= names, FUN = user.function)
data = gdalcubes::reduce_time(data, names= names, FUN = user.function)
return (data)
}else{
# convert parsed string function to class function
message("apply per pixel function")
func.parse = parse(text = udf)
user.function = eval(func.parse)
# apply per pixel udf
data = apply_pixel(data, FUN = user.function)
data = gdalcubes::apply_pixel(data, FUN = user.function)
return (data)
}
}else{
message("simple reducer udf")
data = reduce_time(data, udf)
data = gdalcubes::reduce_time(data, udf)
return (data)
}
}
Expand Down

0 comments on commit 71313e7

Please sign in to comment.