-
Notifications
You must be signed in to change notification settings - Fork 55
/
spatial.go
280 lines (220 loc) · 7.84 KB
/
spatial.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
// Package spatial implements geo spatial types and functions.
package spatial
import (
"math"
"reflect"
"strings"
hdbreflect "github.com/SAP/go-hdb/driver/internal/reflect"
)
// NaN returns a 'not-a-number' value.
func NaN() float64 { return math.NaN() }
// Geometry is the interface representing a spatial type.
type Geometry interface {
geotype()
}
// Geometry2d is the interface representing a two dimensional spatial type.
type Geometry2d interface {
type2d()
}
// GeometryZ is the interface representing a three dimensional spatial type.
type GeometryZ interface {
Geometry
typeZ()
}
// GeometryM is the interface representing an annotated two dimensional spatial type.
type GeometryM interface {
Geometry
typeM()
}
// GeometryZM is the interface representing an annotated three dimensional annotated spatial type.
type GeometryZM interface {
Geometry
typeZM()
}
// Coord represents a two dimensional coordinate.
type Coord struct{ X, Y float64 }
// CoordZ represents a three dimensional coordinate.
type CoordZ struct{ X, Y, Z float64 }
// CoordM represents an annotated two dimensional coordinate.
type CoordM struct{ X, Y, M float64 }
// CoordZM represents an annotated three dimensional coordinate.
type CoordZM struct{ X, Y, M, Z float64 }
var (
coordType = hdbreflect.TypeFor[Coord]()
coordZType = hdbreflect.TypeFor[CoordZ]()
coordMType = hdbreflect.TypeFor[CoordM]()
coordZMType = hdbreflect.TypeFor[CoordZM]()
)
// Point represents a two dimensional point.
type Point Coord
// PointZ represents a three dimensional point.
type PointZ CoordZ
// PointM represents an annotated two dimensional point.
type PointM CoordM
// PointZM represents an annotated three dimensional point.
type PointZM CoordZM
// LineString represents a two dimensional line string.
type LineString []Coord
// LineStringZ represents a three dimensional line string.
type LineStringZ []CoordZ
// LineStringM represents an annotated two dimensional line string.
type LineStringM []CoordM
// LineStringZM represents an annotated three dimensional line string.
type LineStringZM []CoordZM
// CircularString represents a two dimensional circular string.
type CircularString []Coord
// CircularStringZ represents a three dimensional circular string.
type CircularStringZ []CoordZ
// CircularStringM represents an annotated two dimensional circular string.
type CircularStringM []CoordM
// CircularStringZM represents an annotated three dimensional circular string.
type CircularStringZM []CoordZM
// Polygon represents a two dimensional polygon.
type Polygon [][]Coord
// PolygonZ represents a three dimensional polygon.
type PolygonZ [][]CoordZ
// PolygonM represents an annotated two dimensional polygon.
type PolygonM [][]CoordM
// PolygonZM represents an annotated three dimensional polygon.
type PolygonZM [][]CoordZM
// MultiPoint represents a two dimensional multi point.
type MultiPoint []Point
// MultiPointZ represents a three dimensional multi point.
type MultiPointZ []PointZ
// MultiPointM represents an annotated two dimensional multi point.
type MultiPointM []PointM
// MultiPointZM represents an annotated three dimensional multi point.
type MultiPointZM []PointZM
// MultiLineString represents a two dimensional multi line string.
type MultiLineString []LineString
// MultiLineStringZ represents a three dimensional multi line string.
type MultiLineStringZ []LineStringZ
// MultiLineStringM represents an annotated two dimensional multi line string.
type MultiLineStringM []LineStringM
// MultiLineStringZM represents an annotated three dimensional multi line string.
type MultiLineStringZM []LineStringZM
// MultiPolygon represents a two dimensional multi polygon.
type MultiPolygon []Polygon
// MultiPolygonZ represents a three dimensional multi polygon.
type MultiPolygonZ []PolygonZ
// MultiPolygonM represents an annotated two dimensional multi polygon.
type MultiPolygonM []PolygonM
// MultiPolygonZM represents an annotated three dimensional multi polygon.
type MultiPolygonZM []PolygonZM
// GeometryCollection represents a two dimensional geometry collection.
type GeometryCollection []Geometry2d
// GeometryCollectionZ represents a three dimensional geometry collection.
type GeometryCollectionZ []GeometryZ
// GeometryCollectionM represents an annotated two dimensional geometry collection.
type GeometryCollectionM []GeometryM
// GeometryCollectionZM represents an annotated three dimensional geometry collection.
type GeometryCollectionZM []GeometryZM
// marker interface.
func (g Point) geotype() {}
func (g PointZ) geotype() {}
func (g PointM) geotype() {}
func (g PointZM) geotype() {}
func (g LineString) geotype() {}
func (g LineStringZ) geotype() {}
func (g LineStringM) geotype() {}
func (g LineStringZM) geotype() {}
func (g CircularString) geotype() {}
func (g CircularStringZ) geotype() {}
func (g CircularStringM) geotype() {}
func (g CircularStringZM) geotype() {}
func (g Polygon) geotype() {}
func (g PolygonZ) geotype() {}
func (g PolygonM) geotype() {}
func (g PolygonZM) geotype() {}
func (g MultiPoint) geotype() {}
func (g MultiPointZ) geotype() {}
func (g MultiPointM) geotype() {}
func (g MultiPointZM) geotype() {}
func (g MultiLineString) geotype() {}
func (g MultiLineStringM) geotype() {}
func (g MultiLineStringZ) geotype() {}
func (g MultiLineStringZM) geotype() {}
func (g MultiPolygon) geotype() {}
func (g MultiPolygonZ) geotype() {}
func (g MultiPolygonM) geotype() {}
func (g MultiPolygonZM) geotype() {}
func (g GeometryCollection) geotype() {}
func (g GeometryCollectionZ) geotype() {}
func (g GeometryCollectionM) geotype() {}
func (g GeometryCollectionZM) geotype() {}
func (g Point) type2d() {}
func (g LineString) type2d() {}
func (g CircularString) type2d() {}
func (g Polygon) type2d() {}
func (g MultiPoint) type2d() {}
func (g MultiLineString) type2d() {}
func (g MultiPolygon) type2d() {}
func (g GeometryCollection) type2d() {}
func (g PointZ) typeZ() {}
func (g LineStringZ) typeZ() {}
func (g CircularStringZ) typeZ() {}
func (g PolygonZ) typeZ() {}
func (g MultiPointZ) typeZ() {}
func (g MultiLineStringZ) typeZ() {}
func (g MultiPolygonZ) typeZ() {}
func (g GeometryCollectionZ) typeZ() {}
func (g PointM) typeM() {}
func (g LineStringM) typeM() {}
func (g CircularStringM) typeM() {}
func (g PolygonM) typeM() {}
func (g MultiPointM) typeM() {}
func (g MultiLineStringM) typeM() {}
func (g MultiPolygonM) typeM() {}
func (g GeometryCollectionM) typeM() {}
func (g PointZM) typeZM() {}
func (g LineStringZM) typeZM() {}
func (g CircularStringZM) typeZM() {}
func (g PolygonZM) typeZM() {}
func (g MultiPointZM) typeZM() {}
func (g MultiLineStringZM) typeZM() {}
func (g MultiPolygonZM) typeZM() {}
func (g GeometryCollectionZM) typeZM() {}
const (
geoPoint uint32 = 1
geoLineString uint32 = 2
geoPolygon uint32 = 3
geoMultiPoint uint32 = 4
geoMultiLineString uint32 = 5
geoMultiPolygon uint32 = 6
geoGeometryCollection uint32 = 7
geoCircularString uint32 = 8
)
func geoTypeName(g Geometry) string {
name := reflect.TypeOf(g).Name()
size := len(name)
switch {
case name[size-2:] == "ZM":
return name[:size-2]
case strings.ContainsAny(name[size-1:], "MZ"):
return name[:size-1]
default:
return name
}
}
func geoType(g Geometry) uint32 {
switch geoTypeName(g) {
case "Point":
return geoPoint
case "LineString":
return geoLineString
case "Polygon":
return geoPolygon
case "MultiPoint":
return geoMultiPoint
case "MultiLineString":
return geoMultiLineString
case "MultiPolygon":
return geoMultiPolygon
case "GeometryCollection":
return geoGeometryCollection
case "CircularString":
return geoCircularString
default:
panic("invalid geoTypeName")
}
}