forked from spatial-go/geoos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordtransform.go
More file actions
110 lines (95 loc) · 2.54 KB
/
coordtransform.go
File metadata and controls
110 lines (95 loc) · 2.54 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
package coordtransform
import (
"errors"
"sync"
"github.com/spatial-go/geoos/algorithm/matrix"
)
const (
MERCATORTOLL = "MERCATORTOLL"
LLTOMERCATOR = "LLTOMERCATOR"
)
type Transformer struct {
CoordType string
}
var instance *Transformer
var once sync.Once
// GetInstance ...
func GetInstance() *Transformer {
once.Do(func() {
instance = &Transformer{}
})
return instance
}
// NewTransformer ...
func NewTransformer(coordType string) *Transformer {
return &Transformer{CoordType: coordType}
}
// TransformLatLng ...
func (t *Transformer) TransformLatLng(lng, lat float64) (float64, float64) {
switch t.CoordType {
case MERCATORTOLL:
lng, lat = MercatorToLL(lng, lat)
case LLTOMERCATOR:
lng, lat = LLToMercator(lng, lat)
default:
}
return lng, lat
}
// TransformPoint ...
func (t *Transformer) TransformPoint(point matrix.Matrix) matrix.Matrix {
lng, lat := t.TransformLatLng(point[0], point[1])
return matrix.Matrix{lng, lat}
}
// TransformMultiPoint ...
func (t *Transformer) TransformMultiPoint(multiPoint []matrix.Matrix) []matrix.Matrix {
for i := range multiPoint {
multiPoint[i] = t.TransformPoint(multiPoint[i])
}
return multiPoint
}
// TransformLine ...
func (t *Transformer) TransformLine(lineString matrix.LineMatrix) matrix.LineMatrix {
for i := range lineString {
lineString[i] = t.TransformPoint(lineString[i])
}
return lineString
}
// TransformPolygon ...
func (t *Transformer) TransformPolygon(polygon matrix.PolygonMatrix) matrix.PolygonMatrix {
for i := range polygon {
polygon[i] = t.TransformLine(polygon[i])
}
return polygon
}
// TransformMultiLineString ...
func (t *Transformer) TransformMultiLineString(multiLineString []matrix.LineMatrix) []matrix.LineMatrix {
for i := range multiLineString {
multiLineString[i] = t.TransformLine(multiLineString[i])
}
return multiLineString
}
// TransformMultiPolygon ...
func (t *Transformer) TransformMultiPolygon(multiPolygon []matrix.PolygonMatrix) []matrix.PolygonMatrix {
for i := range multiPolygon {
multiPolygon[i] = t.TransformPolygon(multiPolygon[i])
}
return multiPolygon
}
// TransformGeometry ...
func (t *Transformer) TransformGeometry(geom matrix.Steric) (matrix.Steric, error) {
switch mt := geom.(type) {
case matrix.Matrix:
return t.TransformPoint(mt), nil
case matrix.LineMatrix:
return t.TransformLine(mt), nil
case matrix.PolygonMatrix:
return t.TransformPolygon(mt), nil
case matrix.Collection:
for i, _ := range mt {
mt[i], _ = t.TransformGeometry(mt[i])
}
return mt, nil
default:
return nil, errors.New("error geometry type")
}
}