spTransform():“无法从 NA 参考系统进行转换”

Cam*_*ron 4 r proj

我有一个包含 2017 年发生的所有 Stop 和 Frisks 的表格。我得到了它们在长岛坐标中发生位置的坐标,但我想将其转换为纬度和经度坐标,以便我可以在 Leaflet 中绘制它。

我有以下代码片段:

library(sp)
library(dplyr)

fd <- "https://www1.nyc.gov/assets/nypd/downloads/excel/analysis_and_planning/stop-question-frisk/sqf-2017.csv"
stop_and_frisk <- read.csv(fd)

saf <- stop_and_frisk %>% filter(STOP_FRISK_ID < 5 ) # filtering to keep data small
saf_spdf <- saf

coordinates(saf_spdf) <- ~STOP_LOCATION_X + STOP_LOCATION_Y
CRS_obj <- CRS('+proj=lcc +lat_1=41.03333333333333 +lat_2=40.66666666666666 +lat_0=40.16666666666666 +lon_0=-74 +x_0=300000.0000000001 +y_0=0 +ellps=GRS80 +datum=NAD83 +to_meter=0.3048006096012192 +no_defs')
spTransform(saf_spdf, CRS_obj)
Run Code Online (Sandbox Code Playgroud)

我希望坐标会发生变化,但我不断收到错误消息

No transformation possible from NA reference system

我不知道为什么。我之前没有做过很多 CRS 转换。我认为上面的代码应该足以重现问题

Ric*_*loo 6

proj4string(同CRS)没有设定,这解释了错误:spTransform(): “No transformation possible from NA reference system”

如果您检查proj4string,您会看到它是NA

coordinates(saf_spdf) <- ~STOP_LOCATION_X + STOP_LOCATION_Y
proj4string(saf_spdf)
Run Code Online (Sandbox Code Playgroud)

返回:

[1] NA
Run Code Online (Sandbox Code Playgroud)

您需要首先设置proj4string这个对象,然后你可以改变它的纬度/经度那leaflet()需要。

# make data.frame a spatial object
coordinates(saf_spdf) <- ~STOP_LOCATION_X + STOP_LOCATION_Y

# SET the CRS of the object
proj4string(saf_spdf) <- CRS('the CRS of these coordinates as a character string')
# NOW we can transform to lat/lon
new <- spTransform(saf_spdf, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))

# and finally, leaflet will accept this spatial object
new %>% leaflet()
Run Code Online (Sandbox Code Playgroud)

这个错误背后的一些直觉:

空间变换的工作原理如下:(1) R 需要知道您在开始时所处的参考系统;(2)一旦我们知道起始参考系是什么,那么我们就可以将这些点转换成一个新的参考系。您收到此错误是因为您尚未指定WHERE开始。如果你不知道它从哪里开始,你就无法改变它。这就像要求计算机“乘以 5”。我们首先需要一些东西来“倍增”!设置 CRS 告诉 R 从哪里开始转换。