简体   繁体   English

是否有 R 函数来更改栅格的投影?

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

I am trying to plot crash data with a density interactive map however the raster doesn't add projection.我正在尝试使用密度交互式地图绘制崩溃数据,但是栅格没有添加投影。

I have data frame looks like:我有数据框看起来像:

TA_name TA_name crashes崩溃
chch胆小鬼 13223 13223
grey灰色的 2322 2322
buller布勒 123 123

Here is waht I have tried so far:这是我迄今为止尝试过的内容:

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"

However I get an error of:但是我得到一个错误:

Error in CRS(SRS_string = x) : NA

Would this be due to an error in my data这是否是由于我的数据错误地图

My plot runs just in wrong place due to not executing projection.由于没有执行投影,我的情节在错误的地方运行。

When asking a R question, please provide a minimal, self-contained, reproducible example that creates the error.在询问 R 问题时,请提供一个最小的、独立的、可重现的示例来创建错误。 There is no point in adding a long script that we cannot run and does not appear to be related to the question at hand.添加一个我们无法运行且似乎与手头问题无关的长脚本是没有意义的。

I think you should use "EPSG:2193" instead of "epsg:2193"我认为你应该使用“EPSG:2193”而不是“epsg:2193”

Example data示例数据

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

This fails这失败了

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

It works (with a warning) with它适用于(有警告)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM