Подтвердить что ты не робот

Преобразование координат широты и долготы в название страны в R

У меня есть список координат широты и долготы, и я хочу узнать, в какой стране они все проживают.

Я изменил ответ на этот вопрос о lat-long для штатов США и имею рабочую функцию, но я столкнулся с проблемой, что карта worldHires (из пакета mapdata) ужасно устарела и содержит много устаревших стран, таких как как Югославия и СССР.

Как мне изменить эту функцию, чтобы использовать более современный пакет, такой как rworldmap? Мне до сих пор удалось расстроить себя...

library(sp)
library(maps)
library(rgeos)
library(maptools)

# The single argument to this function, points, is a data.frame in which:
#   - column 1 contains the longitude in degrees
#   - column 2 contains the latitude in degrees
coords2country = function(points)
{
    # prepare a SpatialPolygons object with one poly per country
    countries = map('worldHires', fill=TRUE, col="transparent", plot=FALSE)
    names = sapply(strsplit(countries$names, ":"), function(x) x[1])

    # clean up polygons that are out of bounds
    filter = countries$x < -180 & !is.na(countries$x)
    countries$x[filter] = -180

    filter = countries$x > 180 & !is.na(countries$x)
    countries$x[filter] = 180

    countriesSP = map2SpatialPolygons(countries, IDs=ids, proj4string=CRS("+proj=longlat +datum=wgs84"))


    # convert our list of points to a SpatialPoints object
    pointsSP = SpatialPoints(points, proj4string=CRS("+proj=longlat +datum=wgs84"))


    # use 'over' to get indices of the Polygons object containing each point 
    indices = over(pointsSP, countriesSP)


    # Return the state names of the Polygons object containing each point
    myNames = sapply([email protected], function(x) [email protected])
    myNames[indices]
}

##
## this works... but it has obsolete countries in it
## 

# set up some points to test
points = data.frame(lon=c(0, 5, 10, 15, 20), lat=c(51.5, 50, 48.5, 47, 44.5))

# plot them on a map
map("worldHires", xlim=c(-10, 30), ylim=c(30, 60))
points(points$lon, points$lat, col="red")

# get a list of country names
coords2country(points)
# returns [1] "UK"         "Belgium"    "Germany"    "Austria"    "Yugoslavia"
# number 5 should probably be in Serbia...
4b9b3361

Ответ 1

Спасибо за тщательно оформленный вопрос. Для того, чтобы использовать rworldmap (содержащий обновленные страны), требуется всего несколько изменений в строке, как показано ниже. Я не эксперт по CRS, но я не думаю, что изменение, которое мне пришлось внести в proj4string, имеет значение. Другие могут прокомментировать это.

Это сработало для меня и дало:

> coords2country(points)
[1] United Kingdom     Belgium            Germany            Austria           
[5] Republic of Serbia

Всего наилучшего, Энди

library(sp)
library(rworldmap)

# The single argument to this function, points, is a data.frame in which:
#   - column 1 contains the longitude in degrees
#   - column 2 contains the latitude in degrees
coords2country = function(points)
{  
  countriesSP <- getMap(resolution='low')
  #countriesSP <- getMap(resolution='high') #you could use high res map from rworldxtra if you were concerned about detail

  # convert our list of points to a SpatialPoints object

  # pointsSP = SpatialPoints(points, proj4string=CRS(" +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0"))

  #setting CRS directly to that from rworldmap
  pointsSP = SpatialPoints(points, proj4string=CRS(proj4string(countriesSP)))  


  # use 'over' to get indices of the Polygons object containing each point 
  indices = over(pointsSP, countriesSP)

  # return the ADMIN names of each country
  indices$ADMIN  
  #indices$ISO3 # returns the ISO3 code 
  #indices$continent   # returns the continent (6 continent model)
  #indices$REGION   # returns the continent (7 continent model)
}

Ответ 2

Вы можете использовать мой пакет geonames для поиска из службы http://geonames.org/:

> GNcountryCode(51.5,0)
$languages
[1] "en-GB,cy-GB,gd"

$distance
[1] "0"

$countryName
[1] "United Kingdom of Great Britain and Northern Ireland"

$countryCode
[1] "GB"

> GNcountryCode(44.5,20)
$languages
[1] "sr,hu,bs,rom"

$distance
[1] "0"

$countryName
[1] "Serbia"

$countryCode
[1] "RS"

Получите это от r-forge, потому что я не уверен, что я потрудился выпустить его в CRAN:

https://r-forge.r-project.org/projects/geonames/

Да, это зависит от внешней службы, но, по крайней мере, она знает, что случилось с коммунизмом... :)