如何使用经度和纬度找到最近的机场站?
例如我在我的数据库中有这个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
,因此我将通过以下网址向 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
而不是 EGTE
或EGDR
- 因为Plympton 更接近到普利茅斯?
那么我可以在我的数据库中使用下面的lat和lng来确定哪个电台是最近的吗?
"lat" : "50.38693379999999",
"lng" : "-4.0598999999999705"
那我怎么知道上面的lat和lng 去 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"
答案 0 :(得分:2)
如果您已有数据,请说
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
来计算loc
与df
的每次观察之间的距离(默认为米):
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