-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_export_postgis.go
90 lines (77 loc) · 1.83 KB
/
cmd_export_postgis.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
package catTrackslib
import (
"database/sql"
"encoding/json"
"log"
"github.com/cridenour/go-postgis"
"github.com/davecgh/go-spew/spew"
_ "github.com/lib/pq"
bolt "go.etcd.io/bbolt"
)
var uniqueCatNames = []string{}
func collectUniqCatName(s string) {
for _, v := range uniqueCatNames {
if v == s {
return
}
}
uniqueCatNames = append(uniqueCatNames, s)
}
func pgTrackID(tp *TrackPoint) string {
return ""
}
func ExportPostGIS(exportTarget string) {
log.Println("running export...")
log.Println("export target:", exportTarget)
db, err := sql.Open("postgres", exportTarget)
if err != nil {
log.Fatal(err)
}
defer func() {
if err := db.Close(); err != nil {
log.Fatal(err)
}
}()
/*
CREATE TABLE cat_tracks (
row_id serial PRIMARY KEY,
geom_point VARCHAR
);
*/
_, err = db.Exec("CREATE EXTENSION IF NOT EXISTS postgis")
if err != nil {
log.Fatal(err)
}
count := 0
GetDB("master").View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(trackKey))
c := b.Cursor()
// bucketStats := b.Stats()
// log.Printf("bucket stats: %+v", bucketStats)
for k, v := c.First(); k != nil && len(uniqueCatNames) < 2; k, v = c.Next() {
count++
var track TrackPoint
err := json.Unmarshal(v, &track)
if err != nil {
log.Println(err)
continue
}
collectUniqCatName(track.Name)
spew.Println(track)
point := postgis.PointS{
SRID: 4326,
X: track.Lng,
Y: track.Lat,
}
res, err := db.Exec("INSERT INTO cat_tracks (track_id, point) VALUES ($1, ST_SetSRID(ST_MakePoint($2, $3), 4326))", track.ID, track.Lng, track.Lat)
if err != nil {
log.Fatal(err)
}
rowsAffected, _ := res.RowsAffected()
log.Println("rows affected:", rowsAffected, point.GetType())
}
return nil
})
log.Printf("unique cat names: %+v", uniqueCatNames)
log.Println("count:", count)
}