Skip to content

Commit

Permalink
feat: function to convert data into sf - fix #38
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Feb 12, 2024
1 parent 777012c commit fbb2aa5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 22 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions R/data_to_sf.R
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 1 addition & 22 deletions R/map_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----
Expand Down
21 changes: 21 additions & 0 deletions man/data_to_sf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fbb2aa5

Please sign in to comment.