You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mapasia.cc 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. Support for MapAsia (.tr7) track file format.
  3. Copyright (C) 2008 Olaf Klein, o.b.klein@gpsbabel.org
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  15. */
  16. #include <ctype.h>
  17. #include <math.h>
  18. #include <string.h>
  19. #include <time.h>
  20. #include "defs.h"
  21. //#include "session.h"
  22. #define MYNAME "mapasia"
  23. #define TR7_TRACK_MAGIC 0x223EADB
  24. #define TR7_S_SIZE 32
  25. #define TR7_S_YEAR 0
  26. #define TR7_S_MONTH 2
  27. #define TR7_S_DAY 6
  28. #define TR7_S_HOUR 8
  29. #define TR7_S_MIN 10
  30. #define TR7_S_SEC 12
  31. #define TR7_S_LON 16
  32. #define TR7_S_LAT 20
  33. #define TR7_S_SPEED 24
  34. #define TR7_S_COURSE 26
  35. #define TR7_S_VALID 28
  36. #define TR7_S_FIX 29
  37. static gbfile* fin, *fout;
  38. static const Waypoint* wpt_tmp;
  39. static const route_head* trk_tmp;
  40. static int course_tmp, speed_tmp;
  41. static
  42. arglist_t tr7_args[] = {
  43. ARG_TERMINATOR
  44. };
  45. /*******************************************************************************
  46. * %%% R E A D E R %%% *
  47. *******************************************************************************/
  48. static void
  49. tr7_rd_init(const QString& fname)
  50. {
  51. fin = gbfopen_le(fname, "rb", MYNAME);
  52. }
  53. static void
  54. tr7_read(void)
  55. {
  56. route_head* trk = NULL;
  57. unsigned int magic;
  58. Waypoint* prev = NULL;
  59. magic = gbfgetint32(fin);
  60. if (magic != TR7_TRACK_MAGIC) {
  61. fatal(MYNAME ": Invalid magic number in header (%X, but %X expected)!\n", magic, TR7_TRACK_MAGIC);
  62. }
  63. while (! gbfeof(fin)) {
  64. unsigned char buff[TR7_S_SIZE];
  65. double lat, lon;
  66. Waypoint* wpt;
  67. float speed, course;
  68. gbfread(buff, 1, sizeof(buff), fin);
  69. lat = (double)le_read32(&buff[TR7_S_LAT]) / 1000000.0;
  70. lon = (double)le_read32(&buff[TR7_S_LON]) / 1000000.0;
  71. if ((fabs(lat) > 90) || (fabs(lon) > 180)) { /* that really happens */
  72. trk = NULL;
  73. continue;
  74. }
  75. QDate date(le_read16(&buff[TR7_S_YEAR]),
  76. buff[TR7_S_MONTH],
  77. buff[TR7_S_DAY]);
  78. QTime time(buff[TR7_S_HOUR],
  79. buff[TR7_S_MIN],
  80. buff[TR7_S_SEC]);
  81. if (!date.isValid() || !time.isValid()) {
  82. continue;
  83. }
  84. speed = KPH_TO_MPS(le_read16(&buff[TR7_S_SPEED]));
  85. course = 360 - le_read16(&buff[TR7_S_COURSE]);
  86. if ((speed < 0) || (course > 360) || (course < 0)) {
  87. continue;
  88. }
  89. wpt = new Waypoint;
  90. wpt->latitude = lat;
  91. wpt->longitude = lon;
  92. wpt->SetCreationTime(QDateTime(date, time, Qt::UTC));
  93. WAYPT_SET(wpt, course, course);
  94. WAYPT_SET(wpt, speed, speed);
  95. #if 0 /* unsure, not validated items */
  96. wpt->fix = buff[TR7_S_FIX];
  97. if (buff[TR7_S_VALID] != 'A') {
  98. delete wpt;
  99. continue;
  100. }
  101. #endif
  102. if (waypt_speed(prev, wpt) > 9999.9) { /* filter out some bad trackpoints */
  103. delete wpt;
  104. continue;
  105. }
  106. if (prev) { /* other track or bad timestamp */
  107. if (wpt->creation_time.isValid() && (prev->creation_time.toTime_t() > wpt->creation_time.toTime_t())) {
  108. trk = NULL;
  109. } else if (waypt_distance(prev, wpt) > 9999.9) {
  110. trk = NULL;
  111. }
  112. }
  113. if (! trk) {
  114. trk = route_head_alloc();
  115. track_add_head(trk);
  116. }
  117. track_add_wpt(trk, wpt);
  118. prev = wpt;
  119. }
  120. }
  121. static void
  122. tr7_check_after_read_head_cb(const route_head* trk)
  123. {
  124. trk_tmp = trk;
  125. course_tmp = 0;
  126. speed_tmp = 0;
  127. }
  128. static void
  129. tr7_check_after_read_wpt_cb(const Waypoint* wpt)
  130. {
  131. if (wpt->speed != 0) {
  132. speed_tmp = 1;
  133. }
  134. if (wpt->course != 360.0) {
  135. course_tmp = 1;
  136. }
  137. }
  138. static void
  139. tr7_check_after_read_trailer_cb(const route_head* trk)
  140. {
  141. queue* elem, *tmp;
  142. QUEUE_FOR_EACH((queue*)&trk->waypoint_list, elem, tmp) {
  143. Waypoint* wpt = (Waypoint*)elem;
  144. if (speed_tmp == 0) {
  145. WAYPT_UNSET(wpt, speed);
  146. }
  147. if (course_tmp == 0) {
  148. WAYPT_UNSET(wpt, course);
  149. wpt->course = 0;
  150. }
  151. }
  152. }
  153. static void
  154. tr7_rd_deinit(void)
  155. {
  156. track_disp_session(curr_session(),
  157. tr7_check_after_read_head_cb,
  158. tr7_check_after_read_trailer_cb,
  159. tr7_check_after_read_wpt_cb);
  160. gbfclose(fin);
  161. }
  162. /*******************************************************************************
  163. * %%% W R I T E R %%% *
  164. *******************************************************************************/
  165. static void
  166. tr7_disp_track_head_cb(const route_head* trk)
  167. {
  168. wpt_tmp = NULL;
  169. }
  170. static void
  171. tr7_disp_waypt_cb(const Waypoint* wpt)
  172. {
  173. unsigned char buff[TR7_S_SIZE];
  174. double speed, course;
  175. memset(buff, 0, sizeof(buff));
  176. le_write32(&buff[TR7_S_LON], (int)(wpt->longitude * 1000000.0));
  177. le_write32(&buff[TR7_S_LAT], (int)(wpt->latitude * 1000000.0));
  178. if WAYPT_HAS(wpt, course) {
  179. course = wpt->course;
  180. } else if (wpt_tmp != NULL) {
  181. course = waypt_course(wpt_tmp, wpt);
  182. } else {
  183. course = -1;
  184. }
  185. if (course >= 0) {
  186. le_write16(&buff[TR7_S_COURSE], (int)(360 - course));
  187. }
  188. QDateTime dt = wpt->GetCreationTime().toUTC();
  189. if (dt.isValid()) {
  190. QDate d = dt.date();
  191. le_write16(&buff[TR7_S_YEAR], d.year());
  192. buff[TR7_S_MONTH] = d.month();
  193. buff[TR7_S_DAY] = d.day();
  194. QTime t = dt.time();
  195. buff[TR7_S_HOUR] = t.hour();
  196. buff[TR7_S_MIN] = t.minute();
  197. buff[TR7_S_SEC] = t.second();
  198. if WAYPT_HAS(wpt, speed) {
  199. speed = wpt->speed;
  200. } else if (wpt_tmp != NULL) {
  201. speed = waypt_speed(wpt_tmp, wpt);
  202. } else {
  203. speed = -1;
  204. }
  205. if (speed >= 0) {
  206. le_write16(&buff[TR7_S_SPEED], (int)MPS_TO_KPH(speed));
  207. }
  208. }
  209. buff[TR7_S_VALID] = 'A'; /* meaning unknown */
  210. #if 0 /* not validated */
  211. if (wpt->fix != fix_unknown) {
  212. buff[TR7_S_FIX] = wpt->fix;
  213. }
  214. #endif
  215. gbfwrite(buff, 1, sizeof(buff), fout);
  216. wpt_tmp = wpt;
  217. }
  218. static void
  219. tr7_wr_init(const QString& fname)
  220. {
  221. fout = gbfopen_le(fname, "wb", MYNAME);
  222. gbfputint32(TR7_TRACK_MAGIC, fout);
  223. }
  224. static void
  225. tr7_wr_deinit(void)
  226. {
  227. gbfclose(fout);
  228. }
  229. static void
  230. tr7_write(void)
  231. {
  232. track_disp_all(tr7_disp_track_head_cb, NULL, tr7_disp_waypt_cb);
  233. }
  234. /**************************************************************************/
  235. ff_vecs_t mapasia_tr7_vecs = { /* we can read and write tracks */
  236. ff_type_file,
  237. {
  238. ff_cap_none /* waypoints */,
  239. (ff_cap)(ff_cap_read | ff_cap_write) /* tracks */,
  240. ff_cap_none /* routes */
  241. },
  242. tr7_rd_init,
  243. tr7_wr_init,
  244. tr7_rd_deinit,
  245. tr7_wr_deinit,
  246. tr7_read,
  247. tr7_write,
  248. NULL,
  249. tr7_args,
  250. CET_CHARSET_UTF8, 1 /* FIXED - CET-REVIEW - */
  251. };
  252. /**************************************************************************/