Apollo 10.0
自动驾驶开放平台
util_xml_parser.cc
浏览该文件的文档.
1/* Copyright 2017 The Apollo Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14=========================================================================*/
16
17#include <algorithm>
18
19#include "cyber/common/log.h"
21
22namespace apollo {
23namespace hdmap {
24namespace adapter {
25
26Status UtilXmlParser::ParseCurve(const tinyxml2::XMLElement& xml_node,
27 PbCurve* curve) {
28 CHECK_NOTNULL(curve);
29
30 const tinyxml2::XMLElement* sub_node = xml_node.FirstChildElement("geometry");
31 while (sub_node) {
32 PbCurveSegment* curve_segment = curve->add_segment();
33 RETURN_IF_ERROR(UtilXmlParser::ParseGeometry(*sub_node, curve_segment));
34 sub_node = sub_node->NextSiblingElement("geometry");
35 }
36
37 return Status::OK();
38}
39
40Status UtilXmlParser::ParseGeometry(const tinyxml2::XMLElement& xml_node,
41 PbCurveSegment* curve_segment) {
42 CHECK_NOTNULL(curve_segment);
43
44 // Read geometry attributes
45 double s = 0.0;
46 double ptx = 0.0;
47 double pty = 0.0;
48 double ptz = 0.0;
49 double length = 0.0;
50
51 int checker = tinyxml2::XML_SUCCESS;
52
53 checker += xml_node.QueryDoubleAttribute("sOffset", &s);
54 checker += xml_node.QueryDoubleAttribute("x", &ptx);
55 checker += xml_node.QueryDoubleAttribute("y", &pty);
56 checker += xml_node.QueryDoubleAttribute("length", &length);
57
58 if (checker == tinyxml2::XML_SUCCESS) {
59 curve_segment->set_s(s);
60
61 double output_x = 0.0;
62 double output_y = 0.0;
63 double output_z = 0.0;
64
65 WGS84ToUTM(ptx, pty, ptz, &output_x, &output_y, &output_z);
66
67 curve_segment->mutable_start_position()->set_x(output_x);
68 curve_segment->mutable_start_position()->set_y(output_y);
69 curve_segment->set_length(length);
70 }
71
72 const auto sub_node = xml_node.FirstChildElement("pointSet");
73 if (sub_node) {
74 PbLineSegment* line_segment = curve_segment->mutable_line_segment();
75 RETURN_IF_ERROR(ParsePointSet(*sub_node, line_segment));
76 return Status::OK();
77 }
78
79 std::string err_msg = "Error geometry object";
81}
82
83Status UtilXmlParser::ParsePointSet(const tinyxml2::XMLElement& xml_node,
84 PbLineSegment* line_segment) {
85 const tinyxml2::XMLElement* sub_node = xml_node.FirstChildElement("point");
86 while (sub_node) {
87 double ptx = 0.0;
88 double pty = 0.0;
89 double ptz = 0.0;
90 int checker = tinyxml2::XML_SUCCESS;
91 checker += sub_node->QueryDoubleAttribute("x", &ptx);
92 checker += sub_node->QueryDoubleAttribute("y", &pty);
93
94 if (checker != tinyxml2::XML_SUCCESS) {
95 std::string err_msg = "Error parsing geometry point attributes";
97 }
98
99 PbPoint3D* pt = line_segment->add_point();
100 double output_x = 0.0;
101 double output_y = 0.0;
102 double output_z = 0.0;
103 WGS84ToUTM(ptx, pty, ptz, &output_x, &output_y, &output_z);
104 pt->set_x(output_x);
105 pt->set_y(output_y);
106
107 sub_node = sub_node->NextSiblingElement("point");
108 }
109
110 return Status::OK();
111}
112
113Status UtilXmlParser::ParseOutline(const tinyxml2::XMLElement& xml_node,
114 PbPolygon* polygon) {
115 const tinyxml2::XMLElement* sub_node =
116 xml_node.FirstChildElement("cornerGlobal");
117 while (sub_node) {
118 double ptx = 0.0;
119 double pty = 0.0;
120 double ptz = 0.0;
121 int checker = tinyxml2::XML_SUCCESS;
122 checker += sub_node->QueryDoubleAttribute("x", &ptx);
123 checker += sub_node->QueryDoubleAttribute("y", &pty);
124 checker += sub_node->QueryDoubleAttribute("z", &ptz);
125
126 if (checker != tinyxml2::XML_SUCCESS) {
127 std::string err_msg = "Error parsing cornerGlobal point attributes";
129 }
130
131 PbPoint3D* pt = polygon->add_point();
132 double output_x = 0.0;
133 double output_y = 0.0;
134 double output_z = 0.0;
135 WGS84ToUTM(ptx, pty, ptz, &output_x, &output_y, &output_z);
136 pt->set_x(output_x);
137 pt->set_y(output_y);
138 pt->set_z(output_z);
139
140 sub_node = sub_node->NextSiblingElement("cornerGlobal");
141 }
142
143 return Status::OK();
144}
145
146Status UtilXmlParser::ParsePoint(const tinyxml2::XMLElement& xml_node,
147 PbPoint3D* pt) {
148 CHECK_NOTNULL(pt);
149
150 const auto sub_node = xml_node.FirstChildElement("centerPoint");
151 ACHECK(sub_node != nullptr);
152 int checker = tinyxml2::XML_SUCCESS;
153 double ptx = 0.0;
154 double pty = 0.0;
155 double ptz = 0.0;
156 checker += sub_node->QueryDoubleAttribute("x", &ptx);
157 checker += sub_node->QueryDoubleAttribute("y", &pty);
158 checker += sub_node->QueryDoubleAttribute("z", &ptz);
159
160 if (checker != tinyxml2::XML_SUCCESS) {
161 std::string err_msg = "Error parse point attributes";
163 }
164
165 double output_x = 0.0;
166 double output_y = 0.0;
167 double output_z = 0.0;
168 WGS84ToUTM(ptx, pty, ptz, &output_x, &output_y, &output_z);
169 pt->set_x(output_x);
170 pt->set_y(output_y);
171 pt->set_z(output_z);
172
173 return Status::OK();
174}
175
176std::string UtilXmlParser::ToUpper(const std::string& s) {
177 std::string value = s;
178 std::transform(value.begin(), value.end(), value.begin(),
179 [](unsigned char c) { return std::toupper(c); });
180
181 return value;
182}
183
184void UtilXmlParser::WGS84ToUTM(const double x, const double y, const double z,
185 double* output_x, double* output_y,
186 double* output_z) {
188 output_y, output_z);
189}
190
192 double length = 0.0;
193 for (int i = 0; i < curve.segment_size(); ++i) {
194 length += curve.segment(i).length();
195 }
196
197 return length;
198}
199
201 const tinyxml2::XMLElement& xml_node, const std::string& name,
202 std::string* value) {
203 CHECK_NOTNULL(value);
204 const char* val = xml_node.Attribute(name.c_str());
205 if (val == nullptr) {
206 return tinyxml2::XML_NO_ATTRIBUTE;
207 }
208
209 *value = val;
210 return tinyxml2::XML_SUCCESS;
211}
212
213} // namespace adapter
214} // namespace hdmap
215} // namespace apollo
A general class to denote the return status of an API call.
Definition status.h:43
static Status OK()
generate a success status.
Definition status.h:60
Status CoordiateConvert(const double longitude, const double latitude, const double height_ellipsoid, double *utm_x, double *utm_y, double *utm_z)
static void WGS84ToUTM(const double x, const double y, const double z, double *output_x, double *output_y, double *output_z)
static Status ParseGeometry(const tinyxml2::XMLElement &xml_node, PbCurveSegment *curve_segment)
static std::string ToUpper(const std::string &s)
static double CurveLength(const PbCurve &curve)
static tinyxml2::XMLError QueryStringAttribute(const tinyxml2::XMLElement &xml_node, const std::string &name, std::string *value)
static Status ParsePointSet(const tinyxml2::XMLElement &xml_node, PbLineSegment *line_segment)
static Status ParseCurve(const tinyxml2::XMLElement &xml_node, PbCurve *curve)
static Status ParseOutline(const tinyxml2::XMLElement &xml_node, PbPolygon *polygon)
static Status ParsePoint(const tinyxml2::XMLElement &xml_node, PbPoint3D *pt)
#define ACHECK(cond)
Definition log.h:80
#define RETURN_IF_ERROR(expr)
Definition status.h:25
class register implement
Definition arena_queue.h:37
repeated CurveSegment segment