-
Notifications
You must be signed in to change notification settings - Fork 3
/
proj.go
349 lines (308 loc) · 8.29 KB
/
proj.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package proj
/*
#cgo LDFLAGS: /usr/local/lib/libproj.a
#cgo LDFLAGS: -lm
#include "proj_go.h"
*/
import "C"
import (
"errors"
"fmt"
"math"
"runtime"
"unsafe"
)
type Context struct {
pj_context *C.PJ_CONTEXT
opened bool
counter uint64
projections map[uint64]*PJ
}
// A projection object
type PJ struct {
pj *C.PJ
context *Context
index uint64
opened bool
}
type LibInfo struct {
Major int // Major version number.
Minor int // Minor version number.
Patch int // Patch level of release.
Release string // Release info. Version number and release date, e.g. “Rel. 4.9.3, 15 August 2016”.
Version string // Text representation of the full version number, e.g. “4.9.3”.
Searchpath string // Search path for PROJ. List of directories separated by semicolons (Windows) or colons (non-Windows).
}
type ProjInfo struct {
ID string // Short ID of the operation the PJ object is based on, that is, what comes afther the +proj= in a proj-string, e.g. “merc”.
Description string // Long describes of the operation the PJ object is based on, e.g. “Mercator Cyl, Sph&Ell lat_ts=”.
Definition string // The proj-string that was used to create the PJ object with, e.g. “+proj=merc +lat_0=24 +lon_0=53 +ellps=WGS84”.
HasInverse bool // True if an inverse mapping of the defined operation exists,
Accuracy float64 // Expected accuracy of the transformation. -1 if unknown.
}
// The direction of a transformation
type Direction C.PJ_DIRECTION
const (
Fwd = Direction(C.PJ_FWD) // Forward transformation
Ident = Direction(C.PJ_IDENT) // Do nothing
Inv = Direction(C.PJ_INV) // Inverse transformation
)
var (
errContextClosed = errors.New("Context is closed")
errDataSizeMismatch = errors.New("Data size mismatch")
errMissingData = errors.New("Missing data")
errProjectionClosed = errors.New("Projection is closed")
)
// Create a context
func NewContext() *Context {
ctx := Context{
pj_context: C.proj_context_create(),
counter: 0,
projections: make(map[uint64]*PJ),
opened: true,
}
runtime.SetFinalizer(&ctx, (*Context).Close)
return &ctx
}
// Close a context
func (ctx *Context) Close() {
if ctx.opened {
indexen := make([]uint64, 0, len(ctx.projections))
for i := range ctx.projections {
indexen = append(indexen, i)
}
for _, i := range indexen {
p := ctx.projections[i]
if p.opened {
C.proj_destroy(p.pj)
p.context = nil
p.opened = false
}
delete(ctx.projections, i)
}
C.proj_context_destroy(ctx.pj_context)
ctx.pj_context = nil
ctx.opened = false
}
}
// Create a transformation object
func (ctx *Context) Create(definition string) (*PJ, error) {
if !ctx.opened {
return &PJ{}, errContextClosed
}
cs := C.CString(definition)
defer C.free(unsafe.Pointer(cs))
pj := C.proj_create(ctx.pj_context, cs)
if pj == nil {
errno := C.proj_context_errno(ctx.pj_context)
err := C.GoString(C.proj_errno_string(errno))
return &PJ{}, errors.New(err)
}
p := PJ{
opened: true,
context: ctx,
index: ctx.counter,
pj: pj,
}
ctx.projections[ctx.counter] = &p
ctx.counter++
runtime.SetFinalizer(&p, (*PJ).Close)
return &p, nil
}
// Close a transformation object
func (p *PJ) Close() {
if p.opened {
C.proj_destroy(p.pj)
if p.context.opened {
delete(p.context.projections, p.index)
}
p.context = nil
p.opened = false
}
}
// Get information about the transformation object
func (p *PJ) Info() (ProjInfo, error) {
if !p.opened {
return ProjInfo{}, errProjectionClosed
}
info := C.proj_pj_info(p.pj)
hasInv := false
if info.has_inverse != 0 {
hasInv = true
}
return ProjInfo{
ID: C.GoString(info.id),
Description: C.GoString(info.description),
Definition: C.GoString(info.definition),
HasInverse: hasInv,
Accuracy: float64(info.accuracy),
}, nil
}
// Transform a single coordinate
func (p *PJ) Trans(direction Direction, u1, v1, w1, t1 float64) (u2, v2, w2, t2 float64, err error) {
if !p.opened {
return 0, 0, 0, 0, errProjectionClosed
}
var u, v, w, t C.double
C.trans(p.pj, C.PJ_DIRECTION(direction), C.double(u1), C.double(v1), C.double(w1), C.double(t1), &u, &v, &w, &t)
e := C.proj_errno(p.pj)
if e != 0 {
return 0, 0, 0, 0, errors.New(C.GoString(C.proj_errno_string(e)))
}
return float64(u), float64(v), float64(w), float64(t), nil
}
/*
Transform a series of coordinates, where the individual coordinate dimension may be represented by a slice that is either
1. fully populated
2. nil and/or a length of zero, which will be treated as a fully populated slice of zeroes
3. of length one, i.e. a constant, which will be treated as a fully populated slice of that constant value
Note: if an input coordinate is constant, but the output coordinate varies, you need to supply a fully populated slice as input
*/
func (p *PJ) TransSlice(direction Direction, u1, v1, w1, t1 []float64) (u2, v2, w2, t2 []float64, err error) {
if !p.opened {
return nil, nil, nil, nil, errProjectionClosed
}
if u1 == nil || v1 == nil {
return nil, nil, nil, nil, errMissingData
}
var un, vn, wn, tn int
var u, v, w, t []C.double
var up, vp, wp, tp *C.double
var unc, vnc, wnc, tnc C.size_t
un = len(u1)
unc = C.size_t(un)
vn = len(v1)
vnc = C.size_t(vn)
if w1 != nil {
wn = len(w1)
wnc = C.size_t(wn)
if t1 != nil {
tn = len(t1)
tnc = C.size_t(tn)
}
}
r := []int{un, vn, tn, wn}
var n int
for _, i := range r {
if i > n {
n = i
}
}
for _, i := range r {
if i > 1 && i < n {
return nil, nil, nil, nil, errDataSizeMismatch
}
}
u = make([]C.double, un)
up = &u[0]
for i := 0; i < un; i++ {
u[i] = C.double(u1[i])
}
v = make([]C.double, vn)
vp = &v[0]
for i := 0; i < vn; i++ {
v[i] = C.double(v1[i])
}
if w1 != nil {
w = make([]C.double, wn)
wp = &w[0]
for i := 0; i < wn; i++ {
w[i] = C.double(w1[i])
}
if t1 != nil {
t = make([]C.double, tn)
tp = &t[0]
for i := 0; i < tn; i++ {
t[i] = C.double(t1[i])
}
}
}
st := C.size_t(C.sizeof_double)
count := int(C.proj_trans_generic(
p.pj,
C.PJ_DIRECTION(direction),
up, st, unc,
vp, st, vnc,
wp, st, wnc,
tp, st, tnc))
e := C.proj_errno(p.pj)
if e != 0 {
return nil, nil, nil, nil, errors.New(C.GoString(C.proj_errno_string(e)))
}
if count != n {
return nil, nil, nil, nil, fmt.Errorf("Got %d coordinates, translated %d coordinates", n, count)
}
u2 = make([]float64, un)
for i := 0; i < un; i++ {
u2[i] = float64(u[i])
}
v2 = make([]float64, vn)
for i := 0; i < vn; i++ {
v2[i] = float64(v[i])
}
if w1 != nil {
w2 = make([]float64, wn)
for i := 0; i < wn; i++ {
w2[i] = float64(w[i])
}
if t1 != nil {
t2 = make([]float64, tn)
for i := 0; i < tn; i++ {
t2[i] = float64(t[i])
}
}
}
return
}
// Calculate geodesic distance between two points in geodetic coordinates
//
// The calculated distance is between the two points located on the ellipsoid
func (p *PJ) Dist(u1, v1, u2, v2 float64) (float64, error) {
if !p.opened {
return 0, errProjectionClosed
}
a := C.uvwt(C.double(u1), C.double(v1), 0, 0)
b := C.uvwt(C.double(u2), C.double(v2), 0, 0)
d := C.proj_lp_dist(p.pj, a, b)
e := C.proj_errno(p.pj)
if e != 0 {
return 0, errors.New(C.GoString(C.proj_errno_string(e)))
}
return float64(d), nil
}
// Calculate geodesic distance between two points in geodetic coordinates
//
// Similar to Dist() but also takes the height above the ellipsoid into account
func (p *PJ) Dist3(u1, v1, w1, u2, v2, w2 float64) (float64, error) {
if !p.opened {
return 0, errProjectionClosed
}
a := C.uvwt(C.double(u1), C.double(v1), C.double(w1), 0)
b := C.uvwt(C.double(u2), C.double(v2), C.double(w2), 0)
d := C.proj_lpz_dist(p.pj, a, b)
e := C.proj_errno(p.pj)
if e != 0 {
return 0, errors.New(C.GoString(C.proj_errno_string(e)))
}
return float64(d), nil
}
// Get information about the current instance of the PROJ library
func Info() LibInfo {
info := C.proj_info()
return LibInfo{
Major: int(info.major),
Minor: int(info.minor),
Patch: int(info.patch),
Release: C.GoString(info.release),
Version: C.GoString(info.version),
Searchpath: C.GoString(info.searchpath),
}
}
// Convert degrees to radians
func DegToRad(deg float64) float64 {
return deg / 180 * math.Pi
}
// Convert radians to degrees
func RadToDeg(rad float64) float64 {
return rad / math.Pi * 180
}