[R-sig-Geo] Clustering spatial points in a SpatialDataFrame object
ASANTOS
@|ex@ndre@@nto@br @end|ng |rom y@hoo@com@br
Wed Dec 16 13:10:41 CET 2020
Dear r-sig-geo Members,
I'd like to calculate the centroid of each cluster (considering points
with < 10m distance to the same cluster) of spatial points using mean
operation (spdplyr package) for coordinates and another operation (sum)
for the attribute(area) without success.
In my example:
#Packages
library(sp)
library(maptools)
library(spdplyr)
library(cluster)
# Small sample (40 points)
small.sample<-read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/sample_points.csv")
#Convert to spatial object
xy.small.sample <- small.sample[,c(1,2)]
spdf.small.sample <- SpatialPointsDataFrame(coords = xy.small.sample,
data = small.sample,
proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
# Transform to UTM
utm.wgs.84 <- "+proj=utm +zone=21 +south +datum=WGS84 +units=m +no_defs"
small.sample.utm <- spTransform(spdf.small.sample, utm.wgs.84)
#
#Convert each cluster to one point
Rc=10 #Maximum distance between points is 10 meters
small.sample.utm.dm<-spDists(small.sample.utm) # Distance matrix
clusters <- as.hclust(agnes(small.sample.utm.dm, diss = T))
#Hierarchical Clustering
small.sample.utm using data$class<- cutree(clusters, h=Rc) # Cut into groups
each 10 meters
# Average of x and y coordinates and area using spdplyr package
small.sample.utm.classification<- small.sample.utm %>%
group_by (class) %>%
summarise
(area_clu=mean(area),x_clu=mean(coords[,1]),y_clu=mean(coords[,2]))
#Error: Problem with `summarise()` input `x_clu`.
#i Input `x_clu` is `mean(coords[, 1])`.
#i The error occurred in group 1: class = 1.
Here I believe that this error is a cause of the spatial data frame
attributes have individual names and coordinates not. If I try to use
something like coords[,1], it doesn't work!!
My goal is:
# Original points representation
plot(small.sample.utm, pch=16)
# Center of the centroids representation
points(small.sample.utm.classification$x_clu,small.sample.utm.classification$y_clu,
col="red")
# Labelling the area
text(small.sample.utm.classification$x_clu ~
small.sample.utm.classification$x_clu,
labels=small.sample.utm.classification$area_clu, cex=0.9, font=2)
Thanks in advanced,
Alexandre
More information about the R-sig-Geo
mailing list