如何在文本文件中找到最近的lat和long?

时间:2013-06-24 12:45:42

标签: r

我有一个包含以下信息的文本文件:

   Grid Point Index, Latitude, Longitude, Cell
 167,    0.000000,    9.432301, 1350
 169,    0.000000,    9.544590, 1350
 171,    0.000000,    9.656878, 1350
 173,    0.000000,    9.769168, 1350
 175,    0.000000,    9.881457, 1350
 177,    0.000000,    9.993747, 1350
 179,    0.000000,   10.106036, 1386
 181,    0.000000,   10.218326, 1386

我想找出这个lat和long的相应Grid点和单元格 :0.000000,11.902665。 好吧,我可以手动完成,但这需要花费很多时间。 可以确定没有与我的输入相对应的精确坐标,所以我希望文件中最接近的坐标与输入相对应。 任何人都可以帮我构建这个功能

   insert lat and long
然后

将在文件中找到最接近的lat-long以及相应的网格点索引和单元格

阅读文件

      das= read.table("C:\\Users\\lonlatnter.txt", sep=",",header=TRUE)

4 个答案:

答案 0 :(得分:6)

来自Imap包的

gdist函数计算Great-circle distance。试试这个:

install.packages("Imap")
library("Imap")

#Dummy data
dat <- read.table(text="   Grid Point Index, Latitude, Longitude, Cell
 167,    0.000000,    9.432301, 1350
 169,    0.000000,    9.544590, 1350
 171,    0.000000,    9.656878, 1350
 173,    0.000000,    9.769168, 1350
 175,    0.000000,    9.881457, 1350
 177,    0.000000,    9.993747, 1350
 179,    0.000000,   10.106036, 1386
 181,    0.000000,   10.218326, 1386", header=T, sep=",")

#MyPoint
myLatitude <- 0.000000 
myLongitude <- 11.902665

#gdist Geodesic distance (great circle distance) between points
dat$Dist <- gdist(lat.1=dat$Latitude,
                       lon.1=dat$Longitude,
                       lat.2=myLatitude,
                       lon.2=myLongitude)

#Output the shortest distance - Min
dat[dat$Dist == min(dat$Dist),]

#Output
#   Grid.Point.Index Latitude Longitude Cell     Dist
#8              181        0  10.21833 1386 101.2418

答案 1 :(得分:4)

非常简单,但不是100%精确(地球不平坦:-)):

df <- read.table(text="   Grid Point Index, Latitude, Longitude, Cell
 167,    0.000000,    9.432301, 1350
 169,    0.000000,    9.544590, 1350
 171,    0.000000,    9.656878, 1350
 173,    0.000000,    9.769168, 1350
 175,    0.000000,    9.881457, 1350
 177,    0.000000,    9.993747, 1350
 179,    0.000000,   10.106036, 1386
 181,    0.000000,   10.218326, 1386", header=T, sep=",")


findGrid <- function(lat, lon){
  index <- which.min(sqrt((df$Latitude-lat)^2+(df$Longitude-lon)^2))
  df$Grid.Point.Index[index]
}

> findGrid(0,9.9)
[1] 175
> findGrid(0,9.7)
[1] 171

答案 2 :(得分:3)

另一种选择是使用ggmapmapdist使用 Google地图计算地图距离。

library(ggmap)
origin <- revgeocode(c( 11.902665,0.000000))
do.call(rbind,apply(DT,1,function(x){
  end <- revgeocode(c(x['Longitude'],x['Latitude']))
  mapdist(from=origin,to=end)[,c('from','to','km')]
  }))

我们有一个智能结果,位置名称(notlat / long),距离为km。

        from                       to      km
1 R14, Gabon       Komo-Mondah, Gabon      NA
2 R14, Gabon       Komo-Mondah, Gabon      NA
3 R14, Gabon       Komo-Mondah, Gabon      NA
4 R14, Gabon       Komo-Mondah, Gabon      NA
5 R14, Gabon              Komo, Gabon 324.206
6 R14, Gabon                N1, Gabon 281.145
7 R14, Gabon N1, Ekouk Village, Gabon 304.186
8 R14, Gabon          N1, Oyan, Gabon 308.246

我不知道为什么会给出一些NA。但是,如果我们省略这个细节:)近期点是:

res [which.min(res$km),]
 from        to      km
6 R14, Gabon N1, Gabon 281.145

答案 3 :(得分:1)

试试这个

data = read.table("C:\\Users\\lonlatnter.txt", sep=",",header=TRUE)

distance = function(Latitude, Longitude,x,y)
{
    sqrt((Latitude-y)^2+(Longitude-x)^2)
}
nearby <- function(y,x)
{
    dist= (with(data,distance(Latitude, Longitude,x,y)))
    data[match(min(dist),dist),]
}

nearby(0,9.6)

# Grid.Point.Index Latitude Longitude Cell
# 2              169        0   9.54459 1350