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
/
map.cpp
513 lines (424 loc) · 16.1 KB
/
map.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#include <mbgl/annotation/annotation_manager.hpp>
#include <mbgl/layermanager/layer_manager.hpp>
#include <mbgl/map/camera.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/map/map_impl.hpp>
#include <mbgl/map/transform.hpp>
#include <mbgl/math/log2.hpp>
#include <mbgl/renderer/renderer_frontend.hpp>
#include <mbgl/renderer/renderer_observer.hpp>
#include <mbgl/renderer/update_parameters.hpp>
#include <mbgl/storage/file_source_manager.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/style/observer.hpp>
#include <mbgl/style/style_impl.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/exception.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/util/mapbox.hpp>
#include <mbgl/util/math.hpp>
#include <mbgl/util/tile_coordinate.hpp>
#include <utility>
namespace mbgl {
using namespace style;
Map::Map(RendererFrontend& frontend,
MapObserver& observer,
const MapOptions& mapOptions,
const ResourceOptions& resourceOptions)
: impl(std::make_unique<Impl>(
frontend,
observer,
FileSourceManager::get()
? std::shared_ptr<FileSource>(FileSourceManager::get()->getFileSource(ResourceLoader, resourceOptions))
: nullptr,
mapOptions)) {}
Map::Map(std::unique_ptr<Impl> impl_) : impl(std::move(impl_)) {}
Map::~Map() = default;
void Map::renderStill(StillImageCallback callback) {
if (!callback) {
Log::Error(Event::General, "StillImageCallback not set");
return;
}
if (impl->mode != MapMode::Static && impl->mode != MapMode::Tile) {
callback(std::make_exception_ptr(util::MisuseException("Map is not in static or tile image render modes")));
return;
}
if (impl->stillImageRequest) {
callback(std::make_exception_ptr(util::MisuseException("Map is currently rendering an image")));
return;
}
if (impl->style->impl->getLastError()) {
callback(impl->style->impl->getLastError());
return;
}
impl->stillImageRequest = std::make_unique<StillImageRequest>(std::move(callback));
impl->onUpdate();
}
void Map::renderStill(const CameraOptions& camera, MapDebugOptions debugOptions, StillImageCallback callback) {
impl->cameraMutated = true;
impl->debugOptions = debugOptions;
impl->transform.jumpTo(camera);
renderStill(std::move(callback));
}
void Map::triggerRepaint() {
impl->onUpdate();
}
#pragma mark - Style
style::Style& Map::getStyle() {
return *impl->style;
}
const style::Style& Map::getStyle() const {
return *impl->style;
}
void Map::setStyle(std::unique_ptr<Style> style) {
assert(style);
impl->onStyleLoading();
impl->style = std::move(style);
if (LayerManager::annotationsEnabled) {
impl->annotationManager.setStyle(*impl->style);
}
}
#pragma mark - Transitions
void Map::cancelTransitions() {
impl->transform.cancelTransitions();
impl->onUpdate();
}
void Map::setGestureInProgress(bool inProgress) {
impl->transform.setGestureInProgress(inProgress);
impl->onUpdate();
}
bool Map::isGestureInProgress() const {
return impl->transform.isGestureInProgress();
}
bool Map::isRotating() const {
return impl->transform.isRotating();
}
bool Map::isScaling() const {
return impl->transform.isScaling();
}
bool Map::isPanning() const {
return impl->transform.isPanning();
}
#pragma mark -
CameraOptions Map::getCameraOptions(const optional<EdgeInsets>& padding) const {
return impl->transform.getCameraOptions(padding);
}
void Map::jumpTo(const CameraOptions& camera) {
impl->jumpTo(camera);
}
void Map::easeTo(const CameraOptions& camera, const AnimationOptions& animation) {
impl->cameraMutated = true;
impl->transform.easeTo(camera, animation);
impl->onUpdate();
}
void Map::flyTo(const CameraOptions& camera, const AnimationOptions& animation) {
impl->cameraMutated = true;
impl->transform.flyTo(camera, animation);
impl->onUpdate();
}
void Map::moveBy(const ScreenCoordinate& point, const AnimationOptions& animation) {
impl->cameraMutated = true;
impl->transform.moveBy(point, animation);
impl->onUpdate();
}
void Map::pitchBy(double pitch, const AnimationOptions& animation) {
easeTo(CameraOptions().withPitch((impl->transform.getPitch() * util::RAD2DEG) - pitch), animation);
}
void Map::scaleBy(double scale, const optional<ScreenCoordinate>& anchor, const AnimationOptions& animation) {
const double zoom = impl->transform.getZoom() + impl->transform.getState().scaleZoom(scale);
easeTo(CameraOptions().withZoom(zoom).withAnchor(anchor), animation);
}
void Map::rotateBy(const ScreenCoordinate& first, const ScreenCoordinate& second, const AnimationOptions& animation) {
impl->cameraMutated = true;
impl->transform.rotateBy(first, second, animation);
impl->onUpdate();
}
CameraOptions Map::cameraForLatLngBounds(const LatLngBounds& bounds,
const EdgeInsets& padding,
const optional<double>& bearing,
const optional<double>& pitch) const {
return cameraForLatLngs(
{
bounds.northwest(),
bounds.southwest(),
bounds.southeast(),
bounds.northeast(),
},
padding,
bearing,
pitch);
}
CameraOptions cameraForLatLngs(const std::vector<LatLng>& latLngs, const Transform& transform, const EdgeInsets& padding) {
if (latLngs.empty()) {
return {};
}
Size size = transform.getState().getSize();
// Calculate the bounds of the possibly rotated shape with respect to the viewport.
ScreenCoordinate nePixel = {-INFINITY, -INFINITY};
ScreenCoordinate swPixel = {INFINITY, INFINITY};
for (LatLng latLng : latLngs) {
ScreenCoordinate pixel = transform.latLngToScreenCoordinate(latLng);
swPixel.x = std::min(swPixel.x, pixel.x);
nePixel.x = std::max(nePixel.x, pixel.x);
swPixel.y = std::min(swPixel.y, pixel.y);
nePixel.y = std::max(nePixel.y, pixel.y);
}
double width = nePixel.x - swPixel.x;
double height = nePixel.y - swPixel.y;
// Calculate the zoom level.
double minScale = INFINITY;
if (width > 0 || height > 0) {
double scaleX = double(size.width) / width;
double scaleY = double(size.height) / height;
scaleX -= (padding.left() + padding.right()) / width;
scaleY -= (padding.top() + padding.bottom()) / height;
minScale = util::min(scaleX, scaleY);
}
double zoom = transform.getZoom();
if (minScale > 0) {
zoom = util::clamp(zoom + util::log2(minScale), transform.getState().getMinZoom(), transform.getState().getMaxZoom());
} else {
Log::Error(Event::General, "Unable to calculate appropriate zoom level for bounds. Vertical or horizontal padding is greater than map's height or width.");
}
// Calculate the center point of a virtual bounds that is extended in all directions by padding.
ScreenCoordinate centerPixel = nePixel + swPixel;
centerPixel /= 2.0;
return CameraOptions()
.withCenter(transform.screenCoordinateToLatLng(centerPixel))
.withPadding(padding)
.withZoom(zoom);
}
CameraOptions Map::cameraForLatLngs(const std::vector<LatLng>& latLngs,
const EdgeInsets& padding,
const optional<double>& bearing,
const optional<double>& pitch) const {
if (!bearing && !pitch) {
return mbgl::cameraForLatLngs(latLngs, impl->transform, padding);
}
Transform transform(impl->transform.getState());
if (bearing || pitch) {
transform.jumpTo(CameraOptions().withBearing(bearing).withPitch(pitch));
}
return mbgl::cameraForLatLngs(latLngs, transform, padding)
.withBearing(-transform.getBearing() * util::RAD2DEG)
.withPitch(transform.getPitch() * util::RAD2DEG);
}
CameraOptions Map::cameraForGeometry(const Geometry<double>& geometry,
const EdgeInsets& padding,
const optional<double>& bearing,
const optional<double>& pitch) const {
std::vector<LatLng> latLngs;
forEachPoint(geometry, [&](const Point<double>& pt) {
latLngs.emplace_back(pt.y, pt.x);
});
return cameraForLatLngs(latLngs, padding, bearing, pitch);
}
LatLngBounds Map::latLngBoundsForCamera(const CameraOptions& camera) const {
Transform shallow { impl->transform.getState() };
Size size = shallow.getState().getSize();
shallow.jumpTo(camera);
return LatLngBounds::hull(
shallow.screenCoordinateToLatLng({}),
shallow.screenCoordinateToLatLng({ double(size.width), double(size.height) })
);
}
LatLngBounds Map::latLngBoundsForCameraUnwrapped(const CameraOptions& camera) const {
Transform shallow{impl->transform.getState()};
Size size = shallow.getState().getSize();
shallow.jumpTo(camera);
LatLng nw = shallow.screenCoordinateToLatLng({});
LatLng se = shallow.screenCoordinateToLatLng({double(size.width), double(size.height)});
LatLng ne = shallow.screenCoordinateToLatLng({double(size.width), 0.0});
LatLng sw = shallow.screenCoordinateToLatLng({0.0, double(size.height)});
LatLng center = shallow.screenCoordinateToLatLng({double(size.width) / 2, double(size.height) / 2});
nw.unwrapForShortestPath(center);
se.unwrapForShortestPath(center);
ne.unwrapForShortestPath(center);
sw.unwrapForShortestPath(center);
LatLngBounds bounds = LatLngBounds::hull(nw, se);
bounds.extend(ne);
bounds.extend(sw);
bounds.extend(center);
return bounds;
}
#pragma mark - Bounds
void Map::setBounds(const BoundOptions& options) {
bool changeCamera = false;
CameraOptions cameraOptions;
if (options.bounds) {
changeCamera = true;
impl->transform.setLatLngBounds(*options.bounds);
}
if (options.minZoom) {
impl->transform.setMinZoom(*options.minZoom);
if (impl->transform.getZoom() < *options.minZoom) {
changeCamera = true;
cameraOptions.withZoom(*options.minZoom);
}
}
if (options.maxZoom) {
impl->transform.setMaxZoom(*options.maxZoom);
if (impl->transform.getZoom() > *options.maxZoom) {
changeCamera = true;
cameraOptions.withZoom(*options.maxZoom);
}
}
if (options.maxPitch) {
impl->transform.setMaxPitch(*options.maxPitch);
if (impl->transform.getPitch() > impl->transform.getState().getMaxPitch()) {
changeCamera = true;
cameraOptions.withPitch(*options.maxPitch);
}
}
if (options.minPitch) {
impl->transform.setMinPitch(*options.minPitch);
if (impl->transform.getPitch() < impl->transform.getState().getMinPitch()) {
changeCamera = true;
cameraOptions.withPitch(*options.minPitch);
}
}
if (changeCamera) {
jumpTo(cameraOptions);
}
}
BoundOptions Map::getBounds() const {
return BoundOptions()
.withLatLngBounds(impl->transform.getState().getLatLngBounds())
.withMinZoom(impl->transform.getState().getMinZoom())
.withMaxZoom(impl->transform.getState().getMaxZoom())
.withMinPitch(impl->transform.getState().getMinPitch() * util::RAD2DEG)
.withMaxPitch(impl->transform.getState().getMaxPitch() * util::RAD2DEG);
}
#pragma mark - Map options
void Map::setSize(const Size size) {
impl->transform.resize(size);
impl->onUpdate();
}
void Map::setNorthOrientation(NorthOrientation orientation) {
impl->transform.setNorthOrientation(orientation);
impl->onUpdate();
}
void Map::setConstrainMode(mbgl::ConstrainMode mode) {
impl->transform.setConstrainMode(mode);
impl->onUpdate();
}
void Map::setViewportMode(mbgl::ViewportMode mode) {
impl->transform.setViewportMode(mode);
impl->onUpdate();
}
MapOptions Map::getMapOptions() const {
return std::move(MapOptions()
.withMapMode(impl->mode)
.withConstrainMode(impl->transform.getConstrainMode())
.withViewportMode(impl->transform.getViewportMode())
.withCrossSourceCollisions(impl->crossSourceCollisions)
.withNorthOrientation(impl->transform.getNorthOrientation())
.withSize(impl->transform.getState().getSize())
.withPixelRatio(impl->pixelRatio));
}
#pragma mark - Projection mode
void Map::setProjectionMode(const ProjectionMode& options) {
impl->transform.setProjectionMode(options);
impl->onUpdate();
}
ProjectionMode Map::getProjectionMode() const {
return impl->transform.getProjectionMode();
}
#pragma mark - Projection
ScreenCoordinate Map::pixelForLatLng(const LatLng& latLng) const {
// If the center and point longitudes are not in the same side of the
// antimeridian, we unwrap the point longitude so it would be seen if
// e.g. the next antimeridian side is visible.
LatLng unwrappedLatLng = latLng.wrapped();
unwrappedLatLng.unwrapForShortestPath(impl->transform.getLatLng());
return impl->transform.latLngToScreenCoordinate(unwrappedLatLng);
}
LatLng Map::latLngForPixel(const ScreenCoordinate& pixel) const {
return impl->transform.screenCoordinateToLatLng(pixel);
}
std::vector<ScreenCoordinate> Map::pixelsForLatLngs(const std::vector<LatLng>& latLngs) const {
std::vector<ScreenCoordinate> ret;
ret.reserve(latLngs.size());
for (const auto& latLng : latLngs) {
ret.emplace_back(pixelForLatLng(latLng));
}
return ret;
}
std::vector<LatLng> Map::latLngsForPixels(const std::vector<ScreenCoordinate>& screenCoords) const {
std::vector<LatLng> ret;
ret.reserve(screenCoords.size());
for (const auto& point : screenCoords) {
ret.emplace_back(latLngForPixel(point));
}
return ret;
}
#pragma mark - Annotations
void Map::addAnnotationImage(std::unique_ptr<style::Image> image) {
if (LayerManager::annotationsEnabled) {
impl->annotationManager.addImage(std::move(image));
}
}
void Map::removeAnnotationImage(const std::string& id) {
if (LayerManager::annotationsEnabled) {
impl->annotationManager.removeImage(id);
}
}
double Map::getTopOffsetPixelsForAnnotationImage(const std::string& id) {
if (LayerManager::annotationsEnabled) {
return impl->annotationManager.getTopOffsetPixelsForImage(id);
}
return 0.0;
}
AnnotationID Map::addAnnotation(const Annotation& annotation) {
if (LayerManager::annotationsEnabled) {
auto result = impl->annotationManager.addAnnotation(annotation);
impl->onUpdate();
return result;
}
return 0;
}
void Map::updateAnnotation(AnnotationID id, const Annotation& annotation) {
if (LayerManager::annotationsEnabled) {
if (impl->annotationManager.updateAnnotation(id, annotation)) {
impl->onUpdate();
}
}
}
void Map::removeAnnotation(AnnotationID annotation) {
if (LayerManager::annotationsEnabled) {
impl->annotationManager.removeAnnotation(annotation);
impl->onUpdate();
}
}
#pragma mark - Toggles
void Map::setDebug(MapDebugOptions debugOptions) {
impl->debugOptions = debugOptions;
impl->onUpdate();
}
MapDebugOptions Map::getDebug() const {
return impl->debugOptions;
}
void Map::setPrefetchZoomDelta(uint8_t delta) {
impl->prefetchZoomDelta = delta;
}
uint8_t Map::getPrefetchZoomDelta() const {
return impl->prefetchZoomDelta;
}
bool Map::isFullyLoaded() const {
return impl->style->impl->isLoaded() && impl->rendererFullyLoaded;
}
void Map::dumpDebugLogs() const {
Log::Info(Event::General, "--------------------------------------------------------------------------------");
impl->style->impl->dumpDebugLogs();
Log::Info(Event::General, "--------------------------------------------------------------------------------");
}
void Map::setFreeCameraOptions(const FreeCameraOptions& camera) {
impl->transform.setFreeCameraOptions(camera);
impl->cameraMutated = true;
impl->onUpdate();
}
FreeCameraOptions Map::getFreeCameraOptions() const {
return impl->transform.getFreeCameraOptions();
}
} // namespace mbgl