- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
执行 rgeos::gCentroid()
返回的值和 sf::st_centroid()
不同?如果是这样,怎么办?
阅读 relevant commands exported by rgeos
后r-spatial/sf
wiki 内的部分,我很高兴看到我只需要 sf
包 - 不再需要导入 rgeos
包 - 计算给定几何图形的质心。
但是,使用 sf::st_centroid()
给了我这个警告,which is addressed here :
Warning message:
In st_centroid.sfc(x = comarea606$geometry) :
st_centroid does not give correct centroids for longitude/latitude data
该警告促使我测试两种质心检索方法之间的相等性,只是为了确保无论使用哪种方法,坐标都是相同的。
虽然我使用identical()
和 %in%
导致不相同的匹配,all.equal()
绘制每种方法质心的 map 似乎表明这两种方法几乎相同。
是否有任何原因导致一个方法返回一组与另一个方法不同的值?
# 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 #
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) is a utility to compare R objects x and y testing ‘near equality’.
对于identical()
The safe and reliable way to test two objects for being exactly equal. It returns TRUE in this case, FALSE in every other case.
让我们仔细看看 all.equal.numeric()
,这是在这两个对象上调用的方法,因为两者都返回 "double"
以及 >typeof()
。我们看到 all.equal.numeric()
中有一个 tolerance
参数,它被设置为 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
这两个矩阵毫无疑问非常接近相同(但没有一个值完全相同)。
关于r - rgeos::gCentroid() 和 sf::st_centroid() 返回的值是否不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49343958/
我有 table 像这样 -------------------------------------------- id size title priority
我的应用在不同的 Activity (4 个 Activity )中仅包含横幅广告。所以我的疑问是, 我可以对所有横幅广告使用一个广告单元 ID 吗? 或者 每个 Activity 使用不同的广告单元
我有任意(但统一)数字列表的任意列表。 (它们是 n 空间中 bin 的边界坐标,我想绘制其角,但这并不重要。)我想生成所有可能组合的列表。所以:[[1,2], [3,4],[5,6]] 产生 [[1
我刚刚在学校开始学习 Java,正在尝试自定义控件和图形。我目前正在研究图案锁,一开始一切都很好,但突然间它绘制不正确。我确实更改了一些代码,但是当我看到错误时,我立即将其更改回来(撤消,ftw),但
在获取 Distinct 的 Count 时,我在使用 Group By With Rollup 时遇到了一个小问题。 问题是 Rollup 摘要只是所有分组中 Distinct 值的总数,而不是所有
这不起作用: select count(distinct colA, colB) from mytable 我知道我可以通过双选来简单地解决这个问题。 select count(*) from (
这个问题在这里已经有了答案: JavaScript regex whitespace characters (5 个回答) 2年前关闭。 你能解释一下为什么我会得到 false比较 text ===
这个问题已经有答案了: 奥 git _a (56 个回答) 已关闭 9 年前。 我被要求用 Javascript 编写一个函数 sortByFoo 来正确响应此测试: // Does not cras
所以,我不得不说,SQL 是迄今为止我作为开发人员最薄弱的一面。也许我想要完成的事情很简单。我有这样的东西(这不是真正的模型,但为了使其易于理解而不浪费太多时间解释它,我想出了一个完全模仿我必须使用的
这个问题在这里已经有了答案: How does the "this" keyword work? (22 个回答) 3年前关闭。 简而言之:为什么在使用 Objects 时,直接调用的函数和通过引用传
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: what is the difference between (.) dot operator and (-
我真的不明白这里发生了什么但是: 当我这样做时: colorIndex += len - stopPos; for(int m = 0; m < len - stopPos; m++) { c
思考 MySQL 中的 Group By 函数的最佳方式是什么? 我正在编写一个 MySQL 查询,通过 ODBC 连接在 Excel 的数据透视表中提取数据,以便用户可以轻松访问数据。 例如,我有:
我想要的SQL是这样的: SELECT week_no, type, SELECT count(distinct user_id) FROM group WHERE pts > 0 FROM bas
商店表: +--+-------+--------+ |id|name |date | +--+-------+--------+ |1 |x |Ma
对于 chrome 和 ff,当涉及到可怕的 ie 时,这个脚本工作完美。有问题 function getY(oElement) { var curtop = 0; if (oElem
我现在无法提供代码,因为我目前正在脑海中研究这个想法并在互联网上四处乱逛。 我了解了进程间通信和使用共享内存在进程之间共享数据(特别是结构)。 但是,在对保存在不同 .c 文件中的程序使用 fork(
我想在用户集合中使用不同的功能。在 mongo shell 中,我可以像下面这样使用: db.users.distinct("name"); 其中名称是用于区分的集合字段。 同样我想要,在 C
List nastava_izvjestaj = new List(); var data_context = new DataEvidencijaDataContext();
我的 Rails 应用程序中有 Ransack 搜索和 Foundation,本地 css 渲染正常,而生产中的同一个应用程序有一个怪癖: 应用程序中的其他内容完全相同。 我在 Chrome 和 Sa
我是一名优秀的程序员,十分优秀!