R中几何形状的计算

Calculations on geometrical shapes in R

我想在 R 中显示相交的几何形状并对其进行一些计算(例如面积、大小)。有人可以告诉我该怎么做吗? (之前的措辞“有人可以为此推荐一个合适的包”导致问题被关闭,但这真的是我所需要的,我熟悉解析几何)。

互联网上的广泛研究导致了关于地理对象和球面几何的负载material。在这里,我只对笛卡尔坐标中的普通二维形状感兴趣。

举个简单的例子就好了:一个圆和一个矩形(大小和位置不重要)相交,估计相交区域的最小外圆的面积和半径。

我可以使用图像分析库来完成这项工作并立即准备就绪,但 R 确实是进行以下评估的首选工具。

您可能想要使用 sf 包。虽然这个包最常用于地理空间数据,但它也可以处理几何交叉点。让我提供一个计算矩形和椭圆形重叠的基本示例。我还将简要地使用 plotrix 包来生成椭圆形的数据,因为您没有在问题中放置示例数据。

library(sf)
plot(0)
#make a oval, then put oval points in a dataframe
oval<-plotrix::draw.circle( 0, 0, .5 ) %>% data.frame(.)

#combine oval data points into an sf object. Convert points to polygon
oval_sf <- sf::st_as_sf(oval, coords = c("x", "y")) %>% 
  sf::st_combine(.) %>% 
  sf::st_cast(., "POLYGON")

#Make a rectangle. I make a 1 X 1.5 rectangle in the example.
rectangle <-sf::st_polygon(list(cbind(c(0, 0, 1.5, 1.5, 0), c(0, 1, 1, 0, 0))))

#Sum of area of the two separate shapes
total_area <- st_area(rectangle) + st_area(oval_sf)
#Sum of area of two shapes if combined into one shape
combined_area<-st_area(st_union(oval_sf,rectangle))
#Then calculate the area of the intersection
intersection_area <- total_area - combined_area
intersection_area
[1] 0.4716912

如需说明 sf 中更复杂的交叉点,请查看: https://r-spatial.github.io/sf/reference/geos_binary_ops.html