r - sf 和 stars:多边形化分类栅格
问题描述
我l
只想为栅格中的“目标”分类绘制栅格计数(),x
而不考虑 NA 值。我尝试这样做:
# Packages
library(stars)
library(sf)
#Vectorizing a raster object to an sf object
tif = system.file("tif/L7_ETMs.tif", package = "stars")
x = read_stars(tif)[, 1:50, 1:50, 1:2]
x[[1]] = round(x[[1]]/5)
x[[1]] = ifelse(x[[1]]<10,NA,"target")
str(x[[1]])
#Polygonizing
l = st_contour(x)
plot(l[1])
Error in CPL_write_gdal(mat, file, driver, options, type, dims, from, :
Not compatible with requested type: [type=character; target=double].
但是,不起作用。请问,有什么帮助吗?
提前致谢,
亚历山大
解决方案
您的脚本有几个错误,首先st_contour
是表明它与字符类型不兼容(参考您在栅格中设置的“目标”字符串)。其次,我建议使用内部的 breaks 参数st_contour
来设置您希望获得轮廓的目标值。此外,您可能希望使用x[rule] <- NA
屏蔽栅格中的某些值。我对您的代码进行了其他修改,可能会有所帮助:
# 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
# Mask values lower than 10
# However, this does not make any change, because the lowest value is 10
x[x<10] <- NA
# 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 = 12)
# Plot the contours
plot(l)
推荐阅读
- database-performance - MySQL停止响应
- angular - 导航到一个惰性组件,该组件是命名辅助/辅助插座中惰性加载子模块的一部分
- reactjs - 如何 onSubmit 将 api 调用的结果重定向到另一个页面?
- firebase - Firebase 函数 document.create 和 user.create 触发器多次触发
- node.js - Auth0 和 Graphql 出错 - TypeError:无法读取未定义的属性“publicKey”
- powershell - 如何在 csv 文件中导出数千个 Azure AD 组,包括其所有属性
- javascript - CSS Flexbox垂直滚动不起作用reactjs应用程序
- python-3.x - 如何在 Tensorflow 2 中使用 GradientTape 和 AutoGraph?
- excel - 我正在尝试汇总来自用户表单的输入
- javascript - 如何在 React 中一键提交多个 post 请求?