2014-04-08 25 views
4

我試圖找到兩點間的歐幾里得距離,由不規則的多邊形限制。 (即距離必須被計算爲通過給窗口的路線)在R {spatstat}中尋找點之間的歐幾里德距離,由不規則的多邊形窗口限制

下面是一個可重複的例子:

library(spatstat) 

#Simple example of a polygon and points. 
ex.poly <- data.frame(x=c(0,5,5,2.5,0), y=c(0,0,5,2.5,5)) 
points <- data.frame(x=c(0.5, 2.5, 4.5), y=c(4,1,4)) 

bound <- owin(poly=data.frame(x=ex.poly$x, y=ex.poly$y)) 

test.ppp <- ppp(x=points$x, y=points$y, window=bound) 

pairdist.ppp(test.ppp)#distance between every point 
#The distance result from this function between point 1 and point 3, is given as 4.0 

但是我們知道剛剛從繪製點

plot(test.ppp) 

路線限制在多邊形的距離應該更大(在這種情況下,5.00)。

是否有另一個功能,我不知道{spatstat}會這樣做?還是有人有其他建議可以做到這一點的另一個包?

我試圖找到水體中兩點之間的距離,所以我的實際數據中的不規則多邊形更加複雜。

任何幫助,非常感謝!

乾杯

+0

有趣的問題。我可能會建議將'bound'轉換爲RasterLayer對象(可能首先使用** maptools **來提供'(bound,SpatialPolygons)'和'as(test.ppp,「SpatialPoints」)'),然後使用** gdistance **包來計算點之間的「最小成本距離」,其中「bound」外的所有網格點的摩擦或成本設置爲無窮大。 [** gdistance **](http://cran.r-project.org/web/packages/gdistance/index.html)附帶一個漂亮的小插曲(運行'vignette(「gdistance」)'看到它)應該給你一個好的開始。 –

回答

5

OK,這裏的gdistance爲基礎的方法,我在昨天的評論中提到。這並不完美,因爲它計算的路徑段都被限制在棋盤上的16個方向之一上發生(國王的移動加上騎士的移動)。也就是說,在你的例子中,對於三個成對距離中的每一個,它都在正確值的2%以內(總是略微高估)。

library(maptools) ## To convert spatstat objects to sp objects 
library(gdistance) ## Loads raster and provides cost-surface functions 

## Convert *.ppp points to SpatialPoints object 
Pts <- as(test.ppp, "SpatialPoints") 

## Convert the lake's boundary to a raster, with values of 1 for 
## cells within the lake and values of 0 for cells on land 
Poly <- as(bound, "SpatialPolygons")   ## 1st to SpatialPolygons-object 
R <- raster(extent(Poly), nrow=100, ncol=100) ## 2nd to RasterLayer ... 
RR <- rasterize(Poly, R)      ## ... 
RR[is.na(RR)]<-0        ## Set cells on land to "0" 

## gdistance requires that you 1st prepare a sparse "transition matrix" 
## whose values give the "conductance" of movement between pairs of 
## adjacent and next-to-adjacent cells (when using directions=16) 
tr1 <- transition(RR, transitionFunction=mean, directions=16) 
tr1 <- geoCorrection(tr1,type="c") 

## Compute a matrix of pairwise distances between points 
## (These should be 5.00 and 3.605; all are within 2% of actual value). 
costDistance(tr1, Pts) 
##   1  2 
## 2 3.650282   
## 3 5.005259 3.650282 

## View the selected paths 
plot(RR) 
plot(Pts, pch=16, col="gold", cex=1.5, add=TRUE) 
SL12 <- shortestPath(tr1, Pts[1,], Pts[2,], output="SpatialLines") 
SL13 <- shortestPath(tr1, Pts[1,], Pts[3,], output="SpatialLines") 
SL23 <- shortestPath(tr1, Pts[2,], Pts[3,], output="SpatialLines") 
lapply(list(SL12, SL13, SL23), function(X) plot(X, col="red", add=TRUE, lwd=2)) 

enter image description here

+0

使用gdistance的好例子!我試圖找出一種使用凸包的方法,但這樣更好!謝謝! – user3389288

+0

所有工作按需要,只是繪圖查看選定的路徑給了我以下錯誤信息:'> SL12 < - shortestPath(tr1,點[1,],點[2,],輸出=「空間線」) 錯誤在有效的對象(.Object)中: 無效的類「CRS」對象:類「CRS」中的槽「projargs」的無效對象:got類「邏輯」,應該是或擴展類「字符」 – user3389288

+0

我將無法(),crs()',並且只能建議你仔細檢查附加到'tr1'和'Pts'的投影元數據, 'identicalCRS()'等。如果它們不匹配,則可能需要將您的點投影到柵格的CRS或採取其他一些精細的步驟來使它們匹配。祝你好運! –

相關問題