2023-09-06 16:20:36 +08:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"joylink.club/rtsssimulation/repository/model/proto"
|
|
|
|
)
|
|
|
|
|
2024-01-10 14:06:00 +08:00
|
|
|
// ConvertKilometer 转换公里标
|
|
|
|
func ConvertKilometer(repo *Repository, km *proto.Kilometer, cs string) (*proto.Kilometer, error) {
|
2023-09-14 15:06:51 +08:00
|
|
|
if km.CoordinateSystem == cs {
|
2023-09-06 16:20:36 +08:00
|
|
|
return km, nil
|
|
|
|
}
|
|
|
|
|
2023-09-25 14:38:13 +08:00
|
|
|
kc, err := repo.getKilometerConvert(km.CoordinateSystem, cs)
|
2023-09-06 16:20:36 +08:00
|
|
|
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
|
|
|
|
}
|
2023-10-07 14:19:46 +08:00
|
|
|
//if value < 0 {
|
|
|
|
// return nil, errors.New(fmt.Sprintf("公里标[%s]转换为坐标系[%s]的公里标的值[%d]小于0", km, cs, value))
|
|
|
|
//}
|
2023-09-06 16:20:36 +08:00
|
|
|
return &proto.Kilometer{
|
|
|
|
Value: value,
|
|
|
|
CoordinateSystem: cs,
|
|
|
|
}, nil
|
|
|
|
}
|