forked from Secoura/cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (111 loc) · 2.73 KB
/
main.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/electrious-go/cluster"
"github.com/electrious-go/cluster/examples/googlemaps/spherand"
_ "github.com/electrious-go/cluster/examples/googlemaps/static"
"github.com/rakyll/statik/fs"
)
type testPoint struct {
Type string
Properties struct {
//we don't need other data
Name string
PointCount int `json:"point_count"`
}
Geometry struct {
Coordinates []float64
}
}
func (tp testPoint) GetCoordinates() cluster.GeoCoordinates {
return cluster.GeoCoordinates{
Lng: tp.Geometry.Coordinates[0],
Lat: tp.Geometry.Coordinates[1],
}
}
type latlng struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
}
func (tp latlng) GetCoordinates() cluster.GeoCoordinates {
return cluster.GeoCoordinates{Lng: tp.Lng, Lat: tp.Lat}
}
type boundingBox struct {
NW latlng `json:"nw"`
SE latlng `json:"se"`
}
type payload struct {
Zoom int `json:"zoom"`
BoundingBox boundingBox `json:"bb"`
}
type payload2 struct {
ClusterID int `json:"clusterID"`
}
var c *cluster.Cluster
func main() {
log.Printf("creating random samples\n")
c = createCluster(1000000)
log.Printf("samples created\n")
statikFS, err := fs.New()
if err != nil {
log.Fatal(err)
}
http.Handle("/", http.FileServer(statikFS))
http.HandleFunc("/clusters", clustersEndpoint)
http.HandleFunc("/zoom", zoomEndpoint)
log.Printf("listening to 8080\n")
http.ListenAndServe(":8080", nil)
}
func clustersEndpoint(rw http.ResponseWriter, request *http.Request) {
log.Println("received request")
log.Println(request.URL.String())
decoder := json.NewDecoder(request.Body)
var t payload
err := decoder.Decode(&t)
if err != nil {
panic(err)
}
points := c.GetClusters(t.BoundingBox.NW, t.BoundingBox.SE, t.Zoom)
data, err := json.Marshal(points)
if err != nil {
panic(err)
}
rw.Write(data)
}
func zoomEndpoint(rw http.ResponseWriter, request *http.Request) {
log.Println("received request")
log.Println(request.URL.String())
decoder := json.NewDecoder(request.Body)
var t payload2
err := decoder.Decode(&t)
if err != nil {
panic(err)
}
zoom := c.GetClusterExpansionZoom(t.ClusterID)
l := []byte(fmt.Sprintf(`{"zoom": %d}`, zoom))
rw.Write(l)
}
func createCluster(num int) *cluster.Cluster {
log.Printf("generating random lat/lng")
coords := make([]cluster.GeoPoint, num)
for i := range coords {
lat, lng := spherand.Geographical()
coords[i] = latlng{lat, lng}
}
geoPoints := make([]cluster.GeoPoint, len(coords))
for i := range coords {
geoPoints[i] = coords[i]
}
log.Printf("starting clustering")
c, err := cluster.New(coords,
cluster.WithNodeSize(64),
cluster.WithPointSize(120))
if err != nil {
panic(err)
}
log.Printf("clustering done")
return c
}