R/legacy/sp_rgdal_maptools_spstat.R

Defines functions xy2xy get_all_shape xy2sp xy2ppp near_point_line_dist near_point_point_dist near_point_point_elie

#' xy to xy
#' @description usually for coordinate transformation. i.e. wgs84 to utm40
#' @param x
#' @param y
#' @param from_prj
#' @param to_prj
#'
#' @return
#' @export
#'
#' @examples
xy2xy <- function(x,y,from_prj='wgs84',to_prj="utm40"){
  spp <- xy2sp(x,y,from_prj='wgs84',to_prj="utm40")
  return(data.frame(spp@coords))
}

#' read shape files
#' @description read shapefile to compute covariates for the null set
#' @return
#' @export
#'
#' @examples
get_all_shape <- function(folder= "data/shp") {
  layers <- rgdal::ogrListLayers(folder)
  shps <- lapply(layers,rgdal::readOGR,dsn=folder)
  names(shps) <- layers
  return(shps)
}

#' xy2sp
#' @description takes x,y coords of the movement data usually in wgs84 crs and project them
#' to usually a planar (non-geographical) coordinate system and then create a spatstat::ppp object.
#' @param x
#' @param y
#' @param from_prj default wgs84
#' @param to_prj default utm40
#'
#' @return
#' @export
#'
#' @examples
xy2sp <- function(x,y,from_prj='wgs84',to_prj="utm40") {
  xy <- data.frame(x,y)
  xy.sp <- sp::SpatialPoints(xy, proj4string = sp::CRS(get_crs(from_prj)))
  if (from_prj != to_prj)
    xy.sp <- sp::spTransform(xy.sp, get_crs(to_prj))
  return(xy.sp)
}

#' xy2ppp
#' @description takes x,y coords of the movement data usually in wgs84 crs and project them
#' to usually a planar (non-geographical) coordinate system and then create a spatstat::ppp object.
#' @param x
#' @param y
#' @param from_prj default wgs84
#' @param to_prj default utm40
#'
#' @return
#' @export
#'
#' @examples
xy2ppp <- function(x,y,from_prj='wgs84',to_prj="utm40") {
  xy.sp_utm <- xy2sp(x,y,from_prj='wgs84',to_prj="utm40")
  ppp1 <- maptools::as.ppp.SpatialPoints(xy.sp_utm)
  return(ppp1)
}

#' near point line dist
#' @description  find nearest distance to lines for a each
#' @param spp
#' @param spl
#'
#' @return
#' @export
#'
#' @examples
near_point_line_dist <- function(spp,spl){
  #spatstat only works with project coordinates
  #create spatstat objects
  psp1 <- maptools::as.psp(spl)
  ppp1 <- maptools::as.ppp(spp)
  #compute distance
  distances <- spatstat::project2segment(ppp1, psp1)$d
  return(distances)
}

#' near point point dist
#' @description finds the nearest point to each point of a dataset and computes the distance
#' @param spp1
#' @param spp2
#'
#' @return
#' @export
#'
#' @examples
near_point_point_dist <- function(spp1,spp2){
  ppp1 <- maptools::as.ppp.SpatialPointsDataFrame(spp1)
  ppp2 <- maptools::as.ppp.SpatialPointsDataFrame(spp2)
  min_dist <- apply(spatstat::crossdist(ppp1, ppp2),1,min)
  return(min_dist)
}

#' near point point elie
#'
#' @param x1
#' @param y1
#' @param x2
#' @param y2
#'
#' @return a vector of distances for the first object
#' @export
#'
#' @examples
near_point_point_elie <- function(x1,y1,x2,y2) {
  c1 <- x1 + 1i * y1
  c2 <- x2 + 1i * y2
  getMinDistance <- function(x,y) {
    min(Mod(x-y))
  }
  ds <- sapply(c1, getMinDistance, y = c2)
  names(ds) <- NULL
  return(ds)
}
faridcher/futils documentation built on May 22, 2019, 12:42 p.m.