在shapefile中创建网格

Per*_*rro 1 r geospatial shapefile

我正在尝试在shapefile中创建一个网格,诸如此类。但是,我无法生成这样的网格。我想知道是否有人对如何实现这一目标有想法。

这是我的代码-

WWWL.Shape<- readOGR("E:/Juan Arango", "WWL_Commerce_OK")
WWWL.Shape
plot(WWWL.Shape)
proj4string(WWWL.Shape)

bb <- bbox(WWWL.Shape)
cs <- c(3.28084, 3.28084)*6000  # cell size 
cc <- bb[, 1] + (cs/2)  # cell offset
cd <- ceiling(diff(t(bb))/cs)  # number of cells per direction
grd <- GridTopology(cellcentre.offset=cc, cellsize=cs, cells.dim=cd)
grd
sp_grd <- SpatialGridDataFrame(grd,
                               data=data.frame(id=1:prod(cd)),
                               proj4string=CRS(proj4string(WWWL.Shape)))
plot(sp_grd)
Run Code Online (Sandbox Code Playgroud)

输出 WWL.Shape

class       : SpatialPolygonsDataFrame 
features    : 1 
extent      : 334367, 334498.7, 4088915, 4089057  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=15 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
variables   : 1
names       : Id 
min values  :  0 
max values  :  0 
Run Code Online (Sandbox Code Playgroud)

lok*_*oki 7

sf

rgdal下面的版本

首先,我们从shapefile开始。您可能会使用从任何地理空间文件加载它st_read

library(sf)
library(raster)
library(ggplot2)

# load some spatial data. Administrative Boundary
shp <- getData('GADM', country = 'aut', level = 0)
shp <- st_as_sf(shp)
# ggplot() + 
  # geom_sf(data = shp)
Run Code Online (Sandbox Code Playgroud)

现在你唯一需要的是组合st_make_gridst_intersection

grid <- shp %>% 
  st_make_grid(cellsize = 0.1, what = "centers") %>% # grid of points
  st_intersection(shp)                               # only within the polygon

# ggplot() + 
  # geom_sf(data = shp) + 
  # geom_sf(data = grid)
Run Code Online (Sandbox Code Playgroud)

rgdal 和版本

要创建像素网格,可以使用该sp::makegrid功能。
让我们从一个可复制的示例开始:

library(raster)
shp <- getData(country = "FRA", level = 0)
Run Code Online (Sandbox Code Playgroud)

现在我们有了一个(多)多边形。让我们将其转换为公制坐标系(因为您的数据和像元大小也是公制的):

shp <- spTransform(shp, CRSobj = "+proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")
plot(shp)
Run Code Online (Sandbox Code Playgroud)

然后,我们使用您指定的像元大小在此多边形内创建一个网格。

cs <- c(3.28084, 3.28084)*6000
grdpts <- makegrid(shp, cellsize = cs)
Run Code Online (Sandbox Code Playgroud)

然后,我们将此网格(基本上是中心点矩阵)转换为SpatialPoints对象:

spgrd <- SpatialPoints(grdpts, proj4string = CRS(proj4string(shp)))
Run Code Online (Sandbox Code Playgroud)

然后可以将其转换为SpatialPixels对象。(注意:添加子集[shp, ]只能选择原始多边形内的点)

spgrdWithin <- SpatialPixels(spgrd[shp,])
plot(spgrdWithin, add = T)
Run Code Online (Sandbox Code Playgroud)

如果需要将网格作为“多边形”或“网格”,则可以使用

spgrdWithin <- as(spgrdWithin, "SpatialPolygons")
# or
spgrdWithin <- as(spgrdWithin, "SpatialGrid")
Run Code Online (Sandbox Code Playgroud)