spTransform(): "No transformation possible from NA reference system"
spTransform(): "No transformation possible from NA reference system"
我有 2017 年发生的所有 Stop 和 Frisk 的 table。我得到了它们在长岛坐标中发生的位置的坐标,但我想将其转换为纬度和经度坐标,以便我可以在 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)
我希望坐标会发生变化,但我却一直收到错误消息
No transformation possible from NA reference system
而且我不确定为什么。我以前没有做过很多 CRS 转换。我认为上面的代码应该足以重现问题
未设置 proj4string
(与 CRS 相同),这解释了错误:spTransform(): “No transformation possible from NA reference system”
。
如果您检查 proj4string
,您会发现它是 NA
。
coordinates(saf_spdf) <- ~STOP_LOCATION_X + STOP_LOCATION_Y
proj4string(saf_spdf)
返回:
[1] NA
你需要先SET这个对象的proj4string
,然后你就可以将它转换成[= leaflet()
需要 34=]。
# 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()
此错误背后的一些直觉:
Spatial transforms work like this: (1) R needs to know what reference system you're in to begin with; (2) once we know what the starting reference system is, then and only then can we transform those points into a new reference system. You're getting this error because you haven't specified WHERE to begin. You can't transform something if you don't know where it's starting from. This like asking a computer to "multiply by 5". We need something to "multiply" in the first place! Setting a CRS tells R where to start the transformation.
我有 2017 年发生的所有 Stop 和 Frisk 的 table。我得到了它们在长岛坐标中发生的位置的坐标,但我想将其转换为纬度和经度坐标,以便我可以在 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)
我希望坐标会发生变化,但我却一直收到错误消息
No transformation possible from NA reference system
而且我不确定为什么。我以前没有做过很多 CRS 转换。我认为上面的代码应该足以重现问题
未设置 proj4string
(与 CRS 相同),这解释了错误:spTransform(): “No transformation possible from NA reference system”
。
如果您检查 proj4string
,您会发现它是 NA
。
coordinates(saf_spdf) <- ~STOP_LOCATION_X + STOP_LOCATION_Y
proj4string(saf_spdf)
返回:
[1] NA
你需要先SET这个对象的proj4string
,然后你就可以将它转换成[= leaflet()
需要 34=]。
# 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()
此错误背后的一些直觉:
Spatial transforms work like this: (1) R needs to know what reference system you're in to begin with; (2) once we know what the starting reference system is, then and only then can we transform those points into a new reference system. You're getting this error because you haven't specified WHERE to begin. You can't transform something if you don't know where it's starting from. This like asking a computer to "multiply by 5". We need something to "multiply" in the first place! Setting a CRS tells R where to start the transformation.