mdsumner / spdplyr

Data Manipulation Verbs for Spatial Classes
http://mdsumner.github.io/spdplyr/
42 stars 5 forks source link

Centroid of spatial clustering points using mean and sum operations #22

Open Leprechault opened 3 years ago

Leprechault commented 3 years ago

I'd like to calculate the centroid of each cluster (in my example I considering points in < 10m distance to the same cluster) of 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 - 10 meters
small.sample.utm.dm<-spDists(small.sample.utm) # Matrix distance
clusters <- as.hclust(agnes(small.sample.utm.dm, diss = T)) #Hierarchical Clustering
small.sample.utm@data$class<- cutree(clusters, h=Rc) # Cut into Groups 4.36 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)