get_meta.R 3.16 KB
Newer Older
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

#' get_short_meta_element

#' get_short_meta_element
#' @param frostObj frostObj
#' @param elements elements ids
#' @keywords frostr
#' @export
get_short_meta_element <- function(frostObj,elements) {

  if (length(elements)>0) {
     res <- data.frame(ID = character(0),CO_NAME=character(0))
     for ( i in 1:length(elements)) {
       tmp <- frostObj$timeSeries(elements=elements[i],format='jsonld')
       if (!is.null(tmp)) {
         stnr <-gsub(":.*","",tmp[,"sourceId"])
         df <- data.frame(ID = stnr,CO_NAME = tmp[,"elementId"])
         res <- rbind(res,df)
       }
     }
  } else {
    res <- NULL
  }
  return(unique(res))
}

#' get_short_meta_station

#' get_short_meta_station
#' @param frostObj frostObj
#' @param stations station ids
#' @param west west
#' @param south south
#' @param east east
#' @param north north
#' @keywords frostr
#' @export
get_short_meta_station <- function(frostObj,stations=NULL,west=NULL,south=NULL,east=NULL,north=NULL) {

  if (!is.null(stations)) {
    res <- data.frame(ALT= numeric(0), LON= numeric(0), NAME = character(0), ID = character(0))
    for ( i in 1:length(stations)) {
      tmp <- frostObj$getSources(ids=stations[i],format='jsonld')
      if(!is.null(tmp)) {
        df <- short_meta_df(tmp=tmp,indice=1)
        res <- rbind(res,df)
      }
    }
  } else if ((!is.null(west)) && (!is.null(south)) && (!is.null(east)) && (!is.null(north))){
      geom <- glue::glue('POLYGON(({west} {south},{west} {north},{east} {north},{west} {south}))')
      tmp <- frostObj$getSources(type="SensorSystem",geometry=geom,format='jsonld')
      if(!is.null(tmp)) {
       nbs <- tmp$totalItemCount
       res <- data.frame(ALT= numeric(0), LON= numeric(0), NAME = character(0), ID = character(0))
       for ( i in 1:nbs) {
         df <- short_meta_df(tmp=tmp,indice=i)
         res <- rbind(res,df)
       }
jml's avatar
jml committed
59
60
      } else {
          res <- NULL
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
      }


  } else {
      res <- NULL
  }
  return(res)

}

#' get_short_meta_station_with_element

#' get_short_meta_station_with_element
#' @param frostObj frostObj
#' @param stations station ids
#' @param west west
#' @param south south
#' @param east east
#' @param north north
#' @param elements elements
#' @keywords frostr
#' @export
get_short_meta_station_with_element <- function(frostObj,stations=NULL,west=NULL,south=NULL,east=NULL,north=NULL,elements) {

  ele_meta <- get_short_meta_element(frostObj=frostObj,elements=elements)
  stn_meta <- get_short_meta_station(frostObj=frostObj,stations=stations,west=west,south=south,east=east,north=north)
  res <- dplyr::inner_join(stn_meta,ele_meta)
  return(res)

}

short_meta_df <- function(tmp,indice){
  df <- data.frame(ALT  = ifelse(is.null(tmp$data[[indice]]$masl),NA,tmp$data[[indice]]$masl),
                   LAT  = ifelse(is.null(tmp$data[[indice]]$geometry$coordinates[[2]]),NA,tmp$data[[indice]]$geometry$coordinates[[2]]),
                   LON  = ifelse(is.null(tmp$data[[indice]]$geometry$coordinates[[1]]),NA,tmp$data[[indice]]$geometry$coordinates[[1]]),
                   NAME = ifelse(is.null(tmp$data[[indice]]$name),NA,tmp$data[[indice]]$name),
                   ID   = ifelse(is.null(tmp$data[[indice]]$id),NA,tmp$data[[indice]]$id))
  return(df)
}