forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linestring.go
344 lines (297 loc) · 10.6 KB
/
linestring.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
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package geomfn
import (
"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geos"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/errors"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/ewkb"
)
// LineStringFromMultiPoint generates a linestring from a multipoint.
func LineStringFromMultiPoint(g geo.Geometry) (geo.Geometry, error) {
t, err := g.AsGeomT()
if err != nil {
return geo.Geometry{}, err
}
mp, ok := t.(*geom.MultiPoint)
if !ok {
return geo.Geometry{}, errors.Wrap(geom.ErrUnsupportedType{Value: t},
"geometry must be a MultiPoint")
}
if mp.NumPoints() == 1 {
return geo.Geometry{}, errors.Newf("a LineString must have at least 2 points")
}
flatCoords := make([]float64, 0, mp.NumCoords()*mp.Stride())
var prevPoint *geom.Point
for i := 0; i < mp.NumPoints(); i++ {
p := mp.Point(i)
// Empty points in multipoints are replaced by the previous point.
// If the previous point does not exist, we append (0, 0, ...) coordinates.
if p.Empty() {
if prevPoint == nil {
prevPoint = geom.NewPointFlat(mp.Layout(), make([]float64, mp.Stride()))
}
flatCoords = append(flatCoords, prevPoint.FlatCoords()...)
continue
}
flatCoords = append(flatCoords, p.FlatCoords()...)
prevPoint = p
}
lineString := geom.NewLineStringFlat(mp.Layout(), flatCoords).SetSRID(mp.SRID())
return geo.MakeGeometryFromGeomT(lineString)
}
// LineMerge merges multilinestring constituents.
func LineMerge(g geo.Geometry) (geo.Geometry, error) {
// Mirrors PostGIS behavior
if g.Empty() {
return g, nil
}
ret, err := geos.LineMerge(g.EWKB())
if err != nil {
return geo.Geometry{}, err
}
return geo.ParseGeometryFromEWKB(ret)
}
// LineLocatePoint returns a float between 0 and 1 representing the location of the
// closest point on LineString to the given Point, as a fraction of total 2d line length.
func LineLocatePoint(line geo.Geometry, point geo.Geometry) (float64, error) {
lineT, err := line.AsGeomT()
if err != nil {
return 0, err
}
lineString, ok := lineT.(*geom.LineString)
if !ok {
return 0, pgerror.Newf(pgcode.InvalidParameterValue,
"first parameter has to be of type LineString")
}
// compute closest point on line to the given point
closestPoint, err := ClosestPoint(line, point)
if err != nil {
return 0, err
}
closestT, err := closestPoint.AsGeomT()
if err != nil {
return 0, err
}
p := closestT.(*geom.Point)
// build new line segment to the closest point we found
lineSegment := geom.NewLineString(geom.XY).MustSetCoords([]geom.Coord{lineString.Coord(0), p.Coords()})
// compute fraction of new line segment compared to total line length
return lineSegment.Length() / lineString.Length(), nil
}
// AddPoint adds a point to a LineString at the given 0-based index. -1 appends.
func AddPoint(lineString geo.Geometry, index int, point geo.Geometry) (geo.Geometry, error) {
g, err := lineString.AsGeomT()
if err != nil {
return geo.Geometry{}, err
}
lineStringG, ok := g.(*geom.LineString)
if !ok {
e := geom.ErrUnsupportedType{Value: g}
return geo.Geometry{}, errors.Wrap(e, "geometry to be modified must be a LineString")
}
g, err = point.AsGeomT()
if err != nil {
return geo.Geometry{}, err
}
pointG, ok := g.(*geom.Point)
if !ok {
e := geom.ErrUnsupportedType{Value: g}
return geo.Geometry{}, errors.Wrapf(e, "invalid geometry used to add a Point to a LineString")
}
g, err = addPoint(lineStringG, index, pointG)
if err != nil {
return geo.Geometry{}, err
}
return geo.MakeGeometryFromGeomT(g)
}
func addPoint(lineString *geom.LineString, index int, point *geom.Point) (*geom.LineString, error) {
if lineString.Layout() != point.Layout() {
return nil, geom.ErrLayoutMismatch{Got: point.Layout(), Want: lineString.Layout()}
}
if point.Empty() {
point = geom.NewPointFlat(point.Layout(), make([]float64, point.Stride()))
}
coords := lineString.Coords()
if index > len(coords) {
return nil, errors.Newf("index %d out of range of LineString with %d coordinates",
index, len(coords))
} else if index == -1 {
index = len(coords)
} else if index < 0 {
return nil, errors.Newf("invalid index %v", index)
}
// Shift the slice right by one element, then replace the element at the index, to avoid
// allocating an additional slice.
coords = append(coords, geom.Coord{})
copy(coords[index+1:], coords[index:])
coords[index] = point.Coords()
return lineString.SetCoords(coords)
}
// SetPoint sets the point at the given index of lineString; index is 0-based.
func SetPoint(lineString geo.Geometry, index int, point geo.Geometry) (geo.Geometry, error) {
g, err := lineString.AsGeomT()
if err != nil {
return geo.Geometry{}, err
}
lineStringG, ok := g.(*geom.LineString)
if !ok {
e := geom.ErrUnsupportedType{Value: g}
return geo.Geometry{}, errors.Wrap(e, "geometry to be modified must be a LineString")
}
g, err = point.AsGeomT()
if err != nil {
return geo.Geometry{}, err
}
pointG, ok := g.(*geom.Point)
if !ok {
e := geom.ErrUnsupportedType{Value: g}
return geo.Geometry{}, errors.Wrapf(e, "invalid geometry used to replace a Point on a LineString")
}
g, err = setPoint(lineStringG, index, pointG)
if err != nil {
return geo.Geometry{}, err
}
return geo.MakeGeometryFromGeomT(g)
}
func setPoint(lineString *geom.LineString, index int, point *geom.Point) (*geom.LineString, error) {
if lineString.Layout() != point.Layout() {
return nil, geom.ErrLayoutMismatch{Got: point.Layout(), Want: lineString.Layout()}
}
if point.Empty() {
point = geom.NewPointFlat(point.Layout(), make([]float64, point.Stride()))
}
coords := lineString.Coords()
hasNegIndex := index < 0
if index >= len(coords) || (hasNegIndex && index*-1 > len(coords)) {
return nil, errors.Newf("index %d out of range of LineString with %d coordinates", index, len(coords))
}
if hasNegIndex {
index = len(coords) + index
}
coords[index].Set(point.Coords())
return lineString.SetCoords(coords)
}
// RemovePoint removes the point at the given index of lineString; index is 0-based.
func RemovePoint(lineString geo.Geometry, index int) (geo.Geometry, error) {
g, err := lineString.AsGeomT()
if err != nil {
return geo.Geometry{}, err
}
lineStringG, ok := g.(*geom.LineString)
if !ok {
e := geom.ErrUnsupportedType{Value: g}
return geo.Geometry{}, errors.Wrap(e, "geometry to be modified must be a LineString")
}
if lineStringG.NumCoords() == 2 {
return geo.Geometry{}, errors.Newf("cannot remove a point from a LineString with only two Points")
}
g, err = removePoint(lineStringG, index)
if err != nil {
return geo.Geometry{}, err
}
return geo.MakeGeometryFromGeomT(g)
}
func removePoint(lineString *geom.LineString, index int) (*geom.LineString, error) {
coords := lineString.Coords()
if index >= len(coords) || index < 0 {
return nil, errors.Newf("index %d out of range of LineString with %d coordinates", index, len(coords))
}
coords = append(coords[:index], coords[index+1:]...)
return lineString.SetCoords(coords)
}
// LineSubstring returns a LineString being a substring by start and end
func LineSubstring(g geo.Geometry, start, end float64) (geo.Geometry, error) {
if start < 0 || start > 1 || end < 0 || end > 1 {
return g, pgerror.Newf(pgcode.InvalidParameterValue,
"start and and must be within 0 and 1")
}
if start > end {
return g, pgerror.Newf(pgcode.InvalidParameterValue,
"end must be greater or equal to the start")
}
if start == end {
return LineInterpolatePoints(g, start, false)
}
lineT, err := g.AsGeomT()
if err != nil {
return g, err
}
lineString, ok := lineT.(*geom.LineString)
if !ok {
return g, pgerror.Newf(pgcode.InvalidParameterValue,
"first parameter has to be of type LineString")
}
// Flat line should be return point empty immediately,
if lineString.Length() == 0 {
return geo.MakeGeometryFromGeomT(geom.NewPointEmpty(geom.XY).SetSRID(lineString.SRID()))
}
var newFlatCoords []float64
startDistance, endDistance := start*lineString.Length(), end*lineString.Length()
for i := range lineString.Coords() {
currentLineString, err := geom.NewLineString(geom.XY).SetCoords(lineString.Coords()[0 : i+1])
if err != nil {
return geo.Geometry{}, err
}
// If the current distance exceeds the end distance, find the last point and terminate the loop early.
// If the current distance exceeds the end distance but the `newFlatCoords` is still empty,
// Interpolate the start point and add to `newFlatCoords`
if currentLineString.Length() >= endDistance {
if len(newFlatCoords) == 0 {
coords, err := interpolateFlatCoordsFromDistance(g, startDistance)
if err != nil {
return geo.Geometry{}, err
}
newFlatCoords = append(newFlatCoords, coords...)
}
coords, err := interpolateFlatCoordsFromDistance(g, endDistance)
if err != nil {
return geo.Geometry{}, err
}
newFlatCoords = append(newFlatCoords, coords...)
break
}
// If we are past the beginning, check if we already have points in the line string.
// If this is our first point, interpolate the first point.
// If we have already added a point, simply add the current coordinate in.
if currentLineString.Length() >= startDistance {
if len(newFlatCoords) == 0 {
coords, err := interpolateFlatCoordsFromDistance(g, startDistance)
if err != nil {
return geo.Geometry{}, err
}
newFlatCoords = append(newFlatCoords, coords...)
// If it starts from 0, we don't need to add the first coords
// because has already added by the previous point.
if startDistance != 0 {
newFlatCoords = append(newFlatCoords, currentLineString.Coord(i)...)
}
} else {
newFlatCoords = append(newFlatCoords, lineString.Coord(i)...)
}
}
}
return geo.MakeGeometryFromGeomT(geom.NewLineStringFlat(geom.XY, newFlatCoords).SetSRID(lineString.SRID()))
}
// interpolateFlatCoordsFromDistance interpolates the geometry at a given distance and returns its flat coordinates.
func interpolateFlatCoordsFromDistance(g geo.Geometry, distance float64) ([]float64, error) {
pointEWKB, err := geos.InterpolateLine(g.EWKB(), distance)
if err != nil {
return []float64{}, err
}
point, err := ewkb.Unmarshal(pointEWKB)
if err != nil {
return []float64{}, err
}
return point.FlatCoords(), nil
}