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.

lowranceusr.cc 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /*
  2. Access to Lowrance USR files.
  3. Contributed to gpsbabel by Jason Rust (jrust at rustyparts.com)
  4. Copyright (C) 2005, 2006, 2007, 2008 Robert Lipe, robertlipe+source@gpsbabel.org
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  16. HISTORY:
  17. 6/21/05 - Ling Nero (rnlnero@yahoo.com)
  18. - Added Routes, Icons, & Tracks support
  19. - Fixed waypoint date/time stamp conversion
  20. 02/09/08 - oliskoli
  21. - gbfile API
  22. - check for buffer overflows when reading names or comments
  23. 02/25/2008 - Alan Porter (alan@kr4jb.net)
  24. - Added new icons for Lowrance iFinder Expedition C
  25. - Categorized geocaching waypoints using different icons
  26. */
  27. #include "defs.h"
  28. #include <QtCore/QDebug>
  29. #include <stdio.h>
  30. #include <cmath> /* for lat/lon conversion */
  31. #include <stdlib.h> // atoi
  32. typedef struct lowranceusr_icon_mapping {
  33. const int value;
  34. const char* icon;
  35. } lowranceusr_icon_mapping_t;
  36. #define DEF_ICON 10001
  37. /* Taken from iFinder 1.8 */
  38. const lowranceusr_icon_mapping_t lowranceusr_icon_value_table[] = {
  39. { 10000, "diamond 1" },
  40. { 10001, "diamond 2" },
  41. { 10002, "diamond 3" },
  42. { 10003, "x 1" },
  43. { 10004, "x 2" },
  44. { 10005, "x 3" },
  45. { 10006, "cross" },
  46. { 10007, "house" },
  47. { 10008, "car" },
  48. { 10009, "store" },
  49. { 10010, "gas station" },
  50. { 10011, "fork and spoon" },
  51. { 10012, "telephone" },
  52. { 10013, "airplane" },
  53. { 10014, "exit sign" },
  54. { 10015, "stop sign" },
  55. { 10016, "exclamation" },
  56. { 10017, "traffic light" },
  57. { 10018, "american flag" },
  58. { 10019, "person" },
  59. { 10020, "restrooms" },
  60. { 10021, "tree" },
  61. { 10022, "mountains" },
  62. { 10023, "campsite" },
  63. { 10024, "picnic table" },
  64. { 10025, "deer" },
  65. { 10026, "deer tracks" },
  66. { 10027, "turkey tracks" },
  67. { 10028, "tree stand" },
  68. { 10029, "bridge" },
  69. { 10030, "skull and crossbones" },
  70. { 10031, "fish" },
  71. { 10032, "two fish" },
  72. { 10033, "dive flag" },
  73. { 10034, "wreck" },
  74. { 10035, "anchor" },
  75. { 10036, "boat" },
  76. { 10037, "boat ramp" },
  77. { 10038, "flag buoy" },
  78. { 10039, "dam" },
  79. { 10040, "swimmer" },
  80. { 10041, "pier"},
  81. /* The following list is from TopoFusion */
  82. { 10000, "Waypoint" }, /* diamond 1 */
  83. { DEF_ICON, "Text Label (No Dot)" },
  84. { 10018, "Trailhead" }, /* american flag */
  85. { 10023, "Campground" }, /* campsite */
  86. { 10022, "Summit" }, /* mountains */
  87. { DEF_ICON, "Tall Tower" },
  88. { DEF_ICON, "Short Tower" },
  89. { 10021, "Forest" }, /* tree */
  90. { DEF_ICON, "Mine" },
  91. // { 10038, "Geocache" }, /* flag buoy */
  92. // { 10016, "Geocache Found" }, /* exclamation */
  93. { DEF_ICON, "Skiing Area" },
  94. { 10029, "Crossing" }, /* bridge */
  95. { 10007, "House" }, /* house */
  96. { 10003, "Dot" }, /* x 1 */
  97. { 10025, "Hunting Area" }, /* deer */
  98. { 10031, "Fishing Area" }, /* fish */
  99. { 10040, "Swimming Area" }, /* swimmer */
  100. { 10012, "Telephone" }, /* telephone */
  101. { 10024, "Rest Area" }, /* picnic table */
  102. { 10021, "Park" }, /* tree */
  103. { 10007, "Information" }, /* house */
  104. { 10022, "Scenic Area" }, /* mountains */
  105. { DEF_ICON, "Bank/Dollar" },
  106. { 10009, "Hotel" }, /* store */
  107. { 10011, "Restaurant" }, /* fork and spoon */
  108. { 10030, "Danger Area" }, /* skull and crossbones */
  109. { 10035, "Anchor" }, /* anchor */
  110. { 10002, "City (Large)" }, /* diamond 3 */
  111. { 10001, "City (Medium)" }, /* diamond 2 */
  112. { 10000, "City (Small)" }, /* diamond 1 */
  113. { DEF_ICON, "Drinking Water" },
  114. { 10008, "Parking Area" }, /* car */
  115. { 10023, "RV Park" }, /* campsite */
  116. { 10020, "Rest Room" }, /* restroom */
  117. { 10019, "Shower" }, /* person */
  118. { DEF_ICON, "Tunnel" },
  119. /* This list comes from 'wifinder' from ifinder H20 Color */
  120. { 10062, "Interesting Land Feature" },
  121. { 10063, "Global Location" },
  122. { 10064, "Note" },
  123. { 10065, "Ghost" },
  124. { 10066, "Letter" },
  125. { 10067, "Multi-Treasure" },
  126. { 10068, "Mystery Or Puzzle" },
  127. { 10069, "Treasure" },
  128. { 10070, "Webmail" },
  129. { 10071, "Sun" },
  130. { 10072, "Musical Note" },
  131. { 10073, "Camera/Movie Theater" },
  132. { 10074, "Star" },
  133. { 10075, "Coffee Mug" },
  134. { 10076, "Books" },
  135. { 10077, "Historical Marker" },
  136. { 10078, "Tools/Repair" },
  137. { 10079, "Favorite" },
  138. { 10080, "Arena" },
  139. { 10081, "Golf Course" },
  140. { 10082, "Money/Atm" },
  141. /* This list comes from Alan Porter <alan@kr4jb.net>, using an iFinder Expedition C */
  142. { 10042, "icon42" }, // black box with red X
  143. { 10043, "icon43" }, // small red dot
  144. { 10044, "icon44" }, // 4-wheeler
  145. { 10045, "icon45" }, // hiding hunter
  146. { 10046, "icon46" }, // tree (yellow base)
  147. { 10047, "icon47" }, // windmill
  148. { 10048, "icon48" }, // camera
  149. { 10049, "icon49" }, // tree (something in front of base)
  150. { 10050, "icon50" }, // tree (something hanging from left side)
  151. { 10051, "icon51" }, // 4 dots in rhombus shape
  152. { 10052, "icon52" }, // bare winter tree
  153. { 10053, "icon53" }, // hiding deer head peeking over bushes
  154. { 10054, "icon54" }, // piston? over a pile of salt?
  155. { 10055, "icon55" }, // corn
  156. { 10056, "icon56" }, // turkey
  157. { 10057, "icon57" }, // duck
  158. { 10058, "icon58" }, // hen
  159. { 10059, "icon59" }, // rabbit
  160. { 10060, "icon60" }, // paw print
  161. { 10061, "icon61" }, // 2 red flames?
  162. /* These are the icons that gpsbabel will use */
  163. { 10038, "Geocache" }, // flag buoy
  164. { 10016, "Geocache Found" }, // exclamation
  165. { 10043, "Micro-Cache" }, // small red dot
  166. { 10065, "Virtual cache" }, // ghost
  167. { 10051, "Multi-Cache" }, // 4 dots in rhombus shape
  168. { 10068, "Unknown Cache" }, // ? mark
  169. { 10045, "Locationless (Reverse) Cache" }, // hiding hunter
  170. { 10066, "Post Office" }, // letter
  171. { 10019, "Event Cache" }, // person
  172. { 10070, "Webcam Cache" }, // webcam
  173. { 10042, "Disabled Cache" }, // black box with red X
  174. { -1, NULL }
  175. };
  176. static gbfile* file_in;
  177. static gbfile* file_out;
  178. static short_handle mkshort_handle;
  179. static unsigned short waypt_out_count;
  180. static unsigned int trail_count, lowrance_route_count;
  181. static int trail_point_count;
  182. static char continuous = 1;
  183. static short num_section_points;
  184. static route_head* trk_head;
  185. static route_head* rte_head;
  186. static char* ignoreicons;
  187. static char* writeasicons;
  188. static char* merge;
  189. static char* seg_break;
  190. static char* wversion_arg;
  191. static int reading_version;
  192. static int writing_version;
  193. #define MYNAME "Lowrance USR"
  194. #define MAXUSRSTRINGSIZE 256
  195. #define SEMIMINOR 6356752.3142
  196. #define DEGREESTORADIANS 0.017453292
  197. #define SECSTO2000 946713600
  198. #define MAX_TRAIL_POINTS 9999
  199. #define UNKNOWN_USR_ALTITUDE METERS_TO_FEET(-10000) /* -10000ft is how the unit stores unknown */
  200. /* Jan 1, 2000 00:00:00 */
  201. const time_t base_time_secs = 946706400;
  202. static int
  203. lowranceusr_readstr(char* buf, const int maxlen, gbfile* file)
  204. {
  205. int org, len;
  206. org = len = gbfgetint32(file);
  207. if (len < 0) {
  208. fatal(MYNAME ": Invalid item length (%d)!\n", len);
  209. } else if (len) {
  210. int i;
  211. if (len > maxlen) {
  212. len = maxlen;
  213. }
  214. (void) gbfread(buf, 1, len, file);
  215. if (org > maxlen) {
  216. (void) gbfseek(file, org - maxlen, SEEK_CUR);
  217. }
  218. // IWay 350C puts 0x01 for the accented o in the street name
  219. // of the Montreal Holiday Inn.
  220. for (i = 0; i < len; i++) {
  221. if (buf[i] == 0x01) {
  222. buf[i] = '*';
  223. }
  224. }
  225. }
  226. return len;
  227. }
  228. const QString
  229. lowranceusr_find_desc_from_icon_number(const int icon)
  230. {
  231. const lowranceusr_icon_mapping_t* i;
  232. for (i = lowranceusr_icon_value_table; i->icon; i++) {
  233. if (icon == i->value) {
  234. return i->icon;
  235. }
  236. }
  237. return "";
  238. }
  239. static int
  240. lowranceusr_find_icon_number_from_desc(const QString& desc)
  241. {
  242. const lowranceusr_icon_mapping_t* i;
  243. int n;
  244. if (desc.isNull()) {
  245. return DEF_ICON;
  246. }
  247. /*
  248. * If we were given a numeric icon number as a description
  249. * (i.e. 8255), just return that.
  250. */
  251. n = desc.toInt();
  252. if (n) {
  253. return n;
  254. }
  255. for (i = lowranceusr_icon_value_table; i->icon; i++) {
  256. if (desc.compare(i->icon,Qt::CaseInsensitive) == 0) {
  257. return i->value;
  258. }
  259. }
  260. return DEF_ICON;
  261. }
  262. static
  263. arglist_t lowranceusr_args[] = {
  264. {
  265. "ignoreicons", &ignoreicons, "Ignore event marker icons on read",
  266. NULL, ARGTYPE_BOOL, ARG_NOMINMAX
  267. },
  268. {
  269. "writeasicons", &writeasicons, "Treat waypoints as icons on write",
  270. NULL, ARGTYPE_BOOL, ARG_NOMINMAX
  271. },
  272. {
  273. "merge", &merge, "(USR output) Merge into one segmented track",
  274. NULL, ARGTYPE_BOOL, ARG_NOMINMAX
  275. },
  276. {
  277. "break", &seg_break, "(USR input) Break segments into separate tracks",
  278. NULL, ARGTYPE_BOOL, ARG_NOMINMAX
  279. },
  280. {
  281. "wversion", &wversion_arg, "(USR output) Write version",
  282. "2", ARGTYPE_INT, "2", "3"
  283. },
  284. ARG_TERMINATOR
  285. };
  286. static void
  287. rd_init(const QString& fname)
  288. {
  289. file_in = gbfopen_le(fname, "rb", MYNAME);
  290. }
  291. static void
  292. rd_deinit(void)
  293. {
  294. gbfclose(file_in);
  295. }
  296. static void
  297. wr_init(const QString& fname)
  298. {
  299. file_out = gbfopen_le(fname, "wb", MYNAME);
  300. mkshort_handle = mkshort_new_handle();
  301. waypt_out_count = 0;
  302. writing_version = atoi(wversion_arg);
  303. }
  304. static void
  305. wr_deinit(void)
  306. {
  307. gbfclose(file_out);
  308. mkshort_del_handle(&mkshort_handle);
  309. }
  310. /**
  311. * Latitude and longitude for USR coords are in the lowrance mercator meter
  312. * format in WGS84. The below code converts them to degrees.
  313. */
  314. static double
  315. lon_mm_to_deg(double x)
  316. {
  317. return x / (DEGREESTORADIANS * SEMIMINOR);
  318. }
  319. static long
  320. lon_deg_to_mm(double x)
  321. {
  322. return lround(x * SEMIMINOR * DEGREESTORADIANS);
  323. }
  324. static double
  325. lat_mm_to_deg(double x)
  326. {
  327. return (2.0 * atan(exp(x / SEMIMINOR)) - M_PI / 2.0) / DEGREESTORADIANS;
  328. }
  329. static long
  330. lat_deg_to_mm(double x)
  331. {
  332. return (long)(SEMIMINOR * log(tan((x * DEGREESTORADIANS + M_PI / 2.0) / 2.0)));
  333. }
  334. static void
  335. lowranceusr_parse_waypt(Waypoint* wpt_tmp)
  336. {
  337. char buff[MAXUSRSTRINGSIZE + 1];
  338. int text_len;
  339. wpt_tmp->latitude = lat_mm_to_deg(gbfgetint32(file_in));
  340. wpt_tmp->longitude = lon_mm_to_deg(gbfgetint32(file_in));
  341. wpt_tmp->altitude = FEET_TO_METERS(gbfgetint32(file_in));
  342. if (METERS_TO_FEET(wpt_tmp->altitude) <= -10000) {
  343. wpt_tmp->altitude = unknown_alt;
  344. }
  345. text_len = lowranceusr_readstr(&buff[0], MAXUSRSTRINGSIZE, file_in);
  346. if (text_len) {
  347. buff[text_len] = '\0';
  348. wpt_tmp->shortname = buff;
  349. }
  350. if (global_opts.debug_level >= 1)
  351. printf(MYNAME " parse_waypt: Waypt name = %s Lat = %f Lon = %f alt = %f\n",qPrintable(wpt_tmp->shortname), wpt_tmp->latitude,
  352. wpt_tmp->longitude, wpt_tmp->altitude);
  353. text_len = lowranceusr_readstr(&buff[0], MAXUSRSTRINGSIZE, file_in);
  354. if (text_len) {
  355. buff[text_len] = '\0';
  356. wpt_tmp->description = buff;
  357. }
  358. /* Time is number of seconds since Jan. 1, 2000 */
  359. time_t waypt_time = gbfgetint32(file_in);
  360. if (waypt_time) {
  361. wpt_tmp->SetCreationTime(base_time_secs + waypt_time);
  362. }
  363. if (global_opts.debug_level >= 2) {
  364. printf(MYNAME " parse_waypt: creation time %d\n",
  365. (int)wpt_tmp->creation_time.toTime_t());
  366. printf(MYNAME " parse_waypt: base_time %d\n", (int)base_time_secs);
  367. printf(MYNAME " parse_waypt: waypt time %d\n", (int)waypt_time);
  368. }
  369. /* Symbol ID */
  370. wpt_tmp->icon_descr = lowranceusr_find_desc_from_icon_number(gbfgetint32(file_in));
  371. if (wpt_tmp->icon_descr.isNull()) {
  372. char nbuf[10];
  373. snprintf(nbuf, sizeof(nbuf), "%d", le_read32(buff));
  374. wpt_tmp->icon_descr = nbuf;
  375. }
  376. /* Waypoint Type (USER, TEMPORARY, POINT_OF_INTEREST) */
  377. short waypt_type = gbfgetint16(file_in);
  378. if (global_opts.debug_level >= 1) {
  379. printf(MYNAME " parse_waypt: waypt_type = %d\n",waypt_type);
  380. }
  381. // Version 3 has a depth field here.
  382. if (reading_version >= 3) {
  383. float depth_feet = gbfgetflt(file_in);
  384. if (std::abs(depth_feet - 99999.0) > .1) {
  385. WAYPT_SET(wpt_tmp, depth, FEET_TO_METERS(depth_feet));
  386. }
  387. }
  388. }
  389. static void
  390. lowranceusr_parse_routes(void)
  391. {
  392. char buff[MAXUSRSTRINGSIZE + 1];
  393. short int num_routes, num_legs;
  394. int i,j;
  395. int text_len;
  396. Waypoint* wpt_tmp;
  397. num_routes = gbfgetint16(file_in);
  398. if (global_opts.debug_level >= 1) {
  399. printf(MYNAME " parse_routes: Num Routes = %d\n", num_routes);
  400. }
  401. for (i=0; i < num_routes; i++) {
  402. rte_head = route_head_alloc();
  403. route_add_head(rte_head);
  404. rte_head->rte_num = i+1;
  405. /* route name */
  406. text_len = lowranceusr_readstr(&buff[0], MAXUSRSTRINGSIZE, file_in);
  407. if (text_len) {
  408. buff[text_len] = '\0';
  409. rte_head->rte_name = buff;
  410. }
  411. /* num Legs */
  412. num_legs = gbfgetint16(file_in);
  413. /* route reversed */
  414. (void) gbfread(&buff[0], 1, 1, file_in);
  415. /* waypoints */
  416. for (j=0; j < num_legs; j++) {
  417. wpt_tmp = new Waypoint;
  418. lowranceusr_parse_waypt(wpt_tmp);
  419. route_add_wpt(rte_head, wpt_tmp);
  420. }
  421. }
  422. }
  423. /*
  424. * Icons are automatically converted to waypoints unless
  425. * option of ignoreicons is used
  426. */
  427. static void
  428. lowranceusr_parse_icons(void)
  429. {
  430. char buff[MAXUSRSTRINGSIZE + 1];
  431. short int num_icons;
  432. int i;
  433. num_icons = gbfgetint16(file_in);
  434. if (global_opts.debug_level >= 1) {
  435. printf(MYNAME " parse_icons: num Icons = %d\n", num_icons);
  436. }
  437. for (i=0; i < num_icons && !gbfeof(file_in); i++) {
  438. if (ignoreicons) {
  439. /* position coord lat & long */
  440. (void) gbfread(&buff[0], 4, 2, file_in);
  441. /* symbol */
  442. (void) gbfread(&buff[0], 4, 1, file_in);
  443. } else {
  444. Waypoint* wpt_tmp;
  445. wpt_tmp = new Waypoint;
  446. /* position coord lat & long */
  447. wpt_tmp->latitude = lat_mm_to_deg(gbfgetint32(file_in));
  448. wpt_tmp->longitude = lon_mm_to_deg(gbfgetint32(file_in));
  449. wpt_tmp->altitude = 0;
  450. snprintf(buff, sizeof(buff), "Icon %d", i+1);
  451. wpt_tmp->shortname = buff;
  452. /* symbol */
  453. wpt_tmp->icon_descr = lowranceusr_find_desc_from_icon_number(gbfgetint32(file_in));
  454. waypt_add(wpt_tmp);
  455. }
  456. }
  457. }
  458. static void
  459. lowranceusr_parse_trails(void)
  460. {
  461. char buff[MAXUSRSTRINGSIZE + 1];
  462. short int num_trails, num_trail_points, num_section_points;
  463. int i,j, trk_num, itmp;
  464. int text_len;
  465. Waypoint* wpt_tmp;
  466. route_head* trk_tmp;
  467. /* num trails */
  468. num_trails = gbfgetint16(file_in);
  469. if (global_opts.debug_level >= 1) {
  470. printf(MYNAME " parse_trails: num trails = %d\n", num_trails);
  471. }
  472. for (i=trk_num=0; i < num_trails && !gbfeof(file_in); i++) {
  473. trk_head = route_head_alloc();
  474. trk_head->rte_num = ++trk_num;
  475. track_add_head(trk_head);
  476. /* trail name */
  477. text_len = lowranceusr_readstr(&buff[0], MAXUSRSTRINGSIZE, file_in);
  478. if (global_opts.debug_level >= 1) {
  479. printf(MYNAME " parse_trails: name text len = %d\n", text_len);
  480. }
  481. if (text_len) {
  482. buff[text_len] = '\0';
  483. trk_head->rte_name = buff;
  484. }
  485. if (global_opts.debug_level >= 1) {
  486. printf(MYNAME " parse_trails: trail name = %s\n", qPrintable(trk_head->rte_name));
  487. }
  488. /* visible */
  489. (void) gbfread(&buff[0], 1, 1, file_in);
  490. /* num trail points */
  491. num_trail_points = gbfgetint16(file_in);
  492. if (global_opts.debug_level >= 1) {
  493. printf(MYNAME " parse_trails: num trail points = %d\n", num_trail_points);
  494. }
  495. /* max trail size */
  496. itmp = gbfgetint16(file_in);
  497. if (global_opts.debug_level >= 1) {
  498. printf(MYNAME " parse_trails: max trail size = %d\n", itmp);
  499. }
  500. if (num_trail_points) {
  501. while (num_trail_points && !gbfeof(file_in)) {
  502. /* num section points */
  503. num_section_points = gbfgetint16(file_in);
  504. if (global_opts.debug_level >= 1) {
  505. printf(MYNAME " parse_trails: num section points = %d\n", num_section_points);
  506. }
  507. for (j=0; j < num_section_points && !gbfeof(file_in);
  508. j++, num_trail_points--) {
  509. wpt_tmp = new Waypoint;
  510. wpt_tmp->latitude = lat_mm_to_deg(gbfgetint32(file_in));
  511. wpt_tmp->longitude = lon_mm_to_deg(gbfgetint32(file_in));
  512. /* continuous */
  513. (void) gbfread(&buff[0], 1, 1, file_in);
  514. if (!buff[0] && seg_break && j) {
  515. trk_tmp = route_head_alloc();
  516. trk_tmp->rte_num = ++trk_num;
  517. trk_tmp->rte_name = trk_head->rte_name;
  518. track_add_head(trk_tmp);
  519. trk_head = trk_tmp;
  520. }
  521. track_add_wpt(trk_head, wpt_tmp);
  522. if (global_opts.debug_level >= 1) {
  523. printf(MYNAME " parse_trails: Trail pt lat %f lon %f\n", wpt_tmp->latitude, wpt_tmp->longitude);
  524. }
  525. }
  526. }
  527. }
  528. /* remove the trail since it's empty */
  529. else {
  530. track_del_head(trk_head);
  531. }
  532. }
  533. }
  534. static void
  535. data_read(void)
  536. {
  537. short int NumWaypoints, MajorVersion, MinorVersion, object_num;
  538. int i;
  539. MajorVersion = gbfgetint16(file_in);
  540. reading_version = MajorVersion;
  541. MinorVersion = gbfgetint16(file_in);
  542. if (global_opts.debug_level >= 1) {
  543. printf(MYNAME " data_read: Major Version %d Minor Version %d\n", MajorVersion, MinorVersion);
  544. }
  545. if (MajorVersion < 2) {
  546. fatal(MYNAME ": input file is from an old version of the USR file and is not supported\n");
  547. }
  548. if (MajorVersion == 4) {
  549. fatal(MYNAME ": this appears to be a USR v4 file: use the lowranceusr4 format\n");
  550. }
  551. if (MajorVersion > 4) {
  552. fatal(MYNAME ": input file version %d is not supported\n",
  553. MajorVersion);
  554. }
  555. NumWaypoints = gbfgetint16(file_in);
  556. if (global_opts.debug_level >= 1) {
  557. printf(MYNAME " data_read: Num waypoints %d\n", NumWaypoints);
  558. }
  559. for (i = 0; i < NumWaypoints && !gbfeof(file_in); i++) {
  560. Waypoint* wpt_tmp;
  561. wpt_tmp = new Waypoint;
  562. /* Object num */
  563. object_num = gbfgetint16(file_in);
  564. if (global_opts.debug_level >= 1) {
  565. printf(MYNAME " data_read: object_num = %d\n", object_num);
  566. }
  567. /* waypoint */
  568. lowranceusr_parse_waypt(wpt_tmp);
  569. waypt_add(wpt_tmp);
  570. }
  571. lowranceusr_parse_routes();
  572. lowranceusr_parse_icons();
  573. lowranceusr_parse_trails();
  574. }
  575. static void
  576. lowranceusr_waypt_disp(const Waypoint* wpt)
  577. {
  578. int text_len, Lat, Lon, Time, SymbolId;
  579. short int WayptType;
  580. int alt = METERS_TO_FEET(wpt->altitude);
  581. if (wpt->altitude == unknown_alt) {
  582. alt = UNKNOWN_USR_ALTITUDE;
  583. }
  584. Lat = lat_deg_to_mm(wpt->latitude);
  585. Lon = lon_deg_to_mm(wpt->longitude);
  586. gbfputint32(Lat, file_out);
  587. gbfputint32(Lon, file_out);
  588. gbfputint32(alt, file_out);
  589. if (writing_version >= 3) {
  590. float depth = WAYPT_HAS(wpt, depth) ?
  591. METERS_TO_FEET(wpt->depth) : -99999.0;
  592. gbfputflt(depth, file_out);
  593. }
  594. if (global_opts.debug_level >= 1) {
  595. /* print lat/lon/alt on one easily greppable line */
  596. printf(MYNAME " waypt_disp: Lat = %d Lon = %d Alt = %d\n",Lat, Lon, alt);
  597. }
  598. /* Try and make sure we have a name */
  599. // this kind of thing would probably be more readable like
  600. // name = blah.
  601. // if name.isEmpty()
  602. // name = planB;
  603. // if name.isEmpty()
  604. // name = planC;
  605. // ...
  606. QString name;
  607. if ((wpt->shortname.isEmpty()) || global_opts.synthesize_shortnames) {
  608. if (!wpt->description.isEmpty() && global_opts.synthesize_shortnames) {
  609. name = mkshort_from_wpt(mkshort_handle, wpt);
  610. } else if (!wpt->shortname.isEmpty()) {
  611. name = wpt->shortname;
  612. } else if (!wpt->description.isEmpty()) {
  613. name = wpt->description;
  614. }
  615. } else {
  616. name = wpt->shortname;
  617. }
  618. text_len = name.length();
  619. if (text_len > MAXUSRSTRINGSIZE) {
  620. text_len = MAXUSRSTRINGSIZE;
  621. }
  622. gbfputint32(text_len, file_out);
  623. gbfwrite(CSTRc(name), 1, text_len, file_out);
  624. if (global_opts.debug_level >= 1) {
  625. printf(MYNAME " waypt_disp: Waypt name = %s\n", qPrintable(name));
  626. }
  627. /**
  628. * Comments are now used by the iFinder (Expedition C supports them)
  629. */
  630. if (wpt->description != wpt->shortname) {
  631. QString comment = wpt->description;
  632. text_len = comment.length();
  633. if (text_len > MAXUSRSTRINGSIZE) {
  634. text_len = MAXUSRSTRINGSIZE;
  635. }
  636. gbfputint32(text_len, file_out);
  637. gbfwrite(CSTR(comment), 1, text_len, file_out);
  638. } else {
  639. text_len = 0;
  640. gbfputint32(text_len, file_out);
  641. }
  642. if (wpt->creation_time.toTime_t() > base_time_secs) {
  643. Time = wpt->creation_time.toTime_t() - base_time_secs;
  644. } else {
  645. Time = 0;
  646. }
  647. if (global_opts.debug_level >= 2) {
  648. time_t wpt_time = Time;
  649. printf(MYNAME " waypt_disp: base_time : %d\n", (int)base_time_secs);
  650. printf(MYNAME " waypt_disp: creation time : %d\n", (int)wpt->creation_time.toTime_t());
  651. printf(MYNAME " waypt_disp: waypt time : %d\n", (int)wpt_time);
  652. printf(MYNAME " waypt_disp: waypt time (local): %s\n", ctime(&wpt_time));
  653. }
  654. gbfputint32(Time, file_out);
  655. if (get_cache_icon(wpt) && wpt->icon_descr.compare("Geocache Found") == 0) {
  656. SymbolId = lowranceusr_find_icon_number_from_desc(get_cache_icon(wpt));
  657. } else {
  658. SymbolId = lowranceusr_find_icon_number_from_desc(wpt->icon_descr);
  659. }
  660. /* If the waypoint is archived or disabled, use a "disabled" icon instead. */
  661. if ((wpt->gc_data->is_archived==status_true) || (wpt->gc_data->is_available==status_false)) {
  662. SymbolId = lowranceusr_find_icon_number_from_desc("Disabled Cache");
  663. }
  664. gbfputint32(SymbolId, file_out);
  665. /* USER waypoint type */
  666. WayptType = 0;
  667. gbfputint16(WayptType, file_out);
  668. }
  669. static void
  670. lowranceusr_waypt_pr(const Waypoint* wpt)
  671. {
  672. /* our personal waypoint counter */
  673. gbfputint16(waypt_out_count, file_out);
  674. if (global_opts.debug_level >= 1) {
  675. printf(MYNAME " waypt_pr: waypoint #%d ",waypt_out_count);
  676. }
  677. waypt_out_count++;
  678. lowranceusr_waypt_disp(wpt);
  679. }
  680. /*
  681. * In Lowrance parlance, an "Icon" is a waypoint but without any
  682. * kind of a name. The header count of icons has already been written
  683. * before we get here, so it's just a matter of spitting out
  684. * 4 bytes lat
  685. * 4 bytes long
  686. * 4 bytes symbol
  687. */
  688. static void
  689. lowranceusr_write_icon(const Waypoint* wpt)
  690. {
  691. int latmm = lat_deg_to_mm(wpt->latitude);
  692. int lonmm = lon_deg_to_mm(wpt->longitude);
  693. int icon = !wpt->icon_descr.isNull() ?
  694. lowranceusr_find_icon_number_from_desc(wpt->icon_descr) :
  695. 10003;
  696. gbfputint32(latmm, file_out);
  697. gbfputint32(lonmm, file_out);
  698. gbfputint32(icon, file_out);
  699. }
  700. /*
  701. * Header format:
  702. * short num_trails,
  703. * int trail_name text length,
  704. * char *trail_name,
  705. * boolean visible,
  706. * short num_trail_points,
  707. * short max_trail_size,
  708. * short num_section_points
  709. * == don't know how many max points per section so
  710. * == use num_trail_points for now
  711. * == Once this is known then the waypoints ought to be
  712. * == broken up into sections
  713. */
  714. static void
  715. lowranceusr_track_hdr(const route_head* trk)
  716. {
  717. int text_len;
  718. QString name;
  719. short num_trail_points, max_trail_size;
  720. char visible=1;
  721. ++trail_count;
  722. //TODO: This whole function needs to be replaced...
  723. if (!trk->rte_name.isEmpty()) {
  724. name = trk->rte_name;
  725. } else if (!trk->rte_desc.isEmpty()) {
  726. name = trk->rte_desc;
  727. } else {
  728. name = name + QString("Babel %1").arg(trail_count);
  729. }
  730. text_len = name.length();
  731. if (text_len > MAXUSRSTRINGSIZE) {
  732. text_len = MAXUSRSTRINGSIZE;
  733. }
  734. if (global_opts.debug_level >= 1) {
  735. printf(MYNAME " track_hdr: trail name text len = %d\n", text_len);
  736. }
  737. gbfputint32(text_len, file_out);
  738. if (global_opts.debug_level >= 1) {
  739. qDebug() << MYNAME << " track_hdr: trail name" << name;
  740. }
  741. gbfwrite(CSTR(name), 1, text_len, file_out);
  742. num_trail_points = (short) trk->rte_waypt_ct;
  743. max_trail_size = MAX_TRAIL_POINTS;
  744. if (num_trail_points > max_trail_size) {
  745. num_trail_points = max_trail_size;
  746. }
  747. num_section_points = num_trail_points;
  748. if (global_opts.debug_level >= 1)
  749. printf(MYNAME " track_hdr: num_trail_points = %d\nmax_trail_size = %d\nnum_section_points = %d\n",
  750. num_trail_points, max_trail_size, num_section_points);
  751. gbfwrite(&visible, 1, 1, file_out);
  752. gbfputint16(num_trail_points, file_out);
  753. gbfputint16(max_trail_size, file_out);
  754. gbfputint16(num_section_points, file_out);
  755. trail_point_count=1;
  756. }
  757. static void
  758. lowranceusr_route_hdr(const route_head* rte)
  759. {
  760. int text_len;
  761. char* name, tmp_name[20];
  762. short num_legs;
  763. char route_reversed=0;
  764. /* route name */
  765. //TODO: This whole function needs to be replaced...
  766. if (!rte->rte_name.isEmpty()) {
  767. name = xstrdup(rte->rte_name);
  768. } else if (!rte->rte_desc.isEmpty()) {
  769. name = xstrdup(rte->rte_desc);
  770. } else {
  771. tmp_name[0]='\0';
  772. snprintf(tmp_name, sizeof(tmp_name), "Babel R%d", ++lowrance_route_count);
  773. name = xstrdup(tmp_name);
  774. }
  775. text_len = strlen(name);
  776. if (text_len > MAXUSRSTRINGSIZE) {
  777. text_len = MAXUSRSTRINGSIZE;
  778. }
  779. gbfputint32(text_len, file_out);
  780. gbfwrite(name, 1, text_len, file_out);
  781. xfree(name);
  782. /* num legs */
  783. num_legs = (short) rte->rte_waypt_ct;
  784. gbfputint16(num_legs, file_out);
  785. gbfwrite(&route_reversed, 1, 1, file_out);
  786. if (global_opts.debug_level >= 1)
  787. printf(MYNAME " route_hdr: route name \"%s\" num_legs = %d\n",
  788. qPrintable(rte->rte_name), num_legs);
  789. }
  790. static void
  791. lowranceusr_track_disp(const Waypoint* wpt)
  792. {
  793. int lat, lon;
  794. if (++trail_point_count <= MAX_TRAIL_POINTS) {
  795. lat = lat_deg_to_mm(wpt->latitude);
  796. lon = lon_deg_to_mm(wpt->longitude);
  797. if (global_opts.debug_level >= 1) {
  798. printf(MYNAME " track_disp: Trail point #%d lat = %d long = %d\n",trail_point_count, lat, lon);
  799. }
  800. gbfputint32(lat, file_out);
  801. gbfputint32(lon, file_out);
  802. gbfwrite(&continuous, 1, 1, file_out);
  803. if (!continuous) {
  804. continuous = 1;
  805. }
  806. }
  807. }
  808. static void
  809. lowranceusr_merge_track_hdr(const route_head* trk)
  810. {
  811. int text_len;
  812. char* name, tmp_name[20];
  813. if (++trail_count == 1) {
  814. if (!trk->rte_name.isEmpty()) {
  815. name = xstrdup(trk->rte_name);
  816. } else if (!trk->rte_desc.isEmpty()) {
  817. name = xstrdup(trk->rte_desc);
  818. } else {
  819. tmp_name[0]='\0';
  820. snprintf(tmp_name, sizeof(tmp_name), "Babel %d", trail_count);
  821. name = xstrdup(tmp_name);
  822. }
  823. text_len = strlen(name);
  824. if (text_len > MAXUSRSTRINGSIZE) {
  825. text_len = MAXUSRSTRINGSIZE;
  826. }
  827. gbfputint32(text_len, file_out);
  828. if (global_opts.debug_level >= 1) {
  829. printf(MYNAME " track_hdr: trail name = %s\n", name);
  830. }
  831. gbfwrite(name, 1, text_len, file_out);
  832. }
  833. trail_point_count += (short) trk->rte_waypt_ct;
  834. }
  835. static void
  836. lowranceusr_merge_track_tlr(const route_head* trk)
  837. {
  838. short num_trail_points, max_trail_size;
  839. char visible=1;
  840. if (trail_count == track_count()) { /* last trail */
  841. num_trail_points = trail_point_count;
  842. max_trail_size = MAX_TRAIL_POINTS;
  843. if (num_trail_points > max_trail_size) {
  844. num_trail_points = max_trail_size;
  845. }
  846. num_section_points = num_trail_points;
  847. if (global_opts.debug_level >= 1)
  848. printf(MYNAME " merge_track_tlr: num_trail_points = %d\nmax_trail_size = %d\nnum_section_points = %d\n",
  849. num_trail_points, max_trail_size, num_section_points);
  850. gbfwrite(&visible, 1, 1, file_out);
  851. gbfputint16(num_trail_points, file_out);
  852. gbfputint16(max_trail_size, file_out);
  853. gbfputint16(num_section_points, file_out);
  854. }
  855. }
  856. static void
  857. lowranceusr_merge_track_hdr_2(const route_head* trk)
  858. {
  859. continuous = 0;
  860. }
  861. static void
  862. data_write(void)
  863. {
  864. short int NumWaypoints, MajorVersion, MinorVersion, NumRoutes, NumTrails, NumIcons;
  865. setshort_length(mkshort_handle, 15);
  866. MajorVersion = writing_version;
  867. MinorVersion = 0;
  868. NumWaypoints = waypt_count();
  869. gbfputint16(MajorVersion, file_out);
  870. gbfputint16(MinorVersion, file_out);
  871. if (global_opts.debug_level >= 1) {
  872. printf(MYNAME " data_write: Num waypoints = %d\n", NumWaypoints);
  873. }
  874. if (writeasicons) {
  875. short zero = 0;
  876. gbfputint16(zero, file_out);
  877. } else {
  878. gbfputint16(NumWaypoints, file_out);
  879. waypt_disp_all(lowranceusr_waypt_pr);
  880. }
  881. /* Route support added 6/21/05 */
  882. NumRoutes = route_count();
  883. gbfputint16(NumRoutes, file_out);
  884. if (global_opts.debug_level >= 1) {
  885. printf(MYNAME " data_write: Num routes = %d\n", NumRoutes);
  886. }
  887. if (NumRoutes) {
  888. lowrance_route_count=0;
  889. route_disp_all(lowranceusr_route_hdr, NULL, lowranceusr_waypt_disp);
  890. }
  891. if (NumWaypoints && writeasicons) {
  892. gbfputint16(NumWaypoints, file_out);
  893. waypt_disp_all(lowranceusr_write_icon);
  894. } else {
  895. NumIcons = 0;
  896. gbfputint16(NumIcons, file_out);
  897. }
  898. /* Track support added 6/21/05 */
  899. NumTrails = track_count();
  900. if (NumTrails && merge) {
  901. NumTrails = 1;
  902. gbfputint16(NumTrails, file_out);
  903. trail_point_count = 0;
  904. trail_count = 0;
  905. /* count the number of total track points */
  906. track_disp_all(lowranceusr_merge_track_hdr, lowranceusr_merge_track_tlr, NULL);
  907. /* write out the new track header */
  908. trail_point_count = 0;
  909. track_disp_all(lowranceusr_merge_track_hdr_2, NULL, lowranceusr_track_disp);
  910. } else {
  911. gbfputint16(NumTrails, file_out);
  912. if (global_opts.debug_level >= 1) {
  913. printf(MYNAME " data_write: Num tracks = %d\n", NumTrails);
  914. }
  915. if (NumTrails) {
  916. trail_count=0;
  917. track_disp_all(lowranceusr_track_hdr, NULL, lowranceusr_track_disp);
  918. }
  919. }
  920. }
  921. ff_vecs_t lowranceusr_vecs = {
  922. ff_type_file,
  923. FF_CAP_RW_ALL,
  924. rd_init,
  925. wr_init,
  926. rd_deinit,
  927. wr_deinit,
  928. data_read,
  929. data_write,
  930. NULL,
  931. lowranceusr_args,
  932. CET_CHARSET_ASCII, 0 /* CET-REVIEW */
  933. };