rts-sim-module/repository/calculate.go

42 lines
989 B
Go

package repository
import (
"joylink.club/rtsssimulation/repository/model/proto"
)
// ConvertKilometer 转换公里标
func ConvertKilometer(repo *Repository, km *proto.Kilometer, cs string) (*proto.Kilometer, error) {
if km.CoordinateSystem == cs {
return km, nil
}
kc, err := repo.getKilometerConvert(km.CoordinateSystem, cs)
if err != nil {
return nil, err
}
var sourceCsKm *proto.Kilometer
var targetCsKm *proto.Kilometer
if kc.KmA.CoordinateSystem == km.CoordinateSystem {
sourceCsKm = kc.KmA
targetCsKm = kc.KmB
} else {
sourceCsKm = kc.KmB
targetCsKm = kc.KmA
}
offset := km.Value - sourceCsKm.Value
var value int64
if kc.SameTrend {
value = targetCsKm.Value + offset
} else {
value = targetCsKm.Value - offset
}
//if value < 0 {
// return nil, errors.New(fmt.Sprintf("公里标[%s]转换为坐标系[%s]的公里标的值[%d]小于0", km, cs, value))
//}
return &proto.Kilometer{
Value: value,
CoordinateSystem: cs,
}, nil
}