From 7b43ca6b5f9bb10179fd7560d0f9b0a0cb0d7a42 Mon Sep 17 00:00:00 2001 From: SPINIELLI Enrico Date: Fri, 29 Mar 2024 09:59:11 +0100 Subject: [PATCH] added OurAirport data; improved docs --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/db.R | 21 ++++-- R/other.R | 94 +++++++++++++++++++++++++++ R/trajectories.R | 71 +++++++++++++++++--- man/airlines_tbl.Rd | 10 ++- man/airports_oa.Rd | 19 ++++++ man/airspace_profile_tbl.Rd | 15 ++++- man/airspace_profiles_tidy.Rd | 13 +++- man/flights_airspace_profiles_tidy.Rd | 13 +++- man/flights_tbl.Rd | 11 +++- man/point_profile_tbl.Rd | 11 +++- man/point_profiles_tidy.Rd | 19 ++++-- 13 files changed, 273 insertions(+), 27 deletions(-) create mode 100644 R/other.R create mode 100644 man/airports_oa.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 4d03ad8..17a2dca 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: eurocontrol Title: Helper functions for EUROCONTROL useRs -Version: 0.1.3 +Version: 0.1.5 Authors@R: c( person("Enrico", "Spinielli", , "enrico.spinielli@eurocontrol.int", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-8584-9131")), diff --git a/NAMESPACE b/NAMESPACE index 8f4cdc4..d0f1a40 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ export(":=") export(.data) export(airlines_tbl) export(airlines_tidy) +export(airports_oa) export(airspace_profile_tbl) export(airspace_profiles_tidy) export(aodf_tbl) diff --git a/R/db.R b/R/db.R index 4abebc5..fbe0a3f 100644 --- a/R/db.R +++ b/R/db.R @@ -66,8 +66,14 @@ db_connection <- function(schema = "PRU_PROD") { #' arl <- airlines_tbl() #' # other operations on arl, i.e. filtering, #' # followed by a collect() to retrieve the concrete data frame -#' arl_filtered <- arl |> dplyr::filter(AO_ISO_CTRY_CODE == "IT") |> collect() -#' # IMPORTANT: close the DB connection +#' arl_filtered <- arl |> +#' dplyr::filter(AO_ISO_CTRY_CODE == "IT") |> +#' collect() +#' +#' # NOTE: you can reuse the connection for other API calls +#' arl$src$con +#' +#' # IMPORTANT: at the end close the DB connection #' DBI::dbDisconnect(arl$src$con) #' } airlines_tbl <- function(conn = NULL) { @@ -98,9 +104,16 @@ airlines_tbl <- function(conn = NULL) { #' @examples #' \dontrun{ #' flt <- flights_tbl() -#' # other operations on arl, i.e. filtering, +#' # other operations on flt, i.e. filtering, #' # followed by a collect() to retrieve the concrete data frame -#' flt_filtered <- flt |> dplyr::filter(AO_ISO_CTRY_CODE == "IT") |> collect() +#' flt_filtered <- flt |> +#' filter(TO_DATE("2023-06-01 10:00", "YYYY-MM-DD HH24:MI") <= IOBT, +#' IOBT < TO_DATE("2023-06-02 10:30", "YYYY-MM-DD HH24:MI")) |> +#' +#' +#' # NOTE: you can reuse the connection for other API calls +#' arl$src$con +#' #' # IMPORTANT: close the DB connection #' DBI::dbDisconnect(flt$src$con) #' } diff --git a/R/other.R b/R/other.R new file mode 100644 index 0000000..8cbb4ac --- /dev/null +++ b/R/other.R @@ -0,0 +1,94 @@ +#' retrieve latest airport list from OurAirports +#' +#' @return a data frame +#' @export +#' +#' @examples +#' \dontrun{ +#' apts <- airports_oa() +#' } +airports_oa <- function() { + oa_url <- "https://raw.githubusercontent.com/davidmegginson/ourairports-data/main/airports.csv" + oa <- readr::read_csv(oa_url) |> + dplyr::mutate( + gps_code = stringr::str_trim(.data$gps_code), + ident = stringr::str_trim(.data$ident)) |> + dplyr::mutate(icao = dplyr::if_else(.data$ident != .data$gps_code, + .data$gps_code, + .data$ident)) |> + dplyr::filter( + # keep the closed ones...EDDT is closed after 2019! + !.data$type %in% c("closed"), + stringr::str_detect(.data$gps_code, "^[A-Z]{4}"), + .data$icao != "EDDT") |> + dplyr::select( + "icao", + "iata" = "iata_code", + "latitude" = "latitude_deg", + "longitude" = "longitude_deg", + "elevation" = "elevation_ft", + "type", + "name", + "iso_country", + "iso_region", + "continent", + NULL + ) |> + dplyr::union( + # from https://metar-taf.com/airport/ENFB-statfjord-b + dplyr::tribble( + # , + ~icao, ~iata, ~longitude, ~latitude, ~elevation, ~type, ~name, ~iso_country, ~iso_region, ~continent, + "EHFO", "", 4.82722, 54.21583, 400.0, "heliport", "F15-A", "NL", "", "EU", + "EHGO", "", 5.43472, 54.16944, 400.0, "heliport", "G14-B", "NL", "", "EU", + "EHGQ", "", 5.43194, 54.04917, 400.0, "heliport", "G17D-A", "NL", "", "EU", + "EHGN", "", 5.49861, 54.22389, 400.0, "heliport", "G14-A", "NL", "", "EU", + "EHFT", "", 4.51278, 53.81806, 400.0, "heliport", "L5-D", "NL", "", "EU", + "EHFR", "", 4.35111, 53.81083, 400.0, "heliport", "L5-FA-1", "NL", "", "EU", + "EHFD", "", 4.69472, 54.85306, 400.0, "heliport", "F3-FB-1", "NL", "", "EU", + "EHFB", "", 4.5725, 54.94444, 400.0, "heliport", "F2-A", "NL", "", "EU", + "EHAX", "", 3.83167, 55.105, 400.0, "heliport", "A18-A", "NL", "", "EU", + "EHFQ", "", 4.49611, 53.96056, 400.0, "heliport", "L2-FA-1", "NL", "", "EU", + "EKAF", "", 3.99, 55.8, 400.0, "heliport", "A6A", "NL", "", "EU", + "ENUG", "", 22.25, 71.3, 400.0, "heliport", "Goliat", "NO", "", "EU", + "ENDR", "", 7.79167, 64.35556, 400.0, "heliport", "Draugen", "NO", "", "EU", + "ENFB", "", 1.82889, 61.20639, 400.0, "heliport", "Statfjord B", "NO", "", "EU", + "ENGA", "", 2.1875, 61.17556, 400.0, "heliport", "Gullfaks A", "NO", "", "EU", + "ENGC", "", 2.2709835,61.2140628, 400.0, "heliport", "Gullfaks C", "NO", "", "EU", + "ENHE", "", 7.31583, 65.32556, 400.0, "heliport", "Heidrun A", "NO", "", "EU", + "ENHM", "", 2.23333, 59.56667, 400.0, "heliport", "Heimdal", "NO", "", "EU", + "ENJS", "", 2.54556, 58.83528, 400.0, "heliport", "Johan Sverdrup", "NO", "", "EU", + "ENLE", "", 3.21667, 56.5333, 400.0, "heliport", "Ekofisk Oil Pltf", "NO", "", "EU", + "ENNE", "", 8.08333, 66.03333, 400.0, "heliport", "Norne A", "NO", "", "EU", + "ENQG", "", 2.1988320,61.2021082, 400.0, "heliport", "Gullfaks B", "NO", "", "EU", + "ENQJ", "", 3.895, 61.33167, 400.0, "heliport", "Gjoa", "NO", "", "EU", + "ENQK", "", 2.5, 61.08333, 400.0, "heliport", "Kvitebjorn", "NO", "", "EU", + "ENQL", "", 2.07333, 60.49833, 400.0, "heliport", "Martin Linge B", "NO", "", "EU", + "ENQM", "", 2.01472, 60.5075, 400.0, "heliport", "Martin Linge A", "NO", "", "EU", + "ENQW", "", 2.36694, 61.04, 400.0, "heliport", "Valemon", "NO", "", "EU", + "ENQR", "", 2.21667, 61.51667, 400.0, "heliport", "Snorre B", "NO", "", "EU", + "ENQS", "", 1.90083, 61.2961, 400.0, "heliport", "Statfjord C", "NO", "", "EU", + # little evidence of geo coordinates for ENUC + "ENUC", "", 6.9259, 65.0863, 400.0, "heliport", "Asgard C", "NO", "", "EU", + "ENQV", "", 2.45722, 61.36972, 400.0, "heliport", "Visund A", "NO", "", "EU", + "ENQW", "", 2.36694, 61.04, 400.0, "heliport", "Valemon", "NO", "", "EU", + "ENSE", "", 2.15, 61.45, 400.0, "heliport", "Snorre A", "NO", "", "EU", + "ENSF", "", 1.8521147,61.2551784, 400.0, "heliport", "Statfjord A", "NO", "", "EU", + "ENUA", "", 6.72556, 65.06417, 400.0, "heliport", "Kristin", "NO", "", "EU", + "ENUB", "", 6.78944, 65.11, 400.0, "heliport", "Aasgard B", "NO", "", "EU", + "ENUS", "", 7.65083, 65.69972, 400.0, "heliport", "Skarv", "NO", "", "EU", + "ENUK", "", 6.55056, 64.99417, 400.0, "heliport", "Aasgard A", "NO", "", "EU", + "ENUR", "", 7.36722, 65.34333, 400.0, "heliport", "Heidrun B", "NO", "", "EU", + "ENXW", "", 2.48667, 59.165, 400.0, "heliport", "Grane", "NO", "", "EU", + "ENWE", "", 2.24722, 58.8425, 400.0, "heliport", "Edvard Grieg", "NO", "", "EU", + "ENWG", "", 1.74333, 58.845, 400.0, "heliport", "Gudrun", "NO", "", "EU", + "ENWI", "", 2.19778, 58.92222, 400.0, "heliport", "Ivar Aasen", "NO", "", "EU", + "ENWK", "", 1.69528, 58.57139, 400.0, "heliport", "Gina Krog", "NO", "", "EU", + "ENWR", "", 1.73278, 58.58333, 400.0, "heliport", "Randgrid", "NO", "", "EU", + "ENWS", "", 3.26278, 56.37361, 400.0, "heliport", "Eldfisk S", "NO", "", "EU", + "ENWV", "", 3.4, 56.28333, 400.0, "heliport", "Valhall PH", "NO", "", "EU" + ) + ) + + +} diff --git a/R/trajectories.R b/R/trajectories.R index 0255887..581bfe1 100644 --- a/R/trajectories.R +++ b/R/trajectories.R @@ -16,7 +16,20 @@ #' #' @examples #' \dontrun{ -#' prf <- airspace_profile_tbl() +#' pp <- airspace_profile_tbl() +#' # other operations on pp, i.e. filtering, +#' # followed by a collect() to retrieve the concrete data frame +#' # IMPORTANT: close the DB connection when done +#' DBI::dbDisconnect(pp$src$con) +#' +#' # if you use a DB connection for many different APIs +#' conn <- eurocontrol::db_connection("PRU_DEV") +#' pp <- airspace_profile_tbl(conn = conn) +#' +#' # ... do something else with conn +#' # ... +#' # then manually close the connection to the DB +#' DBI::dbDisconnect(conn) #' } airspace_profile_tbl <- function(conn = NULL) { if (is.null(conn)) { @@ -81,7 +94,18 @@ airspace_profile_tbl <- function(conn = NULL) { #' #' @examples #' \dontrun{ -#' asp_profs <- airspace_profiles_tidy(wef = "2023-01-01", til = "2023-04-01") +#' ps <- airspace_profiles_tidy(wef = "2023-01-01", til = "2023-04-01") +#' # IMPORTANT: always close the DB connection when done +#' DBI::dbDisconnect(ps$src$con) +#' +#' # if you re-use DB connections +#' conn <- eurocontrol::db_connection("PRU_DEV") +#' ps <- airspace_profiles_tidy(conn = conn) +#' +#' # ... do something else with conn +#' # ... +#' # then manually close the connection to the DB +#' DBI::dbDisconnect(conn) #' } airspace_profiles_tidy <- function(conn = NULL, wef, til, airspace = "FIR", profile = "CTFM") { # magic numbers: tables are indexed on LOBT, but LOBT is not precise to @@ -174,7 +198,18 @@ airspace_profiles_tidy <- function(conn = NULL, wef, til, airspace = "FIR", prof #' #' @examples #' \dontrun{ -#' asp_profs <- flights_airspace_profiles_tidy(wef = "2023-01-01", til = "2023-04-01") +#' aa <- flights_airspace_profiles_tidy(wef = "2023-01-01", til = "2023-04-01") +#' +#' # if you re-use DB connections +#' conn <- eurocontrol::db_connection("PRU_DEV") +#' flights_airspace_profiles_tidy(conn = conn, +#' wef = "2023-01-01", +#' til = "2023-04-01") +#' +#' # ... do something else with conn +#' # ... +#' # then manually close the connection to the DB +#' DBI::dbDisconnect(conn) #' } flights_airspace_profiles_tidy <- function(conn = NULL, wef, til, airspace = "FIR", profile = "CTFM") { # magic numbers: tables are indexed on LOBT, but LOBT is not precise to @@ -229,7 +264,16 @@ flights_airspace_profiles_tidy <- function(conn = NULL, wef, til, airspace = "FI #' #' @examples #' \dontrun{ -#' prf <- point_profile_tbl() +#' pt <- point_profile_tbl() +#' +#' # if you re-use DB connections +#' conn <- eurocontrol::db_connection("PRU_DEV") +#' pt <- point_profile_tbl(conn = conn) +#' +#' # ... do something else with conn +#' # ... +#' # then manually close the connection to the DB +#' DBI::dbDisconnect(conn) #' } point_profile_tbl <- function(conn = NULL) { if (is.null(conn)) { @@ -285,22 +329,31 @@ point_profile_tbl <- function(conn = NULL) { #' @examples #' \dontrun{ #' # export 1 day of NM (planned) trajectories -#' point_profiles_tidy(wef = "2019-07-14", til = "2019-07-15", profile = "FTFM") +#' pf1 <- point_profiles_tidy(wef = "2019-07-14", +#' til = "2019-07-15", +#' profile = "FTFM") #' #' # export 2 hours of NM (flown) trajectories -#' point_profiles_tidy(wef = "2019-07-14 22:00", til = "2019-07-15") +#' pf2 <- point_profiles_tidy(wef = "2019-07-14 22:00", +#' til = "2019-07-15") #' #' # export 1 day of NM (flown) trajectories -#' point_profiles_tidy(wef = "2019-07-14", til = "2019-07-15", profile = "CTFM") +#' pf3 <- point_profiles_tidy(wef = "2019-07-14", +#' til = "2019-07-15", +#' profile = "CTFM") #' #' # export all CTFM trajectories within a bounding box 40 NM around EDDF #' bb <- c(xmin = 7.536746, xmax = 9.604390, ymin = 49.36732, ymax = 50.69920) -#' point_profiles_tidy(wef = "2019-01-01 00:00", til = "2019-01-02 00:00", bbox = bb) +#' pf4 <- point_profiles_tidy(wef = "2019-01-01 00:00", +#' til = "2019-01-02 00:00", +#' bbox = bb) #' #' #' # if you re-use DB connections #' conn <- eurocontrol::db_connection("PRU_DEV") -#' point_profiles_tidy(conn = conn, "2020-01-01") +#' pf <- point_profiles_tidy(conn = conn, +#' wef = "2020-01-01", +#' til = "2020-01-10") #' #' # ... do something else with conn #' # ... diff --git a/man/airlines_tbl.Rd b/man/airlines_tbl.Rd index 6b76465..b8cabb1 100644 --- a/man/airlines_tbl.Rd +++ b/man/airlines_tbl.Rd @@ -28,8 +28,14 @@ default which uses PRU_DEV to establish a \code{\link[=db_connection]{db_connect arl <- airlines_tbl() # other operations on arl, i.e. filtering, # followed by a collect() to retrieve the concrete data frame -arl_filtered <- arl |> dplyr::filter(AO_ISO_CTRY_CODE == "IT") |> collect() -# IMPORTANT: close the DB connection +arl_filtered <- arl |> + dplyr::filter(AO_ISO_CTRY_CODE == "IT") |> + collect() + +# NOTE: you can reuse the connection for other API calls +arl$src$con + +# IMPORTANT: at the end close the DB connection DBI::dbDisconnect(arl$src$con) } } diff --git a/man/airports_oa.Rd b/man/airports_oa.Rd new file mode 100644 index 0000000..db7fcb6 --- /dev/null +++ b/man/airports_oa.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/other.R +\name{airports_oa} +\alias{airports_oa} +\title{retrieve latest airport list from OurAirports} +\usage{ +airports_oa() +} +\value{ +a data frame +} +\description{ +retrieve latest airport list from OurAirports +} +\examples{ +\dontrun{ +apts <- airports_oa() +} +} diff --git a/man/airspace_profile_tbl.Rd b/man/airspace_profile_tbl.Rd index e2bec72..db204aa 100644 --- a/man/airspace_profile_tbl.Rd +++ b/man/airspace_profile_tbl.Rd @@ -24,6 +24,19 @@ go with the default which uses PRU_DEV to establish a \code{\link[=db_connection \examples{ \dontrun{ -prf <- airspace_profile_tbl() +pp <- airspace_profile_tbl() +# other operations on pp, i.e. filtering, +# followed by a collect() to retrieve the concrete data frame +# IMPORTANT: close the DB connection when done +DBI::dbDisconnect(pp$src$con) + +# if you use a DB connection for many different APIs +conn <- eurocontrol::db_connection("PRU_DEV") +pp <- airspace_profile_tbl(conn = conn) + +# ... do something else with conn +# ... +# then manually close the connection to the DB +DBI::dbDisconnect(conn) } } diff --git a/man/airspace_profiles_tidy.Rd b/man/airspace_profiles_tidy.Rd index 62d5e24..df6983d 100644 --- a/man/airspace_profiles_tidy.Rd +++ b/man/airspace_profiles_tidy.Rd @@ -74,6 +74,17 @@ PRU_DEV to establish a \code{\link[=db_connection]{db_connection()}}. \examples{ \dontrun{ -asp_profs <- airspace_profiles_tidy(wef = "2023-01-01", til = "2023-04-01") +ps <- airspace_profiles_tidy(wef = "2023-01-01", til = "2023-04-01") +# IMPORTANT: always close the DB connection when done +DBI::dbDisconnect(ps$src$con) + +# if you re-use DB connections +conn <- eurocontrol::db_connection("PRU_DEV") +ps <- airspace_profiles_tidy(conn = conn) + +# ... do something else with conn +# ... +# then manually close the connection to the DB +DBI::dbDisconnect(conn) } } diff --git a/man/flights_airspace_profiles_tidy.Rd b/man/flights_airspace_profiles_tidy.Rd index 77308b7..b33a6ad 100644 --- a/man/flights_airspace_profiles_tidy.Rd +++ b/man/flights_airspace_profiles_tidy.Rd @@ -58,6 +58,17 @@ PRU_DEV to establish a \code{\link[=db_connection]{db_connection()}}. \examples{ \dontrun{ -asp_profs <- flights_airspace_profiles_tidy(wef = "2023-01-01", til = "2023-04-01") +aa <- flights_airspace_profiles_tidy(wef = "2023-01-01", til = "2023-04-01") + +# if you re-use DB connections +conn <- eurocontrol::db_connection("PRU_DEV") +flights_airspace_profiles_tidy(conn = conn, + wef = "2023-01-01", + til = "2023-04-01") + +# ... do something else with conn +# ... +# then manually close the connection to the DB +DBI::dbDisconnect(conn) } } diff --git a/man/flights_tbl.Rd b/man/flights_tbl.Rd index c66048c..77683bb 100644 --- a/man/flights_tbl.Rd +++ b/man/flights_tbl.Rd @@ -25,9 +25,16 @@ go with the default which uses PRU_DEV to establish a \code{\link[=db_connection \examples{ \dontrun{ flt <- flights_tbl() -# other operations on arl, i.e. filtering, +# other operations on flt, i.e. filtering, # followed by a collect() to retrieve the concrete data frame -flt_filtered <- flt |> dplyr::filter(AO_ISO_CTRY_CODE == "IT") |> collect() +flt_filtered <- flt |> + filter(TO_DATE("2023-06-01 10:00", "YYYY-MM-DD HH24:MI") <= IOBT, + IOBT < TO_DATE("2023-06-02 10:30", "YYYY-MM-DD HH24:MI")) |> + + +# NOTE: you can reuse the connection for other API calls +arl$src$con + # IMPORTANT: close the DB connection DBI::dbDisconnect(flt$src$con) } diff --git a/man/point_profile_tbl.Rd b/man/point_profile_tbl.Rd index 44d8f98..0e544e9 100644 --- a/man/point_profile_tbl.Rd +++ b/man/point_profile_tbl.Rd @@ -25,6 +25,15 @@ PRU_DEV to establish a \code{\link[=db_connection]{db_connection()}}. \examples{ \dontrun{ -prf <- point_profile_tbl() +pt <- point_profile_tbl() + +# if you re-use DB connections +conn <- eurocontrol::db_connection("PRU_DEV") +pt <- point_profile_tbl(conn = conn) + +# ... do something else with conn +# ... +# then manually close the connection to the DB +DBI::dbDisconnect(conn) } } diff --git a/man/point_profiles_tidy.Rd b/man/point_profiles_tidy.Rd index 23e6c6b..39f7180 100644 --- a/man/point_profiles_tidy.Rd +++ b/man/point_profiles_tidy.Rd @@ -74,22 +74,31 @@ PRU_DEV to establish a \code{\link[=db_connection]{db_connection()}}. \examples{ \dontrun{ # export 1 day of NM (planned) trajectories -point_profiles_tidy(wef = "2019-07-14", til = "2019-07-15", profile = "FTFM") +pf1 <- point_profiles_tidy(wef = "2019-07-14", + til = "2019-07-15", + profile = "FTFM") # export 2 hours of NM (flown) trajectories -point_profiles_tidy(wef = "2019-07-14 22:00", til = "2019-07-15") +pf2 <- point_profiles_tidy(wef = "2019-07-14 22:00", + til = "2019-07-15") # export 1 day of NM (flown) trajectories -point_profiles_tidy(wef = "2019-07-14", til = "2019-07-15", profile = "CTFM") +pf3 <- point_profiles_tidy(wef = "2019-07-14", + til = "2019-07-15", + profile = "CTFM") # export all CTFM trajectories within a bounding box 40 NM around EDDF bb <- c(xmin = 7.536746, xmax = 9.604390, ymin = 49.36732, ymax = 50.69920) -point_profiles_tidy(wef = "2019-01-01 00:00", til = "2019-01-02 00:00", bbox = bb) +pf4 <- point_profiles_tidy(wef = "2019-01-01 00:00", + til = "2019-01-02 00:00", + bbox = bb) # if you re-use DB connections conn <- eurocontrol::db_connection("PRU_DEV") -point_profiles_tidy(conn = conn, "2020-01-01") +pf <- point_profiles_tidy(conn = conn, + wef = "2020-01-01", + til = "2020-01-10") # ... do something else with conn # ...