R/buffer_pts.R

Defines functions sequences_within_buffer buffer_pts_within buffer_pts coords2SpatialPoints

Documented in buffer_pts buffer_pts_within coords2SpatialPoints

library(sp)
library(raster)
library(rgeos)

#' convert a list of lat/lon coordinates into SptialPoints
#' @export
coords2SpatialPoints <- function(latitude,longitude) {
  coords <- data.frame(lon = longitude,lat = latitude)
  coordsSp <- sp::SpatialPoints(coords = coords, proj4string = sp::CRS("+init=epsg:4326"))
  return(coordsSp)
}



#' function to get buffer spatial polygon around RLS point
#' coordsSP spatialpoints object in epsg:3347 projection of RLS points
#' bufferDistance in km
#' coordsBoldSeqSp spatialpoints oject in epsg:3347 projection of RLS points buffer
#' @export
buffer_pts <- function(latitude, longitude, bufferDistance, projection="+init=epsg:3347") {
  coordsSp <- coords2SpatialPoints(latitude,longitude)
  ## projection
  coordsSpp <- sp::spTransform( coordsSp, sp::CRS( projection ) )
  buff_distance = bufferDistance*1000
  coordsBuffer <- rgeos::gBuffer(coordsSpp, width=buff_distance, byid=TRUE )
  return(coordsBuffer)
}

#' BOLD spatial points within buffer of RLS spatial points
#' @export
buffer_pts_within <- function(coordsBuffer, boldSp) {
  boldRls.intersct <- rgeos::gIntersects(coordsBuffer , boldSp, byid = TRUE)
  return(boldRls.intersct)
}

#' @export
sequences_within_buffer <- function(latitude, longitude, boldSp, bufferDistance, projection="+init=epsg:3347") {
  coordsBuffer <- buffer_pts(latitude, longitude, bufferDistance, projection)
  bold.intersct <- buffer_pts_within(coordsBuffer, boldSp)
  return(bold.intersct)
}
Grelot/geogendivr documentation built on Sept. 3, 2020, 6:25 p.m.