Apollo 10.0
自动驾驶开放平台
coordinate_convert_tool.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 "glog/logging.h"
18
19namespace apollo {
20namespace hdmap {
21namespace adapter {
22
24 : pj_from_(nullptr), pj_to_(nullptr) {}
25
27 if (pj_from_) {
28 pj_free(pj_from_);
29 pj_from_ = nullptr;
30 }
31
32 if (pj_to_) {
33 pj_free(pj_to_);
34 pj_to_ = nullptr;
35 }
36}
37
39 static CoordinateConvertTool instance;
40 return &instance;
41}
42
43Status CoordinateConvertTool::SetConvertParam(const std::string& source_param,
44 const std::string& dst_param) {
45 source_convert_param_ = source_param;
46 dst_convert_param_ = dst_param;
47 if (pj_from_) {
48 pj_free(pj_from_);
49 pj_from_ = nullptr;
50 }
51
52 if (pj_to_) {
53 pj_free(pj_to_);
54 pj_to_ = nullptr;
55 }
56
57 if (!(pj_from_ = pj_init_plus(source_convert_param_.c_str()))) {
58 std::string err_msg = "Fail to pj_init_plus " + source_convert_param_;
60 }
61
62 if (!(pj_to_ = pj_init_plus(dst_convert_param_.c_str()))) {
63 std::string err_msg = "Fail to pj_init_plus " + dst_convert_param_;
64 pj_free(pj_from_);
65 pj_from_ = nullptr;
67 }
68
69 return Status::OK();
70}
71
73 const double latitude,
74 const double height_ellipsoid,
75 double* utm_x, double* utm_y,
76 double* utm_z) {
77 CHECK_NOTNULL(utm_x);
78 CHECK_NOTNULL(utm_y);
79 CHECK_NOTNULL(utm_z);
80 if (!pj_from_ || !pj_to_) {
81 std::string err_msg = "no transform param";
83 }
84
85 double gps_longitude = longitude;
86 double gps_latitude = latitude;
87 double gps_alt = height_ellipsoid;
88
89 if (pj_is_latlong(pj_from_)) {
90 gps_longitude *= DEG_TO_RAD;
91 gps_latitude *= DEG_TO_RAD;
92 gps_alt = height_ellipsoid;
93 }
94
95 if (0 != pj_transform(pj_from_, pj_to_, 1, 1, &gps_longitude, &gps_latitude,
96 &gps_alt)) {
97 std::string err_msg = "fail to transform coordinate";
99 }
100
101 if (pj_is_latlong(pj_to_)) {
102 gps_longitude *= RAD_TO_DEG;
103 gps_latitude *= RAD_TO_DEG;
104 }
105
106 *utm_x = gps_longitude;
107 *utm_y = gps_latitude;
108 *utm_z = gps_alt;
109
110 return Status::OK();
111}
112
113} // namespace adapter
114} // namespace hdmap
115} // 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 SetConvertParam(const std::string &source_param, const std::string &dst_param)
Status CoordiateConvert(const double longitude, const double latitude, const double height_ellipsoid, double *utm_x, double *utm_y, double *utm_z)
#define DEG_TO_RAD
#define RAD_TO_DEG
class register implement
Definition arena_queue.h:37