-
Notifications
You must be signed in to change notification settings - Fork 2
/
crs.go
64 lines (53 loc) · 1.59 KB
/
crs.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
package proj
import (
"fmt"
"strings"
"github.com/everystreet/go-proj/v8/cproj"
"github.com/golang/geo/s2"
)
// CRS is a coordinate reference system definition.
type CRS string
// Area returns the area of use for the CRS, or false if no such area is defined.
func (c CRS) Area() (*Area, bool, error) {
ctx := cproj.Context_create()
defer cproj.Context_destroy(ctx)
pj, err := c.instantiate(ctx)
if err != nil {
return nil, false, fmt.Errorf("invalid source '%v': %w", c, err)
}
defer cproj.Destroy(pj)
var north, east, south, west float64
if cproj.Get_area_of_use(ctx, pj, &west, &south, &east, &north, nil) != 1 {
return nil, false, nil
}
if north == -1000 || east == -1000 || south == -1000 || west == -1000 {
return nil, false, nil
}
return &Area{
BottomLeft: s2.LatLngFromDegrees(south, west),
TopRight: s2.LatLngFromDegrees(north, east),
}, true, nil
}
// Area of use for a particular CRS.
type Area struct {
BottomLeft s2.LatLng
TopRight s2.LatLng
}
// String returns the lower-cased CRS definition.
// If the definition is a proj-string, the "+type=crs" option if appended if not present.
func (c CRS) String() string {
str := strings.ToLower(string(c))
if strings.HasPrefix(str, "proj=") || strings.HasPrefix(str, "+proj=") ||
strings.HasPrefix(str, "+init=") || strings.HasPrefix(str, "+title=") &&
!strings.Contains(str, "type=crs") {
str += " +type=crs"
}
return str
}
func (c CRS) instantiate(ctx *cproj.PJ_CONTEXT) (*cproj.PJ, error) {
pj := cproj.Create(ctx, c.String())
if pj == nil {
return nil, fmt.Errorf("failed to create CRS")
}
return pj, nil
}