【问题标题】:R doesn't like my coordinates - st_as_sf()R 不喜欢我的坐标 - st_as_sf()
【发布时间】:2020-09-16 21:31:52
【问题描述】:

尝试通过这篇文章 (How to Convert data frame to spatial coordinates) 将数据框转换为 sf 对象。但是我不断收到以下错误.. Error in `[.default`(x, coords) : only 0's may be mixed with negative subscripts

我试图根据这篇文章 (R debugging: "only 0's may be mixed with negative subscripts") 解决它,因为我的坐标是全局的,所以 R 不喜欢值的范围。但是,我将其子集到西北半球并收到相同的错误。下面是我正在使用的代码

dat.sf <- st_as_sf(x=dat2,
                   coords = c(dat2$centroid_lon, dat2$centroid_lat),
                   crs= "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")

其中 dat2 只是与该纬度和经度相关联的风暴事件的坐标和唯一 ID(组 ID)。

更新:我尝试了其他一些方法,例如:

coordinates <- as.data.frame(cbind(dat2$centroid_lon, dat2$centroid_lat))
dat.sf <- st_as_sf(x=dat2,
                   coords = coordinates,
                   crs= "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")

并收到了这个Error in `[.default`(x, coords) : invalid subscript type 'list'

还有这个……

coordinates <- cbind(dat2$centroid_lon, dat2$centroid_lat)
dat.sf <- st_as_sf(x=dat2,
                   coords = coordinates,
                   crs= "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")

并收到此Error in as.matrix(x)[i] : subscript out of bounds。 traceback() 告诉我这绝对是我的坐标问题。

【问题讨论】:

    标签: r debugging gis coordinate-systems sf


    【解决方案1】:

    如果您查看 st_as_sf 的文档,您会看到 coords 参数应该是:

    坐标: 如果是点数据:保存坐标的数字列的名称或编号

    所以您需要传递 列的名称,但您传递的是 列本身。您还将看到coords 在您链接的答案中的使用方式。因此,如果你这样做:

    dat.sf <- st_as_sf(x=dat2,
                       coords = c("centroid_lon", "centroid_lat"),
                       crs= "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
    

    你应该得到正确的结果。

    【讨论】:

    • 这行得通 - 总是最简单的解决方案,不是吗?
    • @DPek 是的,但只有当你知道它之后,它才会变得简单!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多