forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multilinestring.go
228 lines (196 loc) · 6 KB
/
multilinestring.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2022 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package types
import (
"math"
"reflect"
"github.com/dolthub/vitess/go/sqltypes"
"github.com/dolthub/vitess/go/vt/proto/query"
"gopkg.in/src-d/go-errors.v1"
"github.com/gabereiser/go-mysql-server/sql"
)
// MultiLineStringType represents the MUTILINESTRING type.
// https://dev.mysql.com/doc/refman/8.0/en/gis-class-multilinestring.html
// The type of the returned value is MultiLineString.
type MultiLineStringType struct {
SRID uint32
DefinedSRID bool
}
// MultiLineString is the value type returned from MultiLineStringType. Implements GeometryValue.
type MultiLineString struct {
SRID uint32
Lines []LineString
}
var (
ErrNotMultiLineString = errors.NewKind("value of type %T is not a multilinestring")
multilinestringValueType = reflect.TypeOf(MultiLineString{})
)
var _ sql.Type = MultiLineStringType{}
var _ sql.SpatialColumnType = MultiLineStringType{}
var _ GeometryValue = MultiLineString{}
// Compare implements Type interface.
func (t MultiLineStringType) Compare(a interface{}, b interface{}) (int, error) {
return GeometryType{}.Compare(a, b)
}
// Convert implements Type interface.
func (t MultiLineStringType) Convert(v interface{}) (interface{}, error) {
switch buf := v.(type) {
case nil:
return nil, nil
case []byte:
mline, err := GeometryType{}.Convert(buf)
if sql.ErrInvalidGISData.Is(err) {
return nil, sql.ErrInvalidGISData.New("MultiLineString.Convert")
}
return mline, err
case string:
return t.Convert([]byte(buf))
case MultiLineString:
if err := t.MatchSRID(buf); err != nil {
return nil, err
}
return buf, nil
default:
return nil, sql.ErrSpatialTypeConversion.New()
}
}
// Equals implements the Type interface.
func (t MultiLineStringType) Equals(otherType sql.Type) bool {
_, ok := otherType.(MultiLineStringType)
return ok
}
// MaxTextResponseByteLength implements the Type interface
func (t MultiLineStringType) MaxTextResponseByteLength() uint32 {
return GeometryMaxByteLength
}
// Promote implements the Type interface.
func (t MultiLineStringType) Promote() sql.Type {
return t
}
// SQL implements Type interface.
func (t MultiLineStringType) SQL(ctx *sql.Context, dest []byte, v interface{}) (sqltypes.Value, error) {
if v == nil {
return sqltypes.NULL, nil
}
v, err := t.Convert(v)
if err != nil {
return sqltypes.Value{}, nil
}
buf := v.(MultiLineString).Serialize()
return sqltypes.MakeTrusted(sqltypes.Geometry, buf), nil
}
// String implements Type interface.
func (t MultiLineStringType) String() string {
return "multilinestring"
}
// Type implements Type interface.
func (t MultiLineStringType) Type() query.Type {
return sqltypes.Geometry
}
// ValueType implements Type interface.
func (t MultiLineStringType) ValueType() reflect.Type {
return multilinestringValueType
}
// Zero implements Type interface.
func (t MultiLineStringType) Zero() interface{} {
return MultiLineString{Lines: []LineString{LineStringType{}.Zero().(LineString)}}
}
// GetSpatialTypeSRID implements SpatialColumnType interface.
func (t MultiLineStringType) GetSpatialTypeSRID() (uint32, bool) {
return t.SRID, t.DefinedSRID
}
// SetSRID implements SpatialColumnType interface.
func (t MultiLineStringType) SetSRID(v uint32) sql.Type {
t.SRID = v
t.DefinedSRID = true
return t
}
// MatchSRID implements SpatialColumnType interface
func (t MultiLineStringType) MatchSRID(v interface{}) error {
val, ok := v.(MultiLineString)
if !ok {
return ErrNotMultiLineString.New(v)
}
if !t.DefinedSRID {
return nil
} else if t.SRID == val.SRID {
return nil
}
return sql.ErrNotMatchingSRID.New(val.SRID, t.SRID)
}
// implementsGeometryValue implements GeometryValue interface.
func (p MultiLineString) implementsGeometryValue() {}
// GetSRID implements GeometryValue interface.
func (p MultiLineString) GetSRID() uint32 {
return p.SRID
}
// SetSRID implements GeometryValue interface.
func (p MultiLineString) SetSRID(srid uint32) GeometryValue {
lines := make([]LineString, len(p.Lines))
for i, l := range p.Lines {
lines[i] = l.SetSRID(srid).(LineString)
}
return MultiLineString{
SRID: srid,
Lines: lines,
}
}
// Serialize implements GeometryValue interface.
func (p MultiLineString) Serialize() (buf []byte) {
var numPoints int
for _, l := range p.Lines {
numPoints += len(l.Points)
}
buf = AllocateGeoTypeBuffer(numPoints, len(p.Lines)+1, len(p.Lines))
WriteEWKBHeader(buf, p.SRID, WKBMultiLineID)
p.WriteData(buf[EWKBHeaderSize:])
return
}
// WriteData implements GeometryValue interface.
func (p MultiLineString) WriteData(buf []byte) int {
WriteCount(buf, uint32(len(p.Lines)))
buf = buf[CountSize:]
count := CountSize
for _, l := range p.Lines {
WriteWKBHeader(buf, WKBLineID)
buf = buf[WKBHeaderSize:]
c := l.WriteData(buf)
buf = buf[c:]
count += WKBHeaderSize + c
}
return count
}
// Swap implements GeometryValue interface.
func (p MultiLineString) Swap() GeometryValue {
lines := make([]LineString, len(p.Lines))
for i, l := range p.Lines {
lines[i] = l.Swap().(LineString)
}
return MultiLineString{
SRID: p.SRID,
Lines: lines,
}
}
// BBox implements GeometryValue interface.
func (p MultiLineString) BBox() (float64, float64, float64, float64) {
minX, minY, maxX, maxY := math.MaxFloat64, math.MaxFloat64, -math.MaxFloat64, -math.MaxFloat64
for _, l := range p.Lines {
lMinX, lMinY, lMaxX, lMaxY := l.BBox()
minX = math.Min(minX, lMinX)
minY = math.Min(minY, lMinY)
maxX = math.Max(maxX, lMaxX)
maxY = math.Max(maxY, lMaxY)
}
return minX, minY, maxX, maxY
}