如何计算 r 中多边形文件的质心
How to calculate the centroid of a polygon shape file in r
我需要将它上传到 R 并根据经度和纬度提取多边形的质心,以便我可以用它来计算两个质心之间的距离。
到目前为止我已经这样做了:
PC4shape <- read_sf(dsn = "georef-netherlands-postcode-pc4", layer = "georef-netherlands-postcode-pc4")
PC4shape$centroids <- st_transform(PC4shape, 28992) %>%
st_centroid() %>%
st_transform(., '+proj=longlat +ellps=GRS80 +no_defs') %>%
st_geometry()
关注
但是,我确定我的投影和 EPSG 有误。我只是不知道如何找到合适的。非常感谢任何帮助。
不清楚您要测量的质心之间的距离,因此下面的示例提供了几种不同的方法来查找部分或所有距离。
下面的代码找到每一行的质心,显示前两行质心之间的距离,然后是前 10 行的小距离矩阵。
crs是原来的wgs 84,距离单位是米。我没有看到需要更改 crs。
library(tidyverse)
library(sf)
x <- read_sf('shapefile_dir/') # Replace with local directory name
x <- x %>% mutate(centroids = st_centroid(st_geometry(.)))
#Distance between Goeree-Overflakkee (row 1) and Dordrecht (row 2):
st_distance(x$centroids[1], x$centroids[2])
#> Units: [m]
#> [,1]
#> [1,] 53317.83
#distance matrix of first 10 rows:
x[1:10,] %>%
st_set_geometry('centroids') %>% # Since there are 2 geometry cols, this sets the active one to 'centroids'
st_distance()
#> Units: [m]
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> [1,] 0.00 53317.826 49330.003 85844.745 76152.903 69198.288 89437.405
#> [2,] 53317.83 0.000 7355.978 38618.416 37934.810 31387.648 45356.081
#> [3,] 49330.00 7355.978 0.000 38510.970 34633.274 27580.688 44165.444
#> [4,] 85844.75 38618.416 38510.970 0.000 16873.684 19856.326 8741.436
#> [5,] 76152.90 37934.810 34633.274 16873.684 0.000 7298.181 14836.265
#> [6,] 69198.29 31387.648 27580.688 19856.326 7298.181 0.000 20611.458
#> [7,] 89437.40 45356.081 44165.444 8741.436 14836.265 20611.458 0.000
#> [8,] 84873.91 42274.715 40485.556 9634.087 9915.349 15823.963 4928.774
#> [9,] 88788.51 45795.090 44243.643 10476.055 13443.233 19690.611 2255.828
#> [10,] 94857.25 50872.407 49779.022 13205.312 19479.782 25802.514 5617.354
#> [,8] [,9] [,10]
#> [1,] 84873.911 88788.511 94857.246
#> [2,] 42274.715 45795.090 50872.407
#> [3,] 40485.556 44243.643 49779.022
#> [4,] 9634.087 10476.055 13205.312
#> [5,] 9915.349 13443.233 19479.782
#> [6,] 15823.963 19690.611 25802.514
#> [7,] 4928.774 2255.828 5617.354
#> [8,] 0.000 3922.534 9994.837
#> [9,] 3922.534 0.000 6115.383
#> [10,] 9994.837 6115.383 0.000
# A distance matrix of all pairs of points can be found by not
# subsetting the data:
# x %>% st_set_geometry('centroids') %>% st_distance()
由 reprex package (v0.3.0)
创建于 2022-03-10
我需要将它上传到 R 并根据经度和纬度提取多边形的质心,以便我可以用它来计算两个质心之间的距离。
到目前为止我已经这样做了:
PC4shape <- read_sf(dsn = "georef-netherlands-postcode-pc4", layer = "georef-netherlands-postcode-pc4")
PC4shape$centroids <- st_transform(PC4shape, 28992) %>%
st_centroid() %>%
st_transform(., '+proj=longlat +ellps=GRS80 +no_defs') %>%
st_geometry()
关注
但是,我确定我的投影和 EPSG 有误。我只是不知道如何找到合适的。非常感谢任何帮助。
不清楚您要测量的质心之间的距离,因此下面的示例提供了几种不同的方法来查找部分或所有距离。
下面的代码找到每一行的质心,显示前两行质心之间的距离,然后是前 10 行的小距离矩阵。
crs是原来的wgs 84,距离单位是米。我没有看到需要更改 crs。
library(tidyverse)
library(sf)
x <- read_sf('shapefile_dir/') # Replace with local directory name
x <- x %>% mutate(centroids = st_centroid(st_geometry(.)))
#Distance between Goeree-Overflakkee (row 1) and Dordrecht (row 2):
st_distance(x$centroids[1], x$centroids[2])
#> Units: [m]
#> [,1]
#> [1,] 53317.83
#distance matrix of first 10 rows:
x[1:10,] %>%
st_set_geometry('centroids') %>% # Since there are 2 geometry cols, this sets the active one to 'centroids'
st_distance()
#> Units: [m]
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> [1,] 0.00 53317.826 49330.003 85844.745 76152.903 69198.288 89437.405
#> [2,] 53317.83 0.000 7355.978 38618.416 37934.810 31387.648 45356.081
#> [3,] 49330.00 7355.978 0.000 38510.970 34633.274 27580.688 44165.444
#> [4,] 85844.75 38618.416 38510.970 0.000 16873.684 19856.326 8741.436
#> [5,] 76152.90 37934.810 34633.274 16873.684 0.000 7298.181 14836.265
#> [6,] 69198.29 31387.648 27580.688 19856.326 7298.181 0.000 20611.458
#> [7,] 89437.40 45356.081 44165.444 8741.436 14836.265 20611.458 0.000
#> [8,] 84873.91 42274.715 40485.556 9634.087 9915.349 15823.963 4928.774
#> [9,] 88788.51 45795.090 44243.643 10476.055 13443.233 19690.611 2255.828
#> [10,] 94857.25 50872.407 49779.022 13205.312 19479.782 25802.514 5617.354
#> [,8] [,9] [,10]
#> [1,] 84873.911 88788.511 94857.246
#> [2,] 42274.715 45795.090 50872.407
#> [3,] 40485.556 44243.643 49779.022
#> [4,] 9634.087 10476.055 13205.312
#> [5,] 9915.349 13443.233 19479.782
#> [6,] 15823.963 19690.611 25802.514
#> [7,] 4928.774 2255.828 5617.354
#> [8,] 0.000 3922.534 9994.837
#> [9,] 3922.534 0.000 6115.383
#> [10,] 9994.837 6115.383 0.000
# A distance matrix of all pairs of points can be found by not
# subsetting the data:
# x %>% st_set_geometry('centroids') %>% st_distance()
由 reprex package (v0.3.0)
创建于 2022-03-10