我有一个多边形的形状文件,我想找到每个多边形的质心,但我得到的是整个形状文件的质心。
我使用以下代码行将我的 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)
Run Code Online (Sandbox Code Playgroud)
我的数据框如下所示:
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, …Run Code Online (Sandbox Code Playgroud)