Skip to content

Commit

Permalink
terra removed aggregation done with sf
Browse files Browse the repository at this point in the history
  • Loading branch information
vargastat committed Aug 23, 2023
1 parent 6f37e17 commit e6681ee
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Imports:
leaflet (>= 1.1.0),
sp,
sf,
terra,
webshot,
data.table,
methods,
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ importFrom(plotly,add_trace)
importFrom(plotly,config)
importFrom(plotly,layout)
importFrom(plotly,plot_ly)
importFrom(terra,aggregate)
importFrom(shiny,runApp)
importFrom(stats,as.formula)
importFrom(stats,density)
Expand Down
24 changes: 12 additions & 12 deletions R/map_layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ changeCoordsUI <- function(id, map_builder = TRUE) {

# changeCoords Module SERVER function
#' @import sp
#' @importFrom sf st_distance
#' @importFrom terra aggregate
#' @import sf
changeCoordsServer <- function(input, output, session,
layout, what = reactive("areas"),
map = reactive(NULL), map_builder = TRUE,
Expand Down Expand Up @@ -453,24 +452,25 @@ changeCoordsServer <- function(input, output, session,
ind_miss <- which(tmp_map$code %in% cty & is.na(tmp_map$geoAreaId))
areas <- coords[coords$geoAreaId %in% tmp_map$geoAreaId[ind_cty], ]
if (nrow(areas) > 0){
tmp_sf <- st_as_sf(tmp_map[ind_miss, ])
areas_sf <- st_as_sf(areas)
dist_matrix <- st_distance(tmp_sf, areas_sf)
areas_min <- suppressWarnings(apply( apply(dist_matrix, 1, which.min)))
tmp_sf <- sf::st_as_sf(tmp_map[ind_miss, ])
areas_sf <- sf::st_as_sf(areas)
dist_matrix <- sf::st_distance(tmp_sf, areas_sf)
areas_min <- suppressWarnings(apply(dist_matrix, 1, which.min))
tmp_map$geoAreaId[ind_miss] <- areas$geoAreaId[areas_min]
}
}

tmp_map <- terra::aggregate(tmp_map, by = c("geoAreaId"))
map <- tmp_map[match(final_coords_map$geoAreaId, tmp_map$geoAreaId), ]
tmp_sf <- sf::st_cast(tmp_map, "MULTIPOLYGON") # Cast to multipolygon if needed
tmp_sf <- sf::st_cast(tmp_sf, "GEOMETRY") # Remove unused geometry types
tmp_sf <- sf::st_combine(tmp_sf) # Combine features with the same geoAreaId
map <- tmp_sf[match(final_coords_map$geoAreaId, tmp_map$geoAreaId), ]
} else {
map <- map[final_coords_map$geoAreaId, ]
map <- map[match(final_coords_map$geoAreaId, map$geoAreaId), ]
}
}else {
map <- map[final_coords_map$geoAreaId, ]
map <- map[F(final_coords_map$geoAreaId, map$geoAreaId), ]
}
} else {
map <- map[final_coords_map$geoAreaId, ]
map <- map[match(final_coords_map$geoAreaId, map$geoAreaId), ]
}
# remove if multiple same polygon. Needed other change...
# map <- map[!duplicated(map$geoAreaId), ]
Expand Down

0 comments on commit e6681ee

Please sign in to comment.