如何在R中计算xy点的周长?

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

我想计算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 polygon shapes area
1个回答
0
投票

这将会是个好办法。

示例数据

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

enter image description here

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