使用R terra包栅格化后出现垂直白线的原因排查求助
Hey there, let's break down why those vertical white lines are appearing in your plot and walk through better ways to fill your fine-resolution raster with coarse values.
Why the White Lines Happen
The core issue comes from floating-point precision errors in your resolution calculation and an unnecessary detour through point data:
- Your
res_dropcalculates to89.99999instead of the exact integer90(since0.25 / 0.002777778 = 90exactly). Using a non-integerfactinterra::disagg()creates a misalignment between your disaggregated coarse raster and the target fine raster. - Converting the disaggregated raster to points and then rasterizing back introduces gaps: some fine-resolution pixels don't get matched to any points, resulting in the white (NA) vertical lines you see.
Better Solutions to Avoid White Lines
Option 1: Use Exact Integer Disaggregation + Resampling
Skip the point conversion entirely and use precise integer scaling:
# Use the exact integer factor (0.25 / 0.002777778 = 90) res_drop <- 90 # Disaggregate the coarse raster with the exact integer factor raster_coarse_disagg <- terra::disagg(raster_coarse, fact = res_drop) # Resample to match the fine raster's exact extent and alignment (eliminates any tiny misalignment) raster_coarse_filled <- terra::resample(raster_coarse_disagg, raster_fine, method = "near") # Check the result and plot raster_coarse_filled plot(raster_coarse_filled)
Option 2: Rasterize Coarse Raster Polygons Directly
Convert the coarse raster to polygon features and rasterize those to the fine grid—this ensures full coverage without gaps:
# Convert coarse raster to polygons (each cell becomes a polygon) raster_coarse_poly <- terra::as.polygons(raster_coarse) # Rasterize the polygons to the fine raster raster_coarse_filled <- terra::rasterize( x = raster_coarse_poly, y = raster_fine, field = "layer1", fun = max, touches = TRUE ) # Plot the result plot(raster_coarse_filled)
Option 3: Extract Coarse Values to Fine Raster
Use extract() to directly assign coarse values to each fine pixel:
# Extract the coarse raster value for every fine raster pixel coarse_values <- terra::extract(raster_coarse, raster_fine)$layer1 # Create a copy of the fine raster and assign the coarse values raster_coarse_filled <- raster_fine terra::values(raster_coarse_filled) <- coarse_values # Plot the result plot(raster_coarse_filled)
All three methods will eliminate the vertical white lines by ensuring full, aligned coverage of the fine raster with your coarse data.
内容的提问来源于stack exchange,提问作者89_Simple

