将经纬度坐标转换为 R 中的国家/地区名称

2023-12-19

我有一个纬度和经度坐标列表,并希望找出它们都居住在哪个国家/地区。

我修改了一个答案这个关于美国各州的问题 https://stackoverflow.com/questions/8751497/latitude-longitude-coordinates-to-state-code-in-r,并且有一个工作函数,但是我遇到了一个问题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(countriesSP@polygons, function(x) x@ID)
    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...

感谢您精心构造的问题。 只需要进行几行更改就可以使用 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)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将经纬度坐标转换为 R 中的国家/地区名称 的相关文章

  • 分组和计数以获得接近值

    我想计算每country的次数status is open以及次数status is closed 然后计算closerate per country Data customer lt c 1 2 3 4 5 6 7 8 9 country
  • 无法在 Document-Term-Matrix 中看到 `RTextTools::toLower()` 文本的结果

    我尝试创建一个矩阵 为此我想降低文本 为此 我使用此 R 指令 matrix create matrix tweets 1 toLower TRUE language english removeStopwords FALSE remove
  • 从 R 到 C 处理列表并访问它

    我想使用从 R 获得的 C 列表 我意识到这个问题与此非常相似 使用 call 在 R 和 C 之间传递数据帧 https stackoverflow com questions 6658168 passing a data frame f
  • Java 中清除嵌套 Map 的好方法

    public class MyCache AbstractMap
  • 使用starts_with() 将 NA 替换为 0

    我正在尝试替换我的一组特定列的 NA 值tibble 这些列都以相同的前缀开头 所以我想知道是否有一种简洁的方法来使用starts with 函数从dplyr包可以让我做到这一点 我已经看到了有关 SO 的其他几个问题 但是它们都需要使用特
  • 使用 data.table 进行分组并选择最短日期

    My Data df1 lt structure list ID c A A A B B C c1 1 6 c2 1 6 myDate c 01 01 2015 02 02 2014 03 01 2014 09 09 2009 10 10
  • 使用管道语法处理模型列表

    我经常喜欢拟合和检查与 R 数据框中的两个变量相关的多个模型 我可以使用如下语法来做到这一点 require tidyverse require broom models lt list hp exp cyl hp cyl map df m
  • 如何在 R 中为回归量创建“宏”?

    对于长且重复的模型 我想创建一个 宏 在 Stata 中称为 宏 并通过以下命令完成 global var1 var2 其中包含回归量的模型公式 例如来自 library car lm income education prestige d
  • 在 Shiny 中叠加两个 ggplot

    我有一个非常大的数据集 我正在使用 ggplot 在 Shiny 上绘制它 我有一个与 x 轴上的值相关联的滑块 我想用它对选定的数据子集重新着色 并让其余数据保持原样 最简单的选择是重新创建整个绘图 但由于它是一个大型数据集 因此这是一个
  • R闪亮:使用闪亮的JS从数据表中获取信息

    我想读出所有列名称以及它们在数据表中显示的顺序 由于不同的原因 我无法使用 stateSave 等选项 我对 JS 没有什么把握 但我确信用它可以完成 所以我需要你帮助我 我尝试过类似的代码片段 datatable data callbac
  • R data.table 多个条件连接

    我设计了一种解决方案 用于从两个单独数据表的多个列中查找值 并添加基于新列的值计算 多个条件比较 代码如下 它涉及在计算两个表中的值时使用 data table 和联接 但是 这些表没有联接在我正在比较的列上 因此我怀疑我可能无法获得 da
  • R 错误:无法更改锁定绑定的值

    我试图估计无限数字流的平均值和标准差 当我运行代码时 出现错误消息 无法更改锁定绑定的值 我做了一些研究 发现这个错误与我使用全局变量有关 但我无法弄清楚 任何帮助将非常感激 在此先感谢您的帮助 define global variable
  • 如何读取 R 中的每个 .csv 文件并将其导出到单个大文件中

    你好 我有以下格式的数据 101 20130826T155649 3 1 round 0 10552 180 yellow 12002 1 round 1 19502 150 yellow 22452 1 round 2 28957 130
  • 如何总结此R问题中的销售数量、售出酒类数量和花费金额

    我使用以下代码在 R 上上传我的数据 if file exists ames liquor rds url lt https github com ds202 at ISU materials blob master 03 tidyvers
  • RStudio 不会通过 rPython 调用加载所有 Python 模块

    我从 Bash 和 RStudio 中运行相同的脚本时出现一些意外行为 请考虑以下事项 我有一个文件夹 rpython 包含两个脚本 test1 R library rPython setwd rpython python load tes
  • 修改linux下的路径

    虽然我认为我已经接近 Linux 专业人士 但显然我仍然是一个初学者 当我登录服务器时 我需要使用最新版本的R 统计软件 R 安装在 2 个地方 当我运行以下命令时 which R I get usr bin R 进而 R version
  • zsh:未找到命令:使用 Big Sur Mac 的终端上的 R

    我从官方 cran 网站安装了 R 我可以从 Rstudio 运行 R 但是当我尝试从终端使用 R 时 我得到以下结果 base ege Eges MBP R zsh command not found R base ege Eges MB
  • 在 R 中使用逻辑 grep 抓取文本

    下午好 谢谢你帮我解答这个问题 我有兴趣抓取一组超过 5000 个 URL 的列表 我使用 lapply 和 readLines 使用下面的示例代码提取这些网页的文本 multipleURL lt c http dailymed nlm n
  • 通过 r markdown 中的循环创建代码片段

    如同如何使用R中的knitr创建一个包含代码块和文本的循环 https stackoverflow com questions 36373630 how to create a loop that includes both a code
  • R Leaflet Legend:colorBin-删除中断之间的小数

    我正在使用 Leaflet 库在 R 中创建交互式 HTML 地图 传说中采用的是colorBin用于创建将数据分为 6 个类别的方法 使用min values and max values 我已经定义了美国社区调查收入数据的特定范围可能落

随机推荐