u_xosc.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package util
  2. import (
  3. "fmt"
  4. "github.com/beevik/etree"
  5. )
  6. // GetStartAndEnd
  7. // - filepath 文件路径 "D:\\test.xosc"
  8. func GetStartAndEnd(filepath string) (string, string, string, string) {
  9. startPositionX := ""
  10. startPositionY := ""
  11. endPositionX := ""
  12. endPositionY := ""
  13. doc := etree.NewDocument()
  14. if err := doc.ReadFromFile(filepath); err != nil {
  15. panic(err)
  16. }
  17. storys := doc.SelectElement("OpenSCENARIO").SelectElement("Storyboard").SelectElements("Story")
  18. for _, story := range storys {
  19. for _, attr := range story.Attr {
  20. if attr.Key == "name" && attr.Value == "mystory_ego" {
  21. events := story.SelectElement("Act").SelectElement("ManeuverGroup").SelectElement("Maneuver").SelectElements("Event")
  22. for _, event := range events {
  23. for _, attr2 := range event.Attr {
  24. if attr2.Key == "name" && attr2.Value == "Event1" {
  25. vertexs := event.SelectElement("Action").SelectElement("PrivateAction").SelectElement("RoutingAction").SelectElement("FollowTrajectoryAction").SelectElement("Trajectory").SelectElement("Shape").SelectElement("Polyline").SelectElements("Vertex")
  26. for i, vertex := range vertexs {
  27. if i == 0 {
  28. worldPosition := vertex.SelectElement("Position").SelectElement("WorldPosition")
  29. for _, attr3 := range worldPosition.Attr {
  30. if attr3.Key == "x" {
  31. startPositionX = attr3.Value
  32. }
  33. if attr3.Key == "y" {
  34. startPositionY = attr3.Value
  35. }
  36. }
  37. }
  38. if i == len(vertexs)-1 {
  39. worldPosition := vertex.SelectElement("Position").SelectElement("WorldPosition")
  40. for _, attr3 := range worldPosition.Attr {
  41. if attr3.Key == "x" {
  42. endPositionX = attr3.Value
  43. }
  44. if attr3.Key == "y" {
  45. endPositionY = attr3.Value
  46. }
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. fmt.Println(startPositionX)
  57. fmt.Println(startPositionY)
  58. fmt.Println(endPositionX)
  59. fmt.Println(endPositionY)
  60. return startPositionX, startPositionY, endPositionX, endPositionY
  61. }