forked from ruiaylin/pgparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_reference.go
65 lines (62 loc) · 2.25 KB
/
linear_reference.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
// 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/ruiaylin/pgparser/types/geo"
"github.com/ruiaylin/pgparser/types/geo/geos"
"github.com/cockroachdb/errors"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/ewkb"
)
// LineInterpolatePoints returns one or more points along the given
// LineString which are at an integral multiples of given fraction of
// LineString's total length. When repeat is set to false, it returns
// the first point.
func LineInterpolatePoints(g *geo.Geometry, fraction float64, repeat bool) (*geo.Geometry, error) {
if fraction < 0 || fraction > 1 {
return nil, errors.Newf("fraction %f should be within [0 1] range", fraction)
}
geomRepr, err := g.AsGeomT()
if err != nil {
return nil, err
}
switch geomRepr := geomRepr.(type) {
case *geom.LineString:
// In case fraction is greater than 0.5 or equal to 0 or repeat is false,
// then we will have only one interpolated point.
lengthOfLineString := geomRepr.Length()
if repeat && fraction <= 0.5 && fraction != 0 {
numberOfInterpolatedPoints := int(1 / fraction)
interpolatedPoints := geom.NewMultiPoint(geom.XY).SetSRID(geomRepr.SRID())
for pointInserted := 1; pointInserted <= numberOfInterpolatedPoints; pointInserted++ {
pointEWKB, err := geos.InterpolateLine(g.EWKB(), float64(pointInserted)*fraction*lengthOfLineString)
if err != nil {
return nil, err
}
point, err := ewkb.Unmarshal(pointEWKB)
if err != nil {
return nil, err
}
err = interpolatedPoints.Push(point.(*geom.Point))
if err != nil {
return nil, err
}
}
return geo.NewGeometryFromGeomT(interpolatedPoints)
}
interpolatedPointEWKB, err := geos.InterpolateLine(g.EWKB(), fraction*lengthOfLineString)
if err != nil {
return nil, err
}
return geo.ParseGeometryFromEWKB(interpolatedPointEWKB)
default:
return nil, errors.Newf("geometry %s should be LineString", g.ShapeType())
}
}