How do I generate a Hexagonal grid in R

主宰稳场 提交于 2020-05-09 19:14:06

问题


I would like to be able to create a SpatialPolygons object (which is a Hexagonal grid) that covers another SpatialPolygon.

I would like all the Hexagons to have a diameter of 1km (ideally i can vary this) and for all the hexagons together to cover the whole object. The method below only seems to cover a small amount of it...

Below is my attempt using the sp package:

require(sp)
data(meuse.riv)
meuse.sr = SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)), "x")))
plot(meuse.sr)

HexPts <-spsample(meuse.sr,type="hexagonal",cellsize=1000)
HexPols <- HexPoints2SpatialPolygons(HexPts)
plot(HexPols, add=TRUE)

Any help as always is greatly appreciated...


回答1:


replace meuse.sr with some buffered version, like rgeos::gBuffer(meuse.sr, width = 2000) in the call to spsample. Here is a full example that selects only the intersecting hexagons:

require(sp)
data(meuse.riv)
meuse.sr = SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)), "x")))
plot(meuse.sr)

library(rgeos)
meuse.large = gBuffer(meuse.sr, width = 2000)
HexPts <-spsample(meuse.large, type="hexagonal", cellsize=1000)
HexPols <- HexPoints2SpatialPolygons(HexPts)
plot(HexPols[meuse.sr,], add=TRUE)

enter image description here



来源:https://stackoverflow.com/questions/29374004/how-do-i-generate-a-hexagonal-grid-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!