-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplification.go
More file actions
127 lines (104 loc) · 2.71 KB
/
simplification.go
File metadata and controls
127 lines (104 loc) · 2.71 KB
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
package tile
import (
"math"
)
type SimplificationStrategy string
const (
SimplifyNone SimplificationStrategy = "none"
SimplifyConservative SimplificationStrategy = "conservative"
SimplifyModerate SimplificationStrategy = "moderate"
SimplifyAggressive SimplificationStrategy = "aggressive"
SimplifyCustom SimplificationStrategy = "custom"
)
type SimplificationConfig struct {
Enabled bool
Strategy SimplificationStrategy
MaxZoom uint
MinZoom uint
ZoomLevels map[uint]float64
PixelTolerance float64
GeometryType string
KeepTopology bool
MinPoints int
}
var DefaultSimplificationConfig = SimplificationConfig{
Enabled: true,
Strategy: SimplifyModerate,
MaxZoom: 14,
MinZoom: 0,
PixelTolerance: 2.0,
KeepTopology: true,
MinPoints: 4,
}
func GetSimplificationTolerance(zoom uint, strategy SimplificationStrategy, pixelTolerance float64) float64 {
if zoom >= 14 {
return 0
}
switch strategy {
case SimplifyNone:
return 0
case SimplifyConservative:
return pixelTolerance * 0.5
case SimplifyModerate:
if zoom <= 5 {
return pixelTolerance * 2.0
} else if zoom <= 10 {
return pixelTolerance * 1.0
} else {
return pixelTolerance * 0.5
}
case SimplifyAggressive:
if zoom <= 5 {
return pixelTolerance * 4.0
} else if zoom <= 10 {
return pixelTolerance * 2.0
} else {
return pixelTolerance * 1.0
}
default:
return pixelTolerance
}
}
func CalculateZoomBasedTolerance(zoom uint, extent float64, baseTolerance float64) float64 {
if zoom >= 14 {
return 0
}
pixelSize := 40075016.6855785 / (math.Exp2(float64(zoom)) * extent)
tolerance := baseTolerance * pixelSize
if zoom <= 5 {
tolerance *= (1.0 + float64(5-zoom)*0.3)
}
return tolerance
}
func ShouldSimplify(zoom, maxZoom uint, enabled bool) bool {
return enabled && zoom < maxZoom
}
func GetGeometrySpecificTolerance(geomType string, baseTolerance float64) float64 {
switch geomType {
case "Point", "MultiPoint":
return 0
case "LineString", "MultiLineString":
return baseTolerance * 0.8
case "Polygon", "MultiPolygon":
return baseTolerance
default:
return baseTolerance
}
}
func CalculateOptimalTolerance(zoom uint, config SimplificationConfig, geomType string) float64 {
if !config.Enabled {
return 0
}
if zoom < config.MinZoom || zoom >= config.MaxZoom {
if zoom >= config.MaxZoom {
return 0
}
}
if len(config.ZoomLevels) > 0 {
if tolerance, ok := config.ZoomLevels[zoom]; ok {
return GetGeometrySpecificTolerance(geomType, tolerance)
}
}
baseTolerance := GetSimplificationTolerance(zoom, config.Strategy, config.PixelTolerance)
return GetGeometrySpecificTolerance(geomType, baseTolerance)
}