簡體   English   中英

我如何確保 spatstat::owin(poly=<polygon> ) 沒有“負區域”

[英]How do I ensure that the polygon in spatstat::owin(poly=<polygon>) does not have “negative area”

我是 R spatstat 包的新用戶,在使用 owin() 創建多邊形觀察窗口時遇到問題。 代碼如下:

library("maps")
library ("sp")` 
library("spatstat")
mass.map <- map("state", "massachusetts:main", fill=T) # This returns a data frame includding x and y components that form a polygon of massachusetts mainland`

mass.win <- owin(poly=data.frame(x=mass.map$x, y=mass.map$y)

if (w.area < 0) stop(paste("Area of​​多邊形為負 -", "maybe traversed in > wrong direction?")) 中的錯誤:需要 TRUE/FALSE 的缺失值

我嘗試了諸如顛倒多邊形順序之類的方法,但得到了同樣的錯誤。

 mass.win <- owin(poly=data.frame(x=rev(mass.map$x), y=rev(mass.map$y)))

多邊形包含重復的頂點

Polygon is self-intersecting Error in owin(poly = data.frame(x = rev(mass.map$x), y = rev(mass.map$y))) :多邊形數據包含重復的頂點和自相交

然后我想也許 map() 返回的多邊形並不意味着要饋送給 owin()。 所以我嘗試加載馬薩諸塞州的形狀文件(此時我完全猜測)。:

x <- readShapePoly("../Geog/OUTLINE25K_POLY") ## The shape file for MASS, loaded from MassGIS website
mass.poly <- x <- readShapePoly("../Geog/OUTLINE25K_POLY", force_ring=T, delete_null_obj=T) ## I got following error whether or not I used force_ring

mass.owin <- as(mass.poly, "owin") 檢查 1006 個多邊形...1,多邊形 1 包含重復的頂點 [檢查具有 91844 條邊的多邊形...] 2, 3, .. [etd 1:21: 52] ....10 [etd 36:12] ..... [etd 23:10] ....20 [etd 16:59] ..... [etd 13:22] .... 30 [etd 11:01] ..... [etd 9:21] ....40 [etd 8:06] ..... [etd 7:09] ....50 [etd 6:23] ] ..... [etd 5:46] ....60 [etd 5:15] ...[檢查具有 2449 條邊的多邊形...] .. [etd 4:49] ....70 [ etd 4:27] ..... [etd 4:07] ....80 [etd 3:50] ..... [etd 3:36] ....90 [etd 3:22] . .... [etd 3:11] ....100 [等。

我收到了抱怨相交頂點等的消息,但它無法構建多邊形。

關於問題的一些背景:我正在嘗試使用 spatstat 中的函數進行空間相對風險計算,即案例與控件密度的空間比率。 為此,我需要一個觀察窗口和該窗口內的點圖。 我可以作弊,使觀察窗口成為圍繞馬薩諸塞州的矩形,但這可能會扭曲海岸附近的值。 無論如何,我想學習如何為我使用此軟件包所做的任何未來工作正確地做到這一點。 感謝您的任何幫助,您可以提供。

編輯 2021-02-04:我再次遇到了這個順時針/逆時針問題,並找到了我自己的答案,這是唯一一個解決 spatstat 問題的答案。 答案不是很有幫助。 對其進行了改進,使其可用於更廣泛的應用。

spatstat包要owin對象坐標指定逆時針 引用自 1.36-0 版文檔:

單多邊形:如果 poly 是具有兩列的矩陣或數據框,或者是具有兩個相等長度的分量向量 x 和 y 的結構,則這些值被解釋為外接窗口的多邊形頂點的笛卡爾坐標。 頂點必須逆時針列出。 不應重復任何頂點(即不重復第一個頂點)。

library("maps")
library ("sp")
library("spatstat")
mass.map <- map("state", "massachusetts:main", fill=T)

在此處輸入圖片說明

首先,您需要確定多邊形是順時針運行還是逆時針運行。 這里的方法可以用來找出答案。 將其公式化為函數:

#' @title Check whether points for an owin are clockwise
#' @param x a dataframe with x coordinates in the first column and y coordinates in the second. 
#' @details Similarly to owin, the polygon should not be closed
#' @return A logical telling whether the polygon is arranged clockwise.
#' @author The idea has been scavenged from https://stackoverflow.com/a/1165943/1082004

clockwise <- function(x) {
    
  x.coords <- c(x[[1]], x[[1]][1])
  y.coords <- c(x[[2]], x[[2]][1])
  
  double.area <- sum(sapply(2:length(x.coords), function(i) {
    (x.coords[i] - x.coords[i-1])*(y.coords[i] + y.coords[i-1])
  }))
  
  double.area > 0
} 

現在,查看mass.map中的坐標是否為順時針:

clockwise(data.frame(x=mass.map$x, y=mass.map$y))
#> TRUE

他們是。 使用rev函數逆時針旋轉坐標,該函數反轉xy向量:

mass.win <- owin(poly=data.frame(x=rev(mass.map$x), y=rev(mass.map$y)))
plot(mass.win)

在此處輸入圖片說明

我對你遇到的問題很熟悉。 如果我理解正確,您希望獲得 owin 類的多邊形對象。 以下代碼將幫助您獲得您想要的東西。

#Loading the required packages
library (sp)
library(maps)

# Reading the dataset
mass.map <- map("state", "massachusetts:main", fill=T)

IDs <- sapply(strsplit(mass.map$names, ":"), function(x) x[1])
# converting can be done by loading the powerful package "maptools"
library(maptools)
# Converting into SpatialPolygons-object
mass.poly <- map2SpatialPolygons(mass.map, IDs = IDs)

# Converting by coercing into owin-object
mass.owin <- as.owin.SpatialPolygons(mass.poly)

最初的問題是在 7 年前(2014 年)發布的。 spatstat起, spatstat包發生了巨大的變化。

不再出現關於重復頂點和自相交多邊形的錯誤消息,因為代碼現在會自動更正多邊形數據中的這些缺陷。

錯誤消息Area of polygon is negative - maybe traversed in wrong direction? 仍然發生 這是因為owin要求多邊形頂點圍繞外部邊界按逆時針順序(以及圍繞孔邊界的順時針順序)列出。 如果您的窗口是單個多邊形而不是多邊形集合,則解決方法是使用rev簡單地反轉坐標向量的順序:

library(maps)
library(sp)
library(spatstat)
mass.map <- map("state", "massachusetts:main", fill=TRUE)
mass.win <- owin(poly=lapply(mass.map, rev))

我們不能讓這自動發生,因為它並不總是用戶想要的。

有關詳細信息,請參閱spatstat 書籍的第 3 章,您可以從此處免費下載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM