u_xosc.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package util
  2. import (
  3. "fmt"
  4. "github.com/beevik/etree"
  5. "strings"
  6. )
  7. var (
  8. xodr1Key = "OpenDRIVE2_add_stop_line_marking_20231229.xodr"
  9. xodr1Value = "resource/OpenDRIVE2_add_stop_line_marking_20231229.xodr"
  10. xodr2Key = "thq_1116.xodr"
  11. xodr2Value = "resource/thq_1116.xodr"
  12. xodr3Key = "thq_20240706.xodr"
  13. xodr3Value = "resource/thq_20240706.xodr"
  14. xodr4Key = "yizhuang.xodr"
  15. xodr4Value = "resource/yizhuang.xodr"
  16. xodr5Key = "anqing.xodr"
  17. xodr5Value = "resource/anqing.xodr"
  18. osgb1Key = "jinlong_all_addtrafficlight_20240226_5_guazai_2.opt.osgb"
  19. osgb1Value = "resource/jinlong_all_addtrafficlight_20240226_5_guazai_2.opt.osgb"
  20. osgb2Key = "thq_1116.opt.osgb"
  21. osgb2Value = "resource/thq_1116.opt.osgb"
  22. osgb3Key = "thq_20230710.osgb"
  23. osgb3Value = "resource/thq_20230710.osgb"
  24. osgb4Key = "yizhuang.osgb"
  25. osgb4Value = "resource/yizhuang.osgb"
  26. osgb5Key = "anqing.osgb"
  27. osgb5Value = "resource/anqing.osgb"
  28. )
  29. // - filepath 文件路径 "D:\\test.xosc"
  30. func ParseXosc(xoscPath string) (string, string, string, string, string, string, string, string, string, string) {
  31. startPositionX := ""
  32. startPositionY := ""
  33. startPositionZ := ""
  34. startPositionH := ""
  35. endPositionX := ""
  36. endPositionY := ""
  37. endPositionZ := ""
  38. endPositionH := ""
  39. xodrPath := ""
  40. osgbPath := ""
  41. doc := etree.NewDocument()
  42. if err := doc.ReadFromFile(xoscPath); err != nil {
  43. panic(err)
  44. }
  45. storys := doc.SelectElement("OpenSCENARIO").SelectElement("Storyboard").SelectElements("Story")
  46. for _, story := range storys {
  47. for _, attr := range story.Attr {
  48. if attr.Key == "name" && attr.Value == "mystory_ego" {
  49. events := story.SelectElement("Act").SelectElement("ManeuverGroup").SelectElement("Maneuver").SelectElements("Event")
  50. for _, event := range events {
  51. for _, attr2 := range event.Attr {
  52. if attr2.Key == "name" && attr2.Value == "Event1" {
  53. vertexs := event.SelectElement("Action").SelectElement("PrivateAction").SelectElement("RoutingAction").SelectElement("FollowTrajectoryAction").SelectElement("Trajectory").SelectElement("Shape").SelectElement("Polyline").SelectElements("Vertex")
  54. for i, vertex := range vertexs {
  55. if i == 0 {
  56. worldPosition := vertex.SelectElement("Position").SelectElement("WorldPosition")
  57. for _, attr3 := range worldPosition.Attr {
  58. fmt.Printf("【%v】", attr3)
  59. if attr3.Key == "x" {
  60. startPositionX = attr3.Value
  61. }
  62. if attr3.Key == "y" {
  63. startPositionY = attr3.Value
  64. }
  65. if attr3.Key == "z" {
  66. startPositionZ = attr3.Value
  67. }
  68. if attr3.Key == "h" {
  69. startPositionH = attr3.Value
  70. }
  71. }
  72. }
  73. if i == len(vertexs)-1 {
  74. worldPosition := vertex.SelectElement("Position").SelectElement("WorldPosition")
  75. for _, attr3 := range worldPosition.Attr {
  76. fmt.Printf("【%v】", attr3)
  77. if attr3.Key == "x" {
  78. endPositionX = attr3.Value
  79. }
  80. if attr3.Key == "y" {
  81. endPositionY = attr3.Value
  82. }
  83. if attr3.Key == "z" {
  84. endPositionZ = attr3.Value
  85. }
  86. if attr3.Key == "h" {
  87. endPositionH = attr3.Value
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. logicFile := doc.SelectElement("OpenSCENARIO").SelectElement("RoadNetwork").SelectElement("LogicFile")
  99. for _, attr := range logicFile.Attr {
  100. if attr.Key == "filepath" {
  101. xodrKey := attr.Value
  102. xodrPath = getValueByKey(xodrKey)
  103. }
  104. }
  105. sceneGraphFile := doc.SelectElement("OpenSCENARIO").SelectElement("RoadNetwork").SelectElement("SceneGraphFile")
  106. for _, attr := range sceneGraphFile.Attr {
  107. if attr.Key == "filepath" {
  108. osgbKey := attr.Value
  109. osgbPath = getValueByKey(osgbKey)
  110. }
  111. }
  112. return startPositionX, startPositionY, startPositionZ, startPositionH, endPositionX, endPositionY, endPositionZ, endPositionH, xodrPath, osgbPath
  113. }
  114. func getValueByKey(key string) string {
  115. if strings.Contains(key, xodr1Key) {
  116. return xodr1Value
  117. }
  118. if strings.Contains(key, xodr2Key) {
  119. return xodr2Value
  120. }
  121. if strings.Contains(key, xodr3Key) {
  122. return xodr3Value
  123. }
  124. if strings.Contains(key, xodr4Key) {
  125. return xodr4Value
  126. }
  127. if strings.Contains(key, xodr5Key) {
  128. return xodr5Value
  129. }
  130. if strings.Contains(key, osgb1Key) {
  131. return osgb1Value
  132. }
  133. if strings.Contains(key, osgb2Key) {
  134. return osgb2Value
  135. }
  136. if strings.Contains(key, osgb3Key) {
  137. return osgb3Value
  138. }
  139. if strings.Contains(key, osgb4Key) {
  140. return osgb4Value
  141. }
  142. if strings.Contains(key, osgb5Key) {
  143. return osgb5Value
  144. }
  145. fmt.Printf("错误的xosc【%v】", key)
  146. return ""
  147. }