aboutsummaryrefslogtreecommitdiff
path: root/official_climb/model.go
blob: beecd020c246947b043bf9371ffe6ef18ec65c22 (plain) (blame)
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

package official_climb

import (
	"fmt"
	"math"
	"strings"
	"time"

	"github.com/jftuga/geodist"
	"github.com/martinlehoux/biking_home/climb"
)

const DefaultMatchRadiusM = 100.0

type OfficialClimb struct {
	ID         int64
	Name       string
	StartCoord geodist.Coord
	EndCoord   geodist.Coord
	// Sample is the passage the climb was created from; matching rejects candidates outside its band.
	Sample    climb.SegmentSample
	CreatedAt time.Time
	UpdatedAt time.Time
}

type MatchPolicy struct {
	EndpointRadiusM float64
}

func DefaultMatchPolicy() MatchPolicy {
	return MatchPolicy{EndpointRadiusM: DefaultMatchRadiusM}
}

func (policy MatchPolicy) Validate() error {
	if math.IsNaN(policy.EndpointRadiusM) || math.IsInf(policy.EndpointRadiusM, 0) || policy.EndpointRadiusM <= 0 {
		return fmt.Errorf("official climb endpoint radius must be greater than zero")
	}
	return nil
}

func (climb OfficialClimb) validate() error {
	if strings.TrimSpace(climb.Name) == "" {
		return fmt.Errorf("official climb name is required")
	}
	if !validCoord(climb.StartCoord) || !validCoord(climb.EndCoord) {
		return fmt.Errorf("official climb coordinates are invalid")
	}
	if climb.Sample.DistanceM <= 0 || climb.Sample.ElevationGainM <= 0 {
		return fmt.Errorf("official climb sample must record a positive distance and elevation gain")
	}
	return nil
}

func validCoord(coord geodist.Coord) bool {
	return coord.Lat >= -90 && coord.Lat <= 90 && coord.Lon >= -180 && coord.Lon <= 180
}