How to apply geographic projection to raster files in R?

I am trying to create a large (spatial?) DataFrame for use in subsequent multivariate statistical analyzes (e.g. PCA) from a set of 60 raster files.

I am somewhat new to working with spatial data in R and don’t understand how spatial coordinate data is saved or not stored in data files.

I used the following code to read in raster files and created a stack from them:

#load libraries
library(raster)
library(sp)
library(rgdal)
library(spatial.tools) 

#set wd
setwd("C:/Users/...../data/")

#get raster files from wd 
files <- list.raster.files(path = getwd(), pattern = ".tif$", recursive =FALSE, return_rasters = FALSE, return_bbox = FALSE)

#create rasterStack
mystack <- stack(files$raster_files)

#read metadata / summary of rasterStack
mystack
#output has "NA" for coordinate reference system. 

Why did R lose spike help when I imported files? When I look at raster files in ArcGIS, they definitely have projection information.

I tried to manually apply the projection this way:

#first define the projection using the proj4 syntax 
projection <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs"

#then apply the projection to the rasterStack
projectRaster(files, projection, method = bilinear, filename = "STACKprj")

, ( "mystack" ) NA , ..

#I try instead to apply the projection in this way: 
STACKprj <- projection(mystack)

.

- ? !

+4
1

- :

library(raster)
library(sp)
library(rgdal)

#work wd
wd <- "C:/Users/...../data"

#get raster files from work wd 
files <- list.raster.files(path = wd, pattern = ".tif$",
  recursive =FALSE, return_rasters = FALSE, return_bbox = FALSE)

#create rasterStack
mystack <- stack(files$raster_files)
proj4string(mystack) <- CRS("+init=epsg:3857") # OSM Mercator projection
+4

Source: https://habr.com/ru/post/1541348/


All Articles