wgs842utm.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: Sun Yalun
  5. * @Date: 2024-10-17 17:44:07
  6. * @LastEditors: Sun Yalun
  7. * @LastEditTime: 2024-10-22 16:58:59
  8. */
  9. #include <iostream>
  10. #include <iomanip>
  11. #include "wgs842utm.h"
  12. void wgs842utm(const Point& degree_point, Point& point) {
  13. // 定义 WGS84 地理坐标系
  14. OGRSpatialReference wgs84;
  15. wgs84.SetWellKnownGeogCS("WGS84");
  16. // 定义 UTM 50N 投影坐标系
  17. OGRSpatialReference utm50n;
  18. utm50n.SetUTM(50, TRUE); // 50 表示 50 区,TRUE 表示北半球
  19. // 创建坐标转换对象
  20. OGRCoordinateTransformation *poTransform = OGRCreateCoordinateTransformation(&wgs84, &utm50n);
  21. if (poTransform == NULL) {
  22. std::cerr << "无法创建坐标转换对象" << std::endl;
  23. return;
  24. }
  25. double x = degree_point.pos_x;
  26. double y = degree_point.pos_y;
  27. // 进行坐标转换,注意转换的是经度(lon)和纬度(lat)
  28. if (!poTransform->Transform(1, &y, &x)) {
  29. std::cerr << "坐标转换失败" << std::endl;
  30. OGRCoordinateTransformation::DestroyCT(poTransform);
  31. return;
  32. }
  33. // 输出 UTM 结果
  34. point.pos_x = y; // 转换后的 X(东)坐标
  35. point.pos_y = x; // 转换后的 Y(北)坐标
  36. point.heading = 0.0;
  37. // 清理
  38. OGRCoordinateTransformation::DestroyCT(poTransform);
  39. // std::cout << std::fixed << std::setprecision(7);
  40. }
  41. // 对vector中的所有Point进行整体坐标转换为utm坐标
  42. void wgs842utmall(const std::vector<Point>& degree_points, std::vector<Point>& points) {
  43. for (auto& degree_point : degree_points) {
  44. Point point(0.0, 0.0, 0.0);
  45. wgs842utm(degree_point, point);
  46. points.push_back(point);
  47. }
  48. }