【问题标题】:Creating sf object from dataframe (UTM)从数据框(UTM)创建 sf 对象
【发布时间】:2019-10-16 00:12:18
【问题描述】:

我是 r 中 sf 包的新手,试图从合作者在 UTM 中给我的一组点创建一个对象。我已经看到人们如何使用经纬度坐标的类似方法,但由于点定义的区域部分而无法获得相同的结果

 can.df <- data.frame(
  rbind(
    c("NW",  "9V", 586518, 7077103),
    c("NE", "13W", 645544, 7118728),
    c("SW", "11T", 680262, 4865141),
    c("SE", "14T", 314095, 497555)),
    stringsAsFactors = F)
colnames(can.df) <- c("Corner", "Zone", "Northing", "Easting")
## make xy numeric
num.cols <- c("Northing", "Easting")
can.df[num.cols] <- sapply(can.df[num.cols], as.numeric)
can.df["Zone"] <- as.character(can.df["Zone"])
test <- st_as_sf(can.df,
                   coords = c("Easting", "Northing", "Zone"),
                   epsg = 2955)

这会给我错误:

points_cpp(pts, gdim) 中的错误:与请求的类型不兼容: [类型=字符;目标=双]。

如果我从区域定义中去除字母,并将其用作数字。然后我收到:

st_sf 中的错误(x,...,agr = agr,sf_column_name = sf_column_name):否 存在简单特征几何列

谁能解释一下我错过了什么?

【问题讨论】:

  • 这些坐标是否都在 EPSG:2955(区域 11N)中,或者Zone 列是否指示坐标所指的 UTM 区域?
  • @Spacedman 感谢您的理解,坐标并不都在同一个zone 内,我假设我尝试使用该 EPSG 过于简单化了。有没有一种方法可以让我像这样阅读它们,或者我需要创建一个更基于点的方法?
  • 我认为您必须逐点构建 st_point 对象,从区域中计算出正确的 EPSG 代码或 PROJ 字符串,然后转换为通用 CRS (例如 4326),如果你想在一个 sf data.frame 中找到很多。 (另外,请can.df$Zone &lt;- as.character(can.df$Zone)) 修复该列)

标签: r sf


【解决方案1】:

尝试从coords 中删除"Zone" 并将epsg 更改为crs。 epsg 不是st_sf 接受的参数。

library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.1, PROJ 6.2.0

can.df <- data.frame(
  rbind(
    c("NW",  "9V", 586518, 7077103),
    c("NE", "13W", 645544, 7118728),
    c("SW", "11T", 680262, 4865141),
    c("SE", "14T", 314095, 497555)),
  stringsAsFactors = F)
colnames(can.df) <- c("Corner", "Zone", "Northing", "Easting")
## make xy numeric
num.cols <- c("Northing", "Easting")
can.df[num.cols] <- sapply(can.df[num.cols], as.numeric)
can.df["Zone"] <- as.character(can.df["Zone"])
test <- st_as_sf(can.df,
                 coords = c("Easting", "Northing", "Zone"),
                 crs = 2955)
#> Warning in lapply(x[coords], as.numeric): NAs introduced by coercion
#> Error in st_as_sf.data.frame(can.df, coords = c("Easting", "Northing", : missing values in coordinates not allowed

test <- st_as_sf(can.df,
                 coords = c("Easting", "Northing"),
                 crs = 2955)
test
#> Simple feature collection with 4 features and 2 fields
#> geometry type:  POINT
#> dimension:      XY
#> bbox:           xmin: 497555 ymin: 314095 xmax: 7118728 ymax: 680262
#> epsg (SRID):    2955
#> proj4string:    +proj=utm +zone=11 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
#>   Corner                         Zone               geometry
#> 1     NW c("9V", "13W", "11T", "14T") POINT (7077103 586518)
#> 2     NE c("9V", "13W", "11T", "14T") POINT (7118728 645544)
#> 3     SW c("9V", "13W", "11T", "14T") POINT (4865141 680262)
#> 4     SE c("9V", "13W", "11T", "14T")  POINT (497555 314095)

由reprex package (v0.3.0) 于 2019 年 10 月 15 日创建

【讨论】:

  • 这个答案似乎只是部分完成。您的解决方案将所有坐标设置为具有 UTM 区域 11N。您将如何使用“区域”列中列出的适当区域中的坐标创建一个 sf 对象?是否必须为每个区域创建不同的 sf 对象,将它们转换为共享的 crs,然后将它们合并在一起?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
  • 2021-01-09
  • 2020-12-20
  • 1970-01-01
相关资源
最近更新 更多