【问题标题】:How do I calculate perimeter of xy points in R?如何计算R中xy点的周长?
【发布时间】:2020-09-20 19:59:23
【问题描述】:

我正在计算 xy 点的周长(包括按顺序排列的所有点),其中第一个点和最后一个点并不总是相等。我无法弄清楚为什么下面用于计算周长的代码不起作用。

理想情况下,我想要一种在第一个和最后一个 xy 点不同时进行标记的方法。

谢谢

  m
       x       y
 [1,]  606.3   95.4
 [2,]  612.4  178.7
 [3,]  610.2  222.6
 [4,]  610.2  222.8
 [5,]  625.8  249.8
 [6,]  625.8  250.1
 [7,]  633.9  268.9
 [8,]  668.7  272.2
 [9,]  693.7  222.6
[10,]  723.2  157.0
[11,]  738.6  109.9
[12,]  681.2   90.5
[13,]  606.3   95.4
[14,]  833.3  154.6
[15,]  753.7  267.5
[16,]  747.8  305.1
[17,]  773.8  354.7
[18,]  767.0  393.8
[19,]  763.0  442.0
[20,]  817.4  446.9
[21,]  817.6  446.9
[22,]  840.2  412.3
[23,]  892.1  317.7
[24,]  875.3  218.8
[25,]  833.3  154.6


library(geosphere)
perimeter(m)


**Error in perimeter(m) : could not find function "perimeter"**

【问题讨论】:

  • 你安装了这个包吗?第一次在计算机上使用它时需要安装它,之后每次打开新的 R 环境时都需要library() 调用。你可以用install.packages("geosphere")安装它
  • 是的 - 我已经安装了 geosphere 包

标签: r polygon shapes area


【解决方案1】:

这样就可以了..

样本数据

library( data.table )
m <- fread("x       y
  606.3   95.4
  612.4  178.7
  610.2  222.6
  610.2  222.8
  625.8  249.8
  625.8  250.1
  633.9  268.9
  668.7  272.2
  693.7  222.6
  723.2  157.0
  738.6  109.9
  681.2   90.5
  606.3   95.4
  833.3  154.6
  753.7  267.5
  747.8  305.1
  773.8  354.7
  767.0  393.8
  763.0  442.0
  817.4  446.9
  817.6  446.9
  840.2  412.3
  892.1  317.7
  875.3  218.8
  833.3  154.6")

代码

library( grDevices )

hull <- chull( m ) #create coordinates of convex hull
coords <- m[ c( hull, hull[1]), ]  # create a closed perimeter-polygon

#plot it
plot(m)        #plot points
lines(coords, col="red") #plot perimeter

【讨论】:

  • 非常感谢!我应该在问题中指定 - 是否可以对其进行编辑,以便图中的所有点都包含在周长中(按表中的顺序)?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-17
  • 1970-01-01
相关资源
最近更新 更多