从数百万个GPS坐标中确定COUNTRY的最快方法[R] [英] Fastest way to determine COUNTRY from millions of GPS coordinates [R]

查看:123
本文介绍了从数百万个GPS坐标中确定COUNTRY的最快方法[R]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数百万个GPS坐标,并且想快速添加一列坐标所在的国家/地区。

I have millions of GPS coordinates and want to quickly add a column of the country of the coordinates.

我当前的方法有效,但速度非常慢:

My current method works but is extremely slow:

library(data.table)

#REPRODUCE DATA
data <- data.table(latitude=sample(seq(47,52,by=0.001), 1000000, replace = TRUE),
                   longitude=sample(seq(8,23,by=0.001), 1000000, replace = TRUE))

#REQUIRED PACKAGES
if (!require("sp")) install.packages("sp")
if (!require("rworldmap")) install.packages("rworldmap")
if (!require("sf")) install.packages("sf")
library(sp)
library(rworldmap)
library(sf)

#CURRENT SLOW FUNCTION
coords2country = function(points,latcol,loncol){  
  countriesSP <- getMap(resolution='low')
  pointsSP <- st_as_sf(points,coords=c(loncol,latcol),crs=4326)
  pointsSP<- as(pointsSP,"Spatial")
  # 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)
}

#SLOW!
> system.time(data[,country:=coords2country(data,"latitude","longitude"),])
   user  system elapsed 
121.293   7.849 130.226 

是否有更快/更好的方法?谢谢!

Is there a faster/better way to do this? Thanks!

推荐答案

有两个类似的问题。他们在我上面的评论中。问题在询问如何从坐标中获取国家/地区名称。在这里,OP正在询问哪种是执行任务的更快方法。根据帖子,我们有三个选择。一种是在此问题中使用自定义函数。另一个方法是使用 geonames 软件包。另一种方法是在 map 包中使用 map.where()。第二个选项需要一些设置。所以我刚刚测试了 map.where()。结果如下。正如OP所说,此功能必须运行得更快。

There are two similar questions. They are in my comments above. The questions are asking how to get country names from coordinates. Here the OP is asking which is a faster way to do the task. Based on the posts, we have three options. One is to use the custom function in this question. Another is to use the geonames package. The other is to use map.where() in the map package. The second option needs a bit of setup. So I just tested map.where(). The following is the result. As the OP said, this function is working must faster.

library(maps)
set.seed(111)
data <- data.table(latitude=sample(seq(47,52,by=0.001), 1000000, replace = TRUE),
                   longitude=sample(seq(8,23,by=0.001), 1000000, replace = TRUE))

system.time(data[, country := map.where(x = longitude, y = latitude)])

#   user  system elapsed 
#   7.20    0.05    7.29 

这篇关于从数百万个GPS坐标中确定COUNTRY的最快方法[R]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆