image.png
# Wed Dec 16 13:37:54 2020 -
# 字符编码:UTF-8
# R 版本:R x64 4.0.3 for window 10
# cgh163email@163.com
# 个人笔记不负责任
# —— 拎了个梨🍐
.rs.restartR()
require(terra)
# erase删除SpatialPolygons*或SpatialLines*对象的部分。
rm(list=ls());gc()
require(rgeos)
p <- shapefile(system.file("external/lux.shp", package="raster"))
b <- as(extent(6, 6.4, 49.75, 50), 'SpatialPolygons')
crs(b) <- crs(p)
e <- erase(p, b)
plot(e)
r <- raster(extent(p) +c(-.1,.1,-.1,.1), crs=crs(p))
start <- xyFromCell(r, cellFromCol(r, 1))
end <- xyFromCell(r, cellFromCol(r, ncol(r)))
lines <- do.call(spLines, lapply(1:10, function(i)rbind(start[i,], end[i,])))
crs(lines) <- crs(p)
e2 <- erase(lines, p)
plot(p)
lines(lines, col='blue', lwd=4, lty=3)
lines(e2, col='red', lwd=2)
# Wed Dec 16 13:39:23 2020 --end
dev.copy(png,'erase切走,切割.jpg');dev.off()