rts-sim-module/repository/calculate.go

45 lines
1.0 KiB
Go

package repository
import (
"errors"
"fmt"
"joylink.club/rtsssimulation/repository/model/proto"
)
// 转换公里标
func convertKilometer(repo *Repository, km *proto.Kilometer, cs string, dir proto.Direction) (*proto.Kilometer, error) {
if km.CoordinateSystem == cs {
return km, nil
}
kc, err := repo.getKilometerConvert(km.CoordinateSystem, cs, km.Direction, dir)
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,
Direction: dir,
}, nil
}