Search code examples
rcoordinatesterra

Set the right coordinate system for SpatRaster in terra


I want to map species occurrences and sampling stations on an IBCSO map using the package terra in R. I have my sampling points as latitude, longitude. I don't know how I should define the coordinate system. If I plot my coordinates they all cuddle together at the center of the map. I guess the extend values have to be adjusted. How can I do that without distorting the map?

class       : SpatRaster 
dimensions  : 19200, 19200, 3  (nrow, ncol, nlyr)
resolution  : 500, 500  (x, y)
extent      : -4800000, 4800000, -4800000, 4800000  (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / IBCSO Polar Stereographic (EPSG:9354) 
source      : IBCSO.tif 
colors RGB  : 1, 2, 3 
names       : IBCSO_1, IBCSO_2, IBCSO_3 
min values  :      12,      18,      24 
max values  :     254,     254,     254 

enter image description here


Solution

  • You have coordinates in lon/lat and want to match these to the raster data.

    Example data

    library(terra)
    x <- rast(nrow=19200, ncol=19200, ext=c(-4800000, 4800000, -4800000, 4800000), crs="EPSG:9354")
    lon <- 1:10
    lat <- -(77:86)
     
    

    Solution

    v <- vect(cbind(lon, lat), crs="+proj=longlat")
    p <- project(v, crs(x))