坐标转UTM与R

huangapple go评论74阅读模式
英文:

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&lt;-c(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;)
x&lt;-c(&quot;41.434931824218786&quot;,&quot;41.42672694456612&quot;,&quot;41.647157029217425&quot;)
y&lt;-c(&quot;2.1759165563219116&quot;,&quot;2.1954430374270455&quot;,&quot;1.4942063178224638&quot;)
df&lt;-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 |&gt;
# 将df从数据框转换为sf对象
  st_as_sf(coords = c("x", "y"),
           crs = 4326) |&gt;
  # 将数据重投影到新的坐标参考系统
  st_transform(23031) |&gt;
  # 将坐标作为新列获取
  mutate(coords = st_coordinates(geometry)) |&gt;
  # 删除几何列
  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 |&gt;
# Transform df from data frame to sf object
  st_as_sf(coords = c(&quot;x&quot;, &quot;y&quot;),
           crs = 4326) |&gt;
  # Reproject data to new crs
  st_transform(23031) |&gt;
  # Obtain coordinates as new columns
  mutate(coords = st_coordinates(geometry)) |&gt;
  # 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

huangapple
  • 本文由 发表于 2023年7月3日 23:19:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76606102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定