r - 如何获取带有轮廓的相交线的坐标 - R

标签 r sp sf

我有一个从互联网上传的形状。基于其计算的质心,我想从中绘制一条 50 度线并找到他与轮廓相交的坐标。知道我该怎么做吗?

谢谢。

脚本:

library(ggplot2)

df = read.table("E:/cloud1.txt")  #stored at https://ufile.io/zq679
colnames(df)<- c("x","y")

meanX <- mean(df$x)
meanY <- mean(df$y)

ggplot(df, aes(x, y))+ geom_point()+ geom_point(aes(meanX, meanY),colour="green",size=2)

最佳答案

这是 sf 的解决方案:

df <- read.table("~/Bureau/cloud1.txt")  #stored at https://ufile.io/zq679
colnames(df) <- c("x","y")
meanX <- mean(df$x)
meanY <- mean(df$y)


# Transform your data.frame in a sf polygon (the first and last points
# must have the same coordinates)
library(sf)
poly <- st_sf(st_sfc(st_polygon(list(as.matrix(df)))))

# Choose the angle (in degrees)
angle <- 50

# Find the minimum length for the line segment to be always 
# outside the cloud whatever the choosen angle
maxX <- max(abs(abs(df[,"x"]) - abs(meanX)))
maxY <- max(abs(abs(df[,"y"]) - abs(meanY)))
line_length = sqrt(maxX^2 + maxY^2) + 1

# Find the coordinates of the 2 points to draw a line with 
# the intended angle.
# This is the gray line on the graph below
line <- rbind(c(meanX,meanY), 
              c(meanX + line_length * cos((pi/180)*angle), 
                meanY + line_length * sin((pi/180)*angle)))
# Transform into a sf line object
line <- st_sf(st_sfc(st_linestring(line)))

# Intersect the polygon and line. The result is a two points line
# shown in black on the plot below
intersect_line <- st_intersection(poly, line)

# Extract only the second point of this line.
# This is the intersecting point
intersect_point <- st_coordinates(intersect_line)[2,c("X","Y")] 

# Visualise this with ggplot
# You might need to install the latest github version :
# devtools::install_github("tidyverse/ggplot2")
library(ggplot2)
ggplot() + geom_sf(data=poly, fill = NA) + 
    geom_sf(data=line, color = "gray80", lwd = 3) + 
    geom_sf(data=intersect_line, color = "gray20", lwd = 1) + 
    geom_point(aes(meanX, meanY), colour="orangered", size=2) +
    geom_point(aes(intersect_point["X"], intersect_point["Y"]), 
                   colour="orangered", size=2) +
    theme_bw()

enter image description here



编辑 2018-07-03

来自原始问题的数据集现已消失。
这是一个完全可重现的示例,带有心形,并且没有使用 geom_sf 来回答评论中的问题。

# Generate a heart shape

t <- seq(0, 2*pi, by=0.1)
df <- data.frame(x = 16*sin(t)^3, 
                 y = 13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t))
df <- rbind(df, df[1,]) # close the polygon

meanX <- mean(df$x)
meanY <- mean(df$y)


# Transform your data.frame in a sf polygon (the first and last points
# must have the same coordinates)
library(sf)
#> Linking to GEOS 3.5.1, GDAL 2.1.3, proj.4 4.9.2
poly <- st_sf(st_sfc(st_polygon(list(as.matrix(df)))))

# Choose the angle (in degrees)
angle <- 50

# Find the minimum length for the line segment to be always 
# outside the cloud whatever the choosen angle
maxX <- max(abs(abs(df[,"x"]) - abs(meanX)))
maxY <- max(abs(abs(df[,"y"]) - abs(meanY)))
line_length = sqrt(maxX^2 + maxY^2) + 1

# Find the coordinates of the 2 points to draw a line with 
# the intended angle.
# This is the gray line on the graph below
line <- rbind(c(meanX,meanY), 
              c(meanX + line_length * cos((pi/180)*angle), 
                meanY + line_length * sin((pi/180)*angle)))
# Transform into a sf line object
line <- st_sf(st_sfc(st_linestring(line)))

# Intersect the polygon and line. The result is a two points line
# shown in black on the plot below
intersect_line <- st_intersection(poly, line)

# Extract only the second point of this line.
# This is the intersecting point
intersect_point <- st_coordinates(intersect_line)[2,c("X","Y")] 

# Visualise this with ggplot and without geom_sf

# you need first transform back the lines into data.frame
line <- as.data.frame(st_coordinates(line))[,1:2]
intersect_line <- as.data.frame(st_coordinates(intersect_line))[,1:2]

library(ggplot2)
ggplot() + geom_path(data=df, aes(x = x, y = y)) + 
    geom_line(data=line, aes(x = X, y = Y), color = "gray80", lwd = 3) + 
    geom_line(data=intersect_line, aes(x = X, y = Y), color = "gray20", lwd = 1) + 
    geom_point(aes(meanX, meanY), colour="orangered", size=2) +
    geom_point(aes(intersect_point["X"], intersect_point["Y"]), 
               colour="orangered", size=2) +
    theme_bw()



创建于 2018-07-03 由 reprex package (v0.2.0)。

关于r - 如何获取带有轮廓的相交线的坐标 - R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48354966/

相关文章:

r - 在 R SF 中交叉大空间数据集

r - 在 R 中的高尔夫网站上抓取排行榜表

r - Tidyeval in own functions in own functions inside own functions with the pipe 管道

r - 如何根据列设置 alpha?

对 sf 对象的行操作

r - 错误 : package or namespace load failed for ‘sf’

r - 使用不带 ggplot 的 Plotly 和 R 堆积面积图

r - ergm/statnet 包可以处理丢失的属性数据吗?

r - 使用 sf 快速创建 SpatialPointsDataFrame

r - 基于 r 中给定距离的 5 个最近邻