【发布时间】:2021-09-27 20:59:51
【问题描述】:
我想在 R 中使用 OpenStreetMap 获取列表中每个城市的 GPS 坐标。我的代码基于这个非常好的 example。
唯一的区别是我想将county name 添加到city name 和country code。下面是我的代码:
cities = data.frame(nom=c("Toulouse", "Paris", "Égletons", "Marseille",
"Clermont Ferrand"), region=c("Haute-Garonne", "Paris", "Corrèze",
"Bouches-du-Rhône", "Puy-de-Dôme"), pays=rep("FR",5))
locateCountry = function(nameCity, nameCounty, codeCountry) {
cleanCityName = gsub(' ', '%20', nameCity)
cleanCountyName = gsub(' ', '%20', nameCounty)
url = paste(
"http://nominatim.openstreetmap.org/search?city="
, cleanCityName
, "&county="
, cleanCountyName
, "&countrycodes="
, codeCountry
, "&limit=9&format=json"
, sep="")
resOSM = fromJSON(url)
if(length(resOSM) > 0) {
return(c(resOSM[[1]]$lon, resOSM[[1]]$lat))
} else return(rep(NA,2))
}
coords <- t(apply(cities, 1, function(aRow) locateCountry(aRow[1], aRow[2])))
不幸的是,我收到以下消息:
Error in paste("http://nominatim.openstreetmap.org/search?city=", cleanCityName, :
argument "codeCountry" is missing, with no default
我也在寻找一种方法来合并提取的坐标coords 和cities。
谁能帮忙?
【问题讨论】:
标签: r openstreetmap