-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathGeoJsonDocumentRasterOverlay.cpp
More file actions
853 lines (784 loc) · 29.4 KB
/
GeoJsonDocumentRasterOverlay.cpp
File metadata and controls
853 lines (784 loc) · 29.4 KB
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
#include <CesiumAsync/AsyncSystem.h>
#include <CesiumAsync/Future.h>
#include <CesiumAsync/IAssetAccessor.h>
#include <CesiumGeometry/QuadtreeTileID.h>
#include <CesiumGeometry/QuadtreeTilingScheme.h>
#include <CesiumGeometry/Rectangle.h>
#include <CesiumGeospatial/BoundingRegionBuilder.h>
#include <CesiumGeospatial/Cartographic.h>
#include <CesiumGeospatial/Ellipsoid.h>
#include <CesiumGeospatial/GeographicProjection.h>
#include <CesiumGeospatial/GlobeRectangle.h>
#include <CesiumGeospatial/Projection.h>
#include <CesiumGltf/ImageAsset.h>
#include <CesiumRasterOverlays/CreateRasterOverlayTileProviderParameters.h>
#include <CesiumRasterOverlays/GeoJsonDocumentRasterOverlay.h>
#include <CesiumRasterOverlays/Library.h>
#include <CesiumRasterOverlays/RasterOverlay.h>
#include <CesiumRasterOverlays/RasterOverlayLoadFailureDetails.h>
#include <CesiumRasterOverlays/RasterOverlayTile.h>
#include <CesiumRasterOverlays/RasterOverlayTileProvider.h>
#include <CesiumUtility/Assert.h>
#include <CesiumUtility/IntrusivePointer.h>
#include <CesiumUtility/Math.h>
#include <CesiumVectorData/GeoJsonDocument.h>
#include <CesiumVectorData/GeoJsonObject.h>
#include <CesiumVectorData/GeoJsonObjectTypes.h>
#include <CesiumVectorData/VectorRasterizer.h>
#include <CesiumVectorData/VectorStyle.h>
#include <glm/common.hpp>
#include <glm/ext/vector_double2.hpp>
#include <glm/ext/vector_double3.hpp>
#include <glm/ext/vector_int2.hpp>
#include <nonstd/expected.hpp>
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <variant>
#include <vector>
using namespace CesiumGeometry;
using namespace CesiumGeospatial;
using namespace CesiumUtility;
using namespace CesiumVectorData;
// We won't generate any quadtree nodes past this depth.
const uint32_t DEPTH_LIMIT = 8;
namespace CesiumRasterOverlays {
namespace {
/**
* @brief A single geometry object in a GeoJSON file, with all the information
* required for rendering.
*/
struct QuadtreeGeometryData {
/**
* @brief A pointer to the geometry object to render.
*/
const GeoJsonObject* pObject;
/**
* @brief A pointer to the `VectorStyle` to apply to this geometry object.
*/
const VectorStyle* pStyle;
/**
* @brief The bounding rectangle encompassing this geometry.
*/
GlobeRectangle rectangle;
/**
* The maximum width of all lines or outlines for this geometry with
* `LineWidthMode::Pixels`.
*/
double maxLineWidthPixels = 0.0;
/**
* @brief Calculates the size of the bounding rectangle for this geometry when
* rendered on a tile with the given bounds and texture size.
*
* For a tile that has no line width or where line width is specified in
* `LineWidthMode::Meters`, this will be equivalent to `rectangle`. For tiles
* where the line width is specified in `LineWidthMode::Pixels`, this result
* can change depending on texture size and bounds.
*
* @param tileBounds The bounding box of this tile.
* @param textureSize The size of the texture this tile will be rasterized
* into.
*/
GlobeRectangle calculateBoundingRectangleForTileSize(
const GlobeRectangle& tileBounds,
const glm::ivec2& textureSize) {
LineStyle activeLineStyle;
if (this->pObject->isType<GeoJsonPolygon>() ||
this->pObject->isType<GeoJsonMultiPolygon>()) {
if (!this->pStyle->polygon.outline) {
return this->rectangle;
}
activeLineStyle = *this->pStyle->polygon.outline;
} else {
activeLineStyle = this->pStyle->line;
}
// We already accounted for meters when building the rectangle in the first
// place.
if (activeLineStyle.widthMode == LineWidthMode::Meters) {
return this->rectangle;
}
const double longPerPixel =
tileBounds.computeWidth() / static_cast<double>(textureSize.x);
const double latPerPixel =
tileBounds.computeHeight() / static_cast<double>(textureSize.y);
const double halfX = longPerPixel * activeLineStyle.width * 0.5;
const double halfY = latPerPixel * activeLineStyle.width * 0.5;
double newWest = this->rectangle.getWest() - halfX;
if (newWest < -180.0) {
newWest += 360.0;
}
double newEast = this->rectangle.getEast() + halfX;
if (newEast > 180.0) {
newEast -= 360.0;
}
BoundingRegionBuilder builder;
builder.expandToIncludeGlobeRectangle(this->rectangle);
builder.expandToIncludePosition(
Cartographic(newWest, this->rectangle.getSouth() - halfY));
builder.expandToIncludePosition(
Cartographic(newEast, this->rectangle.getNorth() + halfY));
return builder.toGlobeRectangle();
}
};
struct QuadtreeNode {
/**
* @brief The `GlobeRectangle` defining the bounds of this node.
*/
GlobeRectangle rectangle;
/**
* @brief Indices representing the children of this quadtree node.
*
* `0` represents no child, as the 0 index will always be the root node and
* the root node cannot ever be a child of another node.
*/
uint32_t children[2][2] = {{0, 0}, {0, 0}};
/**
* The maximum width of all lines or outlines in this node with
* `LineWidthMode::Pixels`.
*/
double maxLineWidthPixels = 0.0;
QuadtreeNode(const GlobeRectangle& rectangle_) : rectangle(rectangle_) {}
/**
* @brief Returns `true` if this node has any children.
*/
bool anyChildren() const {
return children[0][0] != 0 && children[0][1] != 0 && children[1][0] != 0 &&
children[1][1] != 0;
}
GlobeRectangle
getRectangleScaledWithPixelSize(const glm::dvec2& halfPixelSize) const {
if (this->maxLineWidthPixels == 0.0) {
return this->rectangle;
}
const glm::dvec2 scaledHalfPixelSize =
halfPixelSize * this->maxLineWidthPixels;
return GlobeRectangle(
this->rectangle.getWest() - scaledHalfPixelSize.x,
this->rectangle.getSouth() - scaledHalfPixelSize.y,
this->rectangle.getEast() + scaledHalfPixelSize.x,
this->rectangle.getNorth() + scaledHalfPixelSize.y);
}
};
/**
* @brief A quadtree used to speed up the selection of GeoJSON objects to
* rasterize.
*
* A GeoJSON document, unlike something like 3D Tiles or a Tile Map Service, is
* a format that is not designed around efficient real-time rendering. There is
* no way to tell from the structure of a GeoJSON document which objects will
* need to be considered when rendering any particular area. This becomes an
* issue when working with GeoJSON documents that have thousands, or tens of
* thousands, or hundreds of thousands of objects that each could be considered
* for rendering in any particular tile. Performing bounding box comparisons of
* hundreds of thousands of objects per tile is not conducive to good
* performance.
*
* To speed up these checks, we use a quadtree to speed up our queries against
* the document. The root level of the quadtree encompasses the entire bounding
* rectangle of the document. Each sub-level then represents one quarter of the
* parent level. The same object may appear in multiple levels at once; a line
* that crosses the north side of one level will appear in both its north east
* and north west sublevels, and every object that can be rendered will be
* included in the root level.
*
* The reason for including the same object across multiple levels is so that
* rendering can do a minimum amount of recursing through the tree. If we need
* to render the entire document at once, for example, the root level will
* provide the full list of objects to render without having to check any
* children.
*/
struct Quadtree {
GlobeRectangle rectangle = GlobeRectangle::EMPTY;
uint32_t rootId = 0;
std::vector<QuadtreeNode> nodes;
std::vector<QuadtreeGeometryData> data;
/**
* @brief A vector containing all the geometry of all the nodes.
*
* Each element in this vector is an index into `data`.
*/
std::vector<uint32_t> dataIndices;
/**
* @brief A vector containing the first index into `dataIndices` for every
* node.
*
* This vector contains `nodeCount + 1` items, with a synthetic final index
* added so that `nodeIndex + 1` will always represent the exclusive end of
* the node's geometry.
*
* For example, for the node at index `i`, the indices into `dataIndices`
* representing its geometry will be the range `[ dataNodeIndicesBegin[i],
* dataNodeIndicesBegin[i + 1] ]`.
*/
std::vector<uint32_t> dataNodeIndicesBegin;
};
struct RectangleAndLineWidthFromObjectVisitor {
std::optional<GlobeRectangle>& rect;
double& maxLineWidthPixels;
const VectorStyle& style;
const Ellipsoid& ellipsoid;
void visitWithLineWidth(
const std::vector<glm::dvec3>& coordinates,
const std::optional<LineStyle>& lineStyle) {
// For geometry where the line width is specified in meters, we can include
// that in the bounding region calculation up front.
if (lineStyle && lineStyle->widthMode == LineWidthMode::Meters) {
const double halfWidth =
(lineStyle->width / ellipsoid.getRadii().x) / 2.0;
for (const glm::dvec3& point : coordinates) {
if (!rect) {
rect = GlobeRectangle(
std::min(
Math::degreesToRadians(point.x) - halfWidth,
-Math::OnePi),
Math::degreesToRadians(point.y) - halfWidth,
std::max(
Math::degreesToRadians(point.x) + halfWidth,
Math::OnePi),
Math::degreesToRadians(point.y) + halfWidth);
} else {
rect->setWest(std::max(
std::min(
rect->getWest(),
Math::degreesToRadians(point.x) - halfWidth),
-Math::OnePi));
rect->setSouth(std::min(
rect->getSouth(),
Math::degreesToRadians(point.y) - halfWidth));
rect->setEast(std::min(
std::max(
rect->getEast(),
Math::degreesToRadians(point.x) + halfWidth),
Math::OnePi));
rect->setNorth(std::max(
rect->getNorth(),
Math::degreesToRadians(point.y) + halfWidth));
}
}
} else {
if (lineStyle && lineStyle->widthMode == LineWidthMode::Pixels) {
this->maxLineWidthPixels =
std::max(this->maxLineWidthPixels, lineStyle->width);
}
for (const glm::dvec3& point : coordinates) {
if (!rect) {
rect = GlobeRectangle(
Math::degreesToRadians(point.x),
Math::degreesToRadians(point.y),
Math::degreesToRadians(point.x),
Math::degreesToRadians(point.y));
} else {
rect->setWest(
std::min(rect->getWest(), Math::degreesToRadians(point.x)));
rect->setSouth(
std::min(rect->getSouth(), Math::degreesToRadians(point.y)));
rect->setEast(
std::max(rect->getEast(), Math::degreesToRadians(point.x)));
rect->setNorth(
std::max(rect->getNorth(), Math::degreesToRadians(point.y)));
}
}
}
}
void operator()(const GeoJsonLineString& line) {
visitWithLineWidth(line.coordinates, style.line);
}
void operator()(const GeoJsonMultiLineString& lines) {
for (const std::vector<glm::dvec3>& line : lines.coordinates) {
visitWithLineWidth(line, style.line);
}
}
void operator()(const GeoJsonPolygon& polygon) {
for (const std::vector<glm::dvec3>& ring : polygon.coordinates) {
visitWithLineWidth(ring, style.polygon.outline);
}
}
void operator()(const GeoJsonMultiPolygon& polygons) {
for (const std::vector<std::vector<glm::dvec3>>& polygon :
polygons.coordinates) {
for (const std::vector<glm::dvec3>& ring : polygon) {
visitWithLineWidth(ring, style.polygon.outline);
}
}
}
void operator()(const GeoJsonFeature&) {}
void operator()(const GeoJsonFeatureCollection&) {}
void operator()(const GeoJsonGeometryCollection&) {}
// While we could calculate a bounding box for a point just fine, they are not
// rendered by the raster overlay, so there's no need.
void operator()(const GeoJsonPoint&) {}
void operator()(const GeoJsonMultiPoint&) {}
};
void addPrimitivesToData(
const GeoJsonObject* pGeoJsonObject,
std::vector<QuadtreeGeometryData>& data,
BoundingRegionBuilder& documentRegionBuilder,
const VectorStyle& style,
const Ellipsoid& ellipsoid);
struct GeoJsonChildVisitor {
std::vector<QuadtreeGeometryData>& data;
BoundingRegionBuilder& documentRegionBuilder;
const VectorStyle& style;
const Ellipsoid& ellipsoid;
void operator()(const GeoJsonFeature& feature) {
if (feature.geometry) {
const std::optional<VectorStyle>& geometryStyle =
feature.geometry->getStyle();
const std::optional<VectorStyle>& featureStyle =
geometryStyle ? geometryStyle : feature.style;
addPrimitivesToData(
feature.geometry.get(),
data,
documentRegionBuilder,
featureStyle.value_or(style),
ellipsoid);
}
}
void operator()(const GeoJsonFeatureCollection& collection) {
for (const GeoJsonObject& feature : collection.features) {
const GeoJsonFeature* pFeature = feature.getIf<GeoJsonFeature>();
if (pFeature && pFeature->geometry) {
const std::optional<VectorStyle>& geometryStyle = feature.getStyle();
const std::optional<VectorStyle>& featureStyle =
geometryStyle ? geometryStyle : pFeature->style;
const std::optional<VectorStyle>& collectionStyle =
featureStyle ? featureStyle : pFeature->style;
addPrimitivesToData(
pFeature->geometry.get(),
data,
documentRegionBuilder,
collectionStyle ? *collectionStyle : style,
ellipsoid);
}
}
}
void operator()(const GeoJsonGeometryCollection& collection) {
for (const GeoJsonObject& geometry : collection.geometries) {
const std::optional<VectorStyle>& childStyle = geometry.getStyle();
const std::optional<VectorStyle>& useStyle =
childStyle ? childStyle : collection.style;
addPrimitivesToData(
&geometry,
data,
documentRegionBuilder,
useStyle ? *useStyle : style,
ellipsoid);
}
}
void operator()(const auto& /*catchAll*/) {}
};
void addPrimitivesToData(
const GeoJsonObject* geoJsonObject,
std::vector<QuadtreeGeometryData>& data,
BoundingRegionBuilder& documentRegionBuilder,
const VectorStyle& style,
const Ellipsoid& ellipsoid) {
std::optional<GlobeRectangle> rect;
double maxLineWidthPixels = 0.0;
std::visit(
RectangleAndLineWidthFromObjectVisitor{
rect,
maxLineWidthPixels,
style,
ellipsoid},
geoJsonObject->value);
// Points and MultiPoints, as well as Features, FeatureCollections, and
// GeometryCollections have no bounding box. For the first two, this is
// because they are not rasterized by this overlay. For the rest, though they
// may contain geometry, they do not themselves have anything to render. We
// can save some effort by ignoring them all now.
if (rect) {
documentRegionBuilder.expandToIncludeGlobeRectangle(*rect);
QuadtreeGeometryData primitive{
geoJsonObject,
&style,
std::move(*rect),
maxLineWidthPixels};
data.emplace_back(primitive);
}
std::visit(
GeoJsonChildVisitor{data, documentRegionBuilder, style, ellipsoid},
geoJsonObject->value);
}
uint32_t buildQuadtreeNode(
Quadtree& tree,
const QuadtreeTilingScheme& tilingScheme,
const GlobeRectangle& rectangle,
std::vector<uint32_t>::iterator begin,
std::vector<uint32_t>::iterator end,
QuadtreeTileID tileId) {
if (begin == end) {
return 0;
}
uint32_t resultId = (uint32_t)tree.nodes.size();
tree.nodes.emplace_back(rectangle);
double maxLineWidthPixels = 0.0;
for (std::vector<uint32_t>::iterator it = begin; it != end; ++it) {
maxLineWidthPixels =
std::max(tree.data[*it].maxLineWidthPixels, maxLineWidthPixels);
}
tree.nodes[resultId].maxLineWidthPixels = maxLineWidthPixels;
uint32_t indicesBegin = (uint32_t)tree.dataIndices.size();
tree.dataIndices.insert(tree.dataIndices.end(), begin, end);
tree.dataNodeIndicesBegin.emplace_back(indicesBegin);
if (begin + 1 == end || tileId.level >= DEPTH_LIMIT ||
std::equal(begin + 1, end, begin)) {
return resultId;
}
const CesiumGeometry::QuadtreeTileID southWestTile(
tileId.level + 1,
tileId.x * 2,
tileId.y * 2);
const CesiumGeometry::QuadtreeTileID southEastTile(
southWestTile.level,
southWestTile.x + 1,
southWestTile.y);
const CesiumGeometry::QuadtreeTileID northWestTile(
southWestTile.level,
southWestTile.x,
southWestTile.y + 1);
const CesiumGeometry::QuadtreeTileID northEastTile(
southWestTile.level,
southWestTile.x + 1,
southWestTile.y + 1);
const GlobeRectangle southWestRect = GlobeRectangle::fromRectangleRadians(
tilingScheme.tileToRectangle(southWestTile));
tree.nodes[resultId].children[0][0] = buildQuadtreeNode(
tree,
tilingScheme,
southWestRect,
begin,
std::partition(
begin,
end,
[&southWestRect, &data = tree.data](uint32_t idx) {
return data[idx]
.rectangle.computeIntersection(southWestRect)
.has_value();
}),
southWestTile);
const GlobeRectangle southEastRect = GlobeRectangle::fromRectangleRadians(
tilingScheme.tileToRectangle(southEastTile));
tree.nodes[resultId].children[0][1] = buildQuadtreeNode(
tree,
tilingScheme,
southEastRect,
begin,
std::partition(
begin,
end,
[&southEastRect, &data = tree.data](uint32_t idx) {
return data[idx]
.rectangle.computeIntersection(southEastRect)
.has_value();
}),
southEastTile);
const GlobeRectangle northWestRect = GlobeRectangle::fromRectangleRadians(
tilingScheme.tileToRectangle(northWestTile));
tree.nodes[resultId].children[1][0] = buildQuadtreeNode(
tree,
tilingScheme,
northWestRect,
begin,
std::partition(
begin,
end,
[&northWestRect, &data = tree.data](uint32_t idx) {
return data[idx]
.rectangle.computeIntersection(northWestRect)
.has_value();
}),
northWestTile);
const GlobeRectangle northEastRect = GlobeRectangle::fromRectangleRadians(
tilingScheme.tileToRectangle(northEastTile));
tree.nodes[resultId].children[1][1] = buildQuadtreeNode(
tree,
tilingScheme,
northEastRect,
begin,
std::partition(
begin,
end,
[&northEastRect, &data = tree.data](uint32_t idx) {
return data[idx]
.rectangle.computeIntersection(northEastRect)
.has_value();
}),
northEastTile);
return resultId;
}
Quadtree buildQuadtree(
const std::shared_ptr<GeoJsonDocument>& document,
const VectorStyle& defaultStyle,
const Ellipsoid& ellipsoid) {
BoundingRegionBuilder builder;
std::vector<QuadtreeGeometryData> data;
const std::optional<VectorStyle>& rootObjectStyle =
document->rootObject.getStyle();
addPrimitivesToData(
&document->rootObject,
data,
builder,
rootObjectStyle ? *rootObjectStyle : defaultStyle,
ellipsoid);
Quadtree tree{
builder.toGlobeRectangle(),
0,
std::vector<QuadtreeNode>(),
std::move(data),
std::vector<uint32_t>(),
std::vector<uint32_t>()};
std::vector<uint32_t> dataIndices;
dataIndices.reserve(tree.data.size());
for (size_t i = 0; i < tree.data.size(); i++) {
dataIndices.emplace_back((uint32_t)i);
}
const QuadtreeTilingScheme tilingScheme(
tree.rectangle.toSimpleRectangle(),
1,
1);
tree.rootId = buildQuadtreeNode(
tree,
tilingScheme,
tree.rectangle,
dataIndices.begin(),
dataIndices.end(),
QuadtreeTileID(0, 0, 0));
// Add last entry so [i + 1] is always valid
tree.dataNodeIndicesBegin.emplace_back((uint32_t)tree.dataIndices.size());
return tree;
}
void rasterizeQuadtreeNode(
const Quadtree& tree,
uint32_t nodeId,
const GlobeRectangle& rectangle,
VectorRasterizer& rasterizer,
std::vector<bool>& primitivesRendered,
const glm::dvec2& halfPixelSize) {
const QuadtreeNode& node = tree.nodes[nodeId];
const GlobeRectangle scaledNodeRectangle =
node.getRectangleScaledWithPixelSize(halfPixelSize);
// If this node has no children, or if it is entirely within the target
// rectangle, let's rasterize this node's contents and not any children.
if (!node.anyChildren() ||
(rectangle.contains(scaledNodeRectangle.getSouthwest()) &&
rectangle.contains(scaledNodeRectangle.getNortheast()))) {
for (uint32_t i = tree.dataNodeIndicesBegin[nodeId];
i < tree.dataNodeIndicesBegin[nodeId + 1];
i++) {
const uint32_t dataIdx = tree.dataIndices[i];
if (primitivesRendered[dataIdx]) {
continue;
}
primitivesRendered[dataIdx] = true;
const QuadtreeGeometryData& data = tree.data[dataIdx];
rasterizer.drawGeoJsonObject(*data.pObject, *data.pStyle);
}
} else {
for (size_t i = 0; i < 2; i++) {
for (size_t j = 0; j < 2; j++) {
if (tree.nodes[node.children[i][j]]
.getRectangleScaledWithPixelSize(halfPixelSize)
.computeIntersection(rectangle)
.has_value()) {
rasterizeQuadtreeNode(
tree,
node.children[i][j],
rectangle,
rasterizer,
primitivesRendered,
halfPixelSize);
}
}
}
}
}
void rasterizeVectorData(
LoadedRasterOverlayImage& result,
const GlobeRectangle& rectangle,
const Quadtree& tree,
const Ellipsoid& ellipsoid,
const glm::dvec2& halfPixelSize) {
// Keeps track of primitives that have already been rendered to avoid
// re-drawing the same primitives that appear in multiple quadtree nodes.
std::vector<bool> primitivesRendered(tree.data.size(), false);
for (size_t i = 0;
i < std::max(result.pImage->mipPositions.size(), (size_t)1);
i++) {
primitivesRendered.assign(primitivesRendered.size(), false);
VectorRasterizer rasterizer(
rectangle,
result.pImage,
(uint32_t)i,
ellipsoid);
rasterizeQuadtreeNode(
tree,
tree.rootId,
rectangle,
rasterizer,
primitivesRendered,
halfPixelSize);
rasterizer.finalize();
}
}
} // namespace
class CESIUMRASTEROVERLAYS_API GeoJsonDocumentRasterOverlayTileProvider final
: public RasterOverlayTileProvider {
private:
std::shared_ptr<GeoJsonDocument> _pDocument;
VectorStyle _defaultStyle;
Quadtree _tree;
Ellipsoid _ellipsoid;
uint32_t _mipLevels;
public:
GeoJsonDocumentRasterOverlayTileProvider(
const IntrusivePointer<const RasterOverlay>& pCreator,
const CreateRasterOverlayTileProviderParameters& parameters,
const GeoJsonDocumentRasterOverlayOptions& geoJsonOptions,
std::shared_ptr<CesiumVectorData::GeoJsonDocument>&& pDocument)
: RasterOverlayTileProvider(
pCreator,
parameters,
GeographicProjection(geoJsonOptions.ellipsoid),
projectRectangleSimple(
GeographicProjection(geoJsonOptions.ellipsoid),
GlobeRectangle::MAXIMUM)),
_pDocument(std::move(pDocument)),
_defaultStyle(geoJsonOptions.defaultStyle),
_tree(),
_ellipsoid(geoJsonOptions.ellipsoid),
_mipLevels(geoJsonOptions.mipLevels) {
CESIUM_ASSERT(this->_pDocument);
this->_tree = buildQuadtree(
this->_pDocument,
this->_defaultStyle,
geoJsonOptions.ellipsoid);
}
virtual CesiumAsync::Future<LoadedRasterOverlayImage>
loadTileImage(const RasterOverlayTile& overlayTile) override {
// Choose the texture size according to the geometry screen size and raster
// SSE, but no larger than the maximum texture size.
const RasterOverlayOptions& options = this->getOwner().getOptions();
glm::ivec2 textureSize = glm::min(
glm::ivec2(
overlayTile.getTargetScreenPixels() /
options.maximumScreenSpaceError),
glm::ivec2(options.maximumTextureSize));
return this->getAsyncSystem().runInWorkerThread(
[&tree = this->_tree,
ellipsoid = this->_ellipsoid,
projection = this->getProjection(),
rectangle = overlayTile.getRectangle(),
textureSize,
mipLevels = this->_mipLevels]() -> LoadedRasterOverlayImage {
const CesiumGeospatial::GlobeRectangle tileRectangle =
CesiumGeospatial::unprojectRectangleSimple(projection, rectangle);
LoadedRasterOverlayImage result;
result.rectangle = rectangle;
const glm::dvec2 halfPixelSize(
tileRectangle.computeWidth() / (double)textureSize.x / 2.0,
tileRectangle.computeHeight() / (double)textureSize.y / 2.0);
if (!tree.nodes[tree.rootId]
.getRectangleScaledWithPixelSize(halfPixelSize)
.computeIntersection(tileRectangle)
.has_value()) {
// Transparent square if this is outside of the contents of this
// vector document.
result.moreDetailAvailable = false;
result.pImage.emplace();
result.pImage->width = 1;
result.pImage->height = 1;
result.pImage->channels = 4;
result.pImage->bytesPerChannel = 1;
result.pImage->pixelData = {
std::byte{0x00},
std::byte{0x00},
std::byte{0x00},
std::byte{0x00}};
} else {
result.moreDetailAvailable = true;
result.pImage.emplace();
result.pImage->width = textureSize.x;
result.pImage->height = textureSize.y;
result.pImage->channels = 4;
result.pImage->bytesPerChannel = 1;
if (mipLevels == 0) {
result.pImage->pixelData.resize(
(size_t)(result.pImage->width * result.pImage->height *
result.pImage->channels *
result.pImage->bytesPerChannel),
std::byte{0});
} else {
size_t totalSize = 0;
result.pImage->mipPositions.reserve((size_t)mipLevels);
for (uint32_t i = 0; i < mipLevels; i++) {
const int32_t width = std::max(textureSize.x >> i, 1);
const int32_t height = std::max(textureSize.y >> i, 1);
result.pImage->mipPositions.emplace_back(
CesiumGltf::ImageAssetMipPosition{
totalSize,
(size_t)(width * height * result.pImage->channels *
result.pImage->bytesPerChannel)});
totalSize += result.pImage->mipPositions[i].byteSize;
}
result.pImage->pixelData.resize(totalSize, std::byte{0});
}
rasterizeVectorData(
result,
tileRectangle,
tree,
ellipsoid,
halfPixelSize);
}
return result;
});
}
};
GeoJsonDocumentRasterOverlay::GeoJsonDocumentRasterOverlay(
const CesiumAsync::AsyncSystem& asyncSystem,
const std::string& name,
const std::shared_ptr<GeoJsonDocument>& document,
const GeoJsonDocumentRasterOverlayOptions& vectorOverlayOptions,
const RasterOverlayOptions& overlayOptions)
: RasterOverlay(name, overlayOptions),
_documentFuture(asyncSystem.createResolvedFuture(
std::shared_ptr<GeoJsonDocument>(document))),
_options(vectorOverlayOptions) {}
GeoJsonDocumentRasterOverlay::GeoJsonDocumentRasterOverlay(
const std::string& name,
CesiumAsync::Future<std::shared_ptr<CesiumVectorData::GeoJsonDocument>>&&
documentFuture,
const GeoJsonDocumentRasterOverlayOptions& vectorOverlayOptions,
const RasterOverlayOptions& overlayOptions)
: RasterOverlay(name, overlayOptions),
_documentFuture(std::move(documentFuture)),
_options(vectorOverlayOptions) {}
GeoJsonDocumentRasterOverlay::~GeoJsonDocumentRasterOverlay() = default;
CesiumAsync::Future<RasterOverlay::CreateTileProviderResult>
GeoJsonDocumentRasterOverlay::createTileProvider(
const CreateRasterOverlayTileProviderParameters& parameters) const {
IntrusivePointer<const GeoJsonDocumentRasterOverlay> thiz = this;
return std::move(
const_cast<GeoJsonDocumentRasterOverlay*>(this)->_documentFuture)
.thenInMainThread(
[thiz, parameters](std::shared_ptr<GeoJsonDocument>&& pDocument)
-> CreateTileProviderResult {
if (!pDocument) {
return nonstd::make_unexpected(RasterOverlayLoadFailureDetails{
.type = RasterOverlayLoadType::Unknown,
.pRequest = nullptr,
.message = "GeoJSON document failed to load."});
}
return IntrusivePointer<RasterOverlayTileProvider>(
new GeoJsonDocumentRasterOverlayTileProvider(
thiz,
parameters,
thiz->_options,
std::move(pDocument)));
});
}
} // namespace CesiumRasterOverlays