FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tilemaker/src/tile_data.cpp at master · cyclemap/tilemaker · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
cyclemap
/
tilemaker
Public
forked from
systemed/tilemaker
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tilemaker
/
src
/
tile_data.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
679 lines (588 loc) · 23.9 KB
Breadcrumbs
tilemaker
/
src
/
tile_data.cpp
Copy path
File metadata and controls
679 lines (588 loc) · 23.9 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
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
#
include
<
algorithm
>
#
include
<
iostream
>
#
include
<
sstream
>
#
include
"
tile_data.h
"
#
include
"
coordinates_geom.h
"
#
include
"
leased_store.h
"
#
include
<
ciso646
>
using
namespace
std
;
extern
bool
verbose;
//
Human-readable name for a Boost.Geometry validity failure, used for diagnostics.
static
const
char
*
validityFailureName
(geom::validity_failure_type failure) {
switch
(failure) {
case
geom::no_failure:
return
"
no_failure
"
;
case
geom::failure_few_points:
return
"
few_points
"
;
case
geom::failure_wrong_topological_dimension:
return
"
wrong_topological_dimension
"
;
case
geom::failure_spikes:
return
"
spikes
"
;
case
geom::failure_duplicate_points:
return
"
duplicate_points
"
;
case
geom::failure_not_closed:
return
"
not_closed
"
;
case
geom::failure_self_intersections:
return
"
self_intersections
"
;
case
geom::failure_wrong_orientation:
return
"
wrong_orientation
"
;
case
geom::failure_interior_rings_outside:
return
"
interior_rings_outside
"
;
case
geom::failure_nested_interior_rings:
return
"
nested_interior_rings
"
;
case
geom::failure_disconnected_interior:
return
"
disconnected_interior
"
;
case
geom::failure_intersecting_interiors:
return
"
intersecting_interiors
"
;
case
geom::failure_wrong_corner_order:
return
"
wrong_corner_order
"
;
case
geom::failure_invalid_coordinate:
return
"
invalid_coordinate
"
;
default
:
return
"
unknown
"
;
}
}
//
Thin wrapper around the shared repair_multi_polygon() that adds per-object
//
verbose diagnostics. See geom.cpp for the dissolve + zero-width buffer logic.
static
bool
repairMultiPolygon
(MultiPolygon &mp, NodeID objectID) {
bool
ok =
repair_multi_polygon
(mp);
if
(!ok && verbose)
std::cerr << (
"
multipolygon repair failed for object
"
+
std::to_string
(objectID) +
"
\n
"
);
return
ok;
}
thread_local
LeasedStore<TileDataSource::
point_store_t
> pointStore;
thread_local
LeasedStore<TileDataSource::
linestring_store_t
> linestringStore;
thread_local
LeasedStore<TileDataSource::
multi_linestring_store_t
> multilinestringStore;
thread_local
LeasedStore<TileDataSource::
multi_polygon_store_t
> multipolygonStore;
TileDataSource::TileDataSource
(
size_t
threadNum,
unsigned
int
indexZoom,
bool
includeID)
:
includeID(includeID),
z6OffsetDivisor(indexZoom >=
CLUSTER_ZOOM
? (
1
<< (indexZoom -
CLUSTER_ZOOM
)) : 1),
objectsMutex(threadNum *
4
),
objects(
CLUSTER_ZOOM_AREA
),
lowZoomObjects(
CLUSTER_ZOOM_AREA
),
objectsWithIds(
CLUSTER_ZOOM_AREA
),
lowZoomObjectsWithIds(
CLUSTER_ZOOM_AREA
),
indexZoom(indexZoom),
pointStores(threadNum),
linestringStores(threadNum),
multilinestringStores(threadNum),
multipolygonStores(threadNum),
multiPolygonClipCache(ClipCache<MultiPolygon>(threadNum, indexZoom)),
multiLinestringClipCache(ClipCache<MultiLinestring>(threadNum, indexZoom))
{
//
TileDataSource can only index up to zoom 14. The caller is responsible for
//
ensuring it does not use a higher zoom.
if
(indexZoom >
14
)
throw
std::out_of_range
(
"
TileDataSource: indexZoom cannot be higher than 14, but was
"
+
std::to_string
(indexZoom));
shardBits =
0
;
numShards =
1
;
while
(numShards < threadNum) {
shardBits++;
numShards *=
2
;
}
for
(
int
i =
0
; i < threadNum; i++) {
availablePointStoreLeases.
push_back
(
std::make_pair
(i, &pointStores[i]));
availableLinestringStoreLeases.
push_back
(
std::make_pair
(i, &linestringStores[i]));
availableMultiLinestringStoreLeases.
push_back
(
std::make_pair
(i, &multilinestringStores[i]));
availableMultiPolygonStoreLeases.
push_back
(
std::make_pair
(i, &multipolygonStores[i]));
}
}
thread_local
std::vector<std::tuple<TileCoordinates, OutputObject,
uint64_t
>>* tlsPendingSmallIndexObjects =
nullptr
;
void
TileDataSource::finalize
(
size_t
threadNum) {
uint64_t
finalized =
0
;
for
(
const
auto
& vec : pendingSmallIndexObjects) {
for
(
const
auto
& tuple : vec) {
finalized++;
addObjectToSmallIndexUnsafe
(std::get<
0
>(tuple), std::get<
1
>(tuple), std::get<
2
>(tuple));
}
}
std::cout <<
"
indexed
"
<< finalized <<
"
contended objects
"
<< std::endl;
finalizeObjects<OutputObjectXY>(
name
(), threadNum, indexZoom, objects.
begin
(), objects.
end
(), lowZoomObjects);
finalizeObjects<OutputObjectXYID>(
name
(), threadNum, indexZoom, objectsWithIds.
begin
(), objectsWithIds.
end
(), lowZoomObjectsWithIds);
}
void
TileDataSource::addObjectToSmallIndex
(
const
TileCoordinates& index,
const
OutputObject& oo,
uint64_t
id) {
//
Pick the z6 index
const
size_t
z6x = index.
x
/ z6OffsetDivisor;
const
size_t
z6y = index.
y
/ z6OffsetDivisor;
if
(z6x >=
64
|| z6y >=
64
) {
if
(verbose) std::cerr <<
"
ignoring OutputObject with invalid z
"
<< indexZoom <<
"
coordinates
"
<< index.
x
<<
"
,
"
<< index.
y
<<
"
(id:
"
<< id <<
"
)
"
<< std::endl;
return
;
}
const
size_t
z6index = z6x *
CLUSTER_ZOOM_WIDTH
+ z6y;
auto
& mutex = objectsMutex[z6index % objectsMutex.
size
()];
if
(mutex.
try_lock
()) {
addObjectToSmallIndexUnsafe
(index, oo, id);
mutex.
unlock
();
}
else
{
//
add to tlsPendingSmallIndexObjects
if
(tlsPendingSmallIndexObjects ==
nullptr
) {
std::lock_guard<std::mutex>
lock
(objectsMutex[
0
]);
pendingSmallIndexObjects.
push_back
(std::vector<std::tuple<TileCoordinates, OutputObject,
uint64_t
>>());
tlsPendingSmallIndexObjects = &pendingSmallIndexObjects.
back
();
}
tlsPendingSmallIndexObjects->
push_back
(
std::make_tuple
(index, oo, id));
}
}
void
TileDataSource::addObjectToSmallIndexUnsafe
(
const
TileCoordinates& index,
const
OutputObject& oo,
uint64_t
id) {
//
Pick the z6 index
const
size_t
z6x = index.
x
/ z6OffsetDivisor;
const
size_t
z6y = index.
y
/ z6OffsetDivisor;
const
size_t
z6index = z6x *
CLUSTER_ZOOM_WIDTH
+ z6y;
if
(id ==
0
|| !includeID)
objects[z6index].
push_back
({
oo,
(Z6Offset)(index.
x
- (z6x * z6OffsetDivisor)),
(Z6Offset)(index.
y
- (z6y * z6OffsetDivisor))
});
else
objectsWithIds[z6index].
push_back
({
oo,
(Z6Offset)(index.
x
- (z6x * z6OffsetDivisor)),
(Z6Offset)(index.
y
- (z6y * z6OffsetDivisor)),
id
});
}
void
TileDataSource::collectTilesWithObjectsAtZoom
(std::vector<std::shared_ptr<TileCoordinatesSet>>& zooms) {
//
Scan through all shards. Convert to base zoom, then convert to the requested zoom.
collectTilesWithObjectsAtZoomTemplate<OutputObjectXY>(indexZoom, objects.
begin
(), objects.
size
(), zooms);
collectTilesWithObjectsAtZoomTemplate<OutputObjectXYID>(indexZoom, objectsWithIds.
begin
(), objectsWithIds.
size
(), zooms);
}
void
addCoveredTilesToOutput
(
const
uint indexZoom, std::vector<std::shared_ptr<TileCoordinatesSet>>& zooms,
const
Box& box) {
size_t
maxZoom = zooms.
size
() -
1
;
//
std::cout << "addCoveredTilesToOutput maxZoom=" << maxZoom << ", indexZoom - maxZoom = " << (indexZoom - maxZoom) << std::endl;
int
scale =
pow
(
2
, indexZoom - maxZoom);
TileCoordinate minx = box.
min_corner
().
x
() / scale;
TileCoordinate maxx = box.
max_corner
().
x
() / scale;
TileCoordinate miny = box.
min_corner
().
y
() / scale;
TileCoordinate maxy = box.
max_corner
().
y
() / scale;
for
(
int
x=minx; x<=maxx; x++) {
for
(
int
y=miny; y<=maxy; y++) {
size_t
zx = x, zy = y;
for
(
int
zoom = maxZoom; zoom >=
0
; zoom--) {
zooms[zoom]->
set
(zx, zy);
zx /=
2
;
zy /=
2
;
}
}
}
}
//
Find the tiles used by the "large objects" from the rtree index
void
TileDataSource::collectTilesWithLargeObjectsAtZoom
(std::vector<std::shared_ptr<TileCoordinatesSet>>& zooms) {
for
(
auto
const
&result: boxRtree)
addCoveredTilesToOutput
(indexZoom, zooms, result.
first
);
for
(
auto
const
&result: boxRtreeWithIds)
addCoveredTilesToOutput
(indexZoom, zooms, result.
first
);
}
//
Copy objects from the tile at dstIndex (in the dataset srcTiles) into output
void
TileDataSource::collectObjectsForTile
(
uint zoom,
TileCoordinates dstIndex,
std::vector<OutputObjectID>& output
) {
if
(zoom <
CLUSTER_ZOOM
) {
collectLowZoomObjectsForTile<OutputObjectXY>(indexZoom, lowZoomObjects, zoom, dstIndex, output);
collectLowZoomObjectsForTile<OutputObjectXYID>(indexZoom, lowZoomObjectsWithIds, zoom, dstIndex, output);
return
;
}
size_t
iStart =
0
;
size_t
iEnd = objects.
size
();
if
(zoom >=
CLUSTER_ZOOM
) {
TileCoordinate z6x = dstIndex.
x
/ (
1
<< (zoom -
CLUSTER_ZOOM
));
TileCoordinate z6y = dstIndex.
y
/ (
1
<< (zoom -
CLUSTER_ZOOM
));
if
(z6x >=
64
|| z6y >=
64
) {
if
(verbose) std::cerr <<
"
collectObjectsForTile: invalid tile z
"
<< zoom <<
"
/
"
<< dstIndex.
x
<<
"
/
"
<< dstIndex.
y
<< std::endl;
return
;
}
iStart = z6x *
CLUSTER_ZOOM_WIDTH
+ z6y;
iEnd = iStart +
1
;
}
collectObjectsForTileTemplate<OutputObjectXY>(indexZoom, objects.
begin
(), iStart, iEnd, zoom, dstIndex, output);
collectObjectsForTileTemplate<OutputObjectXYID>(indexZoom, objectsWithIds.
begin
(), iStart, iEnd, zoom, dstIndex, output);
}
//
Copy objects from the large index into output
void
TileDataSource::collectLargeObjectsForTile
(
uint zoom,
TileCoordinates dstIndex,
std::vector<OutputObjectID>& output
) {
unsigned
int
clampedZoom = zoom;
while
(clampedZoom > indexZoom) {
clampedZoom--;
dstIndex.
x
/=
2
;
dstIndex.
y
/=
2
;
}
int
scale =
pow
(
2
, indexZoom - clampedZoom);
TileCoordinates
srcIndex1
( dstIndex.
x
*scale , dstIndex.
y
*scale );
TileCoordinates
srcIndex2
((dstIndex.
x
+
1
)*scale-
1
, (dstIndex.
y
+
1
)*scale-
1
);
Box box =
Box
(geom::make<Point>(srcIndex1.
x
, srcIndex1.
y
),
geom::make<Point>(srcIndex2.
x
, srcIndex2.
y
));
for
(
auto
const
& result: boxRtree |
boost::geometry::index::adaptors::queried
(
boost::geometry::index::intersects
(box))) {
if
(result.
second
.
minZoom
<= zoom)
output.
push_back
({result.
second
,
0
});
}
for
(
auto
const
& result: boxRtreeWithIds |
boost::geometry::index::adaptors::queried
(
boost::geometry::index::intersects
(box))) {
if
(result.
second
.
oo
.
minZoom
<= zoom)
output.
push_back
({result.
second
.
oo
, result.
second
.
id
});
}
}
//
Build node and way geometries
Geometry
TileDataSource::buildWayGeometry
(OutputGeometryType
const
geomType,
NodeID
const
objectID,
const
TileBbox &bbox) {
switch
(geomType) {
case
POINT_
: {
throw
std::runtime_error
(
"
unexpected geomType in buildWayGeometry
"
);
}
case
LINESTRING_
: {
auto
const
&ls =
retrieveLinestring
(objectID);
MultiLinestring out;
if
(ls.
empty
())
return
out;
Box extBox = bbox.
getExtendBox
();
const
double
minX = extBox.
min_corner
().
x
(), maxX = extBox.
max_corner
().
x
();
const
double
minY = extBox.
min_corner
().
y
(), maxY = extBox.
max_corner
().
y
();
auto
pointInsideExtBox = [minX, maxX, minY, maxY](
const
Point& p) {
return
p.
x
() >= minX && p.
x
() <= maxX && p.
y
() >= minY && p.
y
() <= maxY;
};
bool
needsIntersection = !
pointInsideExtBox
(ls[
0
]);
Linestring current_ls;
geom::append
(current_ls, ls[
0
]);
for
(
size_t
i =
1
; i < ls.
size
(); ++i) {
boost::geometry::model::segment<Point>
segment
(ls[i-
1
], ls[i]);
if
(!
geom::intersects
(segment, bbox.
clippingBox
)) {
if
(current_ls.
size
() >
1
)
out.
push_back
(
std::move
(current_ls));
current_ls.
clear
();
}
geom::append
(current_ls, ls[i]);
if
(!needsIntersection)
needsIntersection = !
pointInsideExtBox
(ls[i]);
}
if
(current_ls.
size
() >
1
)
out.
push_back
(
std::move
(current_ls));
if
(!needsIntersection)
return
out;
MultiLinestring result;
geom::intersection
(out, extBox, result);
return
result;
}
case
MULTILINESTRING_
: {
//
Look for a previously clipped version at z-1, z-2, ...
std::shared_ptr<MultiLinestring> cachedClip = multiLinestringClipCache.
get
(bbox.
zoom
, bbox.
index
.
x
, bbox.
index
.
y
, objectID);
MultiLinestring uncached;
if
(cachedClip ==
nullptr
) {
const
auto
& input =
retrieveMultiLinestring
(objectID);
boost::geometry::assign
(uncached, input);
}
const
auto
&mls = cachedClip ==
nullptr
? uncached : *cachedClip;
//
investigate whether filtering the constituent linestrings improves performance
MultiLinestring result;
geom::intersection
(mls, bbox.
getExtendBox
(), result);
multiLinestringClipCache.
add
(bbox, objectID, result);
return
result;
}
case
POLYGON_
: {
//
Look for a previously clipped version at z-1, z-2, ...
std::shared_ptr<MultiPolygon> cachedClip = multiPolygonClipCache.
get
(bbox.
zoom
, bbox.
index
.
x
, bbox.
index
.
y
, objectID);
MultiPolygon uncached;
if
(cachedClip ==
nullptr
) {
//
The cached multipolygon uses a non-standard allocator, so copy it
populateMultiPolygon
(uncached, objectID);
}
const
auto
&input = cachedClip ==
nullptr
? uncached : *cachedClip;
Box box = bbox.
clippingBox
;
if
(bbox.
endZoom
) {
for
(
auto
const
&p: input) {
for
(
auto
const
&inner: p.
inners
()) {
for
(std::
size_t
i =
0
; i < inner.
size
() -
1
; ++i)
{
Point p1 = inner[i];
Point p2 = inner[i +
1
];
if
(
geom::within
(p1, bbox.
clippingBox
) !=
geom::within
(p2, bbox.
clippingBox
)) {
box.
min_corner
() =
Point
(
std::min
(box.
min_corner
().
x
(),
std::min
(p1.
x
(), p2.
x
())),
std::min
(box.
min_corner
().
y
(),
std::min
(p1.
y
(), p2.
y
())));
box.
max_corner
() =
Point
(
std::max
(box.
max_corner
().
x
(),
std::max
(p1.
x
(), p2.
x
())),
std::max
(box.
max_corner
().
y
(),
std::max
(p1.
y
(), p2.
y
())));
}
}
}
for
(std::
size_t
i =
0
; i < p.
outer
().
size
() -
1
; ++i) {
Point p1 = p.
outer
()[i];
Point p2 = p.
outer
()[i +
1
];
if
(
geom::within
(p1, bbox.
clippingBox
) !=
geom::within
(p2, bbox.
clippingBox
)) {
box.
min_corner
() =
Point
(
std::min
(box.
min_corner
().
x
(),
std::min
(p1.
x
(), p2.
x
())),
std::min
(box.
min_corner
().
y
(),
std::min
(p1.
y
(), p2.
y
())));
box.
max_corner
() =
Point
(
std::max
(box.
max_corner
().
x
(),
std::max
(p1.
x
(), p2.
x
())),
std::max
(box.
max_corner
().
y
(),
std::max
(p1.
y
(), p2.
y
())));
}
}
}
Box extBox = bbox.
getExtendBox
();
box.
min_corner
() =
Point
(
std::max
(box.
min_corner
().
x
(), extBox.
min_corner
().
x
()),
std::max
(box.
min_corner
().
y
(), extBox.
min_corner
().
y
()));
box.
max_corner
() =
Point
(
std::min
(box.
max_corner
().
x
(), extBox.
max_corner
().
x
()),
std::min
(box.
max_corner
().
y
(), extBox.
max_corner
().
y
()));
}
MultiPolygon mp;
if
(cachedClip ==
nullptr
)
mp =
std::move
(uncached);
else
geom::assign
(mp, input);
fast_clip
(mp, box);
geom::correct
(mp);
geom::validity_failure_type failure = geom::validity_failure_type::no_failure;
bool
valid =
geom::is_valid
(mp,failure);
if
(!valid) {
if
(verbose) {
//
Build the whole line first and emit it with a single stream write:
//
tilemaker runs multi-threaded and chained operator<< calls are not
//
atomic, so per-token writes interleave into unreadable output.
std::ostringstream msg;
msg <<
"
invalid multipolygon for object
"
<< objectID
<<
"
at z
"
<< bbox.
zoom
<<
"
"
<< bbox.
index
.
x
<<
"
/
"
<< bbox.
index
.
y
<<
"
:
"
<<
validityFailureName
(failure) <<
"
\n
"
;
std::cerr << msg.
str
();
}
if
(failure==geom::failure_spikes) {
geom::remove_spikes
(mp);
failure = geom::validity_failure_type::no_failure;
valid =
geom::is_valid
(mp,failure);
}
if
(!valid && (failure==geom::failure_self_intersections || failure==geom::failure_intersecting_interiors)) {
//
fast_clip can introduce self-intersections; redo the clip with the
//
slower but robust Boost intersection against the original geometry.
MultiPolygon output;
if
(cachedClip ==
nullptr
) {
MultiPolygon original;
populateMultiPolygon
(original, objectID);
geom::intersection
(original, box, output);
}
else
{
geom::intersection
(input, box, output);
}
geom::correct
(output);
//
The intersection result can itself still be invalid for very complex
//
multipolygons (e.g. large reservoirs), which previously produced
//
dropped or holey tiles. Repair it before returning.
repairMultiPolygon
(output, objectID);
multiPolygonClipCache.
add
(bbox, objectID, output);
return
output;
}
else
if
(!valid) {
//
occasionally also wrong_topological_dimension, disconnected_interior:
//
defects geom::correct cannot mend. Repair mp in place; on failure it
//
is left unchanged so behaviour never regresses.
repairMultiPolygon
(mp, objectID);
}
}
multiPolygonClipCache.
add
(bbox, objectID, mp);
return
mp;
}
default
:
throw
std::runtime_error
(
"
Invalid output geometry
"
);
}
}
LatpLon
TileDataSource::buildNodeGeometry
(NodeID
const
objectID,
const
TileBbox &bbox)
const
{
auto
p =
retrievePoint
(objectID);
LatpLon out;
out.
latp
= p.
y
();
out.
lon
= p.
x
();
return
out;
}
//
Report number of stored geometries
void
TileDataSource::reportSize
()
const
{
size_t
points =
0
, linestrings =
0
, polygons =
0
;
for
(
const
auto
& store : pointStores)
points += store.
size
();
for
(
const
auto
& store : linestringStores)
linestrings += store.
size
();
for
(
const
auto
& store : multilinestringStores)
linestrings += store.
size
();
for
(
const
auto
& store : multipolygonStores)
polygons += store.
size
();
std::cout <<
"
Generated points:
"
<< (points -
1
) <<
"
, lines:
"
<< (linestrings -
2
) <<
"
, polygons:
"
<< (polygons -
1
) << std::endl;
}
void
populateTilesAtZoom
(
const
std::vector<
class
TileDataSource
*>& sources,
std::vector<std::shared_ptr<TileCoordinatesSet>>& zooms
) {
if
(zooms.
size
() >
15
)
throw
std::out_of_range
(
"
populateTilesAtZoom: expected at most z14 zooms (15), but found
"
+
std::to_string
(zooms.
size
()) +
"
vectors
"
);
for
(
size_t
i=
0
; i<sources.
size
(); i++) {
sources[i]->
collectTilesWithObjectsAtZoom
(zooms);
sources[i]->
collectTilesWithLargeObjectsAtZoom
(zooms);
}
}
void
sortOutputObjectIDs
(
const
std::vector<
bool
>& sortOrders,
std::vector<OutputObjectID>& data
);
std::vector<OutputObjectID>
TileDataSource::getObjectsForTile
(
const
std::vector<
bool
>& sortOrders,
unsigned
int
zoom,
TileCoordinates coordinates
) {
std::vector<OutputObjectID> data;
collectObjectsForTile
(zoom, coordinates, data);
collectLargeObjectsForTile
(zoom, coordinates, data);
sortOutputObjectIDs
(sortOrders, data);
data.
erase
(
unique
(data.
begin
(), data.
end
()), data.
end
());
return
data;
}
//
------------------------------------
//
Add geometries to tile/large indices
void
TileDataSource::addGeometryToIndex
(
const
Linestring& geom,
const
std::vector<OutputObject>& outputs,
const
uint64_t
id
) {
unordered_set<TileCoordinates> tileSet;
try
{
insertIntermediateTiles
(geom, indexZoom, tileSet);
bool
polygonExists =
false
;
TileCoordinate minTileX = std::numeric_limits<TileCoordinate>::
max
(), maxTileX =
0
, minTileY = std::numeric_limits<TileCoordinate>::
max
(), maxTileY =
0
;
for
(
auto
it = tileSet.
begin
(); it != tileSet.
end
(); ++it) {
TileCoordinates index = *it;
minTileX =
std::min
(index.
x
, minTileX);
minTileY =
std::min
(index.
y
, minTileY);
maxTileX =
std::max
(index.
x
, maxTileX);
maxTileY =
std::max
(index.
y
, maxTileY);
for
(
const
auto
& output : outputs) {
if
(output.
geomType
==
POLYGON_
) {
polygonExists =
true
;
continue
;
}
addObjectToSmallIndex
(index, output, id);
//
not a polygon
}
}
//
for polygon, fill inner tiles
if
(polygonExists) {
bool
tilesetFilled =
false
;
uint size = (maxTileX - minTileX +
1
) * (maxTileY - minTileY +
1
);
for
(
const
auto
& output : outputs) {
if
(output.
geomType
!=
POLYGON_
)
continue
;
if
(size>=
16
) {
//
Larger objects - add to rtree
Box box =
Box
(geom::make<Point>(minTileX, minTileY),
geom::make<Point>(maxTileX, maxTileY));
addObjectToLargeIndex
(box, output, id);
}
else
{
//
Smaller objects - add to each individual tile index
if
(!tilesetFilled) {
fillCoveredTiles
(tileSet); tilesetFilled =
true
; }
for
(
auto
it = tileSet.
begin
(); it != tileSet.
end
(); ++it) {
TileCoordinates index = *it;
addObjectToSmallIndex
(index, output, id);
}
}
}
}
}
catch
(std::out_of_range &err) {
cerr <<
"
Error calculating intermediate tiles:
"
<< err.
what
() << endl;
}
}
void
TileDataSource::addGeometryToIndex
(
const
MultiLinestring& geom,
const
std::vector<OutputObject>& outputs,
const
uint64_t
id
) {
for
(
const
auto
& ls : geom) {
unordered_set<TileCoordinates> tileSet;
insertIntermediateTiles
(ls, indexZoom, tileSet);
for
(
auto
it = tileSet.
begin
(); it != tileSet.
end
(); ++it) {
TileCoordinates index = *it;
for
(
const
auto
& output : outputs) {
addObjectToSmallIndex
(index, output, id);
}
}
}
}
void
TileDataSource::addGeometryToIndex
(
const
MultiPolygon& geom,
std::vector<OutputObject>& outputs,
const
uint64_t
id
) {
unordered_set<TileCoordinates> tileSet;
bool
singleOuter = geom.
size
()==
1
;
for
(
const
auto
& poly : geom) {
unordered_set<TileCoordinates> tileSetTmp;
insertIntermediateTiles
(poly.
outer
(), indexZoom, tileSetTmp);
fillCoveredTiles
(tileSetTmp);
if
(singleOuter) {
tileSet =
std::move
(tileSetTmp);
}
else
{
tileSet.
insert
(tileSetTmp.
begin
(), tileSetTmp.
end
());
}
}
TileCoordinate minTileX = std::numeric_limits<TileCoordinate>::
max
(), maxTileX =
0
, minTileY = std::numeric_limits<TileCoordinate>::
max
(), maxTileY =
0
;
for
(
auto
it = tileSet.
begin
(); it != tileSet.
end
(); ++it) {
TileCoordinates index = *it;
minTileX =
std::min
(index.
x
, minTileX);
minTileY =
std::min
(index.
y
, minTileY);
maxTileX =
std::max
(index.
x
, maxTileX);
maxTileY =
std::max
(index.
y
, maxTileY);
}
const
size_t
tileSetSize = tileSet.
size
();
for
(
auto
& output : outputs) {
if
(tileSetSize >=
16
) {
//
Larger objects - add to rtree
//
note that the bbox is currently the envelope of the entire multipolygon,
//
which is suboptimal in shapes like (_) ...... (_) where the outers are significantly disjoint
Box box =
Box
(geom::make<Point>(minTileX, minTileY),
geom::make<Point>(maxTileX, maxTileY));
addObjectToLargeIndex
(box, output, id);
}
else
{
//
Smaller objects - add to each individual tile index
for
(
auto
it = tileSet.
begin
(); it != tileSet.
end
(); ++it) {
TileCoordinates index = *it;
addObjectToSmallIndex
(index, output, id);
}
}
}
}
NodeID
TileDataSource::storePoint
(
const
Point& input) {
const
auto
& store = pointStore.
get
(
this
);
NodeID offset = store.
second
->
size
();
store.
second
->
emplace_back
(input);
NodeID rv = (store.
first
<< (
TILE_DATA_ID_SIZE
- shardBits)) + offset;
return
rv;
}
NodeID
TileDataSource::storeLinestring
(
const
Linestring& src) {
const
auto
& store = linestringStore.
get
(
this
);
linestring_t
dst
(src.
begin
(), src.
end
());
NodeID offset = store.
second
->
size
();
store.
second
->
emplace_back
(
std::move
(dst));
NodeID rv = (store.
first
<< (
TILE_DATA_ID_SIZE
- shardBits)) + offset;
return
rv;
}
NodeID
TileDataSource::storeMultiPolygon
(
const
MultiPolygon& src) {
const
auto
& store = multipolygonStore.
get
(
this
);
multi_polygon_t
dst;
dst.
resize
(src.
size
());
for
(std::
size_t
i =
0
; i < src.
size
(); ++i) {
dst[i].
outer
().
resize
(src[i].
outer
().
size
());
boost::geometry::assign
(dst[i].
outer
(), src[i].
outer
());
dst[i].
inners
().
resize
(src[i].
inners
().
size
());
for
(std::
size_t
j =
0
; j < src[i].
inners
().
size
(); ++j) {
dst[i].
inners
()[j].
resize
(src[i].
inners
()[j].
size
());
boost::geometry::assign
(dst[i].
inners
()[j], src[i].
inners
()[j]);
}
}
NodeID offset = store.
second
->
size
();
store.
second
->
emplace_back
(
std::move
(dst));
NodeID rv = (store.
first
<< (
TILE_DATA_ID_SIZE
- shardBits)) + offset;
return
rv;
}
NodeID
TileDataSource::storeMultiLinestring
(
const
MultiLinestring& src) {
const
auto
& store = multilinestringStore.
get
(
this
);
multi_linestring_t
dst;
dst.
resize
(src.
size
());
for
(std::
size_t
i=
0
; i<src.
size
(); ++i) {
boost::geometry::assign
(dst[i], src[i]);
}
NodeID offset = store.
second
->
size
();
store.
second
->
emplace_back
(
std::move
(dst));
NodeID rv = (store.
first
<< (
TILE_DATA_ID_SIZE
- shardBits)) + offset;
return
rv;
}
void
TileDataSource::populateMultiPolygon
(MultiPolygon& dst, NodeID objectID) {
const
auto
&input =
retrieveMultiPolygon
(objectID);
dst.
resize
(input.
size
());
for
(std::
size_t
i =
0
; i < input.
size
(); ++i) {
dst[i].
outer
().
resize
(input[i].
outer
().
size
());
boost::geometry::assign
(dst[i].
outer
(), input[i].
outer
());
dst[i].
inners
().
resize
(input[i].
inners
().
size
());
for
(std::
size_t
j =
0
; j < input[i].
inners
().
size
(); ++j) {
dst[i].
inners
()[j].
resize
(input[i].
inners
()[j].
size
());
boost::geometry::assign
(dst[i].
inners
()[j], input[i].
inners
()[j]);
}
}
}
Back
|
FazBrowse Home
|
New Git URL