0

I have two dataframes in R. One, df_points, has spatial information saved as points. The other, df_raster, is a raster file. I want to extract information from the raster to the points using:

rasValue = extract(df_raster, df_points)

However, they use different projections:

crs(df_raster)
CRS arguments:
 +proj=lcc +lat_0=33.75 +lon_0=-79 +lat_1=34.3333333333333 +lat_2=36.1666666666667 +x_0=609601.22
+y_0=0 +ellps=GRS80 +units=m +no_defs 
crs(df_points)
CRS arguments:
 +proj=lcc +lat_0=33.75 +lon_0=-79 +lat_1=36.1666666666667 +lat_2=34.3333333333333
+x_0=609601.219202438 +y_0=0 +datum=NAD83 +units=us-ft +no_defs 

I want to reproject the raster data to match the df_points data. I thought I could use the sf package with st_transform, which has worked when I was just using simple features spatial data, however, this spits out an error and doesn't seem to work with rasters.

df_raster<- st_transform(
  df_raster,
  crs=st_crs(df_points)
)

Error in UseMethod("st_transform") : 
  no applicable method for 'st_transform' applied to an object of class "c('RasterLayer', 'Raster', 'BasicRaster')"

How else can I do this?

1
  • 3
    Way mor efficient to reproject your points using sf::st_transform. You can pull the correct CRS from the raster usingsf::st_crs(raster::crs(x)) Nov 19, 2021 at 1:19

1 Answer 1

1

"I have two dataframes in R. One, df_points, has spatial information saved as points. The other, df_raster, is a raster file"

Firstly, df_raster is not a data frame. Its also not a raster file. Its an object of class "RasterLayer" (and "Raster" and "BasicRaster") from the raster package. (I'm getting this from the error message you show, but its always a good idea here to show some output from your data, maybe the result of summary on your data or some maps.)

This means the functions to reproject it are going to be in the raster package. A little search should find projectRaster and the help page shows:

pr1 <- projectRaster(r, crs=newproj)

That said, as noted in comments, its nearly always better to project point, polygons, and lines data (vector data) to coordinate systems of rasters, since transforming rasters involves a non-reversible transformation with some information loss. The only time you should have to transform a raster is when you have two rasters with different coordinate systems and you need to match them up for further processing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.