R/dwd_stations_shp.R

#' @title Save DWD stations as shape file
#'
#' @description This function allows you to save the location of all stations of a dwd data tibble as shape file (with +proj=longlat, +datum=WGS84).
#'
#' @param dwd_data a tibble containing DWD observation data with station information.
#' @param out_dir directory of folder where created shape file shall be saved.
#'
#' @import rgdal
#' @import sp
#'
#' @export
#'
#' @examples
#' # filter flexibly using dplyr package
#' rbu_data <- dplyr::filter(rbu_data,stat_id%in%c(11162,11292,11295))
#'
#' # plot location of selected stations within Germany
#' dwd_stations_plot(rbu_data)
#'
#' # save selected stations as shape-file
#' dwd_stations_shp(rbu_data,my_dir)
#'

dwd_stations_shp <- function(dwd_data,out_dir){

  # filter only one entry per station
  dwd_data <- dplyr::distinct(dwd_data,stat_id,.keep_all=TRUE)

  # select only station-relevant columns
  dwd_data <- dplyr::select(dwd_data,stat_id,stat_name,alt,lsc_gr_id,lsc_gr,lsc,close_date,state,lon,lat)

  ## convert tibble to shape file

  # define coordinates
  sp::coordinates(dwd_data)<-~lon+lat

  # set projection
  sp::proj4string(dwd_data) <- sp::CRS("+proj=longlat +datum=WGS84")

  ## write shape file
  writeOGR(dwd_data,out_dir,"dwd_stations",driver="ESRI Shapefile",encoding="UTF-8")

}
malinfischer/phenoTS documentation built on May 14, 2019, 12:56 a.m.