gpt4 book ai didi

r - spTransform() : "No transformation possible from NA reference system"

转载 作者:行者123 更新时间:2023-12-04 10:58:59 28 4
gpt4 key购买 nike

我有一个包含 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)

我希望坐标会发生变化,但我不断收到错误消息
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
您需要先 套装 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()
这个错误背后的一些直觉:

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.

关于r - spTransform() : "No transformation possible from NA reference system",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51295558/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com