我正在处理一个包含日本大约 400,000 个 1km x 1km 网格单元的数据集,我使用 jpgrid 包下载了该数据集。网格单元存储为 sf 对象(多边形),但由于性能问题,我想将此数据集转换为栅格图层。但是,我在使用 terra 包进行光栅化过程时遇到了问题。
这是一个包含数据子集的可重现示例:
library(tidyverse)
library(jpgrid)
library(sf)
library(tmap)
library(terra)
# Subset and preprocess the polygon data
cities_polygon <- head(grid_city_2020, n = 30) %>%
grid_as_sf(crs = 6670) %>%
mutate(key_code = as.character(grid),
key_code = as.numeric(key_code)) %>%
right_join(naro_2020, by = "key_code")
# Visualize the polygons (works as expected)
qtm(cities_polygon)
# Convert to a SpatVector and create a raster template
cities_vector <- vect(cities_polygon)
template_raster <- rast(ext = ext(cities_vector),
resolution = 1000,
crs = crs(cities_vector))
# Attempt to rasterize using a numeric field (error occurs here)
cities_raster <- rasterize(cities_vector, template_raster, field = "avg_hot_days_35")
# Plot the raster
plot(cities_raster)
在 cars_raster 行之后得到的错误是:
Warning: Failed to compute min/max, no valid pixels found in sampling. (GDAL error 1)
非常感谢您的帮助!
我尝试过的:
将多边形光栅化为点会更有效。你能做到吗:
cities_vector <- vect(cities_polygon)
template_raster <- rast(cities_vector, resolution = 1000)
points_vector <- centroids(cities_vector)
cities_raster <- rasterize(points_vector, template_raster, field = "avg_hot_days_35")