diff --git a/NAMESPACE b/NAMESPACE index 29072cd..f831d3e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,16 +6,21 @@ export(get_tiles_path) export(is_tile_existing) export(lfa_capitalize_first_char) export(lfa_check_flag) +export(lfa_combine_sf_obj) export(lfa_create_stacked_distributions_plot) export(lfa_create_stacked_histogram) export(lfa_create_tile_location_objects) export(lfa_detection) export(lfa_download) export(lfa_download_areas) +export(lfa_find_n_nearest_trees) export(lfa_get_all_areas) export(lfa_get_detection_area) +export(lfa_get_detections) export(lfa_get_detections_species) export(lfa_get_flag_path) +export(lfa_get_neighbor_paths) +export(lfa_get_species) export(lfa_ground_correction) export(lfa_init) export(lfa_init_data_structure) @@ -23,9 +28,11 @@ export(lfa_intersect_areas) export(lfa_load_ctg_if_not_present) export(lfa_map_tile_locations) export(lfa_merge_and_save) +export(lfa_modify_gpkg) export(lfa_rd_to_qmd) export(lfa_rd_to_results) export(lfa_read_area_as_catalog) +export(lfa_save_all_neighbours) export(lfa_segmentation) export(lfa_set_flag) export(read_catalog) diff --git a/R/combine_sf_obj.R b/R/combine_sf_obj.R new file mode 100644 index 0000000..e6169d5 --- /dev/null +++ b/R/combine_sf_obj.R @@ -0,0 +1,31 @@ +#' Combine Spatial Feature Objects from Multiple GeoPackage Files +#' +#' This function reads spatial feature objects (sf) from multiple GeoPackage files and combines them into a single sf object. +#' Each GeoPackage file is assumed to contain neighbor information for a specific detection area, and the resulting sf object +#' includes additional columns indicating the corresponding area and species information. +#' +#' @param paths A character vector containing file paths to GeoPackage files with neighbor information. +#' @param area_infos A data frame or list containing information about the corresponding detection areas, including "area" and "specie" columns. +#' @return A combined sf object with additional columns for area and specie information. +#' @export +#' +#' @examples +#' # Assuming paths and area_infos are defined +#' combined_sf <- lfa_combine_sf_obj(paths, area_infos) +#' +#' # Print the combined sf object +#' print(combined_sf) +#' +#'@export +lfa_combine_sf_obj <- function(paths, area_infos) { + sf_obj <- sf::st_read(paths[1]) + sf_obj$area = area_infos$area[1] + sf_obj$specie = area_infos$specie[1] + for (i in 2:length(paths)) { + extend <- sf::st_read(paths[i]) + extend$area = area_infos$area[i] + extend$specie = area_infos$specie[i] + sf_obj <- dplyr::bind_rows(sf_obj, extend) + } + return(sf_obj) +} diff --git a/R/find_n_nearest_trees.R b/R/find_n_nearest_trees.R new file mode 100644 index 0000000..295474f --- /dev/null +++ b/R/find_n_nearest_trees.R @@ -0,0 +1,48 @@ +#' Find n Nearest Trees +#' +#' This function calculates the distances to the n nearest trees for each tree in the input dataset. +#' +#' @param trees A sf object containing tree coordinates. +#' @param n The number of nearest trees to find for each tree (default is 100). +#' @return A data frame with additional columns representing the distances to the n nearest trees. +#' +#' @examples +#' # Load tree data using lfa_get_detections() (not provided) +#' tree_data <- lfa_get_detections() +#' +#' # Filter tree data for a specific species and area +#' tree_data = tree_data[tree_data$specie == "pine" & tree_data$area == "greffen", ] +#' +#' # Find the 100 nearest trees for each tree in the filtered dataset +#' tree_data <- lfa_find_n_nearest_trees(tree_data) +#' +#'@export +lfa_find_n_nearest_trees <- function(trees, n = 100) { + coordinates <- sf::st_coordinates(trees) + + distances_matrix <- matrix(NA, nrow = nrow(coordinates), ncol = n) + + cat("Calculate ", nrow(coordinates), "entries") + for (i in seq_len(nrow(coordinates))) { + query_point <- coordinates[i,] + neighbors <- + spdep::knn2nb(spdep::knearneigh(coordinates, k = n)) + + # Extract the coordinates of neighbors and create an sf object + neighbor_indices <- unlist(neighbors[[i]]) + neighbors_sf <- trees[neighbor_indices,] + + # Set CRS for query point + query_point_sf <- + sf::st_sfc(sf::st_point(c(query_point[1], query_point[2])), crs = sf::st_crs(trees)) + + distances <- sf::st_distance(query_point_sf, neighbors_sf) + distances <- distances[order(distances)] + distances_matrix[i, ] <- distances + cat(i, "/", nrow(coordinates), "\n") + } + distances_matrix <- distances_matrix |> data.frame() + colnames(distances_matrix) <- paste0("Neighbor_", 1:n) + trees <- dplyr::bind_cols(trees, distances_matrix) + return(trees) +} diff --git a/R/get_detections.R b/R/get_detections.R new file mode 100644 index 0000000..df4d09c --- /dev/null +++ b/R/get_detections.R @@ -0,0 +1,43 @@ +#' Retrieve aggregated detection data for multiple species. +#' +#' This function obtains aggregated detection data for multiple species by iterating +#' through the list of species obtained from \code{\link{lfa_get_species}}. For each +#' species, it calls \code{\link{lfa_get_detections_species}} to retrieve the +#' corresponding detection data and aggregates the results into a single data frame. +#' The resulting data frame includes columns for the species, tree detection data, +#' and the area in which the detections occurred. +#' +#' @return A data frame containing aggregated detection data for multiple species. +#' +#' @export +#' @seealso +#' \code{\link{lfa_get_species}}, \code{\link{lfa_get_detections_species}} +#' +#' @examples +#' lfa_get_detections() +#' +#' @keywords data manipulation aggregation +#' +#' @family data retrieval functions +#' +#' @examples +#' # Retrieve aggregated detection data for multiple species +#' detections_data <- lfa_get_detections() +#' +#' @export +lfa_get_detections <- function() { + species <- lfa_get_species() + results <- NULL + for(specie in species){ + trees <- lfa_get_detections_species(specie) + trees$specie <- specie + if(is.null(results)){ + results <- trees + } else { + results <- dplyr::bind_rows(results, trees) + } + } + results$area <- as.factor(results$area) + results$specie <- as.factor(results$specie) + return(results) +} diff --git a/R/get_neighbor_paths.R b/R/get_neighbor_paths.R new file mode 100644 index 0000000..803a539 --- /dev/null +++ b/R/get_neighbor_paths.R @@ -0,0 +1,25 @@ +#' Get Paths to Neighbor GeoPackage Files +#' +#' This function retrieves the file paths to GeoPackage files containing neighbor information for each detection area. +#' The GeoPackage files are assumed to be named "neighbours.gpkg" and organized in a directory structure under the "data" folder. +#' +#' @return A character vector containing file paths to GeoPackage files for each detection area's neighbors. +#' @export +#' +#' @examples +#' # Get paths to neighbor GeoPackage files for all areas +#' paths <- lfa_get_neighbor_paths() +#' +#' # Print the obtained file paths +#' print(paths) +#'@export +lfa_get_neighbor_paths <- function() { + all_areas <- lfa::lfa_get_all_areas() + return(file.path( + getwd(), + "data", + all_areas$specie, + all_areas$area, + "neighbours.gpkg" + )) +} diff --git a/R/get_species.R b/R/get_species.R new file mode 100644 index 0000000..7baaa41 --- /dev/null +++ b/R/get_species.R @@ -0,0 +1,25 @@ +#' Get a list of species from the data directory. +#' +#' This function retrieves a list of species by scanning the "data" directory +#' located in the current working directory. +#' +#' @return A character vector containing the names of species found in the "data" directory. +#' +#' @seealso +#' \code{\link{list.dirs}} +#' +#' @references +#' This function relies on the \code{\link{list.dirs}} function for directory listing. +#' +#' @keywords data manipulation file system +#' +#' @family data retrieval functions +#' +#' @examples +#' # Retrieve the list of species +#' species_list <- lfa_get_species() +#' +#' @export +lfa_get_species <- function() { + return(file.path(getwd(), "data") |> list.dirs(full.names = FALSE, recursive = FALSE)) +} diff --git a/R/save_all_neighbours.R b/R/save_all_neighbours.R new file mode 100644 index 0000000..166a396 --- /dev/null +++ b/R/save_all_neighbours.R @@ -0,0 +1,24 @@ +#' Save Neighbors for All Areas +#' +#' This function iterates through all detection areas, finds the n nearest trees for each tree, +#' and saves the result to a GeoPackage file for each area. +#' +#' @param n The number of nearest trees to find for each tree (default is 100). +#' @export +#' +#' @examples +#' # Save neighbors for all areas with default value (n=100) +#' lfa_save_all_neighbours() +#' +#' # Save neighbors for all areas with a specific value of n (e.g., n=50) +#' lfa_save_all_neighbours(n = 50) +#' @export +lfa_save_all_neighbours <- function (n = 100) { + all_areas <- lfa::lfa_get_all_areas() + for (i in 1:nrow(all_areas)) { + cat("Find neighbours for", all_areas[i, 1], ",", all_areas[i, 2], "\n") + out_path <- + file.path(getwd(), "data", all_areas[i, 1], all_areas[i, 2], "neighbours.gpkg") + lfa::lfa_get_detection_area(all_areas[i, 1], all_areas[i, 2]) |> lfa::lfa_find_n_nearest_trees(n) |> sf::st_write(out_path, append = F) + } + } diff --git a/man/lfa_combine_sf_obj.Rd b/man/lfa_combine_sf_obj.Rd new file mode 100644 index 0000000..9f6db1d --- /dev/null +++ b/man/lfa_combine_sf_obj.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/combine_sf_obj.R +\name{lfa_combine_sf_obj} +\alias{lfa_combine_sf_obj} +\title{Combine Spatial Feature Objects from Multiple GeoPackage Files} +\usage{ +lfa_combine_sf_obj(paths, area_infos) +} +\arguments{ +\item{paths}{A character vector containing file paths to GeoPackage files with neighbor information.} + +\item{area_infos}{A data frame or list containing information about the corresponding detection areas, including "area" and "specie" columns.} +} +\value{ +A combined sf object with additional columns for area and specie information. +} +\description{ +This function reads spatial feature objects (sf) from multiple GeoPackage files and combines them into a single sf object. +Each GeoPackage file is assumed to contain neighbor information for a specific detection area, and the resulting sf object +includes additional columns indicating the corresponding area and species information. +} +\examples{ +# Assuming paths and area_infos are defined +combined_sf <- lfa_combine_sf_obj(paths, area_infos) + +# Print the combined sf object +print(combined_sf) + +} diff --git a/man/lfa_find_n_nearest_trees.Rd b/man/lfa_find_n_nearest_trees.Rd new file mode 100644 index 0000000..ccb755a --- /dev/null +++ b/man/lfa_find_n_nearest_trees.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/find_n_nearest_trees.R +\name{lfa_find_n_nearest_trees} +\alias{lfa_find_n_nearest_trees} +\title{Find n Nearest Trees} +\usage{ +lfa_find_n_nearest_trees(trees, n = 100) +} +\arguments{ +\item{trees}{A sf object containing tree coordinates.} + +\item{n}{The number of nearest trees to find for each tree (default is 100).} +} +\value{ +A data frame with additional columns representing the distances to the n nearest trees. +} +\description{ +This function calculates the distances to the n nearest trees for each tree in the input dataset. +} +\examples{ +# Load tree data using lfa_get_detections() (not provided) +tree_data <- lfa_get_detections() + +# Filter tree data for a specific species and area +tree_data = tree_data[tree_data$specie == "pine" & tree_data$area == "greffen", ] + +# Find the 100 nearest trees for each tree in the filtered dataset +tree_data <- lfa_find_n_nearest_trees(tree_data) + +} diff --git a/man/lfa_get_detections.Rd b/man/lfa_get_detections.Rd new file mode 100644 index 0000000..db15557 --- /dev/null +++ b/man/lfa_get_detections.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_detections.R +\name{lfa_get_detections} +\alias{lfa_get_detections} +\title{Retrieve aggregated detection data for multiple species.} +\usage{ +lfa_get_detections() +} +\value{ +A data frame containing aggregated detection data for multiple species. +} +\description{ +This function obtains aggregated detection data for multiple species by iterating +through the list of species obtained from \code{\link{lfa_get_species}}. For each +species, it calls \code{\link{lfa_get_detections_species}} to retrieve the +corresponding detection data and aggregates the results into a single data frame. +The resulting data frame includes columns for the species, tree detection data, +and the area in which the detections occurred. +} +\examples{ +lfa_get_detections() + +# Retrieve aggregated detection data for multiple species +detections_data <- lfa_get_detections() + +} +\seealso{ +\code{\link{lfa_get_species}}, \code{\link{lfa_get_detections_species}} + +Other data retrieval functions: +\code{\link{lfa_get_species}()} +} +\concept{data retrieval functions} +\keyword{aggregation} +\keyword{data} +\keyword{manipulation} diff --git a/man/lfa_get_neighbor_paths.Rd b/man/lfa_get_neighbor_paths.Rd new file mode 100644 index 0000000..379e4dd --- /dev/null +++ b/man/lfa_get_neighbor_paths.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_neighbor_paths.R +\name{lfa_get_neighbor_paths} +\alias{lfa_get_neighbor_paths} +\title{Get Paths to Neighbor GeoPackage Files} +\usage{ +lfa_get_neighbor_paths() +} +\value{ +A character vector containing file paths to GeoPackage files for each detection area's neighbors. +} +\description{ +This function retrieves the file paths to GeoPackage files containing neighbor information for each detection area. +The GeoPackage files are assumed to be named "neighbours.gpkg" and organized in a directory structure under the "data" folder. +} +\examples{ +# Get paths to neighbor GeoPackage files for all areas +paths <- lfa_get_neighbor_paths() + +# Print the obtained file paths +print(paths) +} diff --git a/man/lfa_get_species.Rd b/man/lfa_get_species.Rd new file mode 100644 index 0000000..6b1e3e3 --- /dev/null +++ b/man/lfa_get_species.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_species.R +\name{lfa_get_species} +\alias{lfa_get_species} +\title{Get a list of species from the data directory.} +\usage{ +lfa_get_species() +} +\value{ +A character vector containing the names of species found in the "data" directory. +} +\description{ +This function retrieves a list of species by scanning the "data" directory +located in the current working directory. +} +\examples{ +# Retrieve the list of species +species_list <- lfa_get_species() + +} +\references{ +This function relies on the \code{\link{list.dirs}} function for directory listing. +} +\seealso{ +\code{\link{list.dirs}} + +Other data retrieval functions: +\code{\link{lfa_get_detections}()} +} +\concept{data retrieval functions} +\keyword{data} +\keyword{file} +\keyword{manipulation} +\keyword{system} diff --git a/man/lfa_save_all_neighbours.Rd b/man/lfa_save_all_neighbours.Rd new file mode 100644 index 0000000..50d3761 --- /dev/null +++ b/man/lfa_save_all_neighbours.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/save_all_neighbours.R +\name{lfa_save_all_neighbours} +\alias{lfa_save_all_neighbours} +\title{Save Neighbors for All Areas} +\usage{ +lfa_save_all_neighbours(n = 100) +} +\arguments{ +\item{n}{The number of nearest trees to find for each tree (default is 100).} +} +\description{ +This function iterates through all detection areas, finds the n nearest trees for each tree, +and saves the result to a GeoPackage file for each area. +} +\examples{ +# Save neighbors for all areas with default value (n=100) +lfa_save_all_neighbours() + +# Save neighbors for all areas with a specific value of n (e.g., n=50) +lfa_save_all_neighbours(n = 50) +}