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.

tmpro.cc 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. ----------------------------------------------------------------------------------------
  3. TopoMapPro (.txt)
  4. New Zealand Mapping Software
  5. www.topomappro.com
  6. (Tab Delimited text file)
  7. Based on gpsbabel .MXF format by Alex Mottram (geo_alexm at cox-internet.com)
  8. Tweaked for TopoMapPro by Nick Heaphy (nick at automata dot co dot nz)
  9. Group sID sDescription fLat fLong fEasting fNorthing fAlt iColour iSymbol sHyperLink
  10. 25 6 80 8 8 8 8 8 4 4 128 (lengths)
  11. Based on the specifications found in the TopoMapPro documentation available from website
  12. ----------------------------------------------------------------------------------------
  13. Copyright (C) 2002-2014 Robert Lipe, robertlipe+source@gpsbabel.org
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation; either version 2 of the License, or
  17. (at your option) any later version.
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program; if not, write to the Free Software
  24. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  25. */
  26. #include "defs.h"
  27. #include "cet_util.h"
  28. #include "csv_util.h"
  29. #include <stdlib.h>
  30. #define MYNAME "TMPro"
  31. static gbfile* file_in, *file_out;
  32. static short_handle mkshort_handle;
  33. static void
  34. rd_init(const QString& fname)
  35. {
  36. file_in = gbfopen(fname, "rb", MYNAME);
  37. }
  38. static void
  39. rd_deinit(void)
  40. {
  41. gbfclose(file_in);
  42. }
  43. static void
  44. wr_init(const QString& fname)
  45. {
  46. file_out = gbfopen(fname, "w", MYNAME);
  47. }
  48. static void
  49. wr_deinit(void)
  50. {
  51. gbfclose(file_out);
  52. }
  53. static void
  54. data_read(void)
  55. {
  56. char* buff;
  57. char* s;
  58. Waypoint* wpt_tmp;
  59. int i;
  60. int linecount = 0;
  61. while ((buff = gbfgetstr(file_in))) {
  62. if ((linecount++ == 0) && file_in->unicode) {
  63. cet_convert_init(CET_CHARSET_UTF8, 1);
  64. }
  65. /* skip the line if it contains "sHyperLink" as it is a header (I hope :) */
  66. if ((strlen(buff)) && (strstr(buff, "sHyperLink") == NULL)) {
  67. wpt_tmp = new Waypoint;
  68. /* data delimited by tabs, not enclosed in quotes. */
  69. s = buff;
  70. s = csv_lineparse(s, "\t", "", linecount);
  71. i = 0;
  72. while (s) {
  73. switch (i) {
  74. /* Group sID sDescription fLat fLong fEasting fNorthing fAlt iColour iSymbol sHyperLink */
  75. /* 0 1 2 3 4 5 6 7 8 9 10 */
  76. case 0:
  77. /* ignore: group */
  78. break;
  79. case 1:
  80. wpt_tmp->shortname = csv_stringtrim(s, "");
  81. break;
  82. case 2:
  83. /* Description is not a TopoMapPro format requirement.
  84. If we assign "" then .loc/.gpx will generate empty XML tags :(
  85. */
  86. wpt_tmp->description = csv_stringtrim(s, "");
  87. break;
  88. case 3:
  89. wpt_tmp->latitude = atof(s);
  90. break;
  91. case 4:
  92. wpt_tmp->longitude = atof(s);
  93. break;
  94. case 5:
  95. /* ignore: NZMapGrid Easting */
  96. break;
  97. case 6:
  98. /* ignore: NZMapGrid Northing */
  99. break;
  100. case 7:
  101. wpt_tmp->altitude = atof(s);
  102. break;
  103. case 8:
  104. /* ignore: color */
  105. break;
  106. case 9:
  107. /* ignore: symbol (non standard) */
  108. break;
  109. case 10:
  110. /* URL is not a TopoMapPro format requirement.
  111. You can store file links etc, we will discard anything that is not http
  112. (as URLs in TMPro must start "http:") as other GPS formats probably can't
  113. use the TopoMapLinks links.
  114. (plus discards length 0 strings (so no empty XML tags))
  115. */
  116. {
  117. QString link = csv_stringtrim(s, "");
  118. if (link.contains("http:")) {
  119. wpt_tmp->AddUrlLink(link);
  120. }
  121. }
  122. break;
  123. default:
  124. /* whoa! nelly */
  125. warning(MYNAME ": Warning: data fields on line %d exceed specification.\n",
  126. linecount);
  127. break;
  128. }
  129. i++;
  130. s = csv_lineparse(NULL, "\t", "\"", linecount);
  131. }
  132. if (i != 11) {
  133. delete wpt_tmp;
  134. warning(MYNAME ": WARNING - extracted %d fields from line %d. \nData on line ignored.\n",
  135. i, linecount);
  136. } else {
  137. waypt_add(wpt_tmp);
  138. }
  139. } else {
  140. /* empty line */
  141. }
  142. }
  143. }
  144. static void
  145. tmpro_waypt_pr(const Waypoint* wpt)
  146. {
  147. int icon = 1; /* default to "flag" */
  148. int colour = 255; /*default to red */
  149. QString shortname;
  150. QString description;
  151. if ((wpt->shortname.isEmpty()) || (global_opts.synthesize_shortnames)) {
  152. if (!wpt->description.isEmpty()) {
  153. if (global_opts.synthesize_shortnames) {
  154. shortname = mkshort_from_wpt(mkshort_handle, wpt);
  155. } else {
  156. shortname = csv_stringclean(wpt->description, ",\"");
  157. }
  158. } else {
  159. /* no description available */
  160. shortname = xstrdup("");
  161. }
  162. } else {
  163. shortname = csv_stringclean(wpt->shortname, ",\"");
  164. }
  165. if (wpt->description.isEmpty()) {
  166. if (!shortname.isEmpty()) {
  167. description = csv_stringclean(shortname, ",\"");
  168. } else {
  169. description = xstrdup("");
  170. }
  171. } else {
  172. description = csv_stringclean(wpt->description, ",\"");
  173. }
  174. /* Group sID sDescription fLat fLong fEasting fNorthing fAlt iColour iSymbol sHyperLink */
  175. /* 0 1 2 3 4 5 6 7 8 9 10 */
  176. /* Number of characters */
  177. /* 25 6 80 8 8 8 8 8 4 4 128 */
  178. const char* l = NULL;
  179. if (wpt->HasUrlLink()) {
  180. // Yes, it's lame to allocate/copy here.
  181. UrlLink link = wpt->GetUrlLink();
  182. l = xstrdup(link.url_);
  183. }
  184. gbfprintf(file_out, "new\t%.6s\t%.80s\t%08.6f\t%08.6f\t\t\t%.2f\t%d\t%d\t%.128s\n",
  185. CSTRc(shortname),
  186. CSTRc(description),
  187. wpt->latitude,
  188. wpt->longitude,
  189. wpt->altitude,
  190. colour,
  191. icon,
  192. l ? l : ""
  193. );
  194. if (l) {
  195. xfree(l);
  196. }
  197. }
  198. static void
  199. data_write(void)
  200. {
  201. /* Short names */
  202. if (global_opts.synthesize_shortnames) {
  203. mkshort_handle = mkshort_new_handle();
  204. setshort_length(mkshort_handle, 6);
  205. setshort_whitespace_ok(mkshort_handle, 0);
  206. setshort_badchars(mkshort_handle, "\",");
  207. }
  208. /* Write file header */
  209. gbfprintf(file_out, "Group\tsID\tsDescription\tfLat\tfLong\tfEasting\tfNorthing\tfAlt\tiColour\tiSymbol\tsHyperLink\n");
  210. waypt_disp_all(tmpro_waypt_pr);
  211. mkshort_del_handle(&mkshort_handle);
  212. }
  213. ff_vecs_t tmpro_vecs = {
  214. ff_type_file,
  215. FF_CAP_RW_WPT,
  216. rd_init,
  217. wr_init,
  218. rd_deinit,
  219. wr_deinit,
  220. data_read,
  221. data_write,
  222. NULL,
  223. NULL,
  224. CET_CHARSET_ASCII, 0 /* CET-REVIEW */
  225. };