【发布时间】:2020-10-02 04:04:44
【问题描述】:
我有一个多面体的 shapefile,我想找到每个多面体的质心,但我得到的是整个 shapefile 的质心。
我使用以下代码行将我的 csv 文件转换为 shapefile(请参阅此问题 Converting CSV file to shapefile - but want polygon not points):
df <- as.data.frame(read_csv("/Users/alan/Desktop/shapes.csv"))
df1 <- lapply(split(df, df$shape), function(x) { coords <- as.matrix(cbind(x$longitude,
x$latitude)); rbind(coords, coords[1,])})
Coord_Ref <- st_crs(3035)
plot_locations_df <- st_multipolygon(x=list(df1))
plot_locations_df <- st_sfc(plot_locations_df, crs = Coord_Ref)
plot(st_geometry(plot_locations_df))
plot(st_geometry(plot_locations_df, col = sf.colors(12, categorical=TRUE), border='grey',
axes=TRUE))
plot(st_geometry(st_centroid(plot_locations_df)), pch = 3, col = 'red', add=TRUE)
我的数据框如下所示:
structure(list(shape = c(1.1, 1.1, 1.1, 1.1, 2.1, 2.1, 2.1, 2.1,
3.1, 3.1, 3.1, 3.1, 4.1, 4.1, 4.1, 4.1, 5.1, 5.1, 5.1, 5.1, 6.1,
6.1, 6.1, 6.1, 7.1, 7.1, 7.1, 7.1, 8.1, 8.1, 8.1, 8.1, 9.1, 9.1,
9.1, 9.1), longitude = c(43, 43, 40, 40, 23, 23, 20, 20, 25,
25, 38, 38, 25, 25, 38, 38, 45, 50, 50, 45, 65, 60, 60, 65, 60,
60, 80, 80, 60, 60, 80, 80, 20, 20, 80, 80), latitude = c(10,
13, 13, 10, 10, 13, 13, 10, 10, 10.3, 10.3, 10, 12.7, 13, 13,
12.7, 13, 13, 10, 10, 13, 13, 10, 10, 9.8, 9.5, 9.5, 9.8, 6,
5.7, 5.7, 6, 5, 4.5, 4.5, 5)), row.names = c(NA, 36L), class = "data.frame")
【问题讨论】:
-
您必须共享您的数据,否则很难帮助您。
-
我已经发布了我的数据
-
请不要在.NORM format.. 中发布您的数据。请改用
dput().. -
好的,我已经使用了 dput()。希望这将使您更容易回答问题