aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/paulmach/orb/simplify/radial.go
blob: 71ecf1a70ebd3aa971cfda2994bd0c4d180795f9 (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
package simplify

import (
	"github.com/paulmach/orb"
)

var _ orb.Simplifier = &RadialSimplifier{}

// A RadialSimplifier wraps the Radial functions
type RadialSimplifier struct {
	DistanceFunc orb.DistanceFunc
	Threshold    float64 // euclidean distance
}

// Radial creates a new RadialSimplifier.
func Radial(df orb.DistanceFunc, threshold float64) *RadialSimplifier {
	return &RadialSimplifier{
		DistanceFunc: df,
		Threshold:    threshold,
	}
}

func (s *RadialSimplifier) simplify(ls orb.LineString, wim bool) (orb.LineString, []int) {
	var indexMap []int
	if wim {
		indexMap = append(indexMap, 0)
	}

	count := 1
	current := 0
	for i := 1; i < len(ls); i++ {
		if s.DistanceFunc(ls[current], ls[i]) > s.Threshold {
			current = i
			ls[count] = ls[i]
			count++
			if wim {
				indexMap = append(indexMap, current)
			}
		}
	}

	if current != len(ls)-1 {
		ls[count] = ls[len(ls)-1]
		count++
		if wim {
			indexMap = append(indexMap, len(ls)-1)
		}
	}

	return ls[:count], indexMap
}

// Simplify will run the simplification for any geometry type.
func (s *RadialSimplifier) Simplify(g orb.Geometry) orb.Geometry {
	return simplify(s, g)
}

// LineString will simplify the linestring using this simplifier.
func (s *RadialSimplifier) LineString(ls orb.LineString) orb.LineString {
	return lineString(s, ls)
}

// MultiLineString will simplify the multi-linestring using this simplifier.
func (s *RadialSimplifier) MultiLineString(mls orb.MultiLineString) orb.MultiLineString {
	return multiLineString(s, mls)
}

// Ring will simplify the ring using this simplifier.
func (s *RadialSimplifier) Ring(r orb.Ring) orb.Ring {
	return ring(s, r)
}

// Polygon will simplify the polygon using this simplifier.
func (s *RadialSimplifier) Polygon(p orb.Polygon) orb.Polygon {
	return polygon(s, p)
}

// MultiPolygon will simplify the multi-polygon using this simplifier.
func (s *RadialSimplifier) MultiPolygon(mp orb.MultiPolygon) orb.MultiPolygon {
	return multiPolygon(s, mp)
}

// Collection will simplify the collection using this simplifier.
func (s *RadialSimplifier) Collection(c orb.Collection) orb.Collection {
	return collection(s, c)
}