-
Notifications
You must be signed in to change notification settings - Fork 4
/
multi_line.go
168 lines (145 loc) · 3.41 KB
/
multi_line.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package geo
import (
"database/sql/driver"
"encoding/json"
"fmt"
"strings"
)
const (
mlWKTPrefix = `MULTILINESTRING(`
mlWKTSuffix = `)`
)
var (
mlJSONPrefix = []byte(`{"type":"MultiLineString","coordinates":[`)
mlJSONSuffix = []byte(`]}`)
)
// MultiLine is an array of Line's.
type MultiLine [][][3]float64
// Equal compares one MultiLine to another.
func (ml MultiLine) Equal(g Geometry) bool {
p, ok := g.(*MultiLine)
if !ok {
return false
}
if len(ml) != len(*p) {
return false
}
for i, p1 := range ml {
p2 := (*p)[i]
if len(p1) != len(p2) {
return false
}
if !pointsEqual(p1, p2) {
return false
}
}
return true
}
// Contains uses the ray casting algorithm to decide
// if the point is contained in the MultiLine.
func (ml MultiLine) Contains(point Point) bool {
intersections := 0
for _, poly := range ml {
for j, vertex := range poly {
if point.RayhIntersects(poly[(j+1)%len(poly)], vertex) {
intersections++
}
}
}
return (intersections % 2) == 1
}
// MarshalJSON returns the GeoJSON representation of the MultiLine.
func (ml MultiLine) MarshalJSON() ([]byte, error) {
s := mlJSONPrefix
for i, poly := range ml {
if i == 0 {
s = append(s, '[')
} else {
s = append(s, ',', '[')
}
s = append(s, pointsMarshalJSON(poly, "", "")...)
s = append(s, ']')
}
return append(s, mlJSONSuffix...), nil
}
// Scan scans a MultiLine from Well Known Text.
func (ml *MultiLine) Scan(src interface{}) error {
return scan(ml, src)
}
// scan scans a MultiLine from a Well Known Text string.
func (ml *MultiLine) scan(s string) error {
if i := strings.Index(s, mlWKTPrefix); i != 0 {
return fmt.Errorf("malformed MultiLine %s", s)
}
l := len(s)
if s[l-len(mlWKTSuffix):] != mlWKTSuffix {
return fmt.Errorf("malformed MultiLine %s", s)
}
s = s[len(mlWKTPrefix) : l-len(mlWKTSuffix)]
// empty the MultiLine
*ml = MultiLine{}
// get the coordinates
mls := strings.Split(s, "),(")
for _, ss := range mls {
points, err := pointsScan(ss)
if err != nil {
return err
}
*ml = append(*ml, points)
}
return nil
}
// String converts the MultiLine to a string.
func (ml MultiLine) String() string {
if len(ml) == 0 {
return "MULTILINESTRING EMPTY"
}
s := mlWKTPrefix
for i, points := range ml {
if i == 0 {
s += pointsString(points)
} else {
s += "," + pointsString(points)
}
}
return s + mlWKTSuffix
}
// UnmarshalJSON unmarshals the ml from GeoJSON.
func (ml *MultiLine) UnmarshalJSON(data []byte) error {
g := &geometry{}
// Never fails since data is always valid JSON.
_ = json.Unmarshal(data, g)
if expected, got := MultiLineType, g.Type; expected != got {
return fmt.Errorf("expected type %s, got %s", expected, got)
}
p := [][][3]float64{}
if err := json.Unmarshal(g.Coordinates, &p); err != nil {
return err
}
*ml = MultiLine(p)
return nil
}
// Value converts a point to Well Known Text.
func (ml MultiLine) Value() (driver.Value, error) {
return ml.String(), nil
}
// Transform transforms the geometry point by point.
func (ml *MultiLine) Transform(t Transformer) {
np := make([][][3]float64, len(*ml))
for i, line := range *ml {
nl := make([][3]float64, len(line))
for j, point := range line {
nl[j] = t.Transform(point)
}
np[i] = nl
}
*ml = np
}
// VisitCoordinates visits each point in the geometry.
func (ml MultiLine) VisitCoordinates(v Visitor) {
for _, line := range ml {
for _, point := range line {
v.Visit(point)
}
}
}