英文:
Coordinates to UTM with R
问题
p <- c("a", "b", "c")
x <- c("41.434931824218786", "41.42672694456612", "41.647157029217425")
y <- c("2.1759165563219116", "2.1954430374270455", "1.4942063178224638")
df <- data.frame(p, x, y)
我想将这些坐标转换为UTMX和UTMY坐标。
英文:
p<-c("a","b","c")
x<-c("41.434931824218786","41.42672694456612","41.647157029217425")
y<-c("2.1759165563219116","2.1954430374270455","1.4942063178224638")
df<-data.frame(p,x,y)
I want to convert these coordinates into UTMX y UTMY.
答案1
得分: 0
另一种解决方法是直接使用st_as_sf
函数,指定x和y坐标的列名以及其坐标参考系统(crs)。此外,如果您希望保留其他列的信息(例如,p列),可以使用mutate
函数将坐标添加为新列。最后,如果您不再需要几何列,可以使用st_drop_geometry
函数将其删除。
library(sf)
library(dplyr)
df |>
# 将df从数据框转换为sf对象
st_as_sf(coords = c("x", "y"),
crs = 4326) |>
# 将数据重投影到新的坐标参考系统
st_transform(23031) |>
# 将坐标作为新列获取
mutate(coords = st_coordinates(geometry)) |>
# 删除几何列
st_drop_geometry()
# p coords.X coords.Y
#1 a 5137810.8 307601.4
#2 b 5136560.9 310324.5
#3 c 5170504.7 211895.1
英文:
Another solution is to use directly st_as_sf
indicating the columns with the x and y coordinates, as well as its crs. Also, if you wish to retain the other columns info (e.g., p column) you can use mutate
to add the coordinates as new columns. Finally, if you are no longer interested in the geometry column, you can remove it using st_drop_geometry
.
library(sf)
library(dplyr)
df |>
# Transform df from data frame to sf object
st_as_sf(coords = c("x", "y"),
crs = 4326) |>
# Reproject data to new crs
st_transform(23031) |>
# Obtain coordinates as new columns
mutate(coords = st_coordinates(geometry)) |>
# Drop geometry column
st_drop_geometry()
# p coords.X coords.Y
#1 a 5137810.8 307601.4
#2 b 5136560.9 310324.5
#3 c 5170504.7 211895.1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论