我正在尝试在全球栅格地图上覆盖一个空间多边形(来自icosa生成的网格)。我确保对象的边界框/范围相等,并且它们都投影到相同的CRS,但是多边形未正确与栅格范围对齐。有任何想法吗? (基本图片在这里:https://imgur.com/a/c1oVm6y)
# required packages
library(icosa)
library(raster)
library(rgdal)
library(rgeos)
# define projection string
equi <- CRS("+proj=longlat +datum=WGS84")
# load data and set extent
lad <- raster("Data/Maps/Ladinian_grey.jpg", crs = equi)
extent(lad) = c(-180, 180, -90, 90)
# generate grid and convert to spatial polygon
large_grid <- hexagrid(c(2, 3))
large_grid <- newsp(large_grid)
map_grid <- SpPolygons(large_grid, res = 50)
map_grid <- spTransform(map_grid, equi)
# plot
plot(lad)
plot(map_grid, col = NA, add = TRUE)
答案 0 :(得分:0)
很难看到没有数据的情况,以及您所做的奇怪的事情正在改变对象的范围。下面的示例表明它应该可以工作:
library(raster)
library(icosa)
r <- raster()
values(r) <- 1:ncell(r)
large_grid <- hexagrid(c(2, 3))
large_grid <- newsp(large_grid)
map_grid <- SpPolygons(large_grid, res = 50)
plot(r)
lines(map_grid)
您显示的对齐错误可能是由于在绘制后调整了图的大小。
它也可以很好地处理您的数据和(简化的)代码:
library(icosa)
library(raster)
lad <- raster("f91xQIt.jpg")
extent(lad) = c(-180, 180, -90, 90)
crs(lad) <- "+proj=longlat +datum=WGS84"
large_grid <- hexagrid(c(2, 3))
large_grid <- newsp(large_grid)
map_grid <- SpPolygons(large_grid, res=50)
# I use image so that it is clearer where all the lines come from
image(lad)
plot(map_grid, col = NA, add = TRUE)
而且程度符合预期
map_grid
#class : SpatialPolygons
#features : 362
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +a=6371007 +b=6371007