forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakevalid.go
123 lines (105 loc) · 2.85 KB
/
makevalid.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
package makevalid
import (
"context"
"sort"
"github.com/go-spatial/geom"
"github.com/go-spatial/tegola/maths"
"github.com/go-spatial/tegola/maths/points"
)
func allCoordForPts(idx int, pts ...[2]float64) []float64 {
if idx != 0 && idx != 1 {
panic("idx can only be 0 or 1 for x, and y")
}
fs := make([]float64, 0, len(pts))
for i := range pts {
fs = append(fs, pts[i][idx])
}
return fs
}
func sortUniqueF64(fs []float64) []float64 {
if len(fs) == 0 {
return fs
}
sort.Float64s(fs)
count := 0
for i := range fs {
if fs[count] == fs[i] {
continue
}
count++
fs[count] = fs[i]
}
return fs[:count+1]
}
// splitPoints will find the points amount the lines that lines should be split at.
func splitPoints(ctx context.Context, segments []maths.Line) (pts [][]maths.Pt, err error) {
// For each segment we keep a list of point that that segment needs to split
// at.
pts = make([][]maths.Pt, len(segments))
for i := range segments {
pts[i] = append(pts[i], segments[i][0], segments[i][1])
}
maths.FindIntersects(segments, func(src, dest int, ptfn func() maths.Pt) bool {
if ctx.Err() != nil {
// Want to exit early from the loop, the ctx got cancelled.
return false
}
sline, dline := segments[src], segments[dest]
// Check to see if the end points of sline and dline intersect?
if (sline[0].IsEqual(dline[0])) ||
(sline[0].IsEqual(dline[1])) ||
(sline[1].IsEqual(dline[0])) ||
(sline[1].IsEqual(dline[1])) {
return true
}
pt := ptfn().Round() // left most point.
if !sline.InBetween(pt) || !dline.InBetween(pt) {
return true
}
pts[src] = append(pts[src], pt)
pts[dest] = append(pts[dest], pt)
return true
})
if err := ctx.Err(); err != nil {
return nil, err
}
for i := range pts {
// Sort the items.
pts[i] = points.SortAndUnique(pts[i])
}
return pts, nil
}
func splitSegments(ctx context.Context, segments []maths.Line, clipbox *geom.Extent) (lns [][2][2]float64, err error) {
pts, err := splitPoints(ctx, segments)
if err != nil {
return nil, err
}
for _, pt := range pts {
for i := 1; i < len(pt); i++ {
ln := [2][2]float64{
{pt[i-1].X, pt[i-1].Y},
{pt[i].X, pt[i].Y},
}
// if clipbox is provided use it to filter out the lines.
if clipbox != nil && !clipbox.ContainsLine(ln) {
continue
}
lns = append(lns, ln)
}
}
return lns, err
}
func allPointsForSegments(segments [][2][2]float64) (pts [][2]float64) {
for i := range segments {
pts = append(pts, segments[i][0], segments[i][1])
}
return pts
}
// splitLines will split the given line segments at intersection points if they intersect at any point other then the end.
func splitLines(ctx context.Context, segments []maths.Line, clipbox *geom.Extent) ([]maths.Line, error) {
lns, err := splitSegments(ctx, segments, clipbox)
if err != nil {
return nil, err
}
return maths.NewLinesFloat64(lns...), nil
}