簡體   English   中英

R:多邊形合並算法

[英]R: Polygon combining Algorithm

我有不同的多邊形,定義為一個文件data.frameidlnglat 使用以下代碼,我可以在地圖上繪制這些多邊形。

    library(ggmap)
    library(ggplot2)

    map <- get_googlemap(center = c(lon = 9.26, lat = 47.3), zoom=10)
    xy <- data.frame(id = c(rep(1,4), rep(2,4)), lng = c(9,9.5,9.5,9,9.25,9.7,9.7,9.24), lat= c(47.1,47.1,47.4,47.4,47.2,47.2,47.5,47.5))
    p <- ggmap(map) + geom_polygon(data=xy, aes(x=xy$lng, y=xy$lat,group=xy$id),fill='red',alpha= 0.2)
    print(p)

在繪制多邊形之前是否有合並多邊形的功能? 我想繪制一個多邊形,它覆蓋至少一個多邊形下面的所有內容。 因此,我需要在相交處創建新點,並且凸包不會被切割。

如果你的意思是:

我想繪制覆蓋一切,在多邊形的至少一個是一個多邊形。

那么chull (凸包)可能就是您想要的。

作為一個例子(雖然不是ggplot):

# create data: (x,y)-coords
x<-c(1:9,9:1)
y<-runif(length(x))*10
# plot `em
plot(x,y,type="l")
# these are the indexes of points that are the convex hull
print(chull(x,y))
these.idx<-chull(x,y)
# plot the convex hull using the indexes
lines(x[these.idx],y[these.idx],type="l",col="red",lwd=2)

對於您的ggplot場景,如果您的代碼進行了如下修改,則它會繪制一個多邊形(盡管很難知道這就是您想要的):

library(ggmap)
library(ggplot2)

map <- get_googlemap(center = c(lon = 9.26, lat = 47.3), zoom=10)
xy <- data.frame(id = c(rep(1,4), rep(2,4))
    , lng = c(9,9.5,9.5,9,9.25,9.7,9.7,9.24)
    , lat= c(47.1,47.1,47.4,47.4,47.2,47.2,47.5,47.5))
ch.idx<-chull(xy$lng,xy$lat)
xy<-xy[ch.idx,]
# removing:: ,group=xy$id
p <- ggmap(map) 
p <- p + geom_polygon(data=xy, aes(x=xy$lng, y=xy$lat),fill='red',alpha= 0.2)
print(p)

(這更多是評論而不是答案,但是我沒有'rep'來表示抱歉)

暫無
暫無

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

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