これは、距離ルールに基づくポイントのクラスターの検索に基づいていdistm
ますが、geosphere
パッケージの関数を使用しています:
library(sp)
library(rgdal)
library(geosphere)
# example data from the thread
x <- c(-1.482156, -1.482318, -1.482129, -1.482880, -1.485735, -1.485770, -1.485913, -1.484275, -1.485866)
y <- c(54.90083, 54.90078, 54.90077, 54.90011, 54.89936, 54.89935, 54.89935, 54.89879, 54.89902)
# convert data to a SpatialPointsDataFrame object
xy <- SpatialPointsDataFrame(
matrix(c(x,y), ncol=2), data.frame(ID=seq(1:length(x))),
proj4string=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))
# use the distm function to generate a geodesic distance matrix in meters
mdist <- distm(xy)
# cluster all points using a hierarchical clustering approach
hc <- hclust(as.dist(mdist), method="complete")
# define the distance threshold, in this case 40 m
d=40
# define clusters based on a tree "height" cutoff "d" and add them to the SpDataFrame
xy$clust <- cutree(hc, h=d)
次のようなものが得られるはずです。
coordinates ID clust
1 (-1.482156, 54.90083) 1 1
2 (-1.482318, 54.90078) 2 1
3 (-1.482129, 54.90077) 3 1
4 (-1.48288, 54.90011) 4 2
5 (-1.485735, 54.89936) 5 3
6 (-1.48577, 54.89935) 6 3
7 (-1.485913, 54.89935) 7 3
8 (-1.484275, 54.89879) 8 4
9 (-1.485866, 54.89902) 9 3
これらの次のステップは視覚化のためだけです:
library(dismo)
library(rgeos)
# expand the extent of plotting frame
xy@bbox[] <- as.matrix(extend(extent(xy),0.001))
# get the centroid coords for each cluster
cent <- matrix(ncol=2, nrow=max(xy$clust))
for (i in 1:max(xy$clust))
# gCentroid from the rgeos package
cent[i,] <- gCentroid(subset(xy, clust == i))@coords
# compute circles around the centroid coords using a 40m radius
# from the dismo package
ci <- circles(cent, d=d, lonlat=T)
# plot
plot(ci@polygons, axes=T)
plot(xy, col=rainbow(4)[factor(xy$clust)], add=T)