forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.go
539 lines (465 loc) · 15.5 KB
/
geometry.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// 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 (
"bytes"
"encoding/binary"
"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"
)
// GeometryType represents the GEOMETRY type.
// https://dev.mysql.com/doc/refman/8.0/en/gis-class-geometry.html
// The type of the returned value is one of the following (each implements GeometryValue): Point, Polygon, LineString.
type GeometryType struct {
SRID uint32
DefinedSRID bool
}
// GeometryValue is the value type returned from GeometryType, which is an interface over the following types:
// Point, Polygon, LineString, MultiPoint, MultiLineString, MultiPolygon, GeometryCollection.
type GeometryValue interface {
implementsGeometryValue()
GetSRID() uint32
SetSRID(srid uint32) GeometryValue
Serialize() []byte
WriteData(buf []byte) int
Swap() GeometryValue
BBox() (float64, float64, float64, float64)
}
var _ sql.Type = GeometryType{}
var _ sql.SpatialColumnType = GeometryType{}
var (
ErrNotGeometry = errors.NewKind("Value of type %T is not a geometry")
geometryValueType = reflect.TypeOf((*GeometryValue)(nil)).Elem()
)
const (
CartesianSRID = uint32(0)
GeoSpatialSRID = uint32(4326)
)
const (
SRIDSize = 4
EndianSize = 1
TypeSize = 4
EWKBHeaderSize = SRIDSize + EndianSize + TypeSize
WKBHeaderSize = EndianSize + TypeSize
PointSize = 16
CountSize = 4
GeometryMaxByteLength = 4*(1024*1024*1024) - 1
)
// Type IDs
const (
WKBUnknown = iota
WKBPointID
WKBLineID
WKBPolyID
WKBMultiPointID
WKBMultiLineID
WKBMultiPolyID
WKBGeomCollID
)
// isLinearRing checks if a LineString is a linear ring
func isLinearRing(line LineString) bool {
// Get number of points
numPoints := len(line.Points)
// Check length of LineString (must be 0 or 4+) points
if numPoints != 0 && numPoints < 4 {
return false
}
// Check if it is closed (first and last point are the same)
if line.Points[0] != line.Points[numPoints-1] {
return false
}
return true
}
// DeserializeEWKBHeader parses the header portion of a byte array in EWKB format to extract endianness and type
func DeserializeEWKBHeader(buf []byte) (srid uint32, bigEndian bool, typ uint32, err error) {
// Must be right length
if len(buf) < EWKBHeaderSize {
return 0, false, 0, sql.ErrInvalidGISData.New("DeserializeEWKBHeader")
}
srid = binary.LittleEndian.Uint32(buf) // First 4 bytes is SRID always in little endian
buf = buf[SRIDSize:] // Shift pointer over
bigEndian = buf[0] == 0 // Next byte is endianness
buf = buf[EndianSize:] // Shift pointer over
if bigEndian { // Next 4 bytes is type
typ = binary.BigEndian.Uint32(buf)
} else {
typ = binary.LittleEndian.Uint32(buf)
}
return
}
// DeserializeWKBHeader parses the header potion of a byte array in WKB format
// There is no SRID
func DeserializeWKBHeader(buf []byte) (bigEndian bool, typ uint32, err error) {
// Must be right length
if len(buf) < (EndianSize + TypeSize) {
return false, 0, sql.ErrInvalidGISData.New("DeserializeWKBHeader")
}
bigEndian = buf[0] == 0 // First byte is byte order
buf = buf[EndianSize:] // Shift pointer over
if bigEndian { // Next 4 bytes is geometry type
typ = binary.BigEndian.Uint32(buf)
} else {
typ = binary.LittleEndian.Uint32(buf)
}
return
}
// DeserializePoint parses the data portion of a byte array in WKB format to a Point object
func DeserializePoint(buf []byte, isBig bool, srid uint32) (Point, int, error) {
// Must be 16 bytes (2 floats)
if len(buf) != PointSize {
return Point{}, 0, sql.ErrInvalidGISData.New("DeserializePoint")
}
// Read floats x and y
var x, y float64
if isBig {
x = math.Float64frombits(binary.BigEndian.Uint64(buf[:8]))
y = math.Float64frombits(binary.BigEndian.Uint64(buf[8:]))
} else {
x = math.Float64frombits(binary.LittleEndian.Uint64(buf[:8]))
y = math.Float64frombits(binary.LittleEndian.Uint64(buf[8:]))
}
return Point{SRID: srid, X: x, Y: y}, PointSize, nil
}
// DeserializeLine parses the data portion of a byte array in WKB format to a LineString object
func DeserializeLine(buf []byte, isBig bool, srid uint32) (LineString, int, error) {
// Must be at least CountSize and two points
if len(buf) < (CountSize + PointSize + PointSize) {
return LineString{}, 0, sql.ErrInvalidGISData.New("DeserializeLine")
}
// Read number of points
points := make([]Point, readCount(buf, isBig))
buf = buf[CountSize:]
// Read points
var err error
for i := range points {
points[i], _, err = DeserializePoint(buf[:PointSize], isBig, srid)
if err != nil {
return LineString{}, 0, sql.ErrInvalidGISData.New("DeserializeLine")
}
buf = buf[PointSize:]
}
return LineString{SRID: srid, Points: points}, CountSize + PointSize*len(points), nil
}
// DeserializePoly parses the data portion of a byte array in WKB format to a Polygon object
func DeserializePoly(buf []byte, isBig bool, srid uint32) (Polygon, int, error) {
// Must be at least count, count, and four points
if len(buf) < (CountSize + CountSize + 4*PointSize) {
return Polygon{}, 0, sql.ErrInvalidGISData.New("DeserializePoly")
}
// Read number of lines
lines := make([]LineString, readCount(buf, isBig))
buf = buf[CountSize:]
count := CountSize
// Read lines
var err error
var c int
for i := range lines {
lines[i], c, err = DeserializeLine(buf, isBig, srid)
if err != nil {
return Polygon{}, 0, sql.ErrInvalidGISData.New("DeserializePoly")
}
buf = buf[c:]
count += c
}
return Polygon{SRID: srid, Lines: lines}, count, nil
}
func readCount(buf []byte, isBig bool) uint32 {
if isBig {
return binary.BigEndian.Uint32(buf)
}
return binary.LittleEndian.Uint32(buf)
}
// DeserializeMPoint parses the data portion of a byte array in WKB format to a MultiPoint object
func DeserializeMPoint(buf []byte, isBig bool, srid uint32) (MultiPoint, int, error) {
// Must contain at count, wkb header, and one point
if len(buf) < (CountSize + WKBHeaderSize + PointSize) {
return MultiPoint{}, 0, sql.ErrInvalidGISData.New("DeserializeMPoint")
}
// Read number of points in MultiPoint
points := make([]Point, readCount(buf, isBig))
buf = buf[CountSize:]
for i := range points {
// WKBHeaders are inside MultiGeometry Types
isBig, typ, err := DeserializeWKBHeader(buf)
if err != nil {
return MultiPoint{}, 0, err
}
if typ != WKBPointID {
return MultiPoint{}, 0, sql.ErrInvalidGISData.New("DeserializeMPoint")
}
buf = buf[WKBHeaderSize:]
// Read point data
points[i], _, err = DeserializePoint(buf[:PointSize], isBig, srid)
if err != nil {
return MultiPoint{}, 0, err
}
buf = buf[PointSize:]
}
return MultiPoint{SRID: srid, Points: points}, CountSize + (WKBHeaderSize+PointSize)*len(points), nil
}
// DeserializeMLine parses the data portion of a byte array in WKB format to a MultiLineString object
func DeserializeMLine(buf []byte, isBig bool, srid uint32) (MultiLineString, int, error) {
// Must contain at least length, wkb header, length, and two points
if len(buf) < (CountSize + WKBHeaderSize + CountSize + 2*PointSize) {
return MultiLineString{}, 0, sql.ErrInvalidGISData.New("MultiLineString")
}
// Read number of lines
lines := make([]LineString, readCount(buf, isBig))
buf = buf[CountSize:]
count := CountSize
var c int
for i := range lines {
isBig, typ, err := DeserializeWKBHeader(buf)
if typ != WKBLineID {
return MultiLineString{}, 0, sql.ErrInvalidGISData.New("DeserializeMLine")
}
buf = buf[WKBHeaderSize:]
lines[i], c, err = DeserializeLine(buf, isBig, srid)
if err != nil {
return MultiLineString{}, 0, sql.ErrInvalidGISData.New("DeserializeMLine")
}
buf = buf[c:]
count += WKBHeaderSize + c
}
return MultiLineString{SRID: srid, Lines: lines}, count, nil
}
// DeserializeMPoly parses the data portion of a byte array in WKB format to a MultiPolygon object
func DeserializeMPoly(buf []byte, isBig bool, srid uint32) (MultiPolygon, int, error) {
// Must contain at least num polys, wkb header, num lines, num lines, and four points
if len(buf) < (CountSize + WKBHeaderSize + 2*CountSize + 4*PointSize) {
return MultiPolygon{}, 0, sql.ErrInvalidGISData.New("MultiPolygon")
}
// Read number of polygons
polys := make([]Polygon, readCount(buf, isBig))
buf = buf[CountSize:]
count := CountSize
var c int
for i := range polys {
isBig, typ, err := DeserializeWKBHeader(buf)
if typ != WKBPolyID {
return MultiPolygon{}, 0, sql.ErrInvalidGISData.New("DeserializeMPoly")
}
buf = buf[WKBHeaderSize:]
polys[i], c, err = DeserializePoly(buf, isBig, srid)
if err != nil {
return MultiPolygon{}, 0, sql.ErrInvalidGISData.New("DeserializeMPoly")
}
buf = buf[c:]
count += WKBHeaderSize + c
}
return MultiPolygon{SRID: srid, Polygons: polys}, count, nil
}
// DeserializeGeomColl parses the data portion of a byte array in WKB format to a GeometryCollection object
func DeserializeGeomColl(buf []byte, isBig bool, srid uint32) (GeomColl, int, error) {
// Must be at least CountSize
if len(buf) < CountSize {
return GeomColl{}, 0, sql.ErrInvalidGISData.New("DeserializeLine")
}
// Read number of geometry objects
geoms := make([]GeometryValue, readCount(buf, isBig))
buf = buf[CountSize:]
count := CountSize
// Read geometries
var c int
for i := range geoms {
isBig, typ, err := DeserializeWKBHeader(buf)
if err != nil {
return GeomColl{}, 0, sql.ErrInvalidGISData.New("GeometryType.Convert")
}
buf = buf[WKBHeaderSize:]
switch typ {
case WKBPointID:
geoms[i], c, err = DeserializePoint(buf[:PointSize], isBig, srid)
case WKBLineID:
geoms[i], c, err = DeserializeLine(buf, isBig, srid)
case WKBPolyID:
geoms[i], c, err = DeserializePoly(buf, isBig, srid)
case WKBMultiPointID:
geoms[i], c, err = DeserializeMPoint(buf, isBig, srid)
case WKBMultiLineID:
geoms[i], c, err = DeserializeMLine(buf, isBig, srid)
case WKBMultiPolyID:
geoms[i], c, err = DeserializeMPoly(buf, isBig, srid)
case WKBGeomCollID:
geoms[i], c, err = DeserializeGeomColl(buf, isBig, srid)
default:
return GeomColl{}, 0, sql.ErrInvalidGISData.New("GeometryType.Convert")
}
if err != nil {
return GeomColl{}, 0, sql.ErrInvalidGISData.New("GeometryType.Convert")
}
buf = buf[c:]
count += WKBHeaderSize + c
}
return GeomColl{SRID: srid, Geoms: geoms}, count, nil
}
// TODO: unexport
func AllocateGeoTypeBuffer(numPoints, numCounts, numWKBHeaders int) []byte {
return make([]byte, EWKBHeaderSize+PointSize*numPoints+CountSize*numCounts+numWKBHeaders*WKBHeaderSize)
}
// WriteEWKBHeader will write EWKB header to the given buffer
func WriteEWKBHeader(buf []byte, srid, typ uint32) {
binary.LittleEndian.PutUint32(buf, srid) // always write SRID in little endian
buf = buf[SRIDSize:] // shift
buf[0] = 1 // always write in little endian
buf = buf[EndianSize:] // shift
binary.LittleEndian.PutUint32(buf, typ) // write geometry type
}
// WriteWKBHeader will write WKB header to the given buffer
func WriteWKBHeader(buf []byte, typ uint32) {
buf[0] = 1 // always write in little endian
buf = buf[EndianSize:] // shift
binary.LittleEndian.PutUint32(buf, typ) // write geometry type
}
// TODO: rename me, unexport
func WriteCount(buf []byte, count uint32) {
binary.LittleEndian.PutUint32(buf, count)
}
// Compare implements Type interface.
func (t GeometryType) Compare(a any, b any) (int, error) {
if hasNulls, res := CompareNulls(a, b); hasNulls {
return res, nil
}
aa, ok := a.(GeometryValue)
if !ok {
return 0, ErrNotGeometry.New(a)
}
bb, ok := b.(GeometryValue)
if !ok {
return 0, ErrNotGeometry.New(b)
}
return bytes.Compare(aa.Serialize(), bb.Serialize()), nil
}
// Convert implements Type interface.
func (t GeometryType) Convert(v interface{}) (interface{}, error) {
if v == nil {
return nil, nil
}
switch val := v.(type) {
case []byte:
srid, isBig, geomType, err := DeserializeEWKBHeader(val)
if err != nil {
return nil, err
}
val = val[EWKBHeaderSize:]
var geom interface{}
switch geomType {
case WKBPointID:
geom, _, err = DeserializePoint(val, isBig, srid)
case WKBLineID:
geom, _, err = DeserializeLine(val, isBig, srid)
case WKBPolyID:
geom, _, err = DeserializePoly(val, isBig, srid)
case WKBMultiPointID:
geom, _, err = DeserializeMPoint(val, isBig, srid)
case WKBMultiLineID:
geom, _, err = DeserializeMLine(val, isBig, srid)
case WKBMultiPolyID:
geom, _, err = DeserializeMPoly(val, isBig, srid)
case WKBGeomCollID:
geom, _, err = DeserializeGeomColl(val, isBig, srid)
default:
return nil, sql.ErrInvalidGISData.New("GeometryType.Convert")
}
if err != nil {
return nil, err
}
return geom, nil
case string:
return t.Convert([]byte(val))
case GeometryValue:
if err := t.MatchSRID(val); err != nil {
return nil, err
}
return val, nil
default:
return nil, sql.ErrSpatialTypeConversion.New()
}
}
// Equals implements the Type interface.
func (t GeometryType) Equals(otherType sql.Type) (ok bool) {
_, ok = otherType.(GeometryType)
return
}
// MaxTextResponseByteLength implements the Type interface
func (t GeometryType) MaxTextResponseByteLength() uint32 {
return GeometryMaxByteLength
}
// Promote implements the Type interface.
func (t GeometryType) Promote() sql.Type {
return t
}
// SQL implements Type interface.
func (t GeometryType) 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.(GeometryValue).Serialize()
return sqltypes.MakeTrusted(sqltypes.Geometry, buf), nil
}
// String implements Type interface.
func (t GeometryType) String() string {
return "geometry"
}
// Type implements Type interface.
func (t GeometryType) Type() query.Type {
return sqltypes.Geometry
}
// ValueType implements Type interface.
func (t GeometryType) ValueType() reflect.Type {
return geometryValueType
}
// Zero implements Type interface.
func (t GeometryType) Zero() interface{} {
// MySQL throws an error for INSERT IGNORE, UPDATE IGNORE, etc. if the geometry type cannot be parsed:
// ERROR 1416 (22003): Cannot get geometry object from data you send to the GEOMETRY field
// So, we don't implement a zero type for this function.
return nil
}
// GetSpatialTypeSRID implements SpatialColumnType interface.
func (t GeometryType) GetSpatialTypeSRID() (uint32, bool) {
return t.SRID, t.DefinedSRID
}
// SetSRID implements SpatialColumnType interface.
func (t GeometryType) SetSRID(v uint32) sql.Type {
t.SRID = v
t.DefinedSRID = true
return t
}
// MatchSRID implements SpatialColumnType interface
func (t GeometryType) MatchSRID(v interface{}) error {
if !t.DefinedSRID {
return nil
}
// if matched with SRID value of row value
var srid uint32
switch val := v.(type) {
case GeometryValue:
srid = val.GetSRID()
default:
return ErrNotGeometry.New(v)
}
if t.SRID == srid {
return nil
}
return sql.ErrNotMatchingSRID.New(srid, t.SRID)
}