是否有 R 函数来改变光栅的投影?

Is there an R function to change the projection of raster?

我正在尝试使用密度交互式地图绘制崩溃数据,但栅格不添加投影。

我的数据框如下所示:

TA_name crashes
chch 13223
grey 2322
buller 123

这是我到目前为止尝试过的方法:

library(devtools)
install_github("mtennekes/oldtmaptools")
library(oldtmaptools)
# creates a smooth map
# bandwidth is a size of the kernel, default to 1/50th of the shortest side
crash_density <- smooth_map(crashes_TA, bandwidth = 0.5, smooth.raster = T)
tmap_mode("view")
library(viridis) # colour schemes
library(rasterVis) # raster analysis and plotting
plot3D(crash_density$raster, zfac = 0.5, col=viridis_pal(option = "B",direction = -1, alpha = 0.5))
library(spatstat)
# data to ppp class
crashes_pp <- crashes_TA %>% as.ppp()

# just filtering Christchurch City TA
chch <- TA %>%
  filter(TA_name == "chch")
# defining shape of observation window (owin)
# setting it to Christchurch
Window(crashes_pp) <- as.owin(chch)
crashes_pp
# heat map - sigma is a bandwidth, eps - final raster resolution
crashes.kd1 <- density(crashes_pp, sigma = 500, eps = 200)
# airbnb.kd1 <- density(airbnb_pp, sigma = bw.diggle, eps = 200)
image(crashes.kd1)
# multiplying
crashes.kd1 <- crashes.kd1*1000^2
# converting to raster
crashes.kd1 <- raster(crashes.kd1)
# adding crs
crs(crashes.kd1) <- "epsg:2193"

但是我得到一个错误:

Error in CRS(SRS_string = x) : NA

会不会是我的数据有误

由于未执行投影,我的绘图运行在错误的位置。

这是一个产生错误的最小、独立、可重现的示例。

我认为你应该使用“EPSG:2193”而不是“epsg:2193”

示例数据

library(raster)
#Loading required package: sp
r <- raster()

这失败了

crs(r) <- "epsg:2193"
#Error in sp::CRS(SRS_string = x) : NA

它适用于

(有警告)
crs(r) <- "EPSG:2193"
#Warning message:
#In showSRID(SRS_string, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
#  Discarded datum New Zealand Geodetic Datum 2000 in Proj4 definition