task_algo_c.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. ******************************************************************************
  3. * @file : task_alog_c.cpp
  4. * @author : wyj
  5. * @brief : C语言语法测试
  6. * @attention : None
  7. * @date : 2025/5/9
  8. ******************************************************************************
  9. */
  10. #ifdef __cplusplus
  11. extern "C"
  12. {
  13. #endif
  14. #include "geography/studio_proj_c.h"
  15. #include "geometry/studio_geo_algo_c.h"
  16. #ifdef __cplusplus
  17. }
  18. #endif
  19. #include <stdio.h>
  20. #include <stdio.h>
  21. #include "geometry/studio_geo_utils.h"
  22. void studio_line_to_c_line(const studio_line &line, studio_line_c *c_line)
  23. {
  24. // c_line->size = line.size();
  25. c_line->data = (studio_point_c *)malloc(line.size() * sizeof(studio_point_c));
  26. for (size_t i = 0; i < line.size(); ++i)
  27. {
  28. studio_point_c p = studio_point_init(line[i].x, line[i].y);
  29. studio_line_c_add_point(c_line, p);
  30. }
  31. }
  32. void c_line_to_studio_line(const studio_line_c *c_line, studio_line &line)
  33. {
  34. for (size_t i = 0; i < c_line->size; ++i)
  35. {
  36. const studio_point_c *p = studio_line_c_get_point(c_line, i);
  37. line.push_back(studio_point(p->x, p->y));
  38. }
  39. }
  40. int main()
  41. {
  42. printf("\n\n===================== %s =====================\n\n", __FILE__);
  43. silly::geo::utils::init_gdal_env();
  44. std::string path;
  45. #ifdef IS_WINDOWS
  46. path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples.geojson";
  47. #else
  48. path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples.geojson";
  49. #endif
  50. std::cout << "path: " << path << std::endl;
  51. std::vector<studio_geo_coll> res_collections;
  52. std::vector<studio_geo_coll> collections;
  53. silly::geo::utils::read_geo_coll(path, collections);
  54. for (auto &coll : collections)
  55. {
  56. // ------------- 转换为高斯投影 -------------
  57. studio_line gauss_line;
  58. double central = static_cast<int>(coll.m_line[0].x / 3) * 3;
  59. for (auto &point : coll.m_line)
  60. {
  61. double gx = 0.0;
  62. double gy = 0.0;
  63. lonlat_to_gauss(central, point.x, point.y, &gx, &gy);
  64. gauss_line.push_back(studio_point(gx, gy));
  65. }
  66. studio_line_c gauss_line_c = studio_line_c_init();
  67. studio_line_c vac_gauss_line_c = studio_line_c_init();
  68. studio_line_to_c_line(gauss_line, &gauss_line_c);
  69. // 简化线段,目标点数为28个
  70. int max_points = 28;
  71. double epsilon = 1.0;
  72. bool res = line_vacuate_c(&gauss_line_c, max_points, epsilon, &vac_gauss_line_c);
  73. if (!res)
  74. {
  75. std::cout << "Failed to simplify line." << std::endl;
  76. return 1;
  77. }
  78. // 高斯投影在转回经纬度
  79. studio_line simplified_line;
  80. for (int i = 0; i < vac_gauss_line_c.size; i++)
  81. {
  82. double lon = 0.0;
  83. double lat = 0.0;
  84. const studio_point_c *p = studio_line_c_get_point(&vac_gauss_line_c, i);
  85. gauss_to_lonlat(central, p->x, p->y, &lon, &lat);
  86. simplified_line.push_back(studio_point(lon, lat));
  87. }
  88. studio_geo_coll temp;
  89. temp.m_type = enum_geometry_type::egtLineString;
  90. temp.m_line = simplified_line;
  91. res_collections.push_back(temp);
  92. studio_line_c_destroy(&gauss_line_c);
  93. studio_line_c_destroy(&vac_gauss_line_c);
  94. break;
  95. }
  96. std::string output_path;
  97. #ifdef IS_WINDOWS
  98. output_path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples_res_1.geojson";
  99. #else
  100. output_path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples_res_1_c.geojson";
  101. #endif
  102. silly::geo::utils::write_geo_coll(output_path, res_collections);
  103. silly::geo::utils::destroy_gdal_env();
  104. return 0;
  105. }