sp
看sf
下面的版本!
你可以使用创建一个网格 sp::makegrid
library(sp) library(rgdal) library(raster) # load some spatial data. Administrative Boundary us <- getData('GADM', country = 'US', level = 1) us$NAME_1 colorado <- us[us$NAME_1 == "Colorado",] # check the CRS to know which map units are used proj4string(colorado) # "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" # Create a grid of points within the bbox of the SpatialPolygonsDataFrame # colorado with decimal degrees as map units grid <- makegrid(colorado, cellsize = 0.1) # cellsize in map units! # grid is a data.frame. To change it to a spatial data set we have to grid <- SpatialPoints(grid, proj4string = CRS(proj4string(colorado))) plot(colorado) plot(grid, pch = ".", add = T)
奥地利的另一个例子(GADM代码'AUT'
).
要仅提取多边形内的点,请使用`[`
基于位置对点进行子集,如下所示:
grid <- grid[colorado, ]
sf
library(sf) library(raster) library(ggplot2) # load some spatial data. Administrative Boundary aut <- getData('GADM', country = 'aut', level = 0) aut <- st_as_sf(aut) # ggplot() + # geom_sf(data = aut) grid <- aut %>% st_make_grid(cellsize = 0.1, what = "centers") %>% # grid of points st_intersection(aut) # only within the polygon # ggplot() + # geom_sf(data = aut) + # geom_sf(data = grid)