R/PointToPolygon.R

Defines functions PointToPolygon

#' point to geo-polygon
#'
#' point to geo-polygon
#' @param lon Numeric vector. Longitude. DD(Decimal Degrees), not DMS(Degree Minute Second).
#' @param lat Numeric vector. Latitude. DD(Decimal Degrees), not DMS(Degree Minute Second).
#' @param r Numeric. Radius of polygons. unit is meters. default is 150.
#' @param n Numeric. The number of eddges of polygons. default is 12
#' @return data.frame. Polygon coordinate vector matrix.
#' @export
#' @examples
#' lon <- 127
#' lat <- 35
#' poly <- PointToPolygon(lon, lat)

PointToPolygon <- function(lon, lat, r = 150, n = 12){
  
  ## lon 1 degree = 1 nautical miles = 1,852m
  ## 
  
  if(!is.numeric(c(lon, lat, r, "1"))) print("Arguments must be numeric!")
  
  coord_list <- vector(mode = "list", length = length(lon))
  
  kk <- 1/60/1852
  angle <- (360/n)*(c(1:n)-1)
  
  for (j in 1:length(coord_list)) {
    
    pll <- matrix(NA, nrow = n, ncol = 2)
    
    pll[, 1] <- lon[j] + kk*r*cos(angle*pi/180)/cos(lat[j]*pi/180)
    pll[, 2] <- lat[j] + kk*r*sin(angle*pi/180)
    
    coord_list[[j]] <- pll
    
  }
  
  return(coord_list)
  
}
Gi-Seop/ODA documentation built on Jan. 6, 2020, 12:49 p.m.