-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbounds.go
76 lines (66 loc) · 1.37 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
package topojson
import (
"math"
"github.com/paulmach/go.geojson"
)
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 *geojson.Geometry) {
switch g.Type {
case geojson.GeometryCollection:
for _, geom := range g.Geometries {
t.boundGeometry(geom)
}
case geojson.GeometryPoint:
t.boundPoint(g.Point)
case geojson.GeometryMultiPoint:
t.boundPoints(g.MultiPoint)
case geojson.GeometryLineString:
t.boundPoints(g.LineString)
case geojson.GeometryMultiLineString:
t.boundMultiPoints(g.MultiLineString)
case geojson.GeometryPolygon:
t.boundMultiPoints(g.Polygon)
case geojson.GeometryMultiPolygon:
for _, poly := range g.MultiPolygon {
t.boundMultiPoints(poly)
}
}
}
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)
}
}
}