【发布时间】:2021-08-18 01:19:50
【问题描述】:
我想使用线串和按区域选择多边形来关闭所有多边形。在我的例子中:
# Packages
library(stars)
library(sf)
library(ggplot2)
#Vectorizing a raster object to an sf object
tif = system.file("tif/L7_ETMs.tif", package = "stars")
# Let's stay with only the first band, indicated in the final dimension
x = read_stars(tif)[, 1:50, 1:50, 1]
x = round(x/5)
# Calculate the min and max of the raster values
purrr::map(x, min)
# 10
purrr::map(x, max)
# 28
# Change values lower than 15 for 1 or 2
x[[1]] = ifelse(x[[1]]<15,1,2)
# Take a look at the image
plot(x)
# Obtain the contours
l = st_contour(x,
# Remove NA
na.rm = T,
# Obtain contour lines instead of polygons
contour_lines = TRUE,
# raster values at which to draw contour levels
breaks = 1.5)
# Plot the contours
raster_line <- st_as_sf(l, coords = c("x", "y"), crs = 32725) %>%
st_cast("LINESTRING")
ggplot(raster_line) +
geom_sf(aes(color = L7_ETMs.tif), show.legend = "line") +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "none")
#
现在,我只想在多边形大于 900 m^2 的情况下创建新轮廓,但在第一步中,我尝试使用“LINESTRING”转换为“MULTIPOLYGON”(闭合多边形)直接st_cast() 并且不起作用。
我的最终目标是关闭多边形,如果面积 > 900m^2,则选择多边形?
请问,有什么帮助吗?
提前致谢
【问题讨论】: