在shapefile中创建网格 [英] Create a grid inside a shapefile

查看:142
本文介绍了在shapefile中创建网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to crate a grid inside a shapefile, something like this. However, I am unable to generate such grid. I was wondering if anyone has an idea on how to accomplish this.

这是我的代码-

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)

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 

推荐答案

sf版本

请参见下面的rgdal版本

sf version

see rgdal version below

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

First, we start of with a shapefile. You might probably load it from any geospatial file using 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)

现在,您唯一需要的是st_make_gridst_intersection的组合:

Now the only thing you need is a combination of st_make_grid and st_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)

rgdal和版本

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

rgdal and version

To create a grid of pixels you can use the sp::makegrid function.
Let's start with a reproducible example:

library(raster)
shp <- getData(country = "FRA", level = 0)

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

So now we have a (multi)polygon. Let's transform it to a metric coordinate system (since your data and cell sizes were also metric):

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

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

Afterwards, we create a grid within this polygon with your specified cell size.

cs <- c(3.28084, 3.28084)*6000
grdpts <- makegrid(shp, cellsize = cs)

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

Then, we convert this grid (which basically is a matrix of center points) to a SpatialPoints object:

spgrd <- SpatialPoints(grdpts, proj4string = CRS(proj4string(shp)))

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

This can then be converted into a SpatialPixels object. (Note: adding the subset [shp, ] only selects points within the original polygons)

spgrdWithin <- SpatialPixels(spgrd[shp,])
plot(spgrdWithin, add = T)

如果您需要栅格为多边形"或栅格",则可以使用

If you need the grid as Polygons or Grid you can use

spgrdWithin <- as(spgrdWithin, "SpatialPolygons")
# or
spgrdWithin <- as(spgrdWithin, "SpatialGrid")

这篇关于在shapefile中创建网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆