将纬度和经度值转换为 sf 坐标时出错

问题描述 投票:0回答:2

我有一系列坐标,我试图在简单的地图上绘制它们。我有两列,分别包含每个点的纬度和经度。我正在尝试使用

st_as_sf
函数将此数据转换为 sf 点,但我不断收到大量错误,最近一次是

"Error in UseMethod("st_as_sf") : 
no applicable method for 'st_as_sf' applied to an object of class "c('double', 'numeric')"

我尝试将数据的类别更改为数字、双精度、整数等,但我仍然收到此错误。

以下是我的数据示例

point      lat       long
1          38.254    -76.712
2          38.123    -76.710
3          38.438    -76.699
4          38.254    -76.712
5          38.232    -76.733

st_as_sf(coords=c(lat, long), crs=4326)
coordinates latitude-longitude r-sf
2个回答
0
投票

您需要处理

sf::st_as_sf()
函数的参数;首先必须是您的数据对象,很可能是一个包含名为纬度和经度的列的数据框。

在第二个地方,您指定

coords
,它应该是包含在数据对象中的变量名称的字符串向量 - 因此名称“lat”和“long”预计用引号引起来(这些不是 R 变量,但列名称)。

第三,需要一个坐标参考系统来理解第二步中提供的坐标;如果是 WGS84,则为 4326。

所以考虑这段代码;它在形式上是正确的,但将您的点放置在南极洲的某个地方(如果您将坐标翻转为长纬度,则这些点将放置在华盛顿特区)。

library(sf)

raw_pts <- data.frame(point = 1:5,
                      lat = c(38.254, 38.123, 38.438, 38.254, 38.232),
                      long = c(-76.712, -76.710, -76.699, -76.712, -76.733))

points <- st_as_sf(raw_pts, # first argument = data frame with coordinates
                   coords = c("lat", "long"), # name of columns, in quotation marks
                   crs = 4326) # coordinate reference system to make sense of the numbers
points

# Simple feature collection with 5 features and 1 field
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: -76.733 ymin: 38.123 xmax: -76.699 ymax: 38.438
# Geodetic CRS:  WGS 84
#   point               geometry
# 1     1 POINT (38.254 -76.712)
# 2     2  POINT (38.123 -76.71)
# 3     3 POINT (38.438 -76.699)
# 4     4 POINT (38.254 -76.712)
# 5     5 POINT (38.232 -76.733)

0
投票

(抱歉添加为答案,我无法添加评论)

我的 df 的纬度和经度格式为:23°38'34.4'' 和 72°27'38.5'',通常在 GIS 项目的实时数据集中找到,但这些坐标不被 coords=c 接受(经纬度),crs=4326)。

© www.soinside.com 2019 - 2024. All rights reserved.