在管道 R 中将 lat/long 坐标突变为 UTM
mutate lat/long coordinates to UTM in a pipe R
我有一个海岸线空间值的 df,但它们在 lat/lon,我需要它们在 NAD83/UTM 10N 区。我可以通过在 dplyr 管道中进行变异来做到这一点吗?
testdf <- data.frame(long = c(-124.0048, -123.9844, -123.9691, -123.9604, -123.9810, -123.9612),
lat = c(45.04352, 45.10493, 45.20530, 45.29999, 45.34960, 45.40917))
testdf %>% mutate(utm = rgdal::project(c(lon,lat), proj = ???),
# but then I guess I would need to separate the utm column into x and y still.
或者 map()
?
编辑:对不起,我现在意识到这是一个糟糕的代表。实际数据集有投影 +proj=longlat +datum=WGS84 +no_defs
。现在正在将其编辑到 testdf
中。
您可以使用 sf
包:
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
testdf <- data.frame(long = c(-124.0048, -123.9844, -123.9691, -123.9604, -123.9810, -123.9612),
lat = c(45.04352, 45.10493, 45.20530, 45.29999, 45.34960, 45.40917))
testsf = st_as_sf(testdf, coords = c("long", "lat"))
testsf <- st_set_crs(testsf, "+proj=longlat +datum=WGS84")
testsf
#> Simple feature collection with 6 features and 0 fields
#> geometry type: POINT
#> dimension: XY
#> bbox: xmin: -124.0048 ymin: 45.04352 xmax: -123.9604 ymax: 45.40917
#> CRS: +proj=longlat +datum=WGS84
#> geometry
#> 1 POINT (-124.0048 45.04352)
#> 2 POINT (-123.9844 45.10493)
#> 3 POINT (-123.9691 45.2053)
#> 4 POINT (-123.9604 45.29999)
#> 5 POINT (-123.981 45.3496)
#> 6 POINT (-123.9612 45.40917)
utmsf <- st_transform(testsf,"+proj=utm +zone=10 +ellps=GRS80 +datum=NAD83" )
utmsf
#> Simple feature collection with 6 features and 0 fields
#> geometry type: POINT
#> dimension: XY
#> bbox: xmin: 420866.4 ymin: 4988276 xmax: 424783.9 ymax: 5028855
#> CRS: +proj=utm +zone=10 +ellps=GRS80 +datum=NAD83
#> geometry
#> 1 POINT (420866.4 4988276)
#> 2 POINT (422556 4995078)
#> 3 POINT (423893.4 5006214)
#> 4 POINT (424701.9 5016725)
#> 5 POINT (423153.9 5022256)
#> 6 POINT (424783.9 5028855)
由 reprex package (v0.3.0)
于 2020-08-12 创建
关联的管道是:
library(dplyr)
testdf %>% st_as_sf(coords = c("long", "lat")) %>%
st_set_crs("+proj=longlat +datum=WGS84") %>%
st_transform("+proj=utm +zone=10 +ellps=GRS80 +datum=NAD83" )
我有一个海岸线空间值的 df,但它们在 lat/lon,我需要它们在 NAD83/UTM 10N 区。我可以通过在 dplyr 管道中进行变异来做到这一点吗?
testdf <- data.frame(long = c(-124.0048, -123.9844, -123.9691, -123.9604, -123.9810, -123.9612),
lat = c(45.04352, 45.10493, 45.20530, 45.29999, 45.34960, 45.40917))
testdf %>% mutate(utm = rgdal::project(c(lon,lat), proj = ???),
# but then I guess I would need to separate the utm column into x and y still.
或者 map()
?
编辑:对不起,我现在意识到这是一个糟糕的代表。实际数据集有投影 +proj=longlat +datum=WGS84 +no_defs
。现在正在将其编辑到 testdf
中。
您可以使用 sf
包:
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
testdf <- data.frame(long = c(-124.0048, -123.9844, -123.9691, -123.9604, -123.9810, -123.9612),
lat = c(45.04352, 45.10493, 45.20530, 45.29999, 45.34960, 45.40917))
testsf = st_as_sf(testdf, coords = c("long", "lat"))
testsf <- st_set_crs(testsf, "+proj=longlat +datum=WGS84")
testsf
#> Simple feature collection with 6 features and 0 fields
#> geometry type: POINT
#> dimension: XY
#> bbox: xmin: -124.0048 ymin: 45.04352 xmax: -123.9604 ymax: 45.40917
#> CRS: +proj=longlat +datum=WGS84
#> geometry
#> 1 POINT (-124.0048 45.04352)
#> 2 POINT (-123.9844 45.10493)
#> 3 POINT (-123.9691 45.2053)
#> 4 POINT (-123.9604 45.29999)
#> 5 POINT (-123.981 45.3496)
#> 6 POINT (-123.9612 45.40917)
utmsf <- st_transform(testsf,"+proj=utm +zone=10 +ellps=GRS80 +datum=NAD83" )
utmsf
#> Simple feature collection with 6 features and 0 fields
#> geometry type: POINT
#> dimension: XY
#> bbox: xmin: 420866.4 ymin: 4988276 xmax: 424783.9 ymax: 5028855
#> CRS: +proj=utm +zone=10 +ellps=GRS80 +datum=NAD83
#> geometry
#> 1 POINT (420866.4 4988276)
#> 2 POINT (422556 4995078)
#> 3 POINT (423893.4 5006214)
#> 4 POINT (424701.9 5016725)
#> 5 POINT (423153.9 5022256)
#> 6 POINT (424783.9 5028855)
由 reprex package (v0.3.0)
于 2020-08-12 创建关联的管道是:
library(dplyr)
testdf %>% st_as_sf(coords = c("long", "lat")) %>%
st_set_crs("+proj=longlat +datum=WGS84") %>%
st_transform("+proj=utm +zone=10 +ellps=GRS80 +datum=NAD83" )