Skip to content

Commit

Permalink
testing .check_st_valid
Browse files Browse the repository at this point in the history
  • Loading branch information
alikhuseynov authored Jun 5, 2024
1 parent 6a2a2dd commit a48003d
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions R/read.R
Original file line number Diff line number Diff line change
Expand Up @@ -461,24 +461,21 @@ read10xVisiumSFE <- function(samples = "",
# sanity on geometries to remove any self-intersection
#' @importFrom sf st_buffer st_is_valid
.check_st_valid <- function(sf_df = NULL) {
# sf_df is a single sf data frame, not a list of sf data frames
invalid_inds <- which(!st_is_valid(sf_df))
if (length(invalid_inds)) {
geoms_new <- st_buffer(st_geometry(sf_df)[invalid_inds], dist = 0)
# [.sfc<- is slow for st_GEOMETRY when buffer created MULTIPOLYGONs
# due to a vapply somewhere to recompute bbox in an R loop
new_type <- st_geometry_type(geoms_new, by_geometry = FALSE)
if (new_type == "GEOMETRY") {
geoms_new <- st_cast(geoms_new)
new_type <- st_geometry_type(geoms_new, by_geometry = FALSE)
}
old_type <- st_geometry_type(sf_df, by_geometry = FALSE)
if (new_type != old_type && new_type != "GEOMETRY") {
sf_df <- st_cast(sf_df, as.character(new_type))
}
st_geometry(sf_df)[invalid_inds] <- geoms_new
if (!is(sf_df, "list")) { sf_df <- list(sf_df) }
test_st_valid <-
lapply(sf_df, function(i) {
i <- st_geometry(i) |> st_is_valid()
i <- any(x = !(i)) }) |> unlist()
if (any(test_st_valid)) {
seg <- which(test_st_valid)
for (i in seq(seg)) {
geoms <-
st_buffer(sf_df[[i]] |> st_geometry(), dist = 0)
st_geometry(sf_df[[i]] ) <- geoms
}
return(sf_df)
}
if (length(sf_df) == 1) { sf_df <- sf_df[[1]] }
return(sf_df)
}

#' Read Vizgen MERFISH output as SpatialFeatureExperiment
Expand Down

0 comments on commit a48003d

Please sign in to comment.