将sf对象转换为栅格

时间:2020-11-12 15:18:08

标签: leaflet sf r-leaflet

基本上,我正在尝试将sf对象转换为栅格文件,以使用传单进行绘制。 sf对象如下所示:

Simple feature collection with 33901 features and 1 field
geometry type:  POINT
dimension:      XY
bbox:           xmin: 63.42624931 ymin: -18.21972306 xmax: 175.2237467 ymax: 58.60000076
geographic CRS: WGS 84
First 10 features:
     df$prhmax                       geometry
1  24.46245324 POINT (77.57315415 -17.2288...
2  24.48866948 POINT (77.97969243 -17.1712...
3  24.51029786 POINT (78.38618742 -17.1142...
4  24.51992770 POINT (78.79264389 -17.0577...
5  24.52394288 POINT (79.199056 -17.00185241)
6  24.53245239 POINT (79.60542849 -16.9464...
7  24.56160049 POINT (80.01176604 -16.8915...
8  24.60146712 POINT (80.41806278 -16.8372...
9  24.62994385 POINT (80.82432335 -16.783531)
10 24.65465755 POINT (81.23055239 -16.7303...

我一直在努力解决这个问题。如果我只是做plot(sf.object),它就可以正常工作,但是我无法制作栅格。

1 个答案:

答案 0 :(得分:0)

生成栅格图层并将其栅格化。

# Load packages
packs <- list("tidyverse", "raster", "sf")
lapply(packs, require, character.only = T)

# Convert points to sp (assumes that the sf object is called example_points)
example_points <- as(example_points, "Spatial")

# Generate empty raster layer and rasterize points
example_raster <- raster(crs = crs(example_points), vals = 0, resolution = c(0.5, 0.5), ext = extent(c(-180, 180, -90, 90))) %>%
   rasterize(example_points, .)

鉴于这些点是经纬线投影的,因此代码会生成一个30 x 30弧分钟分辨率的全局栅格图层,其像元值设置为零。 rasterize然后根据示例栅格图层将点转换为栅格图层。您可能需要设置rasterize函数的fieldfun参数,它们控制点如何确定单元格值。使用前者,您可以控制哪个变量设置单元格值。后者定义了计算像元值的函数-例如计算与像素相交的点或计算所有相交点值的平均值。