-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (67 loc) · 1.7 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
package main
import (
"context"
"database/sql"
"fmt"
"log"
"os"
_ "github.com/lib/pq"
db "github.com/stephenirven/go-postgis/db/sqlc"
"github.com/stephenirven/go-postgis/geo"
)
const (
dbDriver = "postgres"
dbSource = "postgresql://postgres:secret@localhost:5432/late?sslmode=disable"
)
var testQueries *db.Queries
var testDB *sql.DB
func init() {
testDB, err := sql.Open(dbDriver, dbSource)
if err != nil {
log.Fatal("failed to connect to database", err)
}
testQueries = db.New(testDB)
}
func main() {
point, err := geo.NewPoint([]float64{0, 1, 2}, geo.XYZ)
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("Geo: %v\n", point)
gisGeometry := geo.NewGISGeometry(point)
gisGeometry.SetSRID(4326)
fmt.Printf("Geo: %v\n", gisGeometry)
}
func getLocation(id int64) {
row, err := testQueries.GetLocation(context.Background(), id)
if err != nil {
fmt.Println(err)
}
fmt.Print(row.ID, " - ", row.Geo, "\n")
}
func getNear(point geo.GISGeometry, distance int32) {
args := db.ListLocationsWithinDistanceParams{Limit: 10, Offset: 0, Location: point, Range: distance}
rows, err := testQueries.ListLocationsWithinDistance(context.Background(), args)
if err != nil {
fmt.Println(err)
}
for _, loc := range rows {
fmt.Print(loc.ID, " - ", loc.Geo)
break
}
}
func insert(geometry geo.GISGeometry) {
crargs := db.CreateLocationParams{FullName: sql.NullString{String: "boo"}, Geo: geometry}
_, err := testQueries.CreateLocation(context.Background(), crargs)
if err != nil {
fmt.Println("ERROR:", err)
os.Exit(1)
}
}
func getLocations() []db.GetLocationsRow {
rows, err := testQueries.GetLocations(context.Background())
if err != nil {
fmt.Print(err.Error())
}
return rows
}