R/mark_inside_polygon.R

Defines functions mark_inside_polygon

Documented in mark_inside_polygon

mark_inside_polygon <- function(
  tracks, polygon, bind = TRUE, drop = TRUE, cname = "in_polygon"
) {

  # Check variables
  e <- val_psyo(tracks, 0, 0, 0, 2, 2); if (e != "") {stop(e)}
  e <- val_cname(polygon, "lon", type = "numeric", def = FALSE); if (e != "")
    {stop(e)}
  e <- val_cname(polygon, "lat", type = "numeric", def = FALSE); if (e != "")
    {stop(e)}
  e <- val_var(bind, "logical"); if (e != "") {stop(e)}
  e <- val_var(drop, "logical"); if (e != "") {stop(e)}
  e <- val_var(cname, "character"); if (e != "") {stop(e)}

  # calculate list in polygon
  result <- sp::point.in.polygon(
    tracks$lon,
    tracks$lat,
    polygon[,c("lon")],
    polygon[,c("lat")]
  )

  # Reformat result
  result <- data.frame(result)
  colnames(result)[1] <- cname

  # Return result
  result <- bind_drop_private(tracks, result, bind, drop)
  return(result)

}

Try the psyosphere package in your browser

Any scripts or data that you put into this service are public.

psyosphere documentation built on July 2, 2020, 12:08 a.m.