Spatially partioning lat long data in R with sperrorest partition.kmeans

While trying out sperrorest::partition.kmeans I started wondering whether it did handle the date line (180, -180) correctly. A small clustering test confirmed that sperrorest doesn't take into account the spatial projection of the data and doesn't cluster together points from the same spatial area. To solve this I decided to convert my lat long data into cartesian coordinates and then call the partition.kmeans function.


lonlat_xyz <- function(data, lonlat_cols) {
   rad_coordinates <- data[,lonlat_cols] * (pi/180)
   lon <- rad_coordinates[,lonlat_cols[1]]
   lat <- rad_coordinates[,lonlat_cols[2]]
   r <- cbind(cos(lat)*cos(lon), cos(lat)*sin(lon), sin(lat))
   colnames(r) <- c("x", "y", "z")

cartesian <- lonlat_xyz(data, lonlat) 
partitions <- sperrorest::partition.kmeans(cartesian, coords = c("x","y","z"))

No comments: