utils.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: Sun Yalun
  5. * @Date: 2024-11-04 15:37:55
  6. * @LastEditors: Sun Yalun
  7. * @LastEditTime: 2024-11-08 14:41:50
  8. */
  9. #include "utils.h"
  10. #include <cmath>
  11. #include <iostream>
  12. #include <sstream>
  13. #include <iomanip>
  14. #include <fstream>
  15. #include <algorithm>
  16. #include <unordered_map>
  17. #include <limits>
  18. BoundingBox createBoundingBox(const Point &p1, const Point &p2)
  19. {
  20. BoundingBox box;
  21. box.min_x = std::min(p1.pos_x, p2.pos_x);
  22. box.max_x = std::max(p1.pos_x, p2.pos_x);
  23. box.min_y = std::min(p1.pos_y, p2.pos_y);
  24. box.max_y = std::max(p1.pos_y, p2.pos_y);
  25. return box;
  26. }
  27. bool isPointInBoundingBox(const Point &p, const BoundingBox &box)
  28. {
  29. return (p.pos_x >= box.min_x && p.pos_x <= box.max_x &&
  30. p.pos_y >= box.min_y && p.pos_y <= box.max_y);
  31. }
  32. std::vector<int> parseRef(const std::string &ref_str)
  33. {
  34. std::vector<int> refs;
  35. std::string s = ref_str;
  36. if (s.size() >= 2 && s.front() == '[' && s.back() == ']')
  37. {
  38. s = s.substr(1, s.size() - 2);
  39. }
  40. std::stringstream ss(s);
  41. std::string token;
  42. while (std::getline(ss, token, ','))
  43. {
  44. try
  45. {
  46. refs.push_back(std::stoi(token));
  47. }
  48. catch (const std::invalid_argument &e)
  49. {
  50. std::cerr << "Invalid ref value: " << token << std::endl;
  51. }
  52. }
  53. return refs;
  54. }
  55. std::vector<Point> parseCoords(const std::string &coords_str)
  56. {
  57. std::vector<Point> points;
  58. std::string s = coords_str;
  59. // 移除最外层的中括号
  60. if (!s.empty() && s.front() == '[' && s.back() == ']')
  61. {
  62. s = s.substr(1, s.size() - 2);
  63. }
  64. std::stringstream ss(s);
  65. std::string point_str;
  66. while (std::getline(ss, point_str, ']'))
  67. {
  68. // 每个点以 '[' 开始,移除它
  69. size_t start = point_str.find('[');
  70. if (start != std::string::npos)
  71. {
  72. std::string coords = point_str.substr(start + 1);
  73. // 将逗号替换为空格,以便于后续解析
  74. std::replace(coords.begin(), coords.end(), ',', ' ');
  75. std::stringstream coord_ss(coords);
  76. double x, y;
  77. coord_ss >> x >> y;
  78. Point p(0.0, 0.0, 0.0);
  79. p.pos_x = x;
  80. p.pos_y = y;
  81. points.push_back(p);
  82. }
  83. // 忽略逗号
  84. ss.ignore(1, ',');
  85. }
  86. return points;
  87. }
  88. double calculateDistance(double x1, double y1, double x2, double y2)
  89. {
  90. double dx = x1 - x2;
  91. double dy = y1 - y2;
  92. return std::sqrt(dx * dx + dy * dy);
  93. }
  94. double pointToSegmentDistance(const Point &p, const Point &p1, const Point &p2)
  95. {
  96. double x0 = p.pos_x;
  97. double y0 = p.pos_y;
  98. double x1 = p1.pos_x;
  99. double y1 = p1.pos_y;
  100. double x2 = p2.pos_x;
  101. double y2 = p2.pos_y;
  102. double dx = x2 - x1;
  103. double dy = y2 - y1;
  104. double mag = dx * dx + dy * dy;
  105. if (mag == 0.0)
  106. {
  107. return std::sqrt((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1));
  108. }
  109. double u = ((x0 - x1) * dx + (y0 - y1) * dy) / mag;
  110. u = std::max(0.0, std::min(1.0, u));
  111. double x = x1 + u * dx;
  112. double y = y1 + u * dy;
  113. return std::sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0));
  114. }
  115. double computeMinDistanceToCurve(const Point &queryPoint, const std::vector<Point> &curve, size_t window_size)
  116. {
  117. if (curve.size() < 2)
  118. {
  119. // std::cerr << "曲线点不足以形成线段。" << std::endl;
  120. return std::numeric_limits<double>::max();
  121. }
  122. double min_distance = std::numeric_limits<double>::max();
  123. // 初始滑动窗口范围
  124. for (size_t i = 0; i < curve.size() - 1; ++i)
  125. {
  126. BoundingBox box = createBoundingBox(curve[i], curve[i + 1]);
  127. if (!isPointInBoundingBox(queryPoint, box))
  128. {
  129. continue;
  130. }
  131. // 如果在窗口范围内,计算距离
  132. double dist = pointToSegmentDistance(queryPoint, curve[i], curve[i + 1]);
  133. if (dist < min_distance)
  134. {
  135. min_distance = dist;
  136. }
  137. // 滑动窗口: 超出窗口范围的段不计算
  138. if (i >= window_size && dist > min_distance)
  139. {
  140. break;
  141. }
  142. }
  143. return min_distance;
  144. }
  145. double computeBazierDistanceToCurve(const Point &queryPoint, const std::vector<Point> &coords)
  146. {
  147. double min_dist = std::numeric_limits<double>::max();
  148. if (coords.size() < 2)
  149. {
  150. // std::cerr << "曲线点不足以形成线段。" << std::endl;
  151. return std::numeric_limits<double>::max();
  152. }
  153. for (int i = 0; i < coords.size() - 1; ++i)
  154. {
  155. double dist1 = pointToSegmentDistance(queryPoint, coords[i], coords[i + 1]);
  156. min_dist = std::min(min_dist, dist1);
  157. }
  158. return min_dist;
  159. }
  160. Point calculateCentroid(const std::vector<Point> &coords)
  161. {
  162. double sum_x = 0.0, sum_y = 0.0;
  163. int n = coords.size();
  164. for (const auto &p : coords)
  165. {
  166. sum_x += p.pos_x;
  167. sum_y += p.pos_y;
  168. }
  169. return Point{sum_x / n, sum_y / n, 0.0};
  170. }
  171. std::vector<InterInfo> readIntersectionsFromCSV(const std::string &csvPath)
  172. {
  173. std::vector<InterInfo> intersections;
  174. std::ifstream csvFile(csvPath);
  175. if (!csvFile.is_open())
  176. {
  177. std::cerr << "无法打开路口信息csv文件: " << csvPath << std::endl;
  178. return intersections;
  179. }
  180. std::string line;
  181. // 读取表头
  182. if (!std::getline(csvFile, line))
  183. {
  184. std::cerr << "路口信息csv文件为空或无法读取表头。" << std::endl;
  185. return intersections;
  186. }
  187. // 解析表头,建立列名到索引的映射
  188. std::unordered_map<std::string, int> headerMap;
  189. std::stringstream headerStream(line);
  190. std::string headerField;
  191. int index = 0;
  192. while (std::getline(headerStream, headerField, ','))
  193. {
  194. // 去除可能的引号和空格
  195. headerField.erase(std::remove(headerField.begin(), headerField.end(), '\"'), headerField.end());
  196. headerField.erase(std::remove(headerField.begin(), headerField.end(), ' '), headerField.end());
  197. headerMap[headerField] = index++;
  198. }
  199. // 检查是否存在所有必要的列
  200. std::vector<std::string> requiredColumns = {"inter_id", "inter_type", "inter_in_shape", "inter_ref_link", "inter_coords"};
  201. for (const auto &col : requiredColumns)
  202. {
  203. if (headerMap.find(col) == headerMap.end())
  204. {
  205. std::cerr << "路口信息csv文件中未找到列名为 " << col << " 的列。" << std::endl;
  206. return intersections;
  207. }
  208. }
  209. int inter_id_idx = headerMap["inter_id"];
  210. int inter_type_idx = headerMap["inter_type"];
  211. int inter_in_shape_idx = headerMap["inter_in_shape"];
  212. int inter_ref_link_idx = headerMap["inter_ref_link"];
  213. int inter_coords_idx = headerMap["inter_coords"];
  214. // 读取每一行
  215. while (std::getline(csvFile, line))
  216. {
  217. std::stringstream ss(line);
  218. std::string field;
  219. std::vector<std::string> fields;
  220. bool in_quotes = false;
  221. std::string token;
  222. for (size_t i = 0; i < line.size(); ++i)
  223. {
  224. char c = line[i];
  225. if (c == '\"')
  226. {
  227. in_quotes = !in_quotes;
  228. }
  229. else if (c == ',' && !in_quotes)
  230. {
  231. fields.push_back(token);
  232. token.clear();
  233. }
  234. else
  235. {
  236. token += c;
  237. }
  238. }
  239. fields.push_back(token);
  240. // 检查字段数是否足够
  241. if (fields.size() < requiredColumns.size())
  242. {
  243. std::cerr << "字段数量不足,跳过行: " << line << std::endl;
  244. continue;
  245. }
  246. InterInfo inter;
  247. try
  248. {
  249. inter.inter_id = std::stoi(fields[inter_id_idx]);
  250. inter.inter_type = std::stoi(fields[inter_type_idx]);
  251. inter.inter_in_shape = std::stoi(fields[inter_in_shape_idx]);
  252. inter.inter_ref_link = parseRef(fields[inter_ref_link_idx]);
  253. inter.inter_coords = parseCoords(fields[inter_coords_idx]);
  254. }
  255. catch (const std::exception &e)
  256. {
  257. std::cerr << "解析错误,跳过行: " << line << "\n错误信息: " << e.what() << std::endl;
  258. continue;
  259. }
  260. intersections.push_back(inter);
  261. }
  262. csvFile.close();
  263. return intersections;
  264. }
  265. std::vector<CrosswalkInfo> readCrosswalksFromCSV(const std::string &csvPath)
  266. {
  267. std::vector<CrosswalkInfo> crosswalks;
  268. std::ifstream csvFile(csvPath);
  269. if (!csvFile.is_open())
  270. {
  271. std::cerr << "无法打开人行横道信息csv文件: " << csvPath << std::endl;
  272. return crosswalks;
  273. }
  274. std::string line;
  275. // 读取表头
  276. if (!std::getline(csvFile, line))
  277. {
  278. std::cerr << "人行横道信息csv文件为空或无法读取表头。" << std::endl;
  279. return crosswalks;
  280. }
  281. // 解析表头,建立列名到索引的映射
  282. std::unordered_map<std::string, int> headerMap;
  283. std::stringstream headerStream(line);
  284. std::string headerField;
  285. int index = 0;
  286. while (std::getline(headerStream, headerField, ','))
  287. {
  288. // 去除可能的引号和空格
  289. headerField.erase(std::remove(headerField.begin(), headerField.end(), '\"'), headerField.end());
  290. headerField.erase(std::remove(headerField.begin(), headerField.end(), ' '), headerField.end());
  291. headerMap[headerField] = index++;
  292. }
  293. // 检查是否存在所有必要的列
  294. std::vector<std::string> requiredColumns = {"cross_id", "cross_coords"};
  295. for (const auto &col : requiredColumns)
  296. {
  297. if (headerMap.find(col) == headerMap.end())
  298. {
  299. std::cerr << "人行横道信息csv文件中未找到列名为 " << col << " 的列。" << std::endl;
  300. return crosswalks;
  301. }
  302. }
  303. int cross_id_idx = headerMap["cross_id"];
  304. int cross_coords_idx = headerMap["cross_coords"];
  305. // 读取每一行
  306. while (std::getline(csvFile, line))
  307. {
  308. std::stringstream ss(line);
  309. std::string field;
  310. std::vector<std::string> fields;
  311. bool in_quotes = false;
  312. std::string token;
  313. for (size_t i = 0; i < line.size(); ++i)
  314. {
  315. char c = line[i];
  316. if (c == '\"')
  317. {
  318. in_quotes = !in_quotes;
  319. }
  320. else if (c == ',' && !in_quotes)
  321. {
  322. fields.push_back(token);
  323. token.clear();
  324. }
  325. else
  326. {
  327. token += c;
  328. }
  329. }
  330. fields.push_back(token);
  331. // 检查字段数是否足够
  332. if (fields.size() < requiredColumns.size())
  333. {
  334. std::cerr << "字段数量不足,跳过行: " << line << std::endl;
  335. continue;
  336. }
  337. CrosswalkInfo cross;
  338. try
  339. {
  340. cross.cross_id = std::stoi(fields[cross_id_idx]);
  341. cross.cross_coords = parseCoords(fields[cross_coords_idx]);
  342. }
  343. catch (const std::exception &e)
  344. {
  345. std::cerr << "解析错误,跳过行: " << line << "\n错误信息: " << e.what() << std::endl;
  346. continue;
  347. }
  348. crosswalks.push_back(cross);
  349. }
  350. csvFile.close();
  351. return crosswalks;
  352. }
  353. std::vector<TrafficlightInfo> readTrafficlightsFromCSV(const std::string &csvPath)
  354. {
  355. std::vector<TrafficlightInfo> trafficlights;
  356. std::ifstream csvFile(csvPath);
  357. if (!csvFile.is_open())
  358. {
  359. std::cerr << "无法打开交通信号灯信息csv文件: " << csvPath << std::endl;
  360. return trafficlights;
  361. }
  362. std::string line;
  363. // 读取表头
  364. if (!std::getline(csvFile, line))
  365. {
  366. std::cerr << "交通信号灯信息csv文件为空或无法读取表头。" << std::endl;
  367. return trafficlights;
  368. }
  369. // 解析表头,建立列名到索引的映射
  370. std::unordered_map<std::string, int> headerMap;
  371. std::stringstream headerStream(line);
  372. std::string headerField;
  373. int index = 0;
  374. while (std::getline(headerStream, headerField, ','))
  375. {
  376. // 去除可能的引号和空格
  377. headerField.erase(std::remove(headerField.begin(), headerField.end(), '\"'), headerField.end());
  378. headerField.erase(std::remove(headerField.begin(), headerField.end(), ' '), headerField.end());
  379. headerMap[headerField] = index++;
  380. }
  381. // 检查是否存在所有必要的列
  382. std::vector<std::string> requiredColumns = {"trafficlight_id", "trafficlight_ref_link", "trafficlight_ref_lane", "trafficlight_type", "trafficlight_head", "trafficlight_coords"};
  383. for (const auto &col : requiredColumns)
  384. {
  385. if (headerMap.find(col) == headerMap.end())
  386. {
  387. std::cerr << "交通信号灯信息csv文件中未找到列名为 " << col << " 的列。" << std::endl;
  388. return trafficlights;
  389. }
  390. }
  391. int trafficlight_id_idx = headerMap["trafficlight_id"];
  392. int trafficlight_ref_link_idx = headerMap["trafficlight_ref_link"];
  393. int trafficlight_ref_lane_idx = headerMap["trafficlight_ref_lane"];
  394. int trafficlight_type_idx = headerMap["trafficlight_type"];
  395. int trafficlight_head_idx = headerMap["trafficlight_head"];
  396. int trafficlight_coords_idx = headerMap["trafficlight_coords"];
  397. // 读取每一行
  398. while (std::getline(csvFile, line))
  399. {
  400. std::stringstream ss(line);
  401. std::string field;
  402. std::vector<std::string> fields;
  403. bool in_quotes = false;
  404. std::string token;
  405. for (size_t i = 0; i < line.size(); ++i)
  406. {
  407. char c = line[i];
  408. if (c == '\"')
  409. {
  410. in_quotes = !in_quotes;
  411. }
  412. else if (c == ',' && !in_quotes)
  413. {
  414. fields.push_back(token);
  415. token.clear();
  416. }
  417. else
  418. {
  419. token += c;
  420. }
  421. }
  422. fields.push_back(token);
  423. // 检查字段数是否足够
  424. if (fields.size() < requiredColumns.size())
  425. {
  426. std::cerr << "字段数量不足,跳过行: " << line << std::endl;
  427. continue;
  428. }
  429. TrafficlightInfo trafficlight;
  430. try
  431. {
  432. trafficlight.trafficlight_id = std::stoi(fields[trafficlight_id_idx]);
  433. trafficlight.trafficlight_ref_link = std::stoi(fields[trafficlight_ref_link_idx]);
  434. trafficlight.trafficlight_ref_lane = parseRef(fields[trafficlight_ref_lane_idx]);
  435. trafficlight.trafficlight_type = std::stoi(fields[trafficlight_type_idx]);
  436. trafficlight.trafficlight_head = parseRef(fields[trafficlight_head_idx]);
  437. trafficlight.trafficlight_coords = parseCoords(fields[trafficlight_coords_idx]);
  438. }
  439. catch (const std::exception &e)
  440. {
  441. std::cerr << "解析错误,跳过行: " << line << "\n错误信息: " << e.what() << std::endl;
  442. continue;
  443. }
  444. trafficlights.push_back(trafficlight);
  445. }
  446. csvFile.close();
  447. return trafficlights;
  448. }
  449. std::vector<RoadInfo> readRoadsFromCSV(const std::string &csvPath)
  450. {
  451. std::vector<RoadInfo> roads;
  452. std::ifstream csvFile(csvPath);
  453. if (!csvFile.is_open())
  454. {
  455. std::cerr << "无法打开道路信息csv文件: " << csvPath << std::endl;
  456. return roads;
  457. }
  458. std::string line;
  459. // 读取表头
  460. if (!std::getline(csvFile, line))
  461. {
  462. std::cerr << "道路信息csv文件为空或无法读取表头。" << std::endl;
  463. return roads;
  464. }
  465. // 解析表头,建立列名到索引的映射
  466. std::unordered_map<std::string, int> headerMap;
  467. std::stringstream headerStream(line);
  468. std::string headerField;
  469. int index = 0;
  470. while (std::getline(headerStream, headerField, ','))
  471. {
  472. // 去除可能的引号和空格
  473. headerField.erase(std::remove(headerField.begin(), headerField.end(), '\"'), headerField.end());
  474. headerField.erase(std::remove(headerField.begin(), headerField.end(), ' '), headerField.end());
  475. headerMap[headerField] = index++;
  476. }
  477. // 检查是否存在所有必要的列
  478. std::vector<std::string> requiredColumns = {"link_id", "link_fc", "link_type", "link_speed_max", "link_speed_min", "link_direction", "link_lanenum", "link_coords"};
  479. for (const auto &col : requiredColumns)
  480. {
  481. if (headerMap.find(col) == headerMap.end())
  482. {
  483. std::cerr << "道路信息csv文件中未找到列名为 " << col << " 的列。" << std::endl;
  484. return roads;
  485. }
  486. }
  487. int link_id_idx = headerMap["link_id"];
  488. int link_fc_idx = headerMap["link_fc"];
  489. int link_type_idx = headerMap["link_type"];
  490. int link_speed_max_idx = headerMap["link_speed_max"];
  491. int link_speed_min_idx = headerMap["link_speed_min"];
  492. int link_direction_idx = headerMap["link_direction"];
  493. int link_lanenum_idx = headerMap["link_lanenum"];
  494. int link_coords_idx = headerMap["link_coords"];
  495. // 读取每一行
  496. while (std::getline(csvFile, line))
  497. {
  498. std::stringstream ss(line);
  499. std::string field;
  500. std::vector<std::string> fields;
  501. bool in_quotes = false;
  502. std::string token;
  503. for (size_t i = 0; i < line.size(); ++i)
  504. {
  505. char c = line[i];
  506. if (c == '\"')
  507. {
  508. in_quotes = !in_quotes;
  509. }
  510. else if (c == ',' && !in_quotes)
  511. {
  512. fields.push_back(token);
  513. token.clear();
  514. }
  515. else
  516. {
  517. token += c;
  518. }
  519. }
  520. fields.push_back(token);
  521. // 检查字段数是否足够
  522. if (fields.size() < requiredColumns.size())
  523. {
  524. std::cerr << "字段数量不足,跳过行: " << line << std::endl;
  525. continue;
  526. }
  527. RoadInfo road;
  528. try
  529. {
  530. road.link_id = std::stoi(fields[link_id_idx]);
  531. road.link_fc = std::stoi(fields[link_fc_idx]);
  532. road.link_type = std::stoi(fields[link_type_idx]);
  533. road.link_speed_max = std::stod(fields[link_speed_max_idx]);
  534. road.link_speed_min = std::stod(fields[link_speed_min_idx]);
  535. road.link_direction = std::stoi(fields[link_direction_idx]);
  536. road.link_lanenum = std::stoi(fields[link_lanenum_idx]);
  537. road.link_coords = parseCoords(fields[link_coords_idx]);
  538. }
  539. catch (const std::exception &e)
  540. {
  541. std::cerr << "解析错误,跳过行: " << line << "\n错误信息: " << e.what() << std::endl;
  542. continue;
  543. }
  544. roads.push_back(road);
  545. }
  546. csvFile.close();
  547. return roads;
  548. }
  549. std::vector<LaneInfo> readLanesFromCSV(const std::string &csvPath)
  550. {
  551. std::vector<LaneInfo> lanes;
  552. std::ifstream csvFile(csvPath);
  553. if (!csvFile.is_open())
  554. {
  555. std::cerr << "无法打开车道信息csv文件: " << csvPath << std::endl;
  556. return lanes;
  557. }
  558. std::string line;
  559. // 读取表头
  560. if (!std::getline(csvFile, line))
  561. {
  562. std::cerr << "车道信息csv文件为空或无法读取表头。" << std::endl;
  563. return lanes;
  564. }
  565. // 解析表头,建立列名到索引的映射
  566. std::unordered_map<std::string, int> headerMap;
  567. std::stringstream headerStream(line);
  568. std::string headerField;
  569. int index = 0;
  570. while (std::getline(headerStream, headerField, ','))
  571. {
  572. // 去除可能的引号和空格
  573. headerField.erase(std::remove(headerField.begin(), headerField.end(), '\"'), headerField.end());
  574. headerField.erase(std::remove(headerField.begin(), headerField.end(), ' '), headerField.end());
  575. headerMap[headerField] = index++;
  576. }
  577. // 检查是否存在所有必要的列
  578. std::vector<std::string> requiredColumns = {"lane_id", "lane_length", "lane_type", "lane_width", "lane_turn", "lane_ref_link", "lane_speed_max", "lane_speed_min", "lane_coords"};
  579. for (const auto &col : requiredColumns)
  580. {
  581. if (headerMap.find(col) == headerMap.end())
  582. {
  583. std::cerr << "车道信息csv文件中未找到列名为 " << col << " 的列。" << std::endl;
  584. return lanes;
  585. }
  586. }
  587. int lane_id_idx = headerMap["lane_id"];
  588. int lane_length_idx = headerMap["lane_length"];
  589. int lane_type_idx = headerMap["lane_type"];
  590. int lane_width_idx = headerMap["lane_width"];
  591. int lane_turn_idx = headerMap["lane_turn"];
  592. int lane_ref_link_idx = headerMap["lane_ref_link"];
  593. int lane_speed_max_idx = headerMap["lane_speed_max"];
  594. int lane_speed_min_idx = headerMap["lane_speed_min"];
  595. int lane_coords_idx = headerMap["lane_coords"];
  596. // 读取每一行
  597. while (std::getline(csvFile, line))
  598. {
  599. std::stringstream ss(line);
  600. std::string field;
  601. std::vector<std::string> fields;
  602. bool in_quotes = false;
  603. std::string token;
  604. for (size_t i = 0; i < line.size(); ++i)
  605. {
  606. char c = line[i];
  607. if (c == '\"')
  608. {
  609. in_quotes = !in_quotes;
  610. }
  611. else if (c == ',' && !in_quotes)
  612. {
  613. fields.push_back(token);
  614. token.clear();
  615. }
  616. else
  617. {
  618. token += c;
  619. }
  620. }
  621. fields.push_back(token);
  622. // 检查字段数是否足够
  623. if (fields.size() < requiredColumns.size())
  624. {
  625. std::cerr << "字段数量不足,跳过行: " << line << std::endl;
  626. continue;
  627. }
  628. LaneInfo lane;
  629. try
  630. {
  631. lane.lane_id = std::stoi(fields[lane_id_idx]);
  632. lane.lane_length = std::stod(fields[lane_length_idx]);
  633. lane.lane_type = std::stoi(fields[lane_type_idx]);
  634. lane.lane_width = std::stod(fields[lane_width_idx]);
  635. lane.lane_turn = std::stoi(fields[lane_turn_idx]);
  636. lane.lane_ref_link = std::stoi(fields[lane_ref_link_idx]);
  637. lane.lane_speed_max = std::stoi(fields[lane_speed_max_idx]);
  638. lane.lane_speed_min = std::stoi(fields[lane_speed_min_idx]);
  639. lane.lane_coords = parseCoords(fields[lane_coords_idx]);
  640. }
  641. catch (const std::exception &e)
  642. {
  643. std::cerr << "解析错误,跳过行: " << line << "\n错误信息: " << e.what() << std::endl;
  644. continue;
  645. }
  646. lanes.push_back(lane);
  647. }
  648. csvFile.close();
  649. return lanes;
  650. }
  651. std::vector<SignalInfo> readSignalsFromCSV(const std::string &csvPath)
  652. {
  653. std::vector<SignalInfo> signs;
  654. std::ifstream csvFile(csvPath);
  655. if (!csvFile.is_open())
  656. {
  657. std::cerr << "无法打开交通标牌信息csv文件: " << csvPath << std::endl;
  658. return signs;
  659. }
  660. std::string line;
  661. // 读取表头
  662. if (!std::getline(csvFile, line))
  663. {
  664. std::cerr << "交通标牌信息csv文件为空或无法读取表头。" << std::endl;
  665. return signs;
  666. }
  667. // 解析表头,建立列名到索引的映射
  668. std::unordered_map<std::string, int> headerMap;
  669. std::stringstream headerStream(line);
  670. std::string headerField;
  671. int index = 0;
  672. while (std::getline(headerStream, headerField, ','))
  673. {
  674. // 去除可能的引号和空格
  675. headerField.erase(std::remove(headerField.begin(), headerField.end(), '\"'), headerField.end());
  676. headerField.erase(std::remove(headerField.begin(), headerField.end(), ' '), headerField.end());
  677. headerMap[headerField] = index++;
  678. }
  679. // 检查是否存在所有必要的列
  680. std::vector<std::string> requiredColumns = {"sign_id", "sign_type", "sign_type1", "sign_ref_link", "sign_coords"};
  681. for (const auto &col : requiredColumns)
  682. {
  683. if (headerMap.find(col) == headerMap.end())
  684. {
  685. std::cerr << "交通标牌信息csv文件中未找到列名为 " << col << " 的列。" << std::endl;
  686. return signs;
  687. }
  688. }
  689. int sign_id_idx = headerMap["sign_id"];
  690. int sign_type_idx = headerMap["sign_type"];
  691. int sign_type1_idx = headerMap["sign_type1"];
  692. int sign_ref_link_idx = headerMap["sign_ref_link"];
  693. int sign_coords_idx = headerMap["sign_coords"];
  694. // 读取每一行
  695. while (std::getline(csvFile, line))
  696. {
  697. std::stringstream ss(line);
  698. std::string field;
  699. std::vector<std::string> fields;
  700. bool in_quotes = false;
  701. std::string token;
  702. for (size_t i = 0; i < line.size(); ++i)
  703. {
  704. char c = line[i];
  705. if (c == '\"')
  706. {
  707. in_quotes = !in_quotes;
  708. }
  709. else if (c == ',' && !in_quotes)
  710. {
  711. fields.push_back(token);
  712. token.clear();
  713. }
  714. else
  715. {
  716. token += c;
  717. }
  718. }
  719. fields.push_back(token);
  720. // 检查字段数是否足够
  721. if (fields.size() < requiredColumns.size())
  722. {
  723. std::cerr << "字段数量不足,跳过行: " << line << std::endl;
  724. continue;
  725. }
  726. SignalInfo sign;
  727. try
  728. {
  729. sign.sign_id = std::stoi(fields[sign_id_idx]);
  730. sign.sign_type = std::stoi(fields[sign_type_idx]);
  731. sign.sign_type1 = std::stoi(fields[sign_type1_idx]);
  732. sign.sign_ref_link = std::stoi(fields[sign_ref_link_idx]);
  733. sign.sign_coords = parseCoords(fields[sign_coords_idx]);
  734. }
  735. catch (const std::exception &e)
  736. {
  737. std::cerr << "解析错误,跳过行: " << line << "\n错误信息: " << e.what() << std::endl;
  738. continue;
  739. }
  740. signs.push_back(sign);
  741. }
  742. csvFile.close();
  743. return signs;
  744. }
  745. // 从csv文件中读取停止线信息
  746. std::vector<StoplineInfo> readStopLinesFromCSV(const std::string &csvPath)
  747. {
  748. std::vector<StoplineInfo> stopLines;
  749. std::ifstream csvFile(csvPath);
  750. if (!csvFile.is_open())
  751. {
  752. std::cerr << "无法打开停止线信息csv文件: " << csvPath << std::endl;
  753. return stopLines;
  754. }
  755. std::string line;
  756. // 读取表头
  757. if (!std::getline(csvFile, line))
  758. {
  759. std::cerr << "停止线信息csv文件为空或无法读取表头。" << std::endl;
  760. return stopLines;
  761. }
  762. // 解析表头,建立列名到索引的映射
  763. std::unordered_map<std::string, int> headerMap;
  764. std::stringstream headerStream(line);
  765. std::string headerField;
  766. int index = 0;
  767. while (std::getline(headerStream, headerField, ','))
  768. {
  769. // 去除可能的引号和空格
  770. headerField.erase(std::remove(headerField.begin(), headerField.end(), '\"'), headerField.end());
  771. headerField.erase(std::remove(headerField.begin(), headerField.end(), ' '), headerField.end());
  772. headerMap[headerField] = index++;
  773. }
  774. // 检查是否存在所有必要的列
  775. std::vector<std::string> requiredColumns = {"stopline_id", "stopline_ref_link", "stopline_ref_lane", "stopline_color", "stopline_type", "stopline_coords"};
  776. for (const auto &col : requiredColumns)
  777. {
  778. if (headerMap.find(col) == headerMap.end())
  779. {
  780. std::cerr << "停止线信息csv文件中未找到列名为 " << col << " 的列。" << std::endl;
  781. return stopLines;
  782. }
  783. }
  784. int stop_id_idx = headerMap["stopline_id"];
  785. int stop_ref_link_idx = headerMap["stopline_ref_link"];
  786. int stop_ref_lane_idx = headerMap["stopline_ref_lane"];
  787. int stop_color_idx = headerMap["stopline_color"];
  788. int stop_type_idx = headerMap["stopline_type"];
  789. int stop_coords_idx = headerMap["stopline_coords"];
  790. // 读取每一行
  791. while (std::getline(csvFile, line))
  792. {
  793. std::stringstream ss(line);
  794. std::string field;
  795. std::vector<std::string> fields;
  796. bool in_quotes = false;
  797. std::string token;
  798. for (size_t i = 0; i < line.size(); ++i)
  799. {
  800. char c = line[i];
  801. if (c == '\"')
  802. {
  803. in_quotes = !in_quotes;
  804. }
  805. else if (c == ',' && !in_quotes)
  806. {
  807. fields.push_back(token);
  808. token.clear();
  809. }
  810. else
  811. {
  812. token += c;
  813. }
  814. }
  815. fields.push_back(token);
  816. // 检查字段数是否足够
  817. if (fields.size() < requiredColumns.size())
  818. {
  819. std::cerr << "字段数量不足,跳过行: " << line << std::endl;
  820. continue;
  821. }
  822. StoplineInfo stopLine;
  823. try
  824. {
  825. stopLine.stopline_id = std::stoi(fields[stop_id_idx]);
  826. stopLine.stopline_ref_link = std::stoi(fields[stop_ref_link_idx]);
  827. stopLine.stopline_ref_lane = parseRef(fields[stop_ref_lane_idx]);
  828. stopLine.stopline_color = std::stoi(fields[stop_color_idx]);
  829. stopLine.stopline_type = std::stoi(fields[stop_type_idx]);
  830. stopLine.stopline_coords = parseCoords(fields[stop_coords_idx]);
  831. }
  832. catch (const std::exception &e)
  833. {
  834. std::cerr << "解析错误,跳过行: " << line << "\n错误信息: " << e.what() << std::endl;
  835. continue;
  836. }
  837. stopLines.push_back(stopLine);
  838. }
  839. csvFile.close();
  840. return stopLines;
  841. }
  842. // 从csv文件中读取箭头信息
  843. std::vector<ArrowInfo> readArrowsFromCSV(const std::string &csvPath)
  844. {
  845. std::vector<ArrowInfo> arrows;
  846. std::ifstream csvFile(csvPath);
  847. if (!csvFile.is_open())
  848. {
  849. std::cerr << "无法打开箭头信息csv文件: " << csvPath << std::endl;
  850. return arrows;
  851. }
  852. std::string line;
  853. // 读取表头
  854. if (!std::getline(csvFile, line))
  855. {
  856. std::cerr << "箭头信息csv文件为空或无法读取表头。" << std::endl;
  857. return arrows;
  858. }
  859. // 解析表头,建立列名到索引的映射
  860. std::unordered_map<std::string, int> headerMap;
  861. std::stringstream headerStream(line);
  862. std::string headerField;
  863. int index = 0;
  864. while (std::getline(headerStream, headerField, ','))
  865. {
  866. // 去除可能的引号和空格
  867. headerField.erase(std::remove(headerField.begin(), headerField.end(), '\"'), headerField.end());
  868. headerField.erase(std::remove(headerField.begin(), headerField.end(), ' '), headerField.end());
  869. headerMap[headerField] = index++;
  870. }
  871. // 检查是否存在所有必要的列
  872. std::vector<std::string> requiredColumns = {"arrow_id", "arrow_ref_lane", "arrow_direction"};
  873. for (const auto &col : requiredColumns)
  874. {
  875. if (headerMap.find(col) == headerMap.end())
  876. {
  877. std::cerr << "箭头信息csv文件中未找到列名为 " << col << " 的列。" << std::endl;
  878. return arrows;
  879. }
  880. }
  881. int arrow_id_idx = headerMap["arrow_id"];
  882. int arrow_lane_idx = headerMap["arrow_ref_lane"];
  883. int arrow_direction_idx = headerMap["arrow_direction"];
  884. // 读取每一行
  885. while (std::getline(csvFile, line))
  886. {
  887. std::stringstream ss(line);
  888. std::string field;
  889. std::vector<std::string> fields;
  890. bool in_quotes = false;
  891. std::string token;
  892. for (size_t i = 0; i < line.size(); ++i)
  893. {
  894. char c = line[i];
  895. if (c == '\"')
  896. {
  897. in_quotes = !in_quotes;
  898. }
  899. else if (c == ',' && !in_quotes)
  900. {
  901. fields.push_back(token);
  902. token.clear();
  903. }
  904. else
  905. {
  906. token += c;
  907. }
  908. }
  909. fields.push_back(token);
  910. // 检查字段数是否足够
  911. if (fields.size() < requiredColumns.size())
  912. {
  913. std::cerr << "字段数量不足,跳过行: " << line << std::endl;
  914. continue;
  915. }
  916. ArrowInfo arrow;
  917. try
  918. {
  919. arrow.arrow_id = std::stoi(fields[arrow_id_idx]);
  920. arrow.arrow_ref_lane = std::stoi(fields[arrow_lane_idx]);
  921. arrow.arrow_direction = parseRef(fields[arrow_direction_idx]);
  922. }
  923. catch (const std::exception &e)
  924. {
  925. std::cerr << "解析错误,跳过行: " << line << "\n错误信息: " << e.what() << std::endl;
  926. continue;
  927. }
  928. arrows.push_back(arrow);
  929. }
  930. csvFile.close();
  931. return arrows;
  932. }