I have a land use land cover (lulc) raster in WGS84 coordinate system shown below
class : RasterLayer dimensions : 59575, 57541, 3428005075 (nrow, ncol, ncell) resolution : 0.000509, 0.000509 (x, y) extent : 68.12366, 97.41203, 6.754034, 37.07771 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 data source :<address path> names : lulc raster values : 1, 19 (min, max)
I have another raster in MODIS sinusoidal projection below which I will refer to as the foundation raster
class : RasterLayer dimensions : 7276, 8330, 60609080 (nrow, ncol, ncell) resolution : 463.3127, 463.3127 (x, y) extent : 6508617, 10368012, 751956.5, 4123020 (xmin, xmax, ymin, ymax) coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs
data source : names : foundation raster values : 2, 3 (min, max)
I have to do analysis with both rasters and cannot change the projection of the foundation raster. So I reprojected the lulc raster as below
lulc_projected<-projectRaster(lulc raster, res=res(foundation raster),crs=crs(foundation raster), method='ngb') #categorical classes; hence ngb.
The resulting raster lulc_projected is completed without errors and aligns perfectly with the foundation raster when viewed in QGIS and Arc as below.
class : RasterLayer dimensions : 7258, 10179, 73879182 (nrow, ncol, ncell) resolution : 463.3127, 463.3127 (x, y) extent : 6055308, 10771368, 744727.8, 4107452 (xmin, xmax, ymin, ymax) coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 data source : in memory names : lulc projected values : 1, 19 (min, max)
However, the nrows and ncols differ not allowing me to continue with analysis such as mask() in R. More confusing is that the resolution matches. I know I can change the extent by using extent(lulc_projected)<-extent(foundation raster).
As a solution I resampled the lulc_projected raster as below
After this the rasters match perfectly i.e. extent, resolution, coordinate system and nrows and ncols. But when I view both the lulc_projected_resampled and foundation raster, there is a shift by about 40 km. Refer to this post for a visual example of this shift and comments (with no solution).
So, firstly, why is projectRaster() not giving a raster with the same number of rows and columns? The product at this stage i.e. lulc_projected aligns perfectly with the foundation raster, however I cannot proceed with more analysis in R, such as stack() and mask() because the nrows and ncols do not match.
Secondly, is this not the workflow? If not, what is the workflow to project a raster to match another?