I need to resample a raster from nrow= 467, ncol=805, res=0.0416667 degree to nrow=2335, ncol=4025, res=0.00833333 degree. Basically make the pixel size smaller, because I have too many small polygons for raster statistics.
I have tried it with the code below, the problem is that the resulting raster is slightly above the origin raster, see figure.
In gray "test raster", in yellow "original raster".
The new image, raster comparison.
library(raster) library(sp) library(rgdal) library(ggplot2) library(itertools) path = '~/path' spath = '~/path1' source <- raster(file.path(path,'file.tif')) >source Summary of origin raster: class : RasterLayer dimensions : 467, 805, 375935 (nrow, ncol, ncell) resolution : 0.04166667, 0.04166667 (x, y) extent : -119, -85.45833, 13.54167, 33 (xmin, xmax, ymin, ymax) crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 source : ~/path/file.tif names : raster_name >summary(source) raster_name Min. 5.516960 1st Qu. 9.151464 Median 10.334464 3rd Qu. 12.149574 Max. 17.560150 NA's 0.000000 # How the raster should be target <- raster(nrow=2335, ncol=4025, extent(source), crs(source)) #origin(target) <- c(-119, 32.9583) # Commented for further 'experiments' >target class : RasterLayer dimensions : 2335, 4025, 9398375 (nrow, ncol, ncell) resolution : 0.008333333, 0.008333333 (x, y) extent : -119, -85.45833, 13.54163, 32.99997 (xmin, xmax, ymin, ymax) crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 > summary(target) layer Min. NA 1st Qu. NA Median NA 3rd Qu. NA Max. NA NA's NA # Project source raster to target raster by bilinear dest <- projectRaster(source, target, method='bilinear', tolerance=0.0001) >dest class : RasterLayer dimensions : 2335, 4025, 9398375 (nrow, ncol, ncell) resolution : 0.008333333, 0.008333333 (x, y) extent : -119, -85.45833, 13.54163, 32.99997 (xmin, xmax, ymin, ymax) crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 source : memory names : raster_name values : 5.517167, 17.58864 (min, max) > summary(dest) raster_name Min. 5.517167e+00 1st Qu. 9.154074e+00 Median 1.034819e+01 3rd Qu. 1.217350e+01 Max. 1.758864e+01 NA's 5.232403e+06 # Write new raster file to a path writeRaster(flip(dest, direction='y'), file.path(spath, 'target.tif'))``` 



rfrom the origin oftestwhich it gets viaextent(test)? In your image, which is "the original raster"? The one read fromfile.tifand calledtest? What's the purple?extent(test)but the problem remains, the resulting raster is a little bit up from the original. The original raster isfile.tif = test. The purple is a polygon underneath, I did not turn off the shapefile.file.tifinto a thing calledsourceand showsummary(source). Then set up the raster with the target resolution and extent and call ittargetand showsummary(target). Then do the reproject into something calleddestand showsummary(dest). Everything else is clutter.originset in there. You can't do that. It will shift your rasters. If you have two rasters whereextent(a) == extent(b)and they still don't line up on the plot, let me know.