-
Notifications
You must be signed in to change notification settings - Fork 16
/
transform.go
131 lines (122 loc) · 2.77 KB
/
transform.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
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
128
129
130
131
package geom
import (
"github.com/ctessum/geom/proj"
)
// Transform shifts the coordinates of p according to t.
func (p Point) Transform(t proj.Transformer) (Geom, error) {
if t == nil {
return p, nil
}
var err error
p2 := Point{}
p2.X, p2.Y, err = t(p.X, p.Y)
return p2, err
}
// Transform shifts the coordinates of mp according to t.
func (mp MultiPoint) Transform(t proj.Transformer) (Geom, error) {
if t == nil {
return mp, nil
}
mp2 := make(MultiPoint, len(mp))
for i, p := range mp {
g, err := p.Transform(t)
if err != nil {
return nil, err
}
mp2[i] = g.(Point)
}
return mp2, nil
}
// Transform shifts the coordinates of l according to t.
func (l LineString) Transform(t proj.Transformer) (Geom, error) {
if t == nil {
return l, nil
}
l2 := make(LineString, len(l))
var err error
for i, p := range l {
p2 := Point{}
p2.X, p2.Y, err = t(p.X, p.Y)
if err != nil {
return nil, err
}
l2[i] = p2
}
return l2, nil
}
// Transform shifts the coordinates of ml according to t.
func (ml MultiLineString) Transform(t proj.Transformer) (Geom, error) {
if t == nil {
return ml, nil
}
ml2 := make(MultiLineString, len(ml))
for i, l := range ml {
g, err := l.Transform(t)
ml2[i] = g.(LineString)
if err != nil {
return nil, err
}
}
return ml2, nil
}
// Transform shifts the coordinates of p according to t.
func (p Polygon) Transform(t proj.Transformer) (Geom, error) {
if t == nil {
return p, nil
}
p2 := make(Polygon, len(p))
var err error
for i, r := range p {
p2[i] = make([]Point, len(r))
for j, pp := range r {
pp2 := Point{}
pp2.X, pp2.Y, err = t(pp.X, pp.Y)
if err != nil {
return nil, err
}
p2[i][j] = pp2
}
}
return p2, nil
}
// Transform shifts the coordinates of mp according to t.
func (mp MultiPolygon) Transform(t proj.Transformer) (Geom, error) {
if t == nil {
return mp, nil
}
mp2 := make(MultiPolygon, len(mp))
for i, p := range mp {
g, err := p.Transform(t)
mp2[i] = g.(Polygon)
if err != nil {
return nil, err
}
}
return mp2, nil
}
// Transform shifts the coordinates of gc according to t.
func (gc GeometryCollection) Transform(t proj.Transformer) (Geom, error) {
if t == nil {
return gc, nil
}
gc2 := make(GeometryCollection, len(gc))
var err error
for i, g := range gc {
gc2[i], err = g.Transform(t)
if err != nil {
return nil, err
}
}
return gc2, nil
}
// Transform shifts the coordinates of b according to t.
// If t is not nil, this function returns a Polygon instead of a *Bounds
// because the transformed polygon may not match the transformed bounding
// rectangle.
func (b *Bounds) Transform(t proj.Transformer) (Geom, error) {
if t == nil {
return b, nil
}
p := Polygon{{b.Min, {X: b.Max.X, Y: b.Min.Y}, b.Max, {X: b.Min.X, Y: b.Max.Y}}}
return p.Transform(t)
}