aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/paulmach/orb/simplify/helpers.go
blob: c44bb7990c488ec76929c73ef34c10c6b577f218 (plain) (blame)
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
// Package simplify implements several reducing/simplifying functions for `orb.Geometry` types.
package simplify

import "github.com/paulmach/orb"

type simplifier interface {
	simplify(orb.LineString, bool) (orb.LineString, []int)
}

func simplify(s simplifier, geom orb.Geometry) orb.Geometry {
	if geom == nil {
		return nil
	}

	switch g := geom.(type) {
	case orb.Point:
		return g
	case orb.MultiPoint:
		if g == nil {
			return nil
		}
		return g
	case orb.LineString:
		g = lineString(s, g)
		if len(g) == 0 {
			return nil
		}
		return g
	case orb.MultiLineString:
		g = multiLineString(s, g)
		if len(g) == 0 {
			return nil
		}
		return g
	case orb.Ring:
		g = ring(s, g)
		if len(g) == 0 {
			return nil
		}
		return g
	case orb.Polygon:
		g = polygon(s, g)
		if len(g) == 0 {
			return nil
		}
		return g
	case orb.MultiPolygon:
		g = multiPolygon(s, g)
		if len(g) == 0 {
			return nil
		}
		return g
	case orb.Collection:
		g = collection(s, g)
		if len(g) == 0 {
			return nil
		}
		return g
	case orb.Bound:
		return g
	}

	panic("unsupported type")
}

func lineString(s simplifier, ls orb.LineString) orb.LineString {
	return runSimplify(s, ls)
}

func multiLineString(s simplifier, mls orb.MultiLineString) orb.MultiLineString {
	for i := range mls {
		mls[i] = runSimplify(s, mls[i])
	}
	return mls
}

func ring(s simplifier, r orb.Ring) orb.Ring {
	return orb.Ring(runSimplify(s, orb.LineString(r)))
}

func polygon(s simplifier, p orb.Polygon) orb.Polygon {
	count := 0
	for i := range p {
		r := orb.Ring(runSimplify(s, orb.LineString(p[i])))
		if i != 0 && len(r) <= 2 {
			continue
		}

		p[count] = r
		count++
	}
	return p[:count]
}

func multiPolygon(s simplifier, mp orb.MultiPolygon) orb.MultiPolygon {
	count := 0
	for i := range mp {
		p := polygon(s, mp[i])
		if len(p[0]) <= 2 {
			continue
		}

		mp[count] = p
		count++
	}
	return mp[:count]
}

func collection(s simplifier, c orb.Collection) orb.Collection {
	for i := range c {
		c[i] = simplify(s, c[i])
	}
	return c
}

func runSimplify(s simplifier, ls orb.LineString) orb.LineString {
	if len(ls) <= 2 {
		return ls
	}
	ls, _ = s.simplify(ls, false)
	return ls
}