自然邻点插值。计算多边形相交面积时出错

2023-12-28

我正在尝试写this http://en.wikipedia.org/wiki/Natural_neighborR 中的算法。它已经存在于任何包中吗?!?

这就是我所做的(在SO和各种博客文章的帮助下):

library(rgdal)
library(ggmap)
require("maptools")
require("plyr")

locations<- unique(cbind(data22[,1], data22[,2]))
      [,1]    [,2]
  [1,] 24.9317 60.1657
  [2,] 24.9415 60.1608
  [3,] 24.9331 60.1577
  [4,] 24.9228 60.1477
  [5,] 24.9370 60.1545
  [6,] 24.9491 60.1559
  [7,] 24.9468 60.1591
  [8,] 24.9494 60.1675
  [9,] 24.9561 60.1609
 [10,] 24.9218 60.1632
 [11,] 24.9213 60.1605
 [12,] 24.9219 60.1557
 [13,] 24.9208 60.1704
 [14,] 24.9233 60.1714
 [15,] 24.9469 60.1737
 [16,] 24.9440 60.1738
 [17,] 24.9531 60.1714
 [18,] 24.9601 60.1736
 [19,] 24.9304 60.1687
 [20,] 24.9312 60.1659
 [21,] 24.9313 60.1658
 [22,] 24.9418 60.1608
 [23,] 24.9336 60.1577
 [24,] 24.9213 60.1494
 [25,] 24.9415 60.1538
 [26,] 24.9560 60.1620
 [27,] 24.9610 60.1587
 [28,] 24.9142 60.1635
 [29,] 24.9072 60.1636
 [30,] 24.9132 60.1582
 [31,] 24.9166 60.1668
 [32,] 24.9146 60.1742
 [33,] 24.9259 60.1751
 [34,] 24.9308 60.1742
 [35,] 24.9524 60.1690
 [36,] 24.9601 60.1709
 [37,] 24.9570 60.1742
 [38,] 24.9324 60.1655
 [39,] 24.9426 60.1610
 [40,] 24.9332 60.1581
 [41,] 24.9274 60.1480
 [42,] 24.9393 60.1539
 [43,] 24.9466 60.1550
 [44,] 24.9478 60.1593
 [45,] 24.9431 60.1670
 [46,] 24.9559 60.1615
 [47,] 24.9623 60.1581
 [48,] 24.9144 60.1632
 [49,] 24.9077 60.1634
 [50,] 24.9110 60.1575
 [51,] 24.9212 60.1685
 [52,] 24.9193 60.1739
 [53,] 24.9270 60.1752
 [54,] 24.9305 60.1746
 [55,] 24.9517 60.1700
 [56,] 24.9598 60.1710
 [57,] 24.9565 60.1737
 [58,] 24.9306 60.1686
 [59,] 24.9361 60.1621
 [60,] 24.9415 60.1580
 [61,] 24.9312 60.1561
 [62,] 24.9253 60.1528
 [63,] 24.9501 60.1589
 [64,] 24.9467 60.1591
 [65,] 24.9458 60.1630
 [66,] 24.9374 60.1715
 [67,] 24.9438 60.1707
 [68,] 24.9527 60.1674
 [69,] 24.9556 60.1604
 [70,] 24.9205 60.1698
 [71,] 24.9141 60.1633
 [72,] 24.9082 60.1633
 [73,] 24.9118 60.1569
 [74,] 24.9220 60.1683
 [75,] 24.9231 60.1630
 [76,] 24.9475 60.1735
 [77,] 24.9434 60.1735
 [78,] 24.9535 60.1713
 [79,] 24.9605 60.1739
 [80,] 24.9307 60.1685
 [81,] 24.9373 60.1618
 [82,] 24.9402 60.1582
 [83,] 24.9311 60.1560
 [84,] 24.9257 60.1527
 [85,] 24.9485 60.1589
 [86,] 24.9460 60.1635
 [87,] 24.9374 60.1709
 [88,] 24.9519 60.1673
 [89,] 24.9554 60.1595
 [90,] 24.9228 60.1629
 [91,] 24.9215 60.1602
 [92,] 24.9217 60.1556
 [93,] 24.9212 60.1706
 [94,] 24.9239 60.1715
 [95,] 24.9466 60.1735
 [96,] 24.9436 60.1740
 [97,] 24.9532 60.1715
 [98,] 24.9609 60.1738
 [99,] 24.9354 60.1626
[100,] 24.9351 60.1626
[101,] 24.9374 60.1579
[102,] 24.9300 60.1542
[103,] 24.9263 60.1529
[104,] 24.9522 60.1589
[105,] 24.9435 60.1622
[106,] 24.9369 60.1721
[107,] 24.9580 60.1615
[108,] 24.9620 60.1586
[109,] 24.9545 60.1545

# Carson's Voronoi polygons function
# http://stackoverflow.com/a/9405831/489704
# http://www.carsonfarmer.com/2009/09/voronoi-polygons-with-r/
voronoipolygons <- function(x) {
  require(deldir)
  require(sp)
  if (.hasSlot(x, 'coords')) {
    crds <- x@coords  
  } else crds <- x
  z <- deldir(crds[, 1], crds[, 2])
  w <- tile.list(z)
  polys <- vector(mode='list', length=length(w))
  for (i in seq(along=polys)) {
    pcrds <- cbind(w[[i]]$x, w[[i]]$y)
    pcrds <- rbind(pcrds, pcrds[1, ])
    polys[[i]] <- Polygons(list(Polygon(pcrds)), ID=as.character(i))
  }
  SP <- SpatialPolygons(polys)
  voronoi <- SpatialPolygonsDataFrame(SP, data=data.frame(x=crds[, 1],
    y=crds[,2], row.names=sapply(slot(SP, 'polygons'), 
    function(x) slot(x, 'ID'))))
}


v2 <- voronoipolygons(locations)
a=fortify(v2)

bbox<-c(24.90, 60.14, 
        24.97, 60.18)
predgrid <- expand.grid(lon=seq(from=bbox[1], to=bbox[3], length.out=10), 
                        lat=seq(from=bbox[2], to=bbox[4], length.out=10))

N <- 100
loc <- as.matrix(cbind(predgrid[,1:2]))
proj4string(v2) <- CRS("+proj=longlat +ellps=WGS84 +no_defs+ellps=WGS84 
                       +towgs84=0,0,0 ") 
int<-matrix(nrow=N, ncol=109)
for (i in 1:N){
  loc.temp<-rbind(locations, loc[i,])
  vor.temp<-voronoipolygons(loc.temp)
  proj4string(vor.temp) <- CRS("+proj=longlat +ellps=WGS84 +no_defs+ellps=WGS84 
                               +towgs84=0,0,0 ") 
  for (j in 1:109){
    s<-gIntersection(SpatialPolygons(vor.temp@polygons[110]), 
                     SpatialPolygons(vor.temp@polygons[i]))
    if(is.null(s)) {
      int[i,j]=0
    } else {
      # my.area <- vor.temp@polygons[[i]]@Polygons[[1]]@area 
      int[i,j] <- 
        gArea(gIntersection(SpatialPolygons(vor.temp@polygons[i]), 
                            SpatialPolygons(vor.temp@polygons[overlaid.poly])))
    }
  }
}



max(int)

所以基本上我画了一次添加一个点的voronoi缨子,并尝试计算多边形之间的交集,问题是:max(int)给出0,就好像没有插值一样,为什么会这样呢?是计算面积gIntersection正确的?它们的值非常小,我不知道度量单位。

EDIT: Tassellation before new locationhere and the one after it here. I am not sure about the areas given back by the tassellation, that is where I would think the error is, I am not sure though.


这段代码的问题在于第二个 for 循环,其中函数gIntersection已经用过。在此函数中多边形的交集:

SpatialPolygons(vor.temp@polygons[110])

并且 vor.temp 的所有多边形都已计算出来,显然这些多边形之间不会有交集,因为它们都属于一个 Voronoi。在里面gIntersection第二个参数应更改为:

SpatialPolygons(vor.temp@polygons[i])

to:

SpatialPolygons(v2@polygons[i])

Now the gIntersection函数将获得交集多边形SpatialPolygons(vor.temp@polygons[110])以及来自第一个 Voronoi 的所有多边形 (v2)

另一个问题是使用gArea功能。我没有明白使用的目的overlaid.poly。函数的输入gArea是一个多边形,所以我们可以将该函数的输入设置为s因为已经使用获得了相交多边形gIntersection作为对象的函数s。所以在 if 语句中我们会有:

gArea(s)

代替

gArea(gIntersection(SpatialPolygons(vor.temp@polygons[i]),SpatialPolygons(vor.temp@polygons[overlaid.poly])))

由此产生的对象int将是可用于执行插值的权重矩阵。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自然邻点插值。计算多边形相交面积时出错 的相关文章

  • 删除ggplot2 geom_bar中没有数据的日期列[重复]

    这个问题在这里已经有答案了 我想隐藏 ggplot2 中没有数据的列 这是使用 nycflights13 库的可重现示例 library nycflights13 library dplyr library ggplot2 small da
  • 在前两个冒号上分割字符串

    我想在前两个冒号上拆分一列字符串 但不在任何后续冒号上拆分 my data lt read table text my string some data 123 34 56 78 100 87 65 43 21 200 a4 b6 c888
  • 使管道工 API 可通过互联网使用

    我对 R 中的管道工包相当陌生 我有一个可以在我的计算机上本地运行的工作 API 我可以使用以下代码从网络上的实时 JS 应用程序访问它 r lt plumb my api code r r run host 0 0 0 0 port 80
  • R:几个单独图的重新排序因子水平

    我正在尝试从同一个 data frame 创建多个单独的图 每个图的 y 轴上的因子水平顺序不同 每个图都应该对 y 上的因子水平进行递减排序 我知道这可以为每个图手动完成 但我正在寻找一种更有效和更优雅的方法 因为我需要创建相当多的图 这
  • R data.table:在当前测量之前对出现次数进行计数

    我有一组在几天内进行的测量结果 测量次数通常为 4 任何测量中可以捕获的数字范围为 1 5 在现实生活中 给定测试集 范围可能高达 100 或低至 20 我想每天计算每个值在当天之前发生的次数 让我用一些示例数据来解释 test data
  • 在 R 中,如何让 PRNG 在平台之间给出相同的浮点数?

    在 R 4 1 1 中运行以下代码会在平台之间产生不同的结果 set seed 1 x lt rnorm 3 3 print x 22 0 83562861241004716 intel windows 0 8356286124100471
  • 我们如何获取R中的商品价格?

    正如标题 我知道我们可以使用quantmod包来获取股票价格 但我们如何检索黄金 石油或农产品等商品价格 Use Quandl包 这里有一些例子 Gold lt Quandl LBMA GOLD WTI lt Quandl CHRIS CM
  • R:如何在不耗尽内存的情况下重新绑定两个巨大的数据帧

    我有两个数据框df1 and df2每个都有大约 1000 万行和 4 列 我使用 RODBC sqlQuery 将它们读入 R 没有任何问题 但是当我尝试rbind他们 我收到了最可怕的 R 错误消息 cannot allocate me
  • R 版本 4.0.0 上的 ROracle

    当尝试使用 ROracle 时 我收到以下错误消息 gt library ROracle Error package or namespace load failed for ROracle package ROracle was inst
  • 在水平条形图中绘制连续分布

    这是我之前的question https stackoverflow com questions 71318781 multiple variable distribution plot using ggplot2使用多重分布解决了这个问题
  • 在R中重新排序字母数字年龄组

    假设这就是 R 给我的 df1 data frame grp c lt 2 2 5 21 26 27 32 6 10 val rep 0 5 grp val 1 lt 2 0 2 2 5 0 3 21 26 0 4 27 32 0 5 6
  • 通过 read.big.matrix 读取 R 中的大数据

    我正在使用 r 读取尺寸为 3131875 5 的数据read big matrix 我的数据既有字符列又有数字列 包括日期变量 我应该使用的命令是 as1 lt read big matrix C Documents and Settin
  • 在动画 ggplot2 中的轴标签上包含图像

    我创建了一个动画条形图 显示玩家的进球数 虚构 请参阅示例的复制数据 df lt data frame Player rep c Aguero Salah Aubameyang Kane 6 Team rep c ManCity Liver
  • 使用 select_ 和starts_with R

    为什么这段代码不起作用 mtcars gt select starts with d Error in eval expr envir enclos could not find function starts with 这是简化的示例 我
  • 如何使用 dplyr 将 2 个列集的内连接的列名称作为变量传递

    我一直在研究各种将列名作为变量传递的建议方法 例如使用 bang bang xvar as name xvar 和其他各种方法 但我无法让它工作 有谁知道如何传递使用的列名mtcars在下面的管道中作为变量 i e xvar lt mpg
  • 如何修剪 R 向量?

    我有以下排序向量 gt v 1 1 0 1 2 4 5 2 3 4 5 7 8 5 6 7 8 10 11 如何删除 1 0 和 11 条目无需循环整个向量 使用用户循环还是隐式使用语言关键字 也就是说 我想修剪每个向量edge并且仅在每个
  • scale_y_discrete 忽略中断/标签

    漏洞 可能相关对此 https github com tidyverse ggplot2 issues 1589 dat data frame x 1 4 y ordered c 4 gt 5 1 1 levels c 1 5 gt 5 g
  • R:动态创建变量名

    我正在寻找使用 for 循环创建多个数据帧 然后将它们缝合在一起merge 我可以使用创建我的数据框assign paste blah 但是 在同一个 for 循环中 我需要删除每个数据帧的第一列 这是我的代码的相关部分 for j in
  • 如何通过 R 的 cor() 的相关分析计算 P 值和标准误差

    I have data http dpaste com 1064360 plain 其中包含每个条件 x 和 y 的 54 个样本 我通过以下方式计算了相关性 gt dat lt read table http dpaste com 106
  • 将代表扩展到矩阵?

    如果你打电话rep在矩阵上 它重复其元素而不是整个矩阵 传统的修复方法是调用rep list theMatrix 我想延长rep以便它自动执行此操作 我尝试使用 rep matrix lt function x rep list x 这确实

随机推荐