使用`st_centroid`返回点的质心。

huangapple go评论53阅读模式
英文:

Return centroid of points using st_centroid

问题

我想要一组点的质心。我期望st_centroid返回质心,但它似乎只返回原始点集。解决这个问题应该很容易找到,但在Stack Overflow上搜索时找不到答案。

如何让st_centroid返回质心 - 即一组点中心的单个点。

library(dplyr)
library(sf)

df <- read.table(header=TRUE, text= "site   lat      long 
site1  41.21   -115.11
site2   45.3    -112.31
site3  41.15   -115.15 
site4  41.12   -115.19")

df_sf <- st_as_sf(df, coords = c('long', 'lat'))

st_centroid(df_sf)
英文:

I'm after the centroid of a group of points. I expected st_centroid to return the centroid, but it appears to just return the original set of points. It seems the solution to this should be easy to find, but cannot find answer in a good hunt around stack overflow.

How can I get st_centroid to return the centroid - therefore a single point in the centre of a cluster of points.

library(dplyr)
library(sf)

df &lt;- read.table(header=TRUE, text= &quot;site   lat      long 
site1  41.21   -115.11
site2   45.3    -112.31
site3  41.15   -115.15 
site4  41.12   -115.19&quot;)

df_sf &lt;- st_as_sf(df, coords = c(&#39;long&#39;, &#39;lat&#39;))

st_centroid(df_sf)

答案1

得分: 3

我认为问题在于它假设每对坐标是不同的几何体,并试图找到每个点的质心,而实际上质心就是该点。如果你将所有的点集看作是一个单一的多点几何体,会怎样:

library(dplyr)
library(sf)
library(sfheaders)

df <- read.table(header=TRUE, text= "site   lat      long 
site1  41.21   -115.11
site2   45.3    -112.31
site3  41.15   -115.15 
site4  41.12   -115.19")

sf_multipoint(df[,c("long", "lat")]) %>% 
  st_centroid()
#> Simple feature collection with 1 feature and 1 field
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -114.44 ymin: 42.195 xmax: -114.44 ymax: 42.195
#> CRS:           NA
#>   id               geometry
#> 1  1 POINT (-114.44 42.195)

如果只返回质心的坐标值,你可以这样做:

sf_multipoint(df[,c("long", "lat")]) %>% 
  st_centroid() %>%
  st_coordinates()
#>         X      Y
#> 1 -114.44 42.195

创建于2023-03-31,使用 reprex v2.0.2

英文:

I think the problem is that it assumes each coordinate pair is a different geometry and is trying to find the centroid of each point, which is just that point. What if you treated the entire set of points as a single multi-point geometry:

library(dplyr)
library(sf)
library(sfheaders)


df &lt;- read.table(header=TRUE, text= &quot;site   lat      long 
site1  41.21   -115.11
site2   45.3    -112.31
site3  41.15   -115.15 
site4  41.12   -115.19&quot;)

sf_multipoint(df[,c(&quot;long&quot;, &quot;lat&quot;)]) %&gt;% 
  st_centroid()
#&gt; Simple feature collection with 1 feature and 1 field
#&gt; Geometry type: POINT
#&gt; Dimension:     XY
#&gt; Bounding box:  xmin: -114.44 ymin: 42.195 xmax: -114.44 ymax: 42.195
#&gt; CRS:           NA
#&gt;   id               geometry
#&gt; 1  1 POINT (-114.44 42.195)

To just return the coordinate values of the centroid, you could do the following:

sf_multipoint(df[,c(&quot;long&quot;, &quot;lat&quot;)]) %&gt;% 
  st_centroid() %&gt;%
  st_coordinates()
#&gt;         X      Y
#&gt; 1 -114.44 42.195

<sup>Created on 2023-03-31 with reprex v2.0.2</sup>

答案2

得分: 1

只翻译代码部分:

library(dplyr)
library(sf)

df <- read.table(header=TRUE, text= "site   lat      long 
site1  41.21   -115.11
site2   45.3    -112.31
site3  41.15   -115.15 
site4  41.12   -115.19")

df_sf <- st_as_sf(df, coords = c('long', 'lat'))

st_centroid(df_sf) # your original code; not quite what we want...
# Simple feature collection with 4 features and 1 field
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: -115.19 ymin: 41.12 xmax: -112.31 ymax: 45.3
# CRS:           NA
#    site              geometry
# 1 site1 POINT (-115.11 41.21)
# 2 site2  POINT (-112.31 45.3)
# 3 site3 POINT (-115.15 41.15)
# 4 site4 POINT (-115.19 41.12)

df_mod <- df_sf %>% 
  st_combine()

st_centroid(df_mod) # would this fit your expectations better?
# Geometry set for 1 feature 
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: -114.44 ymin: 42.195 xmax: -114.44 ymax: 42.195
# CRS:           NA
# POINT (-114.44 42.195)
英文:

Are you after 1 centroid of your whole dataset?

If so, consider combining all your points to one feature using a sf::st_combine() call before calculating the centroid.

Because an example is more than words:

library(dplyr)
library(sf)

df &lt;- read.table(header=TRUE, text= &quot;site   lat      long 
site1  41.21   -115.11
site2   45.3    -112.31
site3  41.15   -115.15 
site4  41.12   -115.19&quot;)

df_sf &lt;- st_as_sf(df, coords = c(&#39;long&#39;, &#39;lat&#39;))

st_centroid(df_sf) # your original code; not quite what we want...
# Simple feature collection with 4 features and 1 field
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: -115.19 ymin: 41.12 xmax: -112.31 ymax: 45.3
# CRS:           NA
#    site              geometry
# 1 site1 POINT (-115.11 41.21)
# 2 site2  POINT (-112.31 45.3)
# 3 site3 POINT (-115.15 41.15)
# 4 site4 POINT (-115.19 41.12)

df_mod &lt;- df_sf %&gt;% 
  st_combine()

st_centroid(df_mod) # would this fit your expectations better?
# Geometry set for 1 feature 
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: -114.44 ymin: 42.195 xmax: -114.44 ymax: 42.195
# CRS:           NA
# POINT (-114.44 42.195)

huangapple
  • 本文由 发表于 2023年3月31日 21:55:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定