2012-02-08 37 views
3

我有一套約5000個地理座標(WGS84)。他們都在40公里的廣場內。如何查找與座標集相關的點?

是否有任何算法/ R函數來找到點,內部正方形,而不是在給定的集合中,離集合中的任何點最遠?

我的意思是如何找到在距離集最近點距離爲最長

現在我通過生成等距座標網格並找到每個網格點到最近設置點的距離來完成此操作。有沒有更少的數字/非蠻力方法?

編輯: 我在以前版本的問題中犯了錯誤。也許這將有助於:

一組點是城市中的5000家商店的座標。我想在距離最近的商店最近的城市找到一個地方。

+2

廣場內很容易。其餘的我不完全確定你在問什麼。你想要異常值最大的異常值? – 2012-02-08 18:49:16

+0

檢查柵格庫中的distanceFromPoints函數。 – blindjesse 2012-02-08 20:29:35

回答

2

下面是一個使用多種功能(distanceFromPoints()maxValue()Which(),並xyFromCell())從raster包來執行關鍵計算的例子:

# Load required libraries 
library(sp) 
library(rgdal) 
library(raster) 

# Create a SpatialPoints object with 10 points randomly sampled from 
# the area lying between longitudes 0 and 1 and latitudes 0 and 1 
bbox <- matrix(c(0,0,1,1), ncol=2, dimnames = list(NULL, c("min", "max"))) 
PRJ4 <- CRS("+proj=longlat +datum=WGS84 +ellps=WGS84") 
S <- Spatial(bbox = bbox, proj4string = PRJ4) 
SP <- spsample(S, 10, type="random") 

# Create a raster object covering the same area 
R <- raster(extent(bbox), nrow=100, ncol=100, crs=PRJ4) 

# Find the coordinates of the cell that is farthest from all of the points 
D <- distanceFromPoints(object = R, xy = SP) 
IDmaxD <- Which(D == maxValue(D), cells=TRUE) 
(XY <- xyFromCell(D, IDmaxD)) 
#   x  y 
# [1,] 0.005 0.795 

# Plot the results 
plot(D, main = "Distance map, with most distant cell in red") 
points(SP) 
points(XY, col="red", pch=16, cex=2) 

enter image description here

3

認爲,如果你所尋找的點不在盒子的邊緣,那麼它必須位於點的voronoi tesselation的頂點。如果它位於盒子的邊緣,那麼它必須位於盒子和voronoi分割的邊緣的交點上。

因此,如果您計算voronoi tesselation,然後使用rgeos與框相交,則會給出一組可能的點。然後,您可以使用FNN軟件包來計算從這些可能點到數據點的相鄰距離,進行排序,並找到最近鄰點最大的可能點。

這給你一個沒有任何這種網格業務的確切點。如果不是那麼接近睡眠時間,我會選擇一些代碼來做到這一點。你可能需要deldir包或voronoi tesselations。它甚至可能已經做了十字路口...

好吧,不完全就寢時間。這裏的解決方案:

findM <- function(pts,xmin,xmax,ymin,ymax){ 
    require(deldir) 
    require(FNN) 
    d = deldir(pts[,1],pts[,2],rw=c(xmin,xmax,ymin,ymax)) 

    vpts = rbind(as.matrix(d$dirsgs[,1:2]),as.matrix(d$dirsgs[,3:4])) 
    vpts = rbind(vpts,cbind(c(xmin,xmax,xmin,xmax),c(ymin,ymin,ymax,ymax))) 
    vpts = vpts[!duplicated(vpts),] 

    nn = get.knnx(pts,vpts,k=1) 
    ptmin = which(nn$nn.dist==max(nn$nn.dist)) 

    list(point = vpts[ptmin,,drop=FALSE], dist = nn$nn.dist[ptmin]) 
} 

編輯後的版本現在返回一個點,並增加了角點作爲候選條件。

+0

聰明!謝謝你。我相信你對Voronoi瓦頂點之一的最偏遠點是正確的,只有一點小小的增加:它也可能在方格的四個角落之一。爲了檢查這些,你可以在初始賦值給'vpts'後添加:vpts < - rbind(vpts,as.matrix(expand.grid(c(xmin,xmax),c(ymin,ymax)) ))'。 – 2012-02-09 23:49:43

+0

完成,並且不復制矩陣。我不知道duplicateated()在行上工作。甜。 – Spacedman 2012-02-10 12:25:39