/
vornoi.go
60 lines (51 loc) · 1.29 KB
/
vornoi.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
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"strconv"
"github.com/takoyaki-3/gomc"
"github.com/takoyaki-3/goraph"
"github.com/takoyaki-3/goraph/geometry/h3"
"github.com/takoyaki-3/goraph/loader/osm"
"github.com/takoyaki-3/goraph/search"
)
func main() {
// Graph load
fmt.Println("Graph load")
g := osm.Load("japan-latest.osm.pbf")
// Make index
fmt.Println("Make index")
h3indexes := h3.MakeH3Index(g, 9)
// Load base point
fmt.Println("Load base point")
bases := []int64{}
titles, records := gomc.ReadCSV("./base.csv")
for _, v := range records {
lat, _ := strconv.ParseFloat(v[titles["lat"]], 64)
lon, _ := strconv.ParseFloat(v[titles["lon"]], 64)
bases = append(bases, h3.Find(g, h3indexes, goraph.LatLon{lat, lon}, 9))
}
// Create voronoi diagram
fmt.Println("Create voronoi diagram")
nodes := search.Voronoi(g, bases)
// Output
fmt.Println("Output")
wf, err := os.Create("./output.csv")
if err != nil {
log.Println(err)
}
defer wf.Close()
w := csv.NewWriter(wf) // utf8
w.Write([]string{"base", "lat", "lon"})
for k, v := range nodes {
if k%100 != 0 {
continue
}
lat := strconv.FormatFloat(g.LatLons[k].Lat, 'f', -1, 64)
lon := strconv.FormatFloat(g.LatLons[k].Lon, 'f', -1, 64)
w.Write([]string{strconv.Itoa(int(v)), lat, lon})
}
w.Flush()
}