R/get_nearest_station.R

Defines functions get_nearest_station

Documented in get_nearest_station

NULL
#' Get the nearest stations to the preferd station.
#' @param id The ID of the desired station
#' @param lonlat logical. If TRUE, coordinates should be in degrees; 
#' else they should represent planar ('Euclidean') space (e.g. units of meters).
#' See \link[raster]{pointDistance} for details.
#' @param dt A data.table which contains 3 columns (i.e., ID, Lon, Lat).
#' @param ... Other parameters which passed to \link[raster]{pointDistance}
#' @return A vector with station IDs sorted by distance.
get_nearest_station <- function(id, 
                                lonlat = T, 
                                dt, ...){
  temp_dis <- pointDistance(dt[ID == id, .(Lon, Lat)], 
                            dt[, .(Lon, Lat)],
                            lonlat = T, ...)
  names(temp_dis) <- dt$ID
  sorted_dis <- sort(temp_dis)
  return(as.numeric(names(sorted_dis)))
}
hxfan1227/cmdcr documentation built on Sept. 13, 2020, 12:14 a.m.