r中几何形状的计算

  • 本文关键字:计算 几何形 r geometry
  • 更新时间 :
  • 英文 :


我想显示和做一些计算(例如面积,大小)的几何形状相交,在R.有人能告诉我怎么做吗?(之前的措辞)"有人能推荐一个合适的套餐吗?"导致问题被封闭,但这真的是我所需要的,我熟悉解析几何)。

在互联网上的广泛研究产生了大量关于地理物体和球面几何的材料。这里我只对笛卡尔坐标下的平面二维形状感兴趣。

一个简单的例子会很好:一个圆和一个矩形相交(大小和位置不重要),并且估计相交区域的最小周围圆的面积和半径。

我可以使用图像分析库来完成这项工作,并立即准备好,但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

最新更新