Skip to content

Commit 7aef9cc

Browse files
Merge pull request #1163 from M3nin0/feature/geom-validation
2 parents 99f120e + d7eb9e0 commit 7aef9cc

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

R/api_sf.R

+24-14
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@
2727
.check_set_caller(".sf_get_samples")
2828
# Pre-condition - is the sf object has geometries?
2929
.check_that(nrow(sf_object) > 0)
30-
# Precondition - can the function deal with the geometry_type?
30+
# Pre-condition - can the function deal with the geometry_type?
3131
geom_type <- as.character(sf::st_geometry_type(sf_object)[[1]])
3232
sf_geom_types_supported <- .conf("sf_geom_types_supported")
3333
.check_that(geom_type %in% sf_geom_types_supported)
34-
3534
# Get the points to be read
3635
samples <- .sf_to_tibble(
3736
sf_object = sf_object,
@@ -76,22 +75,12 @@
7675
sampling_type,
7776
start_date,
7877
end_date) {
79-
80-
# Remove empty geometries if exists
81-
are_empty_geoms <- sf::st_is_empty(sf_object)
82-
if (any(are_empty_geoms)) {
83-
if (.check_warnings()) {
84-
warning(.conf("messages", ".sf_to_tibble"),
85-
immediate. = TRUE, call. = FALSE
86-
)
87-
}
88-
sf_object <- sf_object[!are_empty_geoms, ]
89-
}
78+
# Remove invalid geometries (malformed and empty ones)
79+
sf_object <- .sf_clean(sf_object)
9080
# If the sf object is not in planar coordinates, convert it
9181
sf_object <- suppressWarnings(
9282
sf::st_transform(sf_object, crs = "EPSG:4326")
9383
)
94-
9584
# Get the geometry type
9685
geom_type <- as.character(sf::st_geometry_type(sf_object)[[1]])
9786
# Get a tibble with points and labels
@@ -239,3 +228,24 @@
239228
})
240229
return(points_tab)
241230
}
231+
232+
#' @title Clean invalid geometries
233+
#' @name .sf_clean
234+
#' @description Malformed and empty geometries are defined as invalid
235+
#' @keywords internal
236+
#' @noRd
237+
#' @param sf_object sf object to be validated
238+
#' @return sf object with no invalid geometries.
239+
#'
240+
.sf_clean <- function(sf_object) {
241+
# condition 1 - geometry is valid
242+
is_geometry_valid <- sf::st_is_valid(sf_object)
243+
# condition 2 - geometry is not empty
244+
is_geometry_valid <- is_geometry_valid & !sf::st_is_empty(sf_object)
245+
# warning user in case of invalid geometries
246+
if (!all(is_geometry_valid)) {
247+
warning(.conf("messages", ".sf_clean"))
248+
}
249+
# return only valid geometries
250+
sf_object[is_geometry_valid,]
251+
}

inst/extdata/config_messages.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@
228228
.s2_mgrs_to_roi: "invalid MGRS tiles"
229229
.samples_prune: "time series length is smaller than the lenght of the first sample"
230230
.samples_ts: "time_series column not found in samples"
231+
.sf_clean: "invalid geometries detected - these geometries have been removed"
231232
.sf_get_samples: "invalid sf object - only POINT, POLYGON and MULTIPOLYGON objects are supported"
232233
.sf_polygon_to_tibble: "unable to extract data from polygon - check 'label_attr' and 'pol_id' parameters"
233-
.sf_to_tibble: "removed empty geometries"
234234
.shp_transform_to_sf: "invalid shapefile - missing data or invalid geometry types"
235235
.shp_get_samples: "either a label or shape attribute should be provided to read shapefiles"
236236
.signal_odd_filter_length: "sgolay needs an odd filter length n"

0 commit comments

Comments
 (0)