rgeos::gCentroid() 和 sf::st_centroid() 返回的值是否不同?

2024-03-14

Question

返回的值是否为rgeos::gCentroid() https://www.rdocumentation.org/packages/rgeos/versions/0.3-26/topics/gCentroid and sf::st_centroid() https://www.rdocumentation.org/packages/sf/versions/0.6-0/topics/geos_unary不同?如果是这样,怎么办?

Context

读完后相关命令导出rgeos https://github.com/r-spatial/sf/wiki/Migrating#relevant-commands-exported-by-rgeos内的部分r-spatial/sf wiki https://github.com/r-spatial/sf/wiki/Migrating,我很高兴看到我只需要sf https://cran.r-project.org/web/packages/sf/index.html包 - 并且不再需要导入rgeos https://cran.r-project.org/web/packages/rgeos/index.html包 - 计算给定几何形状的质心。

然而,使用sf::st_centroid()给了我这个警告,这是在这里解决的 https://cran.r-project.org/web/packages/sf/vignettes/sf6.html#although-coordinates-are-longitudelatitude-xxx-assumes-that-they-are-planar:

Warning message:
   In st_centroid.sfc(x = comarea606$geometry) :
   st_centroid does not give correct centroids for longitude/latitude data

该警告促使我测试两种质心检索方法之间的相等性,只是为了确保无论使用哪种方法,坐标都是相同的。

虽然我使用identical() https://stat.ethz.ch/R-manual/R-devel/library/base/html/identical.html and %in% https://stat.ethz.ch/R-manual/R-devel/library/base/html/match.html导致不相同的匹配,all.equal() https://stat.ethz.ch/R-manual/R-devel/library/base/html/all.equal.html以及绘制每种方法的质心的地图appear可以说这两种方法几乎相同。

是否有任何原因导致一种方法会返回与另一种方法不同的一组值?

可重复的例子

# load neccessary packages
library( sf )
library( rgeos )

# load data
comarea606 <-
    read_sf( 
      dsn = "https://data.cityofchicago.org/api/geospatial/cauq-8yn6?method=export&format=GeoJSON" 
      , layer = "OGRGeoJSON" 
    )

# find the centroid of each polygon
comarea606$centroids <-
  st_centroid( x = comarea606$geometry ) %>%
  st_geometry()

# Warning message:
#   In st_centroid.sfc(x = comarea606$geometry) :
#   st_centroid does not give correct centroids for longitude/latitude data

# Ensure the st_centroid() method
# contains identical values to gCentroid()
sf.centroids <-
  st_coordinates( x = comarea606$centroids )

rgeos.centroids <-
  gCentroid(
    spgeom = methods::as( 
      object = comarea606
      , Class = "Spatial"
    )
    , byid = TRUE 
  )@coords


# ensure the colnames are the same
colnames( rgeos.centroids ) <-
  colnames( sf.centroids )

# Test for equality
identical(
  x = sf.centroids
  , y = rgeos.centroids
) # [1] FALSE

all.equal(
  target = sf.centroids
  , current = rgeos.centroids
) # [1] TRUE

# View the first six results
head( sf.centroids )
#           X        Y
# 1 -87.61868 41.83512
# 2 -87.60322 41.82375
# 3 -87.63242 41.80909
# 4 -87.61786 41.81295
# 5 -87.59618 41.80892
# 6 -87.68752 41.97517
head( rgeos.centroids )
#           X        Y
# 1 -87.61868 41.83512
# 2 -87.60322 41.82375
# 3 -87.63242 41.80909
# 4 -87.61786 41.81295
# 5 -87.59618 41.80892
# 6 -87.68752 41.97517

# Identify the numbers which aren't identical
sf.centroids[ , "X" ] %in% rgeos.centroids[ , "X" ]
# [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [11] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [21] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [31] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [41] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [51] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [71] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
sf.centroids[ , "Y" ] %in% rgeos.centroids[ , "Y" ]
# [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [11] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [21] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [31] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [41] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [51] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [71] FALSE FALSE FALSE FALSE FALSE FALSE FALSE

# view results
par( 
  mar = c( 2, 0, 3, 0 )
  , bg = "black"
)
plot( 
  x = comarea606$geometry
  , main = "`sf` v. `rgeos`:\nComparing Centroid Coordinates"
  , col.main = "white"
  , col = "black"
  , border = "white"
)
plot( 
  x = comarea606$centroids
  , add = TRUE
  , pch = 24
  , col = "#B4DDF2"
  , bg  = "#B4DDF2"
  , cex = 1.2
)
points( 
  x = rgeos.centroids[ , "X" ]
  , y = rgeos.centroids[ , "Y" ]
  , pch = 24
  , col = "#FB0D1B"
  , bg  = "#FB0D1B"
  , cex = 0.6
)
legend(
  x = "left"
  , legend = c( 
    "Centroids from `sf::st_coordinate()`"
    , "Centroids from `rgeos::gCentroid()`"
  )
  , col      = c( "#B4DDF2", "#FB0D1B" )
  , pt.bg    = c( "#B4DDF2", "#FB0D1B" )
  , pch      = 24
  , bty      = "n"
  , cex      = 0.9
  , text.col = "white"
)
mtext(
  adj    = 0.99
  , line = 1
  , side = 1
  , cex  = 0.9
  , text = "Source: Chicago Data Portal"
  , col  = "white"
)

# end of script #

会议信息 https://stat.ethz.ch/R-manual/R-devel/library/utils/html/sessionInfo.html

R version 3.4.4 (2018-03-15)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.2

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] rgeos_0.3-26 sf_0.6-0    

loaded via a namespace (and not attached):
 [1] modeltools_0.2-21 kernlab_0.9-25    reshape2_1.4.3   
 [4] lattice_0.20-35   colorspace_1.3-2  htmltools_0.3.6  
 [7] stats4_3.4.4      viridisLite_0.3.0 yaml_2.1.18      
[10] utf8_1.1.3        rlang_0.2.0       R.oo_1.21.0      
[13] e1071_1.6-8       pillar_1.2.1      withr_2.1.2      
[16] DBI_0.8           prabclus_2.2-6    R.utils_2.6.0    
[19] sp_1.2-7          fpc_2.1-11        plyr_1.8.4       
[22] robustbase_0.92-8 stringr_1.3.0     munsell_0.4.3    
[25] gtable_0.2.0      raster_2.6-7      R.methodsS3_1.7.1
[28] devtools_1.13.5   mvtnorm_1.0-7     memoise_1.1.0    
[31] evaluate_0.10.1   knitr_1.20        flexmix_2.3-14   
[34] class_7.3-14      DEoptimR_1.0-8    trimcluster_0.1-2
[37] Rcpp_0.12.16      udunits2_0.13     scales_0.5.0     
[40] backports_1.1.2   diptest_0.75-7    classInt_0.1-24  
[43] squash_1.0.8      gridExtra_2.3     ggplot2_2.2.1    
[46] digest_0.6.15     stringi_1.1.6     grid_3.4.4       
[49] rprojroot_1.3-2   rgdal_1.2-18      cli_1.0.0        
[52] tools_3.4.4       magrittr_1.5      lazyeval_0.2.1   
[55] tibble_1.4.2      cluster_2.0.6     crayon_1.3.4     
[58] whisker_0.3-2     dendextend_1.7.0  MASS_7.3-49      
[61] assertthat_0.2.0  rmarkdown_1.9     rstudioapi_0.7   
[64] viridis_0.5.0     mclust_5.4        units_0.5-1      
[67] nnet_7.3-12       compiler_3.4.4   

这个问题简化为:如何all.equal()不同于identical(),我们查看这些函数的文档:

all.equal(x, y) 是一个用于比较 R 对象 x 和 y 测试“接近相等”的实用程序。

and for identical()

测试两个对象是否完全相同的安全可靠的方法。在这种情况下它返回 TRUE,在其他情况下返回 FALSE。

让我们仔细看看all.equal.numeric(),这就是对这两个对象的调用,因为两者都返回"double" with typeof()。我们看到有一个tolerance论证中all.equal.numeric(),设置为sqrt(.Machine$double.eps), 默认情况下。.Machine$double.eps是您的机器可以添加的最小数字1并能够将其与1。虽然不准确,但也差不多是这个数量级。all.equal.numeric()本质上是检查向量中的所有值是否都是near()另一个向量中的所有值。您可以查看源代码(主要是错误检查)以确切了解它是如何执行此操作的。

让自己相信他们实际上不是identical(),尝试查看输出sf.centroids - rgeos.centroids.

head(sf.centroids - rgeos.centroids)
#               X             Y
# 1 -5.056506e-10  2.623175e-09
# 2 -2.961613e-09 -4.269602e-09
# 3  4.235119e-10  4.358100e-09
# 4 -7.688925e-10 -1.051717e-09
# 5  1.226582e-09  1.668568e-10
# 6 -2.957009e-09  4.875247e-10

毫无疑问,这两个矩阵非常接近相同(但没有一个值完全相同)。

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

rgeos::gCentroid() 和 sf::st_centroid() 返回的值是否不同? 的相关文章

  • 加拿大人口普查地图分区 R

    我对 R 和映射非常陌生 我想创建某些数据的映射 我有一组名为 D Montreal 的数据 显示 2010 年前往蒙特利尔的加拿大人口普查部门游客来自哪个国家 我想使用此数据创建一个地图 以显示有多少人来自不同地区 也许可以通过对根据人数
  • R 中多类分类的 ROC 曲线

    我有一个包含 6 个类别的数据集 我想绘制多类别分类的 ROC 曲线 Achim Zeileis 给出的第一个答案非常好 R中使用rpart包的ROC曲线 https stackoverflow com questions 30818188
  • 在闪亮的应用程序和多个页面中进行身份验证

    在我正在开发的系统中 我有 3 个不同的参与者 用户 管理员 支持团队 使用 Shiny App 我想知道如何向这三个参与者进行身份验证 每个参与者只能访问他们的页面 我发现使用闪亮的服务器专业版可以实现这一点 但它不是免费的 有什么方法可
  • 如何在 conda 中静音或抑制 gfortran (或 clang?)后端?

    我一直致力于构建一个非常特殊的 conda 环境 专为python and R与串扰使用rpy2 我想出的方法可以安装正确的R包如下 install main environment sh now date T echo Start Tim
  • 在 Bookdown 中呈现附录图号

    Bookdown 是一个很棒的软件包 我期待看到它如何发展 但现在我在渲染数字方面遇到了麻烦pdf document2附录中的数字时的格式 具体来说 当带有标题的图形位于附录中时 图形编号应采用 A 1 A 2 B 1 B 2 等形式 但图
  • 为 RStudio Server 1.0.44 配置日志目录

    我在 CentOS 7 上运行 RStudio Server 1 0 44 根据文档 https support rstudio com hc en us articles 200554766 RStudio Server Applicat
  • 从 Cox PH 模型预测概率

    我正在尝试使用 cox 模型来预测时间 称为停止 3 后失败的概率 bladder1 lt bladder bladder enum lt 5 coxmodel coxph Surv stop event rx size number cl
  • 网页抓取(R 语言?)

    我想获取中间栏中的公司名称this http www consumercomplaints in bysubcategory mobile service providers page 1 html页面 以蓝色粗体书写 以及登记投诉者的位置
  • 为什么 rbind 会抛出警告

    这与是否有更优雅的方法将不规则的数据转换为整洁的数据框 https stackoverflow com questions 25102617 are there more elegant ways to transform ragged d
  • left_join 表示列不存在,即使它存在

    我想用两个不同的变量 tp join 连接两个数据框 出现错误 表示无法在第二个数据帧中找到变量 但是当我运行函数 colnames 时 会显示列名称 为什么会这样呢 df new lt left join master settlemen
  • r - 从我的应用程序下载shinyapps代码

    我正在尝试从shinyapps io 在另一台电脑上下载我的shiny 应用程序代码 我按照这个例子 https support rstudio com hc en us articles 204536588 从 shinyapps io下
  • 用闪亮的 R 设计 DT 中的展开行按钮

    我正在尝试设计 DT 中可用的展开行按钮的样式 样式可用here https datatables net examples api row details html 我用于创建数据表的代码是 library DT datatable cb
  • 创建后修改 ggplot 对象

    有没有首选的修改方式ggplot创建后的对象 例如 我建议我的学生将 r 对象与 pdf 文件一起保存以供以后更改 library ggplot2 graph lt ggplot mtcars aes x mpg y qsec fill c
  • R 中的 Mapdeck 包 - add_grid 似乎未渲染任何内容

    Problem The add gridR 中的函数mapdeck包很精彩 然而 遵循CRAN 文档 https cran r project org web packages mapdeck mapdeck pdf 我似乎无法获得任何数据
  • 计算数据框中每一行的 R 条件运行总和

    我想创建一个等于 data Rating 的运行总和的列 假设第 3 列和第 4 列中有两个条件成立 特别是 data Year 换句话说 这应该计算直到上一年为止每个 id 的评分累积总和 它应该对数据框中的每一行 大约 50 000 行
  • 数据表中的 NA

    我有一个data table其中包含一些组 我对每个组进行操作 有些组返回数字 其他组返回NA 因为某些原因data table很难将所有东西重新组合在一起 这是一个错误还是我误解了 这是一个例子 dtb lt data table a 1
  • 粘贴两个 data.table 列

    dt lt data table L 1 5 A letters 7 11 B letters 12 16 L A B 1 1 g l 2 2 h m 3 3 i n 4 4 j o 5 5 k p 现在我想粘贴列 A 和 B 以获得一个新
  • 当我用一个观察值运行回归时,为什么“fastLm()”会返回结果?

    为什么fastLm 当我用一项观察进行回归时返回结果吗 下面为什么不lm and fastLm 结果相等吗 library Rcpp library RcppArmadillo library data table set seed 1 D
  • dplyr 总结小计

    Excel 中数据透视表的一大优点是它们会自动提供小计 首先 我想知道 dplyr 中是否已经创建了任何可以实现此目的的东西 如果没有 实现它的最简单方法是什么 在下面的示例中 我按气缸和化油器的数量显示了平均排量 对于每组气缸 4 6 8
  • 如何在r中进行左连接[重复]

    这个问题在这里已经有答案了 我有两个数据集一和二 数据集一 a b c 111 a 1 112 b 2 113 c 3 114 d 4 115 e 5 数据集二 e d g 222 ss 11 111 ff 22 113 ww 33 114

随机推荐