-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocationCluster.kt
52 lines (45 loc) · 1.68 KB
/
LocationCluster.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package clustering
import org.junit.Assert
import org.junit.Test
/**
* Created by berezkin on 10/06/17.
*/
private val EarthRadius = 6371009.0
private val LinkageMaxDistanceMeters = 130.0
class LocationCluster {
@Test
fun test1() {
Assert.assertTrue(clusterCount(
latitudes = doubleArrayOf(35.367949, 35.367026),
longitudes = doubleArrayOf(24.505020, 24.503913
)) == 2)
}
@Test
fun test2() {
Assert.assertTrue(clusterCount(
latitudes = doubleArrayOf(35.367057, 35.367070, 35.367949),
longitudes = doubleArrayOf(24.503904, 24.505031, 24.505015
)) == 1)
}
@Test
fun test3() {
Assert.assertTrue(clusterCount(
latitudes = doubleArrayOf(35.367057, 35.367057, 35.368409),
longitudes = doubleArrayOf(24.503899, 24.505567, 24.505556
)) == 3)
}
private fun clusterCount(latitudes: DoubleArray, longitudes: DoubleArray): Int {
val latCosines = DoubleArray(latitudes.size) {
Math.cos(Math.toRadians(latitudes[it]))
}
val degreeStep = EarthRadius * Math.PI / 180
val linkageDistanceDegree = LinkageMaxDistanceMeters / degreeStep
val linkageDistanceDegree2 = linkageDistanceDegree * linkageDistanceDegree
val pointClusters = SimpleClustering.apply(latitudes.size) { first, second ->
val dx = (longitudes[first] - longitudes[second]) * (latCosines[first] + latCosines[second]) / 2.0
val dy = latitudes[first] - latitudes[second]
dx * dx + dy * dy < linkageDistanceDegree2
}
return pointClusters.size
}
}