是否有一个函数告诉我彼此相邻的单元格

发布于 2025-02-09 02:30:59 字数 1453 浏览 1 评论 0 原文

这是我到目前为止的代码用六角形的顶点做点事。


library(dggridR)
library(dplyr)
library(ggplot2)

#Construct a global grid with cells approximately 1000 miles across
dggs          <- dgconstruct(spacing=1000, metric=FALSE, resround='down')

#Load included test data set
data(dgquakes)

#Get the corresponding grid cells for each earthquake epicenter (lat-long pair)
dgquakes$cell <- dgGEO_to_SEQNUM(dggs, dgquakes$lat, dgquakes$lon)$seqnum

#Get the number of earthquakes in each equally-sized cell
quakecounts   <- dgquakes %>% group_by(cell) %>% summarise(count=n())
quakecounts   <- dgquakes %>% group_by(cell) %>% summarise(total=sum())


#Construct a global grid with cells approximately 1000 miles across
dggs <- dgconstruct(spacing=1000, metric=FALSE, resround='down')
nocells <- dgmaxcell(dggs)

grid <- dgrectgrid(dggs,
                   minlat=-90, minlon=-180, 
                   maxlat=90, maxlon=180, frame = TRUE)


label <- 1:nocells
qgrid <- data.frame(label)

# get the lat-long so we can plot it properly
cellcentres <- dgSEQNUM_to_GEO(dggs, 1:nocells)
qgrid$cent_long <- cellcentres$lon_deg
qgrid$cent_lat <- cellcentres$lat_deg


# create plot
p <- ggplot() +
  geom_path(data=grid, aes(x=long, y=lat, group=group), color="black")  + # draw each hexagon
  geom_label  (data=qgrid,aes(x=cent_long, y=cent_lat, label=label)) + # draw label at cell centres
  coord_equal()

p

This is my code so far but I don't know how to make a function that will tell me what cells are neighboring what other cell, I'm pretty new to coding, so not sure that's why, I think the function would have to do something with the vertices of the hexagons.


library(dggridR)
library(dplyr)
library(ggplot2)

#Construct a global grid with cells approximately 1000 miles across
dggs          <- dgconstruct(spacing=1000, metric=FALSE, resround='down')

#Load included test data set
data(dgquakes)

#Get the corresponding grid cells for each earthquake epicenter (lat-long pair)
dgquakes$cell <- dgGEO_to_SEQNUM(dggs, dgquakes$lat, dgquakes$lon)$seqnum

#Get the number of earthquakes in each equally-sized cell
quakecounts   <- dgquakes %>% group_by(cell) %>% summarise(count=n())
quakecounts   <- dgquakes %>% group_by(cell) %>% summarise(total=sum())


#Construct a global grid with cells approximately 1000 miles across
dggs <- dgconstruct(spacing=1000, metric=FALSE, resround='down')
nocells <- dgmaxcell(dggs)

grid <- dgrectgrid(dggs,
                   minlat=-90, minlon=-180, 
                   maxlat=90, maxlon=180, frame = TRUE)


label <- 1:nocells
qgrid <- data.frame(label)

# get the lat-long so we can plot it properly
cellcentres <- dgSEQNUM_to_GEO(dggs, 1:nocells)
qgrid$cent_long <- cellcentres$lon_deg
qgrid$cent_lat <- cellcentres$lat_deg


# create plot
p <- ggplot() +
  geom_path(data=grid, aes(x=long, y=lat, group=group), color="black")  + # draw each hexagon
  geom_label  (data=qgrid,aes(x=cent_long, y=cent_lat, label=label)) + # draw label at cell centres
  coord_equal()

p

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上的星空 2025-02-16 02:30:59

dgrectgrid()的输出是一个空间对象,因此您可以在“ Terra”软件包中尝试“相邻”函数,或者在“ SF”软件包中使用“ ST_Touches”函数:

library(sf)

neighbours_sf&lt; - st_touches(grid)

library> library>

neighbours_terra&lt; - aideacter(vect(vect(grid),grid),(grid),(grid),grid),(grid),grid) type =“触摸”)

The output of dgrectgrid() is a spatial object, so you can try either the 'adjacent' function in the 'terra' package, or the 'st_touches' function in the 'sf' package:

library(sf)

neighbours_sf <- st_touches(grid)

or

library(terra)

neighbours_terra <- adjacent(vect(grid), type="touches")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文