I have a raster that does not contain any crs. After I assign a crs, the raster still appears in another location. Am I missing something?
library(raster)
#> Loading required package: sp
#> The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
#> which was just loaded, will retire in October 2023.
#> Please refer to R-spatial evolution reports for details, especially
#> https://r-spatial.org/r/2023/05/15/evolution4.html.
#> It may be desirable to make the sf package available;
#> package maintainers should consider adding sf to Suggests:.
#> The sp package is now running under evolution status 2
#> (status 2 uses the sf package in place of rgdal)
library(terra)
#> terra 1.7.46
library(exactextractr)
library(sp)
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.3, PROJ 8.2.1; sf_use_s2() is TRUE
r <- raster('data_bw/no_proj.tif',
package = 'exactextractr')
r
#> class : RasterLayer
#> dimensions : 10043, 8921, 89593603 (nrow, ncol, ncell)
#> resolution : 25, 25 (x, y)
#> extent : 3388038, 3611062, 5266712, 5517788 (xmin, xmax, ymin, ymax)
#> crs : NA
#> source : no_proj.tif
#> names : no_proj
crs(r) <- st_crs(32632)$wkt # a WKT string
terra::writeRaster(r, "data_bw/proj.tif", filetype = "GTiff", overwrite = TRUE)
EDIT: this is the information contained in the .prj file.
PROJCS["Bessel_1841_Transverse_Mercator",GEOGCS["GCS_Bessel_1841",DATUM["D_Bessel_1841",SPHEROID["Bessel_1841",6377397.155,299.1528128]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",3500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",9.0],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]
raster
isn't doing anything. For a second I thought this was getting a sample data file from theexactextractr
package but it isn't. We can't run this code without the data file.terra::rast()
which results in a SpatRaster class object. Your code, as is, would not work. There is not a tif file in the exactextractr package and you are reading the data as a RasterLayer object but not coercing to a SpatRaster before usingterra::writeRaster
to write the new tif, I would highly recommend just dropping raster entirely and moving everything to terra. In the long run, your approach will cause numerous issues and confusion.