【问题标题】:Get country (and continent) from longitude and latitude point in R从R中的经度和纬度点获取国家(和大陆)
【发布时间】:2014-02-11 17:13:18
【问题描述】:

作为a question I posted yesterday 的后续行动,R 中是否有包/功能可以为我提供由经度和纬度定义的点所在的国家(和大陆)?所做的事情here in MatLab.

数据框看起来像这样......

Point_Name              Longitude   Latitude
University of Arkansas  36.067832   -94.173655
Lehigh University       40.601458   -75.360063
Harvard University      42.379393   -71.115897

我想输出上面添加了国家和大陆列的数据框。 作为额外的奖励,对于美国境内的人(以及对于美国以外的人)的美国州列?

【问题讨论】:

  • 看看this question,它是从本页右侧“相关”下的问题列表中挑选出来的。
  • This answer 会告诉你状态。
  • 谢谢,我不知道我是怎么错过那些的(我看了很多但没有找到那些)-一旦我根据旧答案让脚本工作,我将删除我的 Q。
  • (目前国家和州一般都在工作 - 有些人在国家/地区提出 NA,并试图对大陆进行排序)

标签: r maps


【解决方案1】:

要获取大陆,您可以使用 rworldmap 从此 answer 修改 coords2country 函数的最后一行,以创建一个 coords2continent 函数,如下所示。选择您想要 6 大洲还是 7 大洲模型。我会考虑将这段代码放入rworldmap

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
coords2continent = function(points)
{  
  countriesSP <- getMap(resolution='low')
  #countriesSP <- getMap(resolution='high') #you could use high res map from rworldxtra if you were concerned about detail

  # converting points to a SpatialPoints object
  # 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)

  #indices$continent   # returns the continent (6 continent model)
  indices$REGION   # returns the continent (7 continent model)
  #indices$ADMIN  #returns country name
  #indices$ISO3 # returns the ISO3 code 
}

这是一个测试。

points = data.frame(lon=c(0, 90, -45, -100, 130), lat=c(52, 40, -10, 45, -30 ))

coords2continent(points)
#[1] Europe        Asia          South America North America Australia  
coords2country(points)
#[1] United Kingdom  China   Brazil   United States of America  Australia

【讨论】:

  • 大陆怎么好像有点不对劲?就像墨西哥和中美洲是南美洲的一部分(而不是北美),整个俄罗斯都是欧洲的一部分,而其中一些应该在亚洲?
  • 不错的代码,我唯一的问题是我想将落入海洋的点分配给最近的大陆,因为目前结果是 NA。
【解决方案2】:

所以这是在 Google 上使用 reverse geocoding API 的替代方案。此代码部分基于this reference

df上方调用您的数据框,

reverseGeoCode <- function(latlng) {
  require("XML")
  require("httr")
  latlng    <- as.numeric(latlng)
  latlngStr <- gsub(' ','%20', paste(round(latlng,2), collapse=","))
  url   <- "http://maps.google.com"
  path  <- "/maps/api/geocode/xml"
  query <- list(sensor="false",latlng=latlngStr)
  response <- GET(url, path=path, query=query)
  if (response$status !=200) {
    print(paste("HTTP Error:",response$status),quote=F)
    return(c(NA,NA))
  }
  xml    <- xmlInternalTreeParse(content(response,type="text"))
  status <- xmlValue(getNodeSet(xml,"//status")[[1]])
  if (status != "OK"){
    print(paste("Query Failed:",status),quote=F)
    return(c(NA,NA))
  }
  xPath   <- '//result[1]/address_component[type="country"]/long_name[1]'
  country <- xmlValue(getNodeSet(xml,xPath)[[1]])
  xPath   <- '//result[1]/address_component[type="administrative_area_level_1"]/long_name[1]'
  state   <- xmlValue(getNodeSet(xml,xPath)[[1]])
  return(c(state=state,country=country))
}
st.cntry <- t(apply(df,1,function(x)reverseGeoCode(x[2:3])))
result   <- cbind(df,st.cntry)
result
#               Point_Name Longitude  Latitude         state       country
# 1 University of Arkansas  36.06783 -94.17365      Arkansas United States
# 2      Lehigh University  40.60146 -75.36006  Pennsylvania United States
# 3     Harvard University  42.37939 -71.11590 Massachusetts United States

在 API 定义中,“administrative_area_level_1”是国家以下的最高行政区域。在美国,这些是州。在其他国家/地区,定义各不相同(例如,可能是省份)。

顺便说一句,我相当肯定你的经纬度颠倒了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 2016-10-10
    • 2011-05-28
    • 1970-01-01
    • 2017-02-22
    • 2012-12-04
    相关资源
    最近更新 更多