From fbb2aa5afa44f9ab7ae14f2559ccda0473a284c9 Mon Sep 17 00:00:00 2001 From: Nicolas Casajus Date: Mon, 12 Feb 2024 10:13:20 +0100 Subject: [PATCH] feat: function to convert data into sf - fix #38 --- NAMESPACE | 1 + R/data_to_sf.R | 42 ++++++++++++++++++++++++++++++++++++++++++ R/map_distribution.R | 23 +---------------------- man/data_to_sf.Rd | 21 +++++++++++++++++++++ 4 files changed, 65 insertions(+), 22 deletions(-) create mode 100644 R/data_to_sf.R create mode 100644 man/data_to_sf.Rd diff --git a/NAMESPACE b/NAMESPACE index 31a4d9b..e309b14 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ export(compute_abundances) export(compute_concentration_bins_CPR_n) export(compute_concentrations) export(compute_frequencies) +export(data_to_sf) export(filter_by_bbox) export(filter_by_month) export(filter_by_ocean) diff --git a/R/data_to_sf.R b/R/data_to_sf.R new file mode 100644 index 0000000..67e017d --- /dev/null +++ b/R/data_to_sf.R @@ -0,0 +1,42 @@ +#' Convert a data frame into an sf object +#' +#' @description +#' This function can be used to convert a data frame into an sf object. +#' +#' @param data a `data.frame`, i.e. a FORCIS dataset or the output of a +#' `filter_*()` function. +#' +#' @return An `sf POINTS` object. +#' +#' @export +#' +#' @examples +#' ## ADD EXAMPLE ---- + +data_to_sf <- function(data) { + + ## Check data object ---- + + check_if_not_df(data) + + check_field_in_data(data, "site_lon_start_decimal") + check_field_in_data(data, "site_lat_start_decimal") + + + ## Convert data into sf object ----- + + data <- data[!is.na(data$"site_lon_start_decimal"), ] + data <- data[!is.na(data$"site_lat_start_decimal"), ] + + data_sf <- sf::st_as_sf(data, + coords = c("site_lon_start_decimal", + "site_lat_start_decimal"), + crs = sf::st_crs(4326)) + + + ## Project spatial objects into Robinson system ---- + + data_sf <- sf::st_transform(data_sf, sf::st_crs(crs_robinson())) + + data_sf +} diff --git a/R/map_distribution.R b/R/map_distribution.R index f73a0cf..2d30a9b 100644 --- a/R/map_distribution.R +++ b/R/map_distribution.R @@ -19,28 +19,7 @@ map_distribution <- function(data, col = "red", ...) { - ## Check data object ---- - - check_if_not_df(data) - - check_field_in_data(data, "site_lon_start_decimal") - check_field_in_data(data, "site_lat_start_decimal") - - - ## Convert data into sf object ----- - - data <- data[!is.na(data$"site_lon_start_decimal"), ] - data <- data[!is.na(data$"site_lat_start_decimal"), ] - - data_sf <- sf::st_as_sf(data, - coords = c("site_lon_start_decimal", - "site_lat_start_decimal"), - crs = sf::st_crs(4326)) - - - ## Project spatial objects into Robinson system ---- - - data_sf <- sf::st_transform(data_sf, sf::st_crs(crs_robinson())) + data_sf <- data_to_sf(data) ## Map ---- diff --git a/man/data_to_sf.Rd b/man/data_to_sf.Rd new file mode 100644 index 0000000..0b96628 --- /dev/null +++ b/man/data_to_sf.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data_to_sf.R +\name{data_to_sf} +\alias{data_to_sf} +\title{Convert a data frame into an sf object} +\usage{ +data_to_sf(data) +} +\arguments{ +\item{data}{a \code{data.frame}, i.e. a FORCIS dataset or the output of a +\verb{filter_*()} function.} +} +\value{ +An \verb{sf POINTS} object. +} +\description{ +This function can be used to convert a data frame into an sf object. +} +\examples{ +## ADD EXAMPLE ---- +}