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.

173 lines
4.7KB

  1. /*
  2. Copyright (C) 2016 Robert Lipe, robertlipe+source@gpsbabel.org
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  14. */
  15. #include "defs.h"
  16. #include <QtCore/QJsonDocument>
  17. #include <QtCore/QJsonObject>
  18. #include <QtCore/QJsonArray>
  19. #include "src/core/file.h"
  20. static gbfile* ofd;
  21. static const char MYNAME[] = "geojson";
  22. static char* compact_opt = NULL;
  23. static QJsonObject* track_object = NULL;
  24. static QJsonArray* track_coords = NULL;
  25. static arglist_t geojson_args[] = {
  26. {"compact", &compact_opt, "Compact Output. Default is off.",
  27. NULL, ARGTYPE_BOOL, ARG_NOMINMAX } ,
  28. ARG_TERMINATOR
  29. };
  30. static void
  31. geojson_rd_init(const QString& fname) {
  32. }
  33. QJsonArray* feature_collection = nullptr;
  34. static void
  35. geojson_wr_init(const QString& fname) {
  36. feature_collection = new QJsonArray;
  37. ofd = gbfopen(fname, "w", MYNAME);
  38. }
  39. static void
  40. geojson_waypt_pr(const Waypoint* waypoint) {
  41. QJsonObject object;
  42. static const QString kType = QStringLiteral("type");
  43. object[kType] = QStringLiteral("Feature");
  44. QJsonObject geometry;
  45. geometry[kType] = QStringLiteral("Point");
  46. QJsonArray coords;
  47. coords.append(waypoint->longitude);
  48. coords.append(waypoint->latitude);
  49. if (waypoint->altitude != unknown_alt && waypoint->altitude != 0) {
  50. coords.append(waypoint->altitude);
  51. }
  52. geometry[kType] = QStringLiteral("Point");
  53. geometry[QStringLiteral("coordinates")] = coords;
  54. object[QStringLiteral("geometry")] = geometry;
  55. // Build up the properties.
  56. QJsonObject properties;
  57. if (!waypoint->shortname.isEmpty()) {
  58. properties["name"] = waypoint->shortname;
  59. }
  60. if (!waypoint->description.isEmpty()) {
  61. properties["description"] = waypoint->description;
  62. }
  63. if (waypoint->HasUrlLink()) {
  64. UrlLink link = waypoint->GetUrlLink();
  65. if (!link.url_.isEmpty()) {
  66. properties["url"] = link.url_;
  67. }
  68. if (!link.url_link_text_.isEmpty()) {
  69. properties["urlname"] = link.url_link_text_;
  70. }
  71. }
  72. if (!properties.empty()) {
  73. object["properties"] = properties;
  74. }
  75. feature_collection->append(object);
  76. }
  77. static void
  78. geojson_rd_deinit() {
  79. }
  80. static void
  81. geojson_wr_deinit(void) {
  82. QJsonObject object;
  83. object[QStringLiteral("type")] = QStringLiteral("FeatureCollection");
  84. object[QStringLiteral("features")] = *feature_collection;
  85. QJsonDocument save(object);
  86. QJsonDocument::JsonFormat style;
  87. style = compact_opt ? QJsonDocument::Compact : QJsonDocument::Indented;
  88. gbfputs(save.toJson(style),ofd);
  89. gbfclose(ofd);
  90. ofd = NULL;
  91. delete feature_collection;
  92. feature_collection = nullptr;
  93. }
  94. static void
  95. geojson_read(void) {
  96. }
  97. static void geojson_track_hdr(const route_head* track) {
  98. track_object = new QJsonObject();
  99. (*track_object)[QStringLiteral("type")] = QStringLiteral("Feature");
  100. track_coords = new QJsonArray();
  101. QJsonObject properties;
  102. if (!track->rte_name.isEmpty()) {
  103. properties["name"] = track->rte_name;
  104. }
  105. (*track_object)["properties"] = properties;
  106. }
  107. static void geojson_track_disp(const Waypoint* trackpoint) {
  108. QJsonArray coords;
  109. coords.append(trackpoint->longitude);
  110. coords.append(trackpoint->latitude);
  111. if (trackpoint->altitude != unknown_alt && trackpoint->altitude != 0) {
  112. coords.append(trackpoint->altitude);
  113. }
  114. (*track_coords).append(coords);
  115. }
  116. static void geojson_track_tlr(const route_head* track) {
  117. QJsonObject geometry;
  118. geometry[QStringLiteral("type")] = QStringLiteral("LineString");
  119. geometry[QStringLiteral("coordinates")] = *track_coords;
  120. (*track_object)[QStringLiteral("geometry")] = geometry;
  121. feature_collection->append(*track_object);
  122. delete track_object;
  123. track_object = NULL;
  124. delete track_coords;
  125. track_coords = NULL;
  126. }
  127. static void
  128. geojson_write(void) {
  129. waypt_disp_all(geojson_waypt_pr);
  130. track_disp_all(geojson_track_hdr, geojson_track_tlr, geojson_track_disp);
  131. }
  132. ff_vecs_t geojson_vecs = {
  133. ff_type_file,
  134. { (ff_cap)(/*ff_cap_read | */ff_cap_write), ff_cap_write, ff_cap_none },
  135. geojson_rd_init,
  136. geojson_wr_init,
  137. geojson_rd_deinit,
  138. geojson_wr_deinit,
  139. geojson_read,
  140. geojson_write,
  141. NULL,
  142. geojson_args,
  143. CET_CHARSET_UTF8, 0 /* CET-REVIEW */
  144. };