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
/
geojsonvt.hpp
267 lines (188 loc) · 6.96 KB
/
geojsonvt.hpp
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
#ifndef MAPBOX_UTIL_GEOJSONVT
#define MAPBOX_UTIL_GEOJSONVT
#include <array>
#include <vector>
#include <map>
#include <string>
#include <ctime>
#include <rapidjson/document.h>
#include <mbgl/util/variant.hpp>
namespace mapbox { namespace util { namespace geojsonvt {
#pragma mark -
class Time {
public:
inline static void time(std::string activity) {
Time::activities[activity] = clock();
}
inline static void timeEnd(std::string activity) {
printf("%s: %fms\n", activity.c_str(), double(clock() - Time::activities[activity]) / (CLOCKS_PER_SEC / 1000));
}
private:
static std::map<std::string, clock_t> activities;
};
#pragma mark -
struct LonLat {
LonLat(std::array<double, 2> coordinates)
: lon(coordinates[0]), lat(coordinates[1]) {}
double lon;
double lat;
};
#pragma mark -
class ProjectedPoint;
class ProjectedGeometryContainer;
using ProjectedGeometry = mapbox::util::variant<geojsonvt::ProjectedPoint, geojsonvt::ProjectedGeometryContainer>;
#pragma mark -
class ProjectedPoint {
public:
ProjectedPoint(double x_, double y_, double z_)
: x(x_), y(y_), z(z_) {}
ProjectedPoint()
: x(-1), y(-1), z(-1) {}
inline bool isValid() const { return (x >= 0 && y >= 0 && z >= 0); }
inline bool isEqualToPoint(const ProjectedPoint *p2) const { return (x == p2->x && y == p2->y && z == p2->z); }
public:
double x = -1;
double y = -1;
double z = -1;
};
#pragma mark -
using JSDocument = rapidjson::Document;
using JSValue = rapidjson::Value;
#pragma mark -
class ProjectedGeometryContainer {
public:
ProjectedGeometryContainer() {}
ProjectedGeometryContainer(std::vector<ProjectedGeometry> members_)
: members(members_) {}
public:
std::vector<ProjectedGeometry> members;
double area = 0;
double dist = 0;
};
#pragma mark -
using Tags = std::map<std::string, std::string>;
#pragma mark -
enum class ProjectedFeatureType: uint8_t {
Point,
LineString,
Polygon
};
#pragma mark -
class ProjectedFeature {
public:
ProjectedFeature(ProjectedGeometry geometry_, ProjectedFeatureType type_, Tags tags_)
: geometry(geometry_), type(type_), tags(tags_) {}
public:
ProjectedGeometry geometry;
ProjectedFeatureType type;
Tags tags;
ProjectedPoint minPoint = ProjectedPoint(1, 1, 0);
ProjectedPoint maxPoint = ProjectedPoint(0, 0, 0);
};
#pragma mark -
class TilePoint;
class TileRing;
using TileGeometry = mapbox::util::variant<geojsonvt::TilePoint, geojsonvt::TileRing>;
#pragma mark -
class TilePoint {
public:
TilePoint(uint16_t x_, uint16_t y_)
: x(x_), y(y_) {}
public:
const uint16_t x = 0;
const uint16_t y = 0;
};
#pragma mark -
class TileRing {
public:
std::vector<TilePoint> points;
};
#pragma mark -
typedef ProjectedFeatureType TileFeatureType;
#pragma mark -
class TileFeature {
public:
TileFeature(std::vector<TileGeometry> geometry_, TileFeatureType type_, Tags tags_)
: geometry(geometry_), type(type_), tags(tags_) {}
public:
std::vector<TileGeometry> geometry;
TileFeatureType type;
Tags tags;
};
#pragma mark -
class Tile {
public:
static Tile createTile(std::vector<ProjectedFeature> features, uint8_t z2, uint8_t tx, uint8_t ty, double tolerance, uint16_t extent, bool noSimplify);
static void addFeature(Tile &tile, ProjectedFeature feature, uint8_t z2, uint8_t tx, uint8_t ty, double tolerance, uint16_t extent, bool noSimplify);
inline operator bool() const { return this->numPoints > 0; }
private:
static TilePoint transformPoint(const ProjectedPoint &p, uint8_t z2, uint8_t tx, uint8_t ty, uint16_t extent);
public:
std::vector<TileFeature> features;
uint32_t numPoints = 0;
uint32_t numSimplified = 0;
uint32_t numFeatures = 0;
std::vector<ProjectedFeature> source;
};
#pragma mark -
class GeoJSONVT {
public:
GeoJSONVT(const std::string &data, uint8_t baseZoom = 14, uint8_t maxZoom = 4, uint32_t maxPoints = 100, double tolerance = 3, bool debug = true);
Tile& getTile(uint8_t z, uint8_t x, uint8_t y);
private:
void splitTile(std::vector<ProjectedFeature> features, uint8_t z, uint8_t x, uint8_t y, int8_t cz = -1, int8_t cx = -1, int8_t cy = -1);
bool isClippedSquare(const std::vector<TileFeature> features, uint16_t extent, uint8_t buffer) const;
static uint64_t toID(uint32_t z, uint32_t x, uint32_t y);
static ProjectedPoint intersectX(const ProjectedPoint &a, const ProjectedPoint &b, double x);
static ProjectedPoint intersectY(const ProjectedPoint &a, const ProjectedPoint &b, double y);
struct FeatureStackItem {
std::vector<ProjectedFeature> features;
uint8_t z;
uint8_t x;
uint8_t y;
FeatureStackItem(std::vector<ProjectedFeature> features_, uint8_t z_, uint8_t x_, uint8_t y_)
: features(features_), z(z_), x(x_), y(y_) {}
};
private:
uint8_t baseZoom;
uint8_t maxZoom;
uint32_t maxPoints;
double tolerance;
bool debug;
uint16_t extent = 4096;
uint8_t buffer = 64;
std::map<uint64_t, Tile> tiles;
std::map<std::string, uint8_t> stats;
uint16_t total = 0;
};
#pragma mark -
class Convert {
public:
static std::vector<ProjectedFeature> convert(const JSDocument &data, double tolerance);
private:
static void convertFeature(std::vector<ProjectedFeature> &features, const JSValue &feature, double tolerance);
static ProjectedFeature create(Tags tags, ProjectedFeatureType type, ProjectedGeometry geometry);
static ProjectedGeometryContainer project(const std::vector<LonLat> &lonlats, double tolerance = 0);
static ProjectedPoint projectPoint(const LonLat &p);
static void calcSize(ProjectedGeometryContainer &geometryContainer);
static void calcBBox(ProjectedFeature &feature);
static void calcRingBBox(ProjectedPoint &minPoint, ProjectedPoint &maxPoint, const ProjectedGeometryContainer &geometry);
};
#pragma mark -
class Simplify {
public:
static void simplify(ProjectedGeometryContainer &points, double tolerance);
private:
static double getSqSegDist(const ProjectedPoint &p, const ProjectedPoint &a, const ProjectedPoint &b);
};
#pragma mark -
class Clip {
public:
static std::vector<ProjectedFeature> clip(std::vector<ProjectedFeature> features, uint8_t scale, double k1, double k2, uint8_t axis, ProjectedPoint (*intersect)(const ProjectedPoint&, const ProjectedPoint&, double));
private:
static ProjectedGeometryContainer clipPoints(ProjectedGeometryContainer geometry, double k1, double k2, uint8_t axis);
static ProjectedGeometryContainer clipGeometry(ProjectedGeometryContainer geometry, double k1, double k2, uint8_t axis, ProjectedPoint (*intersect)(const ProjectedPoint&, const ProjectedPoint&, double), bool closed);
static ProjectedGeometryContainer newSlice(ProjectedGeometryContainer &slices, ProjectedGeometryContainer &slice, double area, double dist);
};
} /* namespace geojsonvt */ } /* namespace util */ } /* namespace mapbox */
#endif // MAPBOX_UTIL_GEOJSONVT