簡體   English   中英

如何將地理投影應用於R中的柵格文件?

[英]How do I apply a geographic projection to raster files in R?

我試圖從一組60個光柵文件中創建一個大的(空間?)數據幀,用於后續的多變量統計分析(例如,PCA)。

我在使用R中的空間數據方面有些新手,並且不了解空間坐標數據在數據文件中是如何保留或強制執行的。

我使用以下代碼讀取光柵文件並從中創建堆棧:

#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. 

當我導入文件時,為什么R丟失了spaital參考信息? 當我在ArcGIS中查看光柵文件時,他們肯定會有投影信息。

我嘗試以這種方式手動應用投影:

#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)

這不起作用。

有小費嗎? 謝謝!

你應該嘗試這樣的事情:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM