-
Notifications
You must be signed in to change notification settings - Fork 128
/
geojson.cc
287 lines (255 loc) · 8.8 KB
/
geojson.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
Copyright (C) 2016 Robert Lipe, [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <QByteArray> // for QByteArray
#include <QIODevice> // for operator|, QIODevice, QIODevice::ReadOnly, QIODevice::Text
#include <QJsonArray> // for QJsonArray
#include <QJsonDocument> // for QJsonDocument, QJsonDocument::Compact, QJsonDocument::Indented, QJsonDocument::JsonFormat
#include <QJsonObject> // for QJsonObject
#include <QJsonParseError> // for QJsonParseError
#include <QJsonValue> // for QJsonValue
#include <QJsonValueRef> // for QJsonValueRef
#include "defs.h"
#include "geojson.h"
#include "src/core/file.h" // for File
#include "src/core/logging.h" // for Fatal
void
GeoJsonFormat::rd_init(const QString& fname)
{
ifd = new gpsbabel::File(fname);
ifd->open(QIODevice::ReadOnly | QIODevice::Text);
}
void
GeoJsonFormat::wr_init(const QString& fname)
{
feature_collection = new QJsonArray;
ofd = new gpsbabel::File(fname);
ofd->open(QIODevice::WriteOnly);
}
void
GeoJsonFormat::geojson_waypt_pr(const Waypoint* waypoint) const
{
QJsonObject geometry;
geometry[TYPE] = POINT;
QJsonArray coordinates;
coordinates.append(waypoint->longitude);
coordinates.append(waypoint->latitude);
if (waypoint->altitude != unknown_alt && waypoint->altitude != 0) {
coordinates.append(waypoint->altitude);
}
geometry[COORDINATES] = coordinates;
QJsonObject feature;
feature[TYPE] = FEATURE;
feature[GEOMETRY] = geometry;
// Build up the properties.
QJsonObject properties;
if (!waypoint->shortname.isEmpty()) {
properties[NAME] = waypoint->shortname;
}
if (!waypoint->description.isEmpty()) {
properties[DESCRIPTION] = waypoint->description;
}
if (waypoint->HasUrlLink()) {
const UrlLink& link = waypoint->GetUrlLink();
if (!link.url_.isEmpty()) {
properties[URL] = link.url_;
}
if (!link.url_link_text_.isEmpty()) {
properties[URLNAME] = link.url_link_text_;
}
}
if (!properties.empty()) {
feature[PROPERTIES] = properties;
}
feature_collection->append(feature);
}
void
GeoJsonFormat::rd_deinit()
{
ifd->close();
delete ifd;
ifd = nullptr;
}
void
GeoJsonFormat::wr_deinit()
{
QJsonObject object;
object[TYPE] = FEATURE_COLLECTION;
object[FEATURES] = *feature_collection;
QJsonDocument save(object);
QJsonDocument::JsonFormat style = compact_opt ? QJsonDocument::Compact : QJsonDocument::Indented;
ofd->write(save.toJson(style));
ofd->close();
delete ofd;
ofd = nullptr;
delete feature_collection;
feature_collection = nullptr;
}
Waypoint*
GeoJsonFormat::waypoint_from_coordinates(const QJsonArray& coordinates)
{
auto* waypoint = new Waypoint();
waypoint->latitude = coordinates.at(1).toDouble();
waypoint->longitude = coordinates.at(0).toDouble();
if (coordinates.size() > 2) {
waypoint->altitude = coordinates.at(3).toDouble();
}
return waypoint;
}
void
GeoJsonFormat::routes_from_polygon_coordinates(const QJsonArray& polygon)
{
for (auto&& lineStringIterator : polygon) {
QJsonArray coordinates = (lineStringIterator).toArray();
auto* route = new route_head;
route_add_head(route);
for (auto&& coordinate : coordinates) {
auto* waypoint = waypoint_from_coordinates(coordinate.toArray());
route_add_wpt(route, waypoint);
}
}
}
void
GeoJsonFormat::read()
{
QString file_content = ifd->readAll();
QJsonParseError error{};
QJsonDocument document = QJsonDocument::fromJson(file_content.toUtf8(), &error);
if (error.error != QJsonParseError::NoError) {
gbFatal(FatalMsg().nospace() << "GeoJSON parse error in " << ifd->fileName() << ": " << error.errorString());
}
QJsonObject rootObject = document.object();
if (rootObject[TYPE] != FEATURE_COLLECTION) {
return;
}
QJsonArray features = rootObject.value(FEATURES).toArray();
for (auto&& iterator : features) {
QJsonObject feature = iterator.toObject();
QJsonObject properties = (feature.value(PROPERTIES)).toObject();
QString name;
QString description;
if (!properties.empty()) {
if (properties.contains(NAME)) {
name = properties[NAME].toString();
}
if (properties.contains(DESCRIPTION)) {
description = properties[DESCRIPTION].toString();
}
}
QJsonObject geometry = feature.value(GEOMETRY).toObject();
auto geometry_type = geometry[TYPE];
if (geometry_type == POINT) {
QJsonArray coordinates = geometry.value(COORDINATES).toArray();
auto* waypoint = waypoint_from_coordinates(coordinates);
waypoint->shortname = name;
waypoint->description = description;
if (properties.contains(URL)) {
QString url = properties[URL].toString();
if (properties.contains(URLNAME)) {
QString url_text = properties[URLNAME].toString();
waypoint->AddUrlLink(UrlLink(url, url_text));
} else {
waypoint->AddUrlLink(UrlLink(url));
}
}
waypt_add(waypoint);
} else if (geometry_type == MULTIPOINT) {
QJsonArray coordinates = geometry.value(COORDINATES).toArray();
for (auto&& coordinate : coordinates) {
auto* waypoint = waypoint_from_coordinates(coordinate.toArray());
waypt_add(waypoint);
}
} else if (geometry_type == LINESTRING) {
QJsonArray coordinates = geometry.value(COORDINATES).toArray();
auto* route = new route_head;
route->rte_name = name;
route_add_head(route);
for (auto&& coordinate : coordinates) {
auto* waypoint = waypoint_from_coordinates(coordinate.toArray());
route_add_wpt(route, waypoint);
}
} else if (geometry_type == POLYGON) {
QJsonArray polygon = geometry.value(COORDINATES).toArray();
routes_from_polygon_coordinates(polygon);
} else if (geometry_type == MULTIPOLYGON) {
QJsonArray polygons = geometry.value(COORDINATES).toArray();
for (auto&& polygons_iterator : polygons) {
QJsonArray polygon = polygons_iterator.toArray();
routes_from_polygon_coordinates(polygon);
}
} else if (geometry_type == MULTILINESTRING) {
QJsonArray line_strings = geometry.value(COORDINATES).toArray();
for (auto&& line_string : line_strings) {
QJsonArray coordinates = line_string.toArray();
auto* route = new route_head;
track_add_head(route);
for (auto&& coordinate : coordinates) {
auto* waypoint = waypoint_from_coordinates(coordinate.toArray());
route_add_wpt(route, waypoint);
}
}
}
}
}
void GeoJsonFormat::geojson_track_hdr(const route_head* track)
{
track_object = new QJsonObject();
(*track_object)[TYPE] = FEATURE;
track_coords = new QJsonArray();
QJsonObject properties;
if (!track->rte_name.isEmpty()) {
properties[NAME] = track->rte_name;
}
(*track_object)[PROPERTIES] = properties;
}
void GeoJsonFormat::geojson_track_disp(const Waypoint* trackpoint) const
{
QJsonArray coords;
coords.append(trackpoint->longitude);
coords.append(trackpoint->latitude);
if (trackpoint->altitude != unknown_alt && trackpoint->altitude != 0) {
coords.append(trackpoint->altitude);
}
track_coords->append(coords);
}
void GeoJsonFormat::geojson_track_tlr(const route_head* /*unused*/)
{
QJsonObject geometry;
geometry[TYPE] = LINESTRING;
geometry[COORDINATES] = *track_coords;
(*track_object)[GEOMETRY] = geometry;
feature_collection->append(*track_object);
delete track_object;
track_object = nullptr;
delete track_coords;
track_coords = nullptr;
}
void
GeoJsonFormat::write()
{
auto geojson_waypt_pr_lambda = [this](const Waypoint* waypointp)->void {
geojson_waypt_pr(waypointp);
};
waypt_disp_all(geojson_waypt_pr_lambda);
auto geojson_track_hdr_lambda = [this](const route_head* rte)->void {
geojson_track_hdr(rte);
};
auto geojson_track_tlr_lambda = [this](const route_head* rte)->void {
geojson_track_tlr(rte);
};
auto geojson_track_disp_lambda = [this](const Waypoint* waypointp)->void {
geojson_track_disp(waypointp);
};
track_disp_all(geojson_track_hdr_lambda, geojson_track_tlr_lambda, geojson_track_disp_lambda);
}