正在检索 cities/countries 的 latitude/longitude 坐标(此后已更改名称)?

Retrieving latitude/longitude coordinates for cities/countries that have since changed names?

假设我有一个城市和国家的向量,其中可能包含也可能不包含后来更改名称的地名:

locations <- c("Paris, France", "Sarajevo, Yugoslavia", "Rome, Italy", "Leningrad, Soviet Union", "St Petersburg, Russia")

问题是我不能使用 ggmap::geocode 之类的东西,因为它似乎不适用于名称已更改的位置:

ggmap::geocode(locations, source = "dsk")

       lon      lat
1  2.34880 48.85341 #Works for Paris
2       NA       NA #Didn't work for Sarajevo
3 12.48390 41.89474 #Works for Rome
4 98.00000 60.00000 #Didn't work for the old name of St Petersburg  seems to just get the center of Russia
5 30.26417 59.89444 #Worked for St Petersburg

我可以使用其他功能吗?如果我必须 "update" 城市和国家的名称,有没有一种简单的方法可以解决这个问题?我有数百个要收集经度和纬度坐标的位置。

这可能不是您想要的,但是如果您使用仅包含城市名称(而不是国家/地区)的完全相同的代码,至少您提到的两个案例(萨拉热窝和列宁格勒)似乎是工作正常。您可以尝试 运行 使用修改后的 locations 向量(仅包含城市名称)的函数,看看是否仍然出现错误。像这样:

(cities <- gsub(',.*', '', locations))

## [1] "Paris"         "Sarajevo"      "Rome"          "Leningrad"     "St Petersburg"

cbind(ggmap::geocode(cities, source = 'dsk'), cities)

##        lon      lat        cities
## 1  2.34880 48.85341         Paris
## 2 18.35644 43.84864      Sarajevo
## 3 12.48390 41.89474          Rome
## 4 30.26417 59.89444     Leningrad
## 5 30.26417 59.89444 St Petersburg