Skip to content

Instantly share code, notes, and snippets.

@phineas-pta
Last active January 17, 2024 19:41
Show Gist options
  • Save phineas-pta/b4c6f2f31cf5d7e3ec01a3f8eadf260c to your computer and use it in GitHub Desktop.
Save phineas-pta/b4c6f2f31cf5d7e3ec01a3f8eadf260c to your computer and use it in GitHub Desktop.
fishing spatial data with R: convert GPS coordinates to statistical rectangles

fishing spatial data with R: convert GPS coordinates to statistical rectangles

inputs: GPS coordinates (WGS 84)

output:

Usage

import::here(.from = "spatial_grid.R", identify_gfcm_rect, identify_ices_rect)

lon <- rnorm(10, -10, 2)
lat <- rnorm(10, 53, 1)

gfcmStatRecs <- identify_gfcm_rect(lon, lat)
icesStatRecs <- identify_ices_rect(lon, lat)
icesStatSubRecs <- identify_ices_rect(lon, lat, subrect = TRUE)

# with {dplyr}
library(dplyr)
df <- data.frame(lon, lat) %>%
	mutate(gfcmStatRecs = identify_gfcm_rect(lon, lat))

License

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to https://unlicense.org

#------------------------------------------------------------------------------
# helper functions
#' @title my custom `cut` function
cut_bis <- function(x, breaks, labels) as.character(cut(x,
breaks = breaks, labels = labels,
include.lowest = TRUE, right = FALSE
))
#' @title extract the minutes
#' @param x numeric vector
retrieve_mins <- function(x) {
x <- abs(x)
# convert to degree-minute
degrees <- as.integer(x)
mins <- as.integer(round(60 * (x - degrees)))
return(mins)
}
#------------------------------------------------------------------------------
# GFCM statistical grid
gfcm_lims <- c("xmin" = -6, "ymin" = 30, "xmax" = 42, "ymax" = 47.5)
# rectangle labels for latitudes
gfcm_lat_breaks <- seq(gfcm_lims["ymin"], gfcm_lims["ymax"], .5)
gfcm_lat_labels <- sprintf("%02d", 0:34) # 1:(length(gfcm_lat_breaks) - 1) - 1
# rectangle labels for longitudes
gfcm_lon_breaks <- seq(gfcm_lims["xmin"], gfcm_lims["xmax"], .5)
gfcm_col <- LETTERS[1:10] # from “A” to “J”
gfcm_lon_lab <- paste0(rep(gfcm_col, each = 10), rep(0:9, times = length(gfcm_col)))
gfcm_lon_labels <- gfcm_lon_lab[!gfcm_lon_lab %in% paste0("J", 6:9)] # they are ommitted
#' @title identify GFCM Statistical grid
identify_gfcm_rect <- function(LONGITUDES, LATITUDES) {
lat <- cut_bis(LATITUDES, breaks = gfcm_lat_breaks, labels = gfcm_lat_labels)
lon <- cut_bis(LONGITUDES, breaks = gfcm_lon_breaks, labels = gfcm_lon_labels)
res <- paste0(lat, lon)
bad_coords <-
LATITUDES < gfcm_lims["ymin"] | gfcm_lims["ymax"] < LATITUDES |
LONGITUDES < gfcm_lims["xmin"] | gfcm_lims["xmax"] < LONGITUDES
if (any(bad_coords)) {
warning("NAs produced because GPS coordinates out of GFCM defined ranges!")
res[bad_coords] <- NA_character_
}
return(res)
}
#------------------------------------------------------------------------------
# ICES statistical rectangles
ices_lims <- c("xmin" = -44, "ymin" = 36, "xmax" = 68.5, "ymax" = 85.5)
# rectangle labels for latitudes
ices_lat_breaks <- seq(ices_lims["ymin"], ices_lims["ymax"], .5)
ices_lat_labels <- sprintf("%02d", 1:99) # 1:(length(ices_lat_breaks) - 1) - 1
# rectangle labels for longitudes
ices_lon_breaks <- ices_lims["xmin"]:(ices_lims["xmax"] + .5) # because it’s 1° longitude
ices_col <- LETTERS[c(1:8, 10:13)] # from “A” to “M” without letter “I”
ices_lon_lab <- paste0(rep(ices_col, each = 10), rep(0:9, times = length(ices_col)))
ices_lon_labels <- ices_lon_lab[!ices_lon_lab %in% c("M9", paste0("A", 4:9))] # they are ommitted
#' @title identify ICES statistical subrectangles from GPS coordinates
#' @param subrect boolean; if TRUE return subrectangle
identify_ices_rect <- function(LONGITUDES, LATITUDES, subrect = FALSE) {
# rectangle identification
lat <- cut_bis(LATITUDES, breaks = ices_lat_breaks, labels = ices_lat_labels)
lon <- cut_bis(LONGITUDES, breaks = ices_lon_breaks, labels = ices_lon_labels)
res <- paste0(lat, lon)
# subrectangle identification -------------------------
if (isTRUE(subrect)) {
lat_mins <- retrieve_mins(LATITUDES)
lon_mins <- retrieve_mins(LONGITUDES)
# segmenting latitudes every 10’, labels is chosen to easily calculate subrect
lat_seg <- as.integer(cut_bis(lat_mins, breaks = seq(0, 60, 10), labels = rep(1:3, times = 2)))
# segmenting longitudes every 20’, labels is chosen to easily calculate subrect
lon_seg <- as.integer(cut_bis(lon_mins, breaks = seq(0, 60, 20), labels = 0:2))
# change hemisphere (western to eastern)
lon_seg_bis <- ifelse(LONGITUDES < 0, 2L - lon_seg, lon_seg)
sub_rect <- lat_seg + 3L * lon_seg_bis
res <- paste0(res, sub_rect)
}
# finish ----------------------------------------------
bad_coords <-
LATITUDES < ices_lims["ymin"] | ices_lims["ymax"] < LATITUDES |
LONGITUDES < ices_lims["xmin"] | ices_lims["xmax"] < LONGITUDES
if (any(bad_coords)) {
warning("NAs produced because GPS coordinates out of ICES defined ranges!")
res[bad_coords] <- NA_character_
}
return(res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment