To conform the Coordinate Reference System (CRS) of a raster layer to another raster layer in R, you can use the projectRaster() function from the raster package[1][2]. Here's a step-by-step guide on how to do this:
Load the raster package:
library(raster)
Load or create the two raster objects, for example, raster1 and raster2. You want to conform the CRS of raster2 to match the CRS of raster1.
Use the crs() function to obtain the CRS of raster1:
raster1_crs <- crs(raster1)
Use the projectRaster() function to reproject raster2 to match the CRS of raster1:
Thank you for the answer I will try somehow to perform this., maybe with some other Raster Layer, as the layer ttops_chm_p2r is not Raster Layer and that is why I can not use this layer to obtain CRS.
My last post was temporarily hidden, probably because of link with the data.
chm_p2r_2180 is virtual raster. As I have erros with on-disk raster I did chm_p2r = readAll(chm_p2r).
chm_p2r_2180 is full of TIFs.
ttops_chm_p2r is shp.
I checked the CRS on QGIS software and both chm_p2r_2180 and ttops_chm_p2r are in CRS 2180.
library(raster)
library(sf)
r <- raster("/home/roc/projects/demo/rasterize_canopy.vrt")
r@crs@projargs
# read in YOUR shape file, however you do it
s <- st_read(system.file("shape/nc.shp", package="sf"))
# use compareCRS() to make sure that both the objects are FULLY identical``
compareCRS(r,s)
# set the crs equal to the crs of r
s <- st_set_crs(s, r)
check again
compareCRS(r,s)
> compareCRS(chm_p2r,ttops_chm_p2r_wj)
[1] TRUE
> ttops_chm_p2r_wj = st_set_crs(ttops_chm_p2r_wj, chm_p2r)
Error in if (is.na(x)) NA_crs_ else if (inherits(x, "crs")) x else if (is.character(x)) { :
argument is not interpretable as logical
> rasterOptions(todisk = TRUE)
> korony_wj = dalponte2016(chm_p2r, ttops_chm_p2r_wj)()
Error in geos_op2_geom("intersection", x, y, ...) :
st_crs(x) == st_crs(y) is not TRUE
I did ttops_chm_p2r = st_cast(ttops_chm_p2r, to = "POINT")after ttops_chm_p2r = st_read("ttops.shp"), because when it was only ttops_chm_p2r = st_read("ttops.shp") there was an error. But I will try once again, as I use this script for several data sets and for some of them all the script works and for some do not.
EDIT:
After only ttops_chm_p2r = st_read("ttops.shp") and crowns = dalponte2016(chm_p2r, ttops_chm_p2r_wj)()there is an Error: treetops must be a spatial object of points