R + Weather Underground - 如何使用经度和纬度获得最近的机场站?

R + Weather Underground - how to use longitude and latitude to get the closest airport station?

如何使用经度和纬度找到最近的机场车站?

例如,我的数据库中有这个 json 数据存储,

 "location" : {
    "long" : "Devon, 8 Market Road, Plympton, Plymouth PL7 1QW, United Kingdom",
    "street_number" : "",
    "route" : "Market Road",
    "locality" : "Plymouth",
    "administrative_area_level_1" : "England",
    "country" : "United Kingdom",
    "postal_code" : "PL7 1QW",
    "lat" : "50.38693379999999",
    "lng" : "-4.0598999999999705"
}

而且我知道我所在的地区是 Plymouth,所以我将通过下面的 URL 从 Weather Underground 请求站点数据:

http://api.wunderground.com/api/[MY-API-CODE]/geolookup/conditions/q/UK/Plymouth.json

这是我的做法:

locality <- 'Plymouth'

pullUrl <- paste(apiUrl, 'geolookup/conditions/q/UK/', locality, '.json', sep='')

# Reading in as raw lines from the web service.
conn <- url(pullUrl)
rawData <- readLines(conn, n=-1L, ok=TRUE)

# Convert to a JSON.
geoData <- fromJSON(paste(rawData, collapse=""))

# Get the station data in location only.
# Turn the result into a data frame.
stationsDF <- as.data.frame(geoData$location$nearby_weather_stations$airport$station)

所以我得到以下 3 个站:

      city state        country icao         lat         lon
1 Plymouth       United Kingdom EGDB 50.35491562 -4.12105608
2   Exeter                   UK EGTE 50.73714066 -3.40577006
3 Culdrose                   UK EGDR 50.08427429 -5.25711393

但我的问题是如何确保我会得到 EGDB 而不是 EGTEEGDR - 因为 Plympton 是 离普利茅斯更近

那么我可以在我的数据库中使用下面的纬度和经度来确定哪个站是最近的吗?

"lat" : "50.38693379999999",
"lng" : "-4.0598999999999705"

那么我怎么知道上面的纬度和经度应该EGDB 50.35491562 -4.12105608

有什么想法吗?

编辑:

stationsDF <- as.data.frame(geoData$location$nearby_weather_stations$airport$station, stringsAsFactors=FALSE)
df <- setDT(stationsDF)
loc <- c(lat = "50.38693379999999", lng = "-4.0598999999999705")
dists <- geosphere::distHaversine(as.numeric(loc[c('lng', 'lat')]), df[, c('lon', 'lat')])

错误:

Error in .pointsToMatrix(p2) * toRad : 
  non-numeric argument to binary operator
In addition: Warning message:
In .pointsToMatrix(p2) : NAs introduced by coercion

编辑 2:

stationsDF <- as.data.frame(geoData$location$nearby_weather_stations$airport$station, stringsAsFactors=FALSE)
dput(stationsDF)

输出:

structure(list(city = c("Plymouth", "Exeter", "Culdrose"), state = c("", 
"", ""), country = c("United Kingdom", "UK", "UK"), icao = c("EGDB", 
"EGTE", "EGDR"), lat = c("50.35491562", "50.73714066", "50.08427429"
), lon = c("-4.12105608", "-3.40577006", "-5.25711393")), .Names = c("city", 
"state", "country", "icao", "lat", "lon"), class = "data.frame", row.names = c(NA, 
-3L))

编辑 3:

同时:

str(stationsDF)

输出:

'data.frame':   3 obs. of  6 variables:
 $ city   : chr  "Plymouth" "Exeter" "Culdrose"
 $ state  : chr  "" "" ""
 $ country: chr  "United Kingdom" "UK" "UK"
 $ icao   : chr  "EGDB" "EGTE" "EGDR"
 $ lat    : chr  "50.35491562" "50.73714066" "50.08427429"
 $ lon    : chr  "-4.12105608" "-3.40577006" "-5.25711393"

如果您已经有了数据,请说

df <- read.table(text = 'city state        country icao         lat         lon
                   1 Plymouth     "United Kingdom" EGDB 50.35491562 -4.12105608
                   2   Exeter                   UK EGTE 50.73714066 -3.40577006
                   3 Culdrose                   UK EGDR 50.08427429 -5.25711393', head = T)

loc <- c(lat = "50.38693379999999", lng = "-4.0598999999999705")

然后你可以使用geosphere::distHaversine来计算locdf的每个观察值之间的距离(默认情况下以米为单位):

dists <- geosphere::distHaversine(as.numeric(loc[c('lng', 'lat')]), df[, c('lon', 'lat')])

dists
## [1]  5617.667 60493.398 91661.079

有了which.min,你可以索引df给你一个结果:

df[which.min(dists), ]
##   city    state        country icao      lat       lon
## 1    1 Plymouth United Kingdom EGDB 50.35492 -4.121056