forked from dustin/go-heatmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikipedia.go
85 lines (72 loc) · 1.8 KB
/
wikipedia.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
package main
import (
"bufio"
"encoding/json"
"image"
"log"
"net/http"
"os"
"time"
"github.com/markus-wa/go-heatmap/v2"
"github.com/markus-wa/go-heatmap/v2/schemes"
"github.com/dustin/go-humanize"
)
const url = "http://single.couchbase.net/wikipedia2012/_design/places/_spatial/points?bbox=-180,-90,180,90"
type row struct {
ID string `json:"id"`
Value int `json:"value"`
Geometry struct {
Coordinates []float64 `json:"coordinates"`
} `json:"geometry"`
}
func main() {
start := time.Now()
res, err := http.Get(url)
if err != nil {
log.Fatalf("Error opening strem from %q: %v", url, err)
}
defer res.Body.Close()
r := bufio.NewReader(res.Body)
_, _, err = r.ReadLine()
if err != nil {
log.Fatalf("Error reading first line: %v", err)
}
locations := make([]heatmap.DataPoint, 0, 200000)
for {
bytes, isPrefix, err := r.ReadLine()
if err != nil {
log.Fatalf("Error reading line: %v", err)
}
if isPrefix {
log.Fatalf("Crap, that was a prefix...")
}
if bytes[0] != '{' {
break
}
r := row{}
if bytes[len(bytes)-1] == ',' {
bytes = bytes[:len(bytes)-1]
}
err = json.Unmarshal(bytes, &r)
if err != nil {
log.Printf("Couldn't parse %v: %v", string(bytes), err)
break
}
locations = append(locations, heatmap.P(r.Geometry.Coordinates[0],
r.Geometry.Coordinates[1]))
}
end := time.Now()
log.Printf("Parsed %s items in %s",
humanize.Comma(int64(len(locations))), end.Sub(start))
out, err := os.Create("wikipedia.kmz")
if err != nil {
log.Fatalf("Error making output file: %v", err)
}
defer out.Close()
err = heatmap.KMZ(image.Rect(0, 0, 8192, 4096), locations, 50, 96,
schemes.AlphaFire, out)
if err != nil {
log.Fatalf("Error generating thingy: %v", err)
}
log.Printf("Completed heatmap generation in %s", time.Now().Sub(end))
}