This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
geojson_source.cpp
235 lines (193 loc) · 9.08 KB
/
geojson_source.cpp
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
#include <llmr/map/geojson_source.hpp>
#include <llmr/map/map.hpp>
#include <llmr/map/transform.hpp>
#include <llmr/renderer/line_bucket.hpp>
#include <llmr/util/std.hpp>
#include <llmr/util/string.hpp>
#include <cfloat>
#include <stdexcept>
using namespace llmr;
GeoJSONSource::GeoJSONSource(Map &map, Painter &painter, Texturepool &texturepool,
const char *url, std::vector<uint32_t> zooms, uint32_t tile_size,
uint32_t min_zoom, uint32_t max_zoom, bool enabled) :
Source(map, painter, texturepool, url, Type::geojson,
zooms, tile_size, min_zoom, max_zoom, enabled),
all_tiles(),
transforms() {
platform::request_http(url, [zooms, tile_size, &map, this](platform::Response *res) {
if (res->code == 200) {
// parse JSON
rapidjson::Document d = parseJSON(res->body.c_str());
// build transforms
for (uint8_t z = 0; z < zooms.size(); z++) {
std::shared_ptr<Transform> t = std::make_shared<Transform>();
transforms.push_back(t);
t->resize(tile_size, tile_size, map.getState().getPixelRatio(), tile_size, tile_size);
t->setZoom(zooms[z]);
};
// tile the features
tile_GeoJSON(d["routes"][(rapidjson::SizeType)0]["geometry"]);
// re-render
map.update();
} else {
fprintf(stderr, "failed to load GeoJSON\n");
}
}, map.getLoop());
}
rapidjson::Document GeoJSONSource::parseJSON(const char *data) {
rapidjson::Document d;
d.Parse<0>(data);
if (!d.IsObject() ||
!d.HasMember("routes") ||
!d["routes"].IsArray() ||
!d["routes"][(rapidjson::SizeType)0].HasMember("geometry")) {
throw std::runtime_error("malformed GeoJSON");
}
return d;
}
void GeoJSONSource::tile_GeoJSON(JSVal& geojson) {
for (uint8_t k = 0; k < transforms.size(); k++) {
std::shared_ptr<Transform>& transform = transforms[k];
// We should check for Feature/FeatureCollection; assume
// single Feature for now.
// if (geojson.type === 'FeatureCollection') {
// for (var i = 0; i < geojson.features.length; i++) {
// this._tileFeature(geojson.features[i], transform);
// }
//
// } else if (geojson.type === 'Feature') {
// this._tileFeature(geojson, transform);
//
// } else {
// throw('Unrecognized geojson type');
// }
tile_feature(geojson, transform);
}
// Update 'tiles' with renderable tile structures from the
// parsed feature tiles.
for (auto it = all_tiles.begin(); it != all_tiles.end(); it++) {
Tile::ID id = it->first;
GeoJSONTile feature_tile = it->second;
tiles.emplace_front(id);
Tile& new_tile = tiles.front();
const Tile::ID normalized_id = id.normalized();
new_tile.data = std::make_shared<TileData>(normalized_id, map, "", false);
// 1. assume route bucket (for now)
const BucketDescription& bucket_desc = map.getStyle().buckets.find("route")->second;
// 2. create line bucket
std::unique_ptr<LineBucket> bucket = std::make_unique<LineBucket>(new_tile.data->lineVertexBuffer, new_tile.data->triangleElementsBuffer, new_tile.data->pointElementsBuffer, bucket_desc);
// 3. add bucket features
for (Segment segment : feature_tile) {
bucket->addGeometry(segment);
}
// 4. add to parsed tiles
new_tile.data->buckets["route"] = std::move(bucket);
tile_data.push_front(new_tile.data);
new_tile.data->state = TileData::State::parsed;
}
all_tiles.clear();
transforms.clear();
}
void GeoJSONSource::tile_feature(JSVal& feature, std::shared_ptr<Transform>& transform) {
JSVal coords = feature["coordinates"];
JSVal geom_type = feature["type"];
std::map<Tile::ID, GeoJSONTile> tiled;
if (!strcmp(geom_type.GetString(), "Point")) {
fprintf(stderr, "TODO: Point GeoJSON\n");
// tiled = this._tileLineString([coords], transform);
} else if (!strcmp(geom_type.GetString(), "LineString") || strcmp(geom_type.GetString(), "MultiPoint")) {
tiled = tile_line_string(coords, transform);
} else if (!strcmp(geom_type.GetString(), "Polygon") || strcmp(geom_type.GetString(), "MultiLineString")) {
fprintf(stderr, "TODO: Polygon GeoJSON\n");
// tiled = {};
// for (var i = 0; i < coords.length; i++) {
// var tiled_ = this._tileLineString(coords[i], transform, type === 'Polygon');
// for (var tileID in tiled_) {
// if (!tiled[tileID]) tiled[tileID] = [];
// tiled[tileID] = (tiled[tileID] || []).concat(tiled_[tileID]);
// }
// }
} else if (!strcmp(geom_type.GetString(), "MultiPolygon")) {
fprintf(stderr, "TODO: MultiPolygon GeoJSON\n");
} else {
throw std::runtime_error("unrecognized geometry type");
}
for (auto it = tiled.begin(); it != tiled.end(); it++) {
all_tiles[it->first] = it->second;
}
}
std::map<Tile::ID, GeoJSONTile> GeoJSONSource::tile_line_string(JSVal coords, std::shared_ptr<Transform>& transform, bool rejoin) {
JSVal c = coords[(rapidjson::SizeType)0];
double c_lon = c[(rapidjson::SizeType)0].GetDouble();
double c_lat = c[(rapidjson::SizeType)1].GetDouble();
Transform::location_coordinate coord = transform->getLocationCoordinate(c_lon, c_lat);
Transform::location_coordinate prev_coord;
std::map<Tile::ID, GeoJSONTile> feature_tiles;
for (uint32_t i = 0; i < coords.Size(); i++) {
prev_coord = coord;
JSVal c = coords[i];
double c_lon = c[(rapidjson::SizeType)0].GetDouble();
double c_lat = c[(rapidjson::SizeType)1].GetDouble();
coord = transform->getLocationCoordinate(c_lon, c_lat);
double dx = (coord.column - prev_coord.column ? coord.column - prev_coord.column : DBL_MIN);
double dy = (coord.row - prev_coord.row ? coord.row - prev_coord.row : DBL_MIN);
double dir_x = dx / fabs(dx);
double dir_y = dy / fabs(dy);
// Find the rectangular bounding box, in tiles, of the polygon
double start_tile_x = floor(prev_coord.column - dir_x * padding);
double end_tile_x = floor(coord.column + dir_x * padding);
double start_tile_y = floor(prev_coord.row - dir_y * padding);
double end_tile_y = floor(coord.row + dir_y * padding);
// Iterate over all tiles the segment might intersect
// and split the segment across those tiles
for (int32_t x = start_tile_x; (x - end_tile_x) * dir_x <= 0; x += dir_x) {
double left_x = (x - padding - prev_coord.column) / dx;
double right_x = (x + 1 + padding - prev_coord.column) / dx;
for (int32_t y = start_tile_y; (y - end_tile_y) * dir_y <= 0; y += dir_y) {
double top_y = (y - padding - prev_coord.row) / dy;
double bottom_y = (y + 1 + padding - prev_coord.row) / dy;
// fraction of the distance along the segment at which the segment
// enters or exits the tile
double enter = fmax(fmin(left_x, right_x), fmin(top_y, bottom_y));
double exit = fmin(fmax(left_x, right_x), fmax(top_y, bottom_y));
Tile::ID id(floor(transform->getZoom()), x, y);
GeoJSONTile& tile = feature_tiles[id];
Coordinate point;
// segments starts outside the tile, add entry point
if (0 <= enter && enter < 1) {
point.x = ((prev_coord.column + enter * dx) - x) * tile_extent;
point.y = ((prev_coord.row + enter * dy) - y) * tile_extent;
// point.continues = true;
Segment segment;
segment.push_back(point);
tile.push_back(segment);
}
// segments ends outside the tile, add exit point
if (0 <= exit && exit < 1) {
point.x = ((prev_coord.column + exit * dx) - x) * tile_extent;
point.y = ((prev_coord.row + exit * dy) - y) * tile_extent;
// point.continues = true;
tile.back().push_back(point);
// add the point itself
} else {
point.x = (coord.column - x) * tile_extent;
point.y = (coord.row - y) * tile_extent;
if (!tile.size()) {
Segment segment;
segment.push_back(point);
tile.push_back(segment);
} else {
tile.back().push_back(point);
}
}
}
}
}
if (rejoin) {
// TODO
// reassemble the disconnected segments into a linestring
// sections of the linestring outside the tile are replaced with segments
// that follow the tile's edge
}
return feature_tiles;
}