-
Notifications
You must be signed in to change notification settings - Fork 21
/
Convert_test.go
185 lines (158 loc) · 4.17 KB
/
Convert_test.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (C) 2018, Michael P. Gerlek (Flaxen Consulting)
//
// Portions of this code were derived from the PROJ.4 software
// In keeping with the terms of the PROJ.4 project, this software
// is provided under the MIT-style license in `LICENSE.md` and may
// additionally be subject to the copyrights of the PROJ.4 authors.
package proj_test
import (
"fmt"
"testing"
"github.com/go-spatial/proj"
"github.com/stretchr/testify/assert"
)
var inputA = []float64{
-0.127758, 51.507351, // London
2.352222, 48.856614, // Paris
12.496366, 41.902783, // Rome
}
var inputB = []float64{
-77.625583, 38.833846, // mpg
}
type testcase struct {
dest proj.EPSGCode
expectedA []float64
expectedB []float64
}
var testcases = []testcase{
{
dest: proj.EPSG3395,
expectedA: []float64{
-14221.96, 6678068.96,
261848.16, 6218371.80,
1391089.10, 5117883.04,
},
expectedB: []float64{
-8641240.37, 4671101.60,
},
},
{
dest: proj.EPSG3857,
expectedA: []float64{
-14221.96, 6711533.71,
261848.16, 6250566.72,
1391089.10, 5146427.91,
},
expectedB: []float64{
-8641240.37, 4697899.31,
},
},
{
dest: proj.EPSG4087,
expectedA: []float64{
-14221.96, 5733772.09,
261848.16, 5438693.39,
1391089.10, 4664596.47,
},
expectedB: []float64{
-8641240.37, 4322963.96,
},
},
}
func TestConvert(t *testing.T) {
assert := assert.New(t)
for _, tc := range testcases {
outputA, err := proj.Convert(tc.dest, inputA)
assert.NoError(err)
outputB, err := proj.Convert(tc.dest, inputB)
assert.NoError(err)
invA, err := proj.Inverse(tc.dest, tc.expectedA)
assert.NoError(err)
invB, err := proj.Inverse(tc.dest, tc.expectedB)
assert.NoError(err)
const tol = 1.0e-2
for i := range tc.expectedA {
tag := fmt.Sprintf("epsg:%d, input=A.%d", int(tc.dest), i)
assert.InDelta(tc.expectedA[i], outputA[i], tol, tag)
assert.InDelta(tc.expectedA[i], outputA[i], tol, tag)
}
for i := range tc.expectedB {
tag := fmt.Sprintf("epsg:%d, input=B.%d", int(tc.dest), i)
assert.InDelta(tc.expectedB[i], outputB[i], tol, tag)
assert.InDelta(tc.expectedB[i], outputB[i], tol, tag)
}
for i := range tc.expectedA {
tag := fmt.Sprintf("inverse: epsg:%d, input=A.%d", int(tc.dest), i)
assert.InDelta(invA[i], inputA[i], tol, tag)
}
for i := range tc.expectedB {
tag := fmt.Sprintf("inverse: epsg:%d, input=B.%d", int(tc.dest), i)
assert.InDelta(invB[i], inputB[i], tol, tag)
}
}
}
func TestEnsureRaisedError(t *testing.T) {
type testcase struct {
op string
pt []float64
expectedErr string
srid proj.EPSGCode
}
fn := func(tc testcase) func(t *testing.T) {
return func(t *testing.T) {
var err error
if tc.op == "convert" {
_, err = proj.Convert(proj.EPSGCode(tc.srid), tc.pt)
} else {
_, err = proj.Inverse(proj.EPSGCode(tc.srid), tc.pt)
}
if err == nil {
t.Errorf("didn't get expected error: %v", tc.expectedErr)
return
}
if err.Error() != tc.expectedErr {
t.Errorf("error: %v not equal to expected error: %v", err.Error(), tc.expectedErr)
}
}
}
tests := map[string]testcase{
"3857 out of bounds WGS84": {
op: "convert",
srid: proj.WebMercator,
pt: []float64{-180.0, 90.0},
expectedErr: "tolerance condition error",
},
"4326 not supported as source srid": {
op: "convert",
srid: proj.EPSG4326,
pt: []float64{0, 0},
expectedErr: "epsg code is not a supported projection",
},
"convert bad point count": {
op: "convert",
srid: proj.WorldMercator,
pt: []float64{-180.0, 90.0, 11.0},
expectedErr: "input array of lon/lat values must be an even number",
},
"inverse bad point count": {
op: "inverse",
srid: proj.WorldMercator,
pt: []float64{-180.0, 90.0, 11.0},
expectedErr: "input array of x/y values must be an even number",
},
}
for name, tc := range tests {
t.Run(name, fn(tc))
}
}
func ExampleConvert() {
var dd = []float64{
-77.625583, 38.833846,
}
xy, err := proj.Convert(proj.EPSG3395, dd)
if err != nil {
panic(err)
}
fmt.Printf("%.2f, %.2f\n", xy[0], xy[1])
// Output: -8641240.37, 4671101.60
}