task_algo_c.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. for (size_t i = 0; i < line.size(); ++i)
  25. {
  26. studio_point_c p = studio_point_init(line[i].x, line[i].y);
  27. studio_line_c_add_point(c_line, p);
  28. }
  29. }
  30. void c_line_to_studio_line(const studio_line_c *c_line, studio_line &line)
  31. {
  32. for (size_t i = 0; i < c_line->size; ++i)
  33. {
  34. const studio_point_c *p = studio_line_c_get_point(c_line, i);
  35. line.push_back(studio_point(p->x, p->y));
  36. }
  37. }
  38. // ///////////////////// 移除离群点 ////////////////////////////////
  39. // int main()
  40. // {
  41. // printf("\n\n===================== %s =====================\n\n", __FILE__);
  42. // silly::geo::utils::init_gdal_env();
  43. // std::string path;
  44. // #ifdef IS_WINDOWS
  45. // path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples.geojson";
  46. // #else
  47. // path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples.geojson";
  48. // #endif
  49. //
  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. //
  55. // for (auto &coll: collections)
  56. // {
  57. // studio_line_c line_c = studio_line_c_init();
  58. // studio_line_to_c_line(coll.m_line, &line_c);
  59. // std::cout << "原始数据量:" << coll.m_line.size() << std::endl;
  60. //
  61. // studio_line_c outliers_line = studio_line_c_init();
  62. // remove_outliers_c(&line_c, 1, &outliers_line);
  63. //
  64. // studio_line simplified_line;
  65. // c_line_to_studio_line(&outliers_line, simplified_line);
  66. // std::cout << "简化后数据量:" << simplified_line.size() << std::endl;
  67. //
  68. // studio_geo_coll temp;
  69. // temp.m_type = enum_geometry_type::egtLineString;
  70. // temp.m_line = simplified_line;
  71. // res_collections.push_back(temp);
  72. //
  73. // studio_line_c_destroy(&line_c);
  74. // studio_line_c_destroy(&outliers_line);
  75. //
  76. // break;
  77. // }
  78. //
  79. //
  80. // std::string output_path;
  81. // #ifdef IS_WINDOWS
  82. // output_path = "D:/5_file/2_readfile/geojson/multi_point/remove_outliers.geojson";
  83. // #else
  84. // output_path = "/home/wyj/myself/2_data/2_geojson/multi_point/remove_outliers.geojson";
  85. // #endif
  86. // silly::geo::utils::write_geo_coll(output_path, res_collections);
  87. // silly::geo::utils::destroy_gdal_env();
  88. // return 0;
  89. // }
  90. // //////////////////// 路径压缩算法测试 //////////////////////
  91. int main()
  92. {
  93. printf("\n\n===================== %s =====================\n\n", __FILE__);
  94. silly::geo::utils::init_gdal_env();
  95. std::string path;
  96. #ifdef IS_WINDOWS
  97. path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples.geojson";
  98. #else
  99. path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples.geojson";
  100. #endif
  101. std::cout << "path: " << path << std::endl;
  102. std::vector<studio_geo_coll> res_collections;
  103. std::vector<studio_geo_coll> collections;
  104. silly::geo::utils::read_geo_coll(path, collections);
  105. for (auto &coll: collections)
  106. {
  107. studio_line_c line_c = studio_line_c_init();
  108. studio_line_c vac_line_c = studio_line_c_init();
  109. studio_line_to_c_line(coll.m_line, &line_c);
  110. // 简化线段,目标点数为28个
  111. int max_points = 28;
  112. double epsilon = 1.0;
  113. bool res = line_vacuate_gcs_c(&line_c, max_points, epsilon, &vac_line_c);
  114. if (!res)
  115. {
  116. std::cout << "Failed to simplify line." << std::endl;
  117. return 1;
  118. }
  119. studio_line simplified_line;
  120. c_line_to_studio_line(&vac_line_c, simplified_line);
  121. studio_geo_coll temp;
  122. temp.m_type = enum_geometry_type::egtLineString;
  123. temp.m_line = simplified_line;
  124. res_collections.push_back(temp);
  125. studio_line_c_destroy(&line_c);
  126. studio_line_c_destroy(&vac_line_c);
  127. break;
  128. }
  129. std::string output_path;
  130. #ifdef IS_WINDOWS
  131. output_path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples_res_1.geojson";
  132. #else
  133. output_path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples_res_2_c.geojson";
  134. #endif
  135. silly::geo::utils::write_geo_coll(output_path, res_collections);
  136. silly::geo::utils::destroy_gdal_env();
  137. return 0;
  138. }
  139. // int main()
  140. // {
  141. // printf("\n\n===================== %s =====================\n\n", __FILE__);
  142. // silly::geo::utils::init_gdal_env();
  143. // std::string path;
  144. // #ifdef IS_WINDOWS
  145. // path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples.geojson";
  146. // #else
  147. // path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples.geojson";
  148. // #endif
  149. //
  150. // std::cout << "path: " << path << std::endl;
  151. // std::vector<studio_geo_coll> res_collections;
  152. // std::vector<studio_geo_coll> collections;
  153. // silly::geo::utils::read_geo_coll(path, collections);
  154. // for (auto &coll : collections)
  155. // {
  156. // // ------------- 转换为高斯投影 -------------
  157. // studio_line gauss_line;
  158. // double central = static_cast<int>(coll.m_line[0].x / 3) * 3;
  159. // for (auto &point : coll.m_line)
  160. // {
  161. // double gx = 0.0;
  162. // double gy = 0.0;
  163. // lonlat_to_gauss(central, point.x, point.y, &gx, &gy);
  164. // gauss_line.push_back(studio_point(gx, gy));
  165. // }
  166. // studio_line_c gauss_line_c = studio_line_c_init();
  167. // studio_line_c vac_gauss_line_c = studio_line_c_init();
  168. // studio_line_to_c_line(gauss_line, &gauss_line_c);
  169. //
  170. // // 简化线段,目标点数为28个
  171. // int max_points = 28;
  172. // double epsilon = 1.0;
  173. //
  174. // bool res = line_vacuate_c(&gauss_line_c, max_points, epsilon, &vac_gauss_line_c);
  175. // if (!res)
  176. // {
  177. // std::cout << "Failed to simplify line." << std::endl;
  178. // return 1;
  179. // }
  180. //
  181. // // 高斯投影在转回经纬度
  182. // studio_line simplified_line;
  183. // for (int i = 0; i < vac_gauss_line_c.size; i++)
  184. // {
  185. // double lon = 0.0;
  186. // double lat = 0.0;
  187. // const studio_point_c *p = studio_line_c_get_point(&vac_gauss_line_c, i);
  188. // gauss_to_lonlat(central, p->x, p->y, &lon, &lat);
  189. // simplified_line.push_back(studio_point(lon, lat));
  190. // }
  191. //
  192. // studio_geo_coll temp;
  193. // temp.m_type = enum_geometry_type::egtLineString;
  194. // temp.m_line = simplified_line;
  195. // res_collections.push_back(temp);
  196. //
  197. // studio_line_c_destroy(&gauss_line_c);
  198. // studio_line_c_destroy(&vac_gauss_line_c);
  199. //
  200. // break;
  201. // }
  202. // std::string output_path;
  203. // #ifdef IS_WINDOWS
  204. // output_path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples_res_1.geojson";
  205. // #else
  206. // output_path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples_res_1_c.geojson";
  207. // #endif
  208. // silly::geo::utils::write_geo_coll(output_path, res_collections);
  209. //
  210. // silly::geo::utils::destroy_gdal_env();
  211. //
  212. // return 0;
  213. // }