以给定角度创建通过点的空间线

问题描述 投票:0回答:1

我有一个空间多边形,例如格陵兰的形状:

library("rnaturalearth")
Greenland <- ne_countries(scale = "medium", country="Greenland")
plot(Greenland)

而且我知道如何计算其几何中心,即质心:

library("rgeos")
Greenland_cent <- gCentroid(Greenland)
plot(Greenland_cent,add=T,col="red")

现在,我想创建所有角度都穿过质心的线,以将格陵兰岛切成两半(大约,编辑:如@Henry所述,这可能不完全是一半)。但是,我还没有找到允许创建由角度指定的线的任何方法......任何想法?

或者还有其他方法可将多边形切成两个大小大致相同的部分吗?

r polygon spatial angle centroid
1个回答
0
投票

我使用三角法实现了这条线。我以为您总是希望线条在格陵兰岛上或外面开始和结束,因此我找到了最小的封闭圆并使线条在此上开始或结束。将形状切成两个是更多的工作,但我想您可以使用坐标创建两个半圆,并使用它们掩盖原始的格陵兰形状以制作两个新圆。从那里,形心和区域都很容易。

library(rnaturalearth)
library(sp)
library(rgeos)
library(raster)
projection <- '+proj=stere +lat_0=90 +lat_ts=90 +lon_0=-33 +k=0.994 +x_0=2000000 +y_0=2000000 +datum=WGS84 +units=m +no_defs '
Greenland.raw <- ne_countries(scale = "medium", country="Greenland")
# use a different projection to avoid some warnings
Greenland = spTransform(Greenland.raw, CRSobj = projection)
Greenland_cent <- gCentroid(Greenland)
# Make a simpler Greenland shape - expand and contract to smooth out the crinkly edges 
SimplerGreenland <- gBuffer(gBuffer(Greenland, width = 1), width=-1)
# Make a line from the shape
SimplerGreenlandLine <- as(SimplerGreenland, 'SpatialLines')
# sample all around the simpler line
PointsAroundGreenland <- spsample(x = SimplerGreenlandLine, n = 1000, type = 'regular')
# find the furthest point from the centroid
distances <- gDistance(PointsAroundGreenland, Greenland_cent, byid = T)
furthestpointfromcentroid <- distances[which.max(distances)]
# make the smallest circle that encloses Greenland to ensure that any line we draw will be in Greenland
enclosingcircle <- gBuffer(spgeom = Greenland_cent, width = furthestpointfromcentroid, quadsegs = 100)
# choose an angle and make a line that goes through the centroid and starts and ends on the enclosing circle
angleindegrees <- 5
angle <- angleindegrees * pi / 180
dx <- furthestpointfromcentroid * cos(angle)
dy <- furthestpointfromcentroid * sin(angle)
centroid <- as.numeric(coordinates(Greenland_cent))
x1 <- centroid[1] + dx
y1 <- centroid[2] + dy
x2 <- centroid[1] - dx
y2 <- centroid[2] - dy
l1 <- Line(cbind(c(x1,x2), c(y1,y2)))
l2 <- Lines(list(l1), ID = 'line')
lineatangle <- SpatialLines(list(l2))
crs(lineatangle) <- crs(Greenland)

# plot everything
plot(Greenland)
plot(Greenland_cent, add = T, col = 'red')
plot(lineatangle, add = T, col = 'green')

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.