Skip to content

Commit f618525

Browse files
Merge pull request #1419 from M3nin0/fix/area-calculation
enhance area strategy by checking equal area crs
2 parents c911422 + a2bcea2 commit f618525

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

R/api_bbox.R

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,25 @@ NULL
233233
proj4string <- crs_sf[["proj4string"]]
234234
proj4string
235235
}
236-
236+
#' @title Verify if CRS is equal area
237+
#' @name .crs_is_equal_area
238+
#' @noRd
239+
#' @param wkt_crs CRS in WKT name
240+
#' @returns CRS in PROJ4 name
241+
.crs_is_equal_area <- function(crs) {
242+
# Transform CRS to Sf object
243+
proj4_string <- sf::st_crs(crs)$proj4string
244+
# Define equal area codes
245+
equal_area_codes <- c("aea", "laea", "cea", "moll", "sinu", "robin", "vandg")
246+
# Verify if CRS is equal area
247+
is_equal_area <- any(sapply(equal_area_codes, function(code)
248+
grepl(
249+
paste0("\\+proj=", code), proj4_string
250+
))
251+
)
252+
# Return!
253+
return(is_equal_area)
254+
}
237255
#' @title Check if CRS is WGS84
238256
#' @name .is_crs_wgs84
239257
#' @noRd

R/api_tile.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,15 +1394,17 @@ NULL
13941394
#' @export
13951395
.tile_area_freq.class_cube <- function(tile) {
13961396
# get tile crs
1397-
tile_crs <- .tile_crs(tile)
1397+
tile_crs <- sf::st_crs(.tile_crs(tile))
13981398
# get tile crs unit (metre or degree)
1399-
tile_crs_unit <- sf::st_crs(tile_crs)$units_gdal
1399+
tile_crs_unit <- tile_crs$units_gdal
1400+
# validate if crs is equal area
1401+
tile_crs_equal_area <- .crs_is_equal_area(tile_crs)
14001402
# extract the file path
14011403
tile_file <- .tile_paths(tile)
14021404
# read the files with terra
14031405
rast <- .raster_open_rast(tile_file)
14041406
# get area by pixels
1405-
if (tile_crs_unit == "metre") {
1407+
if (!tile_crs_equal_area && tile_crs_unit == "metre") {
14061408
# get a frequency of values
14071409
class_areas <- .raster_freq(rast) |>
14081410
dplyr::select(-.data[["layer"]])

R/sits_summary.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,12 +447,12 @@ summary.variance_cube <- function(object, ...,
447447
#' @export
448448
summary.class_cube <- function(object, ...) {
449449
.check_set_caller("summary_class_cube")
450-
# check if cube has only metre crs
450+
# check if cube has only metre crs and is not equal area
451451
cube_has_only_metre_crs <- slider::slide_lgl(object, function(tile) {
452452
# get tile crs
453-
tile_crs <- .tile_crs(tile)
453+
tile_crs <- sf::st_crs(.tile_crs(tile))
454454
# check unit
455-
sf::st_crs(tile_crs)$units_gdal == "metre"
455+
tile_crs$units_gdal == "metre" && !.crs_is_equal_area(tile_crs)
456456
})
457457
# all must be true
458458
cube_has_only_metre_crs <- all(cube_has_only_metre_crs)

0 commit comments

Comments
 (0)