作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含对角线的多段线的数据集,我想将这些线旋转到垂直或正北,以便正确显示 x 轴数据。我看过 Rodrigues Formula,但它超出了我的理解范围,想知道它们是否是我可以使用的 R 中的一个包。请参阅下面的示例,我需要旋转它。
library(sf)
library(ggplot2)
ex<-structure(list(OBJECTID = c(10526, 10913), geometry = structure(list(
structure(c(-103.47406, -103.46268, 31.47367, 31.48499), .Dim = c(2L,
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(-103.46525,
-103.4788333, 31.4879722000001, 31.4748056), .Dim = c(2L,
2L), class = c("XY", "LINESTRING", "sfg"))), n_empty = 0L, crs = structure(list(
epsg = 4267L, proj4string = "+proj=longlat +datum=NAD27 +no_defs"), class = "crs"), class = c("sfc_LINESTRING",
"sfc"), precision = 0, bbox = structure(c(xmin = -103.4788333,
ymin = 31.47367, xmax = -103.46268, ymax = 31.4879722000001), class = "bbox"))), row.names = c(NA,
-2L), sf_column = "geometry", agr = structure(c(OBJECTID = NA_integer_), .Label = c("constant",
"aggregate", "identity"), class = "factor"), class = c("sf",
"tbl_df", "tbl", "data.frame"))
ggplot(ex)+geom_sf()
包括我所追求的图表
最佳答案
假设所有的线都是平行的,旋转点是所有线的质心,一种方法是
library(sf)
library(sfheaders)
library(ggplot2)
library(geosphere)
## get the centre of the lines
centre <- sf::st_centroid( sf::st_union( ex ) )
## remove class so we just have coordinates as a vector
centre <- unclass( centre[[1]] )
## get each coordinate of the lines. These will each be rotated
coords <- sf::st_coordinates( ex )
## to know the angle of rotation, we need to know due-north from a given point
## under the assumption all lines are parallel, we just need the bearing between the
## start of a line and the end
##
## you're using lon / lat values, so we can use geosphere package to get the bearing
bearing <- geosphere::bearing(
p1 = coords[1, c("X","Y")]
, p2 = coords[2, c("X","Y")]
)
theta <- bearing * pi / 180 ## in radians
#' rotate
#' function to rotate x and y coordinates around a point
#' theta - angle of rotation
#' p - point(s) to rotate
#' centre - centre point
rotate <- function( theta, p, centre ) {
new_x <- cos( theta ) * ( p[, 1] - centre[1] ) - sin( theta ) * ( p[, 2] - centre[2] ) + centre[1]
new_y <- sin( theta ) * ( p[, 1] - centre[1] ) + cos( theta ) * ( p[, 2] - centre[2] ) + centre[2]
return( matrix( c( new_x, new_y ), ncol = 2 ) )
}
## calculate the rotated points
coords_new <- rotate( theta, coords, centre )
## we've kept order in tact, so we can cbind the L1 id back on
coords_new <- cbind( coords_new, coords[, "L1"])
## new sf object (using library(sfheaders) )
sf_new <- sfheaders::sf_linestring( obj = coords_new, linestring_id = 3)
sf::st_crs( sf_new ) <- sf::st_crs( ex )
## plot to verify
ggplot() + geom_sf( data = sf_new ) +
geom_sf( data = ex ) +
geom_sf( data = sf::st_centroid( sf::st_union( ex ) ) )
关于将线旋转到真北或垂直,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58943315/
我是一名优秀的程序员,十分优秀!