FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
geojson-vt-cpp/debug/debug.cpp at master · mapbox/geojson-vt-cpp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Jun 4, 2026. It is now read-only.
mapbox
/
geojson-vt-cpp
Public archive
Notifications
You must be signed in to change notification settings
Fork
64
Star
106
Code
Issues
4
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
geojson-vt-cpp
/
debug
/
debug.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
275 lines (226 loc) · 8.3 KB
Breadcrumbs
geojson-vt-cpp
/
debug
/
debug.cpp
Copy path
File metadata and controls
275 lines (226 loc) · 8.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
../bench/util.hpp
"
#
include
<
GLFW/glfw3.h
>
#
include
<
mapbox/geojson.hpp
>
#
include
<
mapbox/geojson_impl.hpp
>
#
include
<
mapbox/geojsonvt.hpp
>
#
include
<
memory
>
using
namespace
mapbox
::geojsonvt
;
template
<
typename
T>
void
drawLine
(
const
T points) {
glColor4f
(
1
,
0
,
0
,
1
);
glBegin
(
GL_LINE_STRIP
);
for
(
const
auto
& pt : points) {
glVertex2s
(pt.
x
, pt.
y
);
}
glEnd
();
}
struct
DrawFeature
{
void
operator
()(
const
mapbox::geometry::empty&) {}
void
operator
()(
const
mapbox::geometry::point<
int16_t
>&) {}
void
operator
()(
const
mapbox::geometry::line_string<
int16_t
>& points) {
drawLine
(points);
}
void
operator
()(
const
mapbox::geometry::linear_ring<
int16_t
>& points) {
drawLine
(points);
}
void
operator
()(
const
mapbox::geometry::geometry<
int16_t
>& geometry) {
mapbox::geometry::geometry<
int16_t
>::
visit
(geometry, DrawFeature{});
}
template
<
class
T
>
void
operator
()(
const
T& vector) {
for
(
const
auto
& e : vector) {
operator
()(e);
}
}
};
int
main
() {
if
(
glfwInit
() ==
0
) {
return
-
1
;
}
static
int
width =
768
;
static
int
fbWidth = width;
static
int
height =
768
;
static
int
fbHeight = height;
GLFWwindow* window =
glfwCreateWindow
(width, height,
"
GeoJSON VT — Drop a GeoJSON file
"
,
nullptr
,
nullptr
);
if
(window ==
nullptr
) {
glfwTerminate
();
return
-
1
;
}
static
std::unique_ptr<GeoJSONVT> vt;
static
bool
dirty =
true
;
static
struct
TileID
{
int
z;
int
x;
int
y;
} pos = {
0
,
0
,
0
};
enum
class
Horizontal
{ Left, Right, Outside };
enum
class
Vertical
{ Top, Bottom, Outside };
static
Horizontal horizontal = Horizontal::Outside;
static
Vertical vertical = Vertical::Outside;
Tile* tile =
nullptr
;
static
const
auto
updateTile = [&] {
const
std::string name =
std::to_string
(pos.
z
) +
"
/
"
+
std::to_string
(pos.
x
) +
"
/
"
+
std::to_string
(pos.
y
);
if
(vt) {
Timer tileTimer;
tile =
const_cast
<Tile*>(&vt->
getTile
(pos.
z
, pos.
x
, pos.
y
));
tileTimer
(
"
tile
"
+ name);
glfwSetWindowTitle
(window, (std::string{
"
GeoJSON VT —
"
} + name).
c_str
());
}
dirty =
true
;
};
static
const
auto
loadGeoJSON = [&](
const
std::string& filename) {
Timer timer;
const
std::string data =
loadFile
(filename);
timer
(
"
loadFile
"
);
const
auto
features =
mapbox::geojson::parse
(data).
get
<mapbox::geojson::feature_collection>();
timer
(
"
parse into geometry
"
);
vt = std::make_unique<GeoJSONVT>(features);
timer
(
"
generate tile index
"
);
updateTile
();
};
static
const
auto
updateLocation = [](
const
Horizontal newHorizontal,
const
Vertical newVertical) {
if
(newHorizontal != horizontal || newVertical != vertical) {
dirty =
true
;
horizontal = newHorizontal;
vertical = newVertical;
}
};
glfwSetDropCallback
(window, [](GLFWwindow*,
int
count,
const
char
* name[]) {
if
(count >=
1
) {
loadGeoJSON
(name[
0
]);
}
});
glfwSetKeyCallback
(
window, [](GLFWwindow* w,
const
int
key,
const
int
,
const
int
action,
const
int
) {
if
(key ==
GLFW_KEY_Q
&& action ==
GLFW_RELEASE
) {
glfwSetWindowShouldClose
(w,
1
);
}
if
((key ==
GLFW_KEY_BACKSPACE
|| key ==
GLFW_KEY_ESCAPE
) && action ==
GLFW_RELEASE
) {
//
zoom out
if
(pos.
z
>
0
) {
pos.
z
--;
pos.
x
/=
2
;
pos.
y
/=
2
;
updateTile
();
}
}
});
glfwSetWindowSizeCallback
(window, [](GLFWwindow*,
const
int
w,
const
int
h) {
width = w;
height = h;
dirty =
true
;
});
glfwSetFramebufferSizeCallback
(window, [](GLFWwindow*,
const
int
w,
const
int
h) {
fbWidth = w;
fbHeight = h;
});
glfwSetCursorPosCallback
(window, [](GLFWwindow*,
const
double
x,
const
double
y) {
updateLocation
((x >
0
&& x < width) ? (x *
2
< width ? Horizontal::Left : Horizontal::Right)
: Horizontal::Outside,
(y >
0
&& y < height) ? (y *
2
< height ? Vertical::Top : Vertical::Bottom)
: Vertical::Outside);
});
glfwSetCursorEnterCallback
(window, [](GLFWwindow*,
int
entered) {
if
(entered ==
0
) {
updateLocation
(Horizontal::Outside, Vertical::Outside);
}
});
glfwSetMouseButtonCallback
(window, [](GLFWwindow*,
int
button,
int
action,
int
) {
if
(button ==
GLFW_MOUSE_BUTTON_1
&& action ==
GLFW_RELEASE
) {
//
zoom into a particular tile
if
(pos.
z
<
18
&& horizontal != Horizontal::Outside && vertical != Vertical::Outside) {
pos.
z
++;
pos.
x
*=
2
;
pos.
y
*=
2
;
if
(horizontal == Horizontal::Right) {
pos.
x
++;
}
if
(vertical == Vertical::Bottom) {
pos.
y
++;
}
updateTile
();
}
}
else
if
(button ==
GLFW_MOUSE_BUTTON_2
&& action ==
GLFW_RELEASE
) {
//
zoom out
if
(pos.
z
>
0
) {
pos.
z
--;
pos.
x
/=
2
;
pos.
y
/=
2
;
updateTile
();
}
}
});
glfwGetFramebufferSize
(window, &fbWidth, &fbHeight);
glfwMakeContextCurrent
(window);
const
auto
drawTile = [](
bool
active) {
if
(active) {
glLineWidth
(
2
);
glColor4f
(
0
,
0
,
1
,
1
);
}
else
{
glLineWidth
(
1
);
glColor4f
(
0
,
1
,
0
,
1
);
}
glBegin
(
GL_LINE_STRIP
);
glVertex3f
(
0
,
0
, -
static_cast
<
int
>(active));
glVertex3f
(
0
,
4096
, -
static_cast
<
int
>(active));
glVertex3f
(
4096
,
4096
, -
static_cast
<
int
>(active));
glVertex3f
(
4096
,
0
, -
static_cast
<
int
>(active));
glVertex3f
(
0
,
0
, -
static_cast
<
int
>(active));
glEnd
();
};
glMatrixMode
(
GL_MODELVIEW
);
glLoadIdentity
();
glEnable
(
GL_DEPTH_TEST
);
loadGeoJSON
(
"
data/countries.geojson
"
);
while
(
glfwWindowShouldClose
(window) ==
0
) {
if
(dirty) {
dirty =
false
;
if
(vt) {
glClearColor
(
1
,
1
,
1
,
1
);
}
else
{
glClearColor
(
0.8
,
0.8
,
0.8
,
1
);
}
glClear
(
GL_COLOR_BUFFER_BIT
|
GL_DEPTH_BUFFER_BIT
);
glViewport
(
0
,
0
, fbWidth, fbHeight);
if
(tile !=
nullptr
) {
//
main tile
glMatrixMode
(
GL_PROJECTION
);
glLoadIdentity
();
glOrtho
(-
128
,
4096
+
128
,
4096
+
128
, -
128
,
10
, -
10
);
glLineWidth
(
1
);
for
(
const
auto
& feature : tile->
features
) {
mapbox::geometry::geometry<
int16_t
>::
visit
(feature.
geometry
, DrawFeature{});
}
//
top left
glMatrixMode
(
GL_PROJECTION
);
glLoadIdentity
();
glOrtho
(-
256
,
8192
+
256
,
8192
+
256
, -
256
,
10
, -
10
);
drawTile
(horizontal == Horizontal::Left && vertical == Vertical::Top);
//
top right
glMatrixMode
(
GL_PROJECTION
);
glLoadIdentity
();
glOrtho
(-
4096
-
256
,
8192
+
256
-
4096
,
8192
+
256
, -
256
,
10
, -
10
);
drawTile
(horizontal == Horizontal::Right && vertical == Vertical::Top);
//
bottom left
glMatrixMode
(
GL_PROJECTION
);
glLoadIdentity
();
glOrtho
(-
256
,
8192
+
256
,
8192
+
256
-
4096
, -
256
-
4096
,
10
, -
10
);
drawTile
(horizontal == Horizontal::Left && vertical == Vertical::Bottom);
//
bottom right
glMatrixMode
(
GL_PROJECTION
);
glLoadIdentity
();
glOrtho
(-
4096
-
256
,
8192
+
256
-
4096
,
8192
+
256
-
4096
, -
256
-
4096
,
10
, -
10
);
drawTile
(horizontal == Horizontal::Right && vertical == Vertical::Bottom);
}
else
{
}
glfwSwapBuffers
(window);
}
glfwWaitEvents
();
}
glfwTerminate
();
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL