forked from rubenv/topojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbounds.go
96 lines (85 loc) · 1.58 KB
/
bounds.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
package topojson
import (
"math"
"github.com/paulmach/orb"
)
func (t *Topology) bounds() {
t.BoundingBox = []float64{
math.MaxFloat64,
math.MaxFloat64,
-math.MaxFloat64,
-math.MaxFloat64,
}
for _, f := range t.input {
t.boundGeometry(f.Geometry)
}
}
func (t *Topology) boundGeometry(g orb.Geometry) {
switch c := g.(type) {
case orb.Point:
t.BBox(c.Bound())
case orb.MultiPoint:
t.BBox(c.Bound())
case orb.LineString:
t.BBox(c.Bound())
case orb.MultiLineString:
t.BBox(c.Bound())
case orb.Polygon:
t.BBox(c.Bound())
case orb.MultiPolygon:
t.BBox(c.Bound())
case orb.Collection:
t.BBox(c.Bound())
// for _, geo := range c {
// t.boundGeometry(geo)
// }
}
}
func (t *Topology) BBox(b orb.Bound) {
xx := []float64{b.Min[0], b.Max[0]}
yy := []float64{b.Min[1], b.Max[1]}
for _, x := range xx {
if x < t.BoundingBox[0] {
t.BoundingBox[0] = x
}
if x > t.BoundingBox[2] {
t.BoundingBox[2] = x
}
}
for _, y := range yy {
if y < t.BoundingBox[1] {
t.BoundingBox[1] = y
}
if y > t.BoundingBox[3] {
t.BoundingBox[3] = y
}
}
}
func (t *Topology) boundPoint(p []float64) {
x := p[0]
y := p[1]
if x < t.BoundingBox[0] {
t.BoundingBox[0] = x
}
if x > t.BoundingBox[2] {
t.BoundingBox[2] = x
}
if y < t.BoundingBox[1] {
t.BoundingBox[1] = y
}
if y > t.BoundingBox[3] {
t.BoundingBox[3] = y
}
}
func (t *Topology) boundPoints(l [][]float64) {
for _, p := range l {
t.boundPoint(p)
}
}
func (t *Topology) boundMultiPoints(ml [][][]float64) {
for _, l := range ml {
for _, p := range l {
t.boundPoint(p)
}
}
}