Using an API to calculate distance between two airports (two columns) within R?

后端 未结 3 1690
轮回少年
轮回少年 2021-02-09 17:46

I was wondering whether there was a way to compare airport distances(IATA codes). There are some scripts but not is using R. So I tried that with with the API:

developer

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-09 18:27

    Here's another way with the httr package:

    library(httr)
    
    callAPI <- function(from, to) {
      res <- GET("https://airport.api.aero",
                 path = paste0("airport/distance/", from, "/", to),
                 query = list(user_key = "d805e84363494ca03b9b52d5a505c4d1"))
      stop_for_status(res)
      return(content(res, encoding = "UTF-8"))
    }
    
    
    test <- callAPI("DRS", "FKB")
    # test
    test$distance
    # [1] "484.6"
    
    for (i in 1:nrow(df)) {
      from = df[i, "departure"]
      to = df[i, "arrival"]
      df[i, "distance"] <- callAPI(from, to)$distance
    }
    
    #   departure arrival flyID distance
    # 1       DRS     FKB     1    484.6
    # 2       TXL     HER     2  2,131.8
    # 3       STR     BOJ     3  1,575.0
    # 4       DUS     FUE     4  3,066.3
    # 5       LEJ     PMI     5  1,512.4
    # 6       FKB     AYT     6  2,264.2
    # 7       LNZ     FUE     7  3,258.0
    

    If you want to get the full results, you could use:

    all_results <- mapply(function(x,y) { callAPI(x,y) }, df$departure, df$arrival)
    cbind(df, t(all_results))
    #   departure arrival flyID processingDurationMillis authorisedAPI success airline errorMessage distance units
    # 1       DRS     FKB     1                        0          TRUE    TRUE    NULL         NULL    484.6    km
    # 2       TXL     HER     2                        0          TRUE    TRUE    NULL         NULL  2,131.8    km
    # 3       STR     BOJ     3                        0          TRUE    TRUE    NULL         NULL  1,575.0    km
    # 4       DUS     FUE     4                        0          TRUE    TRUE    NULL         NULL  3,066.3    km
    # 5       LEJ     PMI     5                        0          TRUE    TRUE    NULL         NULL  1,512.4    km
    # 6       FKB     AYT     6                        0          TRUE    TRUE    NULL         NULL  2,264.2    km
    # 7       LNZ     FUE     7                        1          TRUE    TRUE    NULL         NULL  3,258.0    km
    

提交回复
热议问题