forked from systemed/tilemaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosm_lua_processing.cpp
More file actions
1222 lines (1057 loc) · 42 KB
/
osm_lua_processing.cpp
File metadata and controls
1222 lines (1057 loc) · 42 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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include "osm_lua_processing.h"
#include "attribute_store.h"
#include "helpers.h"
#include "coordinates_geom.h"
#include "osm_mem_tiles.h"
#include "significant_tags.h"
#include "tag_map.h"
#include "node_store.h"
#include "polylabel.h"
#include <signal.h>
using namespace std;
const std::string EMPTY_STRING = "";
thread_local kaguya::State *g_luaState = nullptr;
thread_local OsmLuaProcessing* osmLuaProcessing = nullptr;
std::mutex vectorLayerMetadataMutex;
std::unordered_map<std::string, std::string> OsmLuaProcessing::dataStore;
std::mutex OsmLuaProcessing::dataStoreMutex;
void handleOsmLuaProcessingUserSignal(int signum) {
osmLuaProcessing->handleUserSignal(signum);
}
class Sigusr1Handler {
public:
Sigusr1Handler() {
#ifndef _WIN32
signal(SIGUSR1, handleOsmLuaProcessingUserSignal);
#endif
}
void initialize() {
// No-op just to ensure the compiler doesn't optimize away
// the handler.
}
};
thread_local Sigusr1Handler sigusr1Handler;
// A key in `currentTags`. If Lua code refers to an absent key,
// found will be false.
struct KnownTagKey {
bool found;
uint32_t index;
// stringValue is populated only in PostScanRelations phase; we could consider
// having osm_store's relationTags use TagMap, in which case we'd be able to
// use the found/index fields
std::string stringValue;
};
template<> struct kaguya::lua_type_traits<KnownTagKey> {
typedef KnownTagKey get_type;
typedef const KnownTagKey& push_type;
static bool strictCheckType(lua_State* l, int index)
{
return lua_type(l, index) == LUA_TSTRING;
}
static bool checkType(lua_State* l, int index)
{
return lua_isstring(l, index) != 0;
}
static get_type get(lua_State* l, int index)
{
KnownTagKey rv = { false, 0 };
size_t size = 0;
const char* buffer = lua_tolstring(l, index, &size);
if (osmLuaProcessing->isPostScanRelation) {
// In this phase, the Holds/Find functions directly query a
// traditional string->string map, so just ensure we expose
// the string.
rv.stringValue = std::string(buffer, size);
return rv;
}
int64_t tagLoc = osmLuaProcessing->currentTags->getKey(buffer, size);
if (tagLoc >= 0) {
rv.found = true;
rv.index = tagLoc;
}
// std::string key(buffer, size);
// std::cout << "for key " << key << ": rv.found=" << rv.found << ", rv.index=" << rv.index << std::endl;
return rv;
}
static int push(lua_State* l, push_type s)
{
throw std::runtime_error("Lua code doesn't know how to use KnownTagKey");
}
};
template<> struct kaguya::lua_type_traits<protozero::data_view> {
typedef protozero::data_view get_type;
typedef const protozero::data_view& push_type;
static bool strictCheckType(lua_State* l, int index)
{
return lua_type(l, index) == LUA_TSTRING;
}
static bool checkType(lua_State* l, int index)
{
return lua_isstring(l, index) != 0;
}
static get_type get(lua_State* l, int index)
{
size_t size = 0;
const char* buffer = lua_tolstring(l, index, &size);
protozero::data_view rv = { buffer, size };
return rv;
}
static int push(lua_State* l, push_type s)
{
throw std::runtime_error("Lua code doesn't know how to use protozero::data_view");
}
};
// Gets a table of all the keys of the OSM tags
kaguya::LuaTable getAllKeys(kaguya::State& luaState, const boost::container::flat_map<std::string, std::string>* tags) {
kaguya::LuaTable tagsTable = luaState.newTable();
int index = 1; // Lua is 1-based
for (const auto& kv: *tags) {
tagsTable[index++] = kv.first;
}
return tagsTable;
}
// Gets a table of all the OSM tags
kaguya::LuaTable getAllTags(kaguya::State& luaState, const boost::container::flat_map<std::string, std::string>* tags) {
kaguya::LuaTable tagsTable = luaState.newTable();
for (const auto& kv: *tags) {
tagsTable[kv.first] = kv.second;
}
return tagsTable;
}
std::string rawId() { return osmLuaProcessing->Id(); }
std::string rawOsmType() { return osmLuaProcessing->OsmType(); }
kaguya::LuaTable rawAllKeys() {
if (osmLuaProcessing->isPostScanRelation) {
return osmLuaProcessing->AllKeys(*g_luaState);
}
auto tags = osmLuaProcessing->currentTags->exportToBoostMap();
return getAllKeys(*g_luaState, &tags);
}kaguya::LuaTable rawAllTags() {
if (osmLuaProcessing->isPostScanRelation) {
return osmLuaProcessing->AllTags(*g_luaState);
}
auto tags = osmLuaProcessing->currentTags->exportToBoostMap();
return getAllTags(*g_luaState, &tags);
}
bool rawHolds(const KnownTagKey& key) {
if (osmLuaProcessing->isPostScanRelation) {
return osmLuaProcessing->Holds(key.stringValue);
}
return key.found;
}
bool rawHasTags() { return osmLuaProcessing->HasTags(); }
void rawSetTag(const std::string &key, const std::string &value) { return osmLuaProcessing->SetTag(key, value); }
const std::string rawFind(const KnownTagKey& key) {
if (osmLuaProcessing->isPostScanRelation)
return osmLuaProcessing->Find(key.stringValue);
if (key.found) {
auto value = *(osmLuaProcessing->currentTags->getValueFromKey(key.index));
return std::string(value.data(), value.size());
}
return EMPTY_STRING;
}
std::vector<std::string> rawFindIntersecting(const std::string &layerName) { return osmLuaProcessing->FindIntersecting(layerName); }
bool rawIntersects(const std::string& layerName) { return osmLuaProcessing->Intersects(layerName); }
std::vector<std::string> rawFindCovering(const std::string& layerName) { return osmLuaProcessing->FindCovering(layerName); }
bool rawCoveredBy(const std::string& layerName) { return osmLuaProcessing->CoveredBy(layerName); }
bool rawIsClosed() { return osmLuaProcessing->IsClosed(); }
bool rawIsMultiPolygon() { return osmLuaProcessing->IsMultiPolygon(); }
double rawArea() { return osmLuaProcessing->Area(); }
double rawLength() { return osmLuaProcessing->Length(); }
kaguya::optional<std::vector<double>> rawCentroid(kaguya::VariadicArgType algorithm) { return osmLuaProcessing->Centroid(algorithm); }
void rawModifyId(const int newId) { return osmLuaProcessing->ModifyId(newId); }
void rawLayer(const std::string& layerName, bool area) { return osmLuaProcessing->Layer(layerName, area); }
void rawLayerAsCentroid(const std::string &layerName, kaguya::VariadicArgType nodeSources) { return osmLuaProcessing->LayerAsCentroid(layerName, nodeSources); }
void rawMinZoom(const double z) { return osmLuaProcessing->MinZoom(z); }
void rawZOrder(const double z) { return osmLuaProcessing->ZOrder(z); }
OsmLuaProcessing::OptionalRelation rawNextRelation() { return osmLuaProcessing->NextRelation(); }
void rawRestartRelations() { return osmLuaProcessing->RestartRelations(); }
std::string rawFindInRelation(const std::string& key) { return osmLuaProcessing->FindInRelation(key); }
void rawAccept() { return osmLuaProcessing->Accept(); }
double rawAreaIntersecting(const std::string& layerName) { return osmLuaProcessing->AreaIntersecting(layerName); }
void rawSetData(const std::string &key, const std::string &value) {
std::lock_guard<std::mutex> lock(osmLuaProcessing->dataStoreMutex);
osmLuaProcessing->dataStore[key] = value;
}
std::string rawGetData(const std::string &key) {
auto r = osmLuaProcessing->dataStore.find(key);
return r==osmLuaProcessing->dataStore.end() ? "" : r->second;
}
bool supportsRemappingShapefiles = false;
int lua_error_handler(int errCode, const char *errMessage)
{
std::cerr << "lua runtime error " << std::to_string(errCode) << ":" << std::endl;
std::cerr << errMessage << std::endl;
kaguya::util::traceBack(g_luaState->state(), errMessage); // full traceback on 5.2+
kaguya::util::stackDump(g_luaState->state());
throw OsmLuaProcessing::luaProcessingException();
}
// ---- initialization routines
OsmLuaProcessing::OsmLuaProcessing(
OSMStore &osmStore,
const class Config &configIn,
class LayerDefinition &layers,
const string &luaFile,
const class ShpMemTiles &shpMemTiles,
class OsmMemTiles &osmMemTiles,
AttributeStore &attributeStore,
bool materializeGeometries,
bool isFirst) :
osmStore(osmStore),
shpMemTiles(shpMemTiles),
osmMemTiles(osmMemTiles),
attributeStore(attributeStore),
config(configIn),
currentTags(NULL),
layers(layers),
materializeGeometries(materializeGeometries) {
sigusr1Handler.initialize();
// ---- Initialise Lua
g_luaState = &luaState;
luaState.setErrorHandler(lua_error_handler);
luaState.dofile(luaFile.c_str());
osmLuaProcessing = this;
luaState["Id"] = &rawId;
luaState["OsmType"] = &rawOsmType;
luaState["AllKeys"] = &rawAllKeys;
luaState["AllTags"] = &rawAllTags;
luaState["Holds"] = &rawHolds;
luaState["Find"] = &rawFind;
luaState["HasTags"] = &rawHasTags;
luaState["SetTag"] = &rawSetTag;
luaState["FindIntersecting"] = &rawFindIntersecting;
luaState["Intersects"] = &rawIntersects;
luaState["FindCovering"] = &rawFindCovering;
luaState["CoveredBy"] = &rawCoveredBy;
luaState["IsClosed"] = &rawIsClosed;
luaState["IsMultiPolygon"] = &rawIsMultiPolygon;
luaState["Area"] = &rawArea;
luaState["AreaIntersecting"] = &rawAreaIntersecting;
luaState["Length"] = &rawLength;
luaState["Centroid"] = &rawCentroid;
luaState["Layer"] = &rawLayer;
luaState["LayerAsCentroid"] = &rawLayerAsCentroid;
luaState["ModifyId"] = &rawModifyId;
luaState["Attribute"] = kaguya::overload(
[](const std::string &key, const protozero::data_view val) { osmLuaProcessing->Attribute(key, val, 0); },
[](const std::string &key, const protozero::data_view val, const char minzoom) { osmLuaProcessing->Attribute(key, val, minzoom); }
);
luaState["AttributeNumeric"] = kaguya::overload(
[](const std::string &key, const double val) { osmLuaProcessing->AttributeNumeric(key, val, 0); },
[](const std::string &key, const double val, const char minzoom) { osmLuaProcessing->AttributeNumeric(key, val, minzoom); }
);
luaState["AttributeInteger"] = kaguya::overload(
[](const std::string &key, const int val) { osmLuaProcessing->AttributeInteger(key, val, 0); },
[](const std::string &key, const int val, const char minzoom) { osmLuaProcessing->AttributeInteger(key, val, minzoom); }
);
luaState["AttributeBoolean"] = kaguya::overload(
[](const std::string &key, const bool val) { osmLuaProcessing->AttributeBoolean(key, val, 0); },
[](const std::string &key, const bool val, const char minzoom) { osmLuaProcessing->AttributeBoolean(key, val, minzoom); }
);
luaState["MinZoom"] = &rawMinZoom;
luaState["ZOrder"] = &rawZOrder;
luaState["Accept"] = &rawAccept;
luaState["NextRelation"] = &rawNextRelation;
luaState["RestartRelations"] = &rawRestartRelations;
luaState["FindInRelation"] = &rawFindInRelation;
luaState["SetData"] = &rawSetData;
luaState["GetData"] = &rawGetData;
supportsRemappingShapefiles = !!luaState["attribute_function"];
supportsReadingRelations = !!luaState["relation_scan_function"];
supportsPostScanRelations = !!luaState["relation_postscan_function"];
supportsWritingNodes = !!luaState["node_function"];
supportsWritingWays = !!luaState["way_function"];
supportsWritingRelations = !!luaState["relation_function"];
// ---- Call init_function of Lua logic
if (!!luaState["init_function"]) {
luaState["init_function"](this->config.projectName, isFirst);
}
}
OsmLuaProcessing::~OsmLuaProcessing() {
// Call exit_function of Lua logic
luaState("if exit_function~=nil then exit_function() end");
}
void OsmLuaProcessing::handleUserSignal(int signum) {
std::cout << "processing OSM ID " << originalOsmID << std::endl;
}
// ---- Helpers provided for main routine
// Has this object been assigned to any layers?
bool OsmLuaProcessing::empty() {
return outputs.size()==0;
}
bool OsmLuaProcessing::canRemapShapefiles() {
return supportsRemappingShapefiles;
}
bool OsmLuaProcessing::canReadRelations() {
return supportsReadingRelations;
}
bool OsmLuaProcessing::canPostScanRelations() {
return supportsPostScanRelations;
}
bool OsmLuaProcessing::canWriteNodes() {
return supportsWritingNodes;
}
bool OsmLuaProcessing::canWriteWays() {
return supportsWritingWays;
}
bool OsmLuaProcessing::canWriteRelations() {
return supportsWritingRelations;
}
kaguya::LuaTable OsmLuaProcessing::newTable() {
return luaState.newTable();//kaguya::LuaTable(luaState);
}
kaguya::LuaTable OsmLuaProcessing::remapAttributes(kaguya::LuaTable& in_table, const std::string &layerName) {
kaguya::LuaTable out_table = luaState["attribute_function"].call<kaguya::LuaTable>(in_table, layerName);
return out_table;
}
// ---- Metadata queries called from Lua
// Get the ID of the current object
string OsmLuaProcessing::Id() const {
return to_string(originalOsmID);
}
// Get the Type of the current object
string OsmLuaProcessing::OsmType() const {
return (isRelation ? "relation" : isWay ? "way" : "node");
}
// Gets a table of all the keys of the OSM tags
kaguya::LuaTable OsmLuaProcessing::AllKeys(kaguya::State& luaState) {
// NOTE: this is only called in the PostScanRelation phase -- other phases are handled in rawAllKeys
return getAllKeys(luaState, currentPostScanTags);
}
// Gets a table of all the OSM tags
kaguya::LuaTable OsmLuaProcessing::AllTags(kaguya::State& luaState) {
// NOTE: this is only called in the PostScanRelation phase -- other phases are handled in rawAllTags
return getAllTags(luaState, currentPostScanTags);
}
// Check if there's a value for a given key
bool OsmLuaProcessing::Holds(const string& key) const {
// NOTE: this is only called in the PostScanRelation phase -- other phases are handled in rawHolds
return currentPostScanTags->find(key)!=currentPostScanTags->end();
}
// Get an OSM tag for a given key (or return empty string if none)
const string OsmLuaProcessing::Find(const string& key) const {
// NOTE: this is only called in the PostScanRelation phase -- other phases are handled in rawFind
auto it = currentPostScanTags->find(key);
if (it == currentPostScanTags->end()) return EMPTY_STRING;
return it->second;
}
// Check if an object has any tags
bool OsmLuaProcessing::HasTags() const {
return isPostScanRelation ? !currentPostScanTags->empty() : !currentTags->empty();
}
// ---- Spatial queries called from Lua
vector<string> OsmLuaProcessing::FindIntersecting(const string &layerName) {
if (!isWay ) { return shpMemTiles.namesOfGeometries(intersectsQuery(layerName, false, getPoint())); }
else if (!isClosed && isRelation) { return shpMemTiles.namesOfGeometries(intersectsQuery(layerName, false, multiLinestringCached())); }
else if (!isClosed) { return shpMemTiles.namesOfGeometries(intersectsQuery(layerName, false, linestringCached())); }
else if (isRelation){ return shpMemTiles.namesOfGeometries(intersectsQuery(layerName, false, multiPolygonCached())); }
else { return shpMemTiles.namesOfGeometries(intersectsQuery(layerName, false, polygonCached())); }
}
bool OsmLuaProcessing::Intersects(const string &layerName) {
if (!isWay ) { return !intersectsQuery(layerName, true, getPoint()).empty(); }
else if (!isClosed) { return !intersectsQuery(layerName, true, linestringCached()).empty(); }
else if (!isClosed && isRelation) { return !intersectsQuery(layerName, true, multiLinestringCached()).empty(); }
else if (isRelation){ return !intersectsQuery(layerName, true, multiPolygonCached()).empty(); }
else { return !intersectsQuery(layerName, true, polygonCached()).empty(); }
}
vector<string> OsmLuaProcessing::FindCovering(const string &layerName) {
if (!isWay ) { return shpMemTiles.namesOfGeometries(coveredQuery(layerName, false, getPoint())); }
else if (!isClosed) { return shpMemTiles.namesOfGeometries(coveredQuery(layerName, false, linestringCached())); }
else if (!isClosed && isRelation) { return shpMemTiles.namesOfGeometries(coveredQuery(layerName, false, multiLinestringCached())); }
else if (isRelation){ return shpMemTiles.namesOfGeometries(coveredQuery(layerName, false, multiPolygonCached())); }
else { return shpMemTiles.namesOfGeometries(coveredQuery(layerName, false, polygonCached())); }
}
bool OsmLuaProcessing::CoveredBy(const string &layerName) {
if (!isWay ) { return !coveredQuery(layerName, true, getPoint()).empty(); }
else if (!isClosed) { return !coveredQuery(layerName, true, linestringCached()).empty(); }
else if (!isClosed && isRelation) { return !coveredQuery(layerName, true, multiLinestringCached()).empty(); }
else if (isRelation){ return !coveredQuery(layerName, true, multiPolygonCached()).empty(); }
else { return !coveredQuery(layerName, true, polygonCached()).empty(); }
}
double OsmLuaProcessing::AreaIntersecting(const string &layerName) {
if (!isWay || !isClosed) { return 0.0; }
else if (isRelation){ return intersectsArea(layerName, multiPolygonCached()); }
else { return intersectsArea(layerName, polygonCached()); }
}
template <typename GeometryT>
std::vector<uint> OsmLuaProcessing::intersectsQuery(const string &layerName, bool once, GeometryT &geom) const {
Box box; geom::envelope(geom, box);
if (!shpMemTiles.mayIntersect(layerName, box))
return std::vector<uint>();
std::vector<uint> ids = shpMemTiles.QueryMatchingGeometries(layerName, once, box,
[&](const RTree &rtree) { // indexQuery
vector<IndexValue> results;
rtree.query(geom::index::intersects(box), back_inserter(results));
return results;
},
[&](OutputObject const &oo) { // checkQuery
return geom::intersects(geom, shpMemTiles.retrieveMultiPolygon(oo.objectID));
}
);
return ids;
}
template <typename GeometryT>
double OsmLuaProcessing::intersectsArea(const string &layerName, GeometryT &geom) const {
Box box; geom::envelope(geom, box);
double area = 0.0;
std::vector<uint> ids = shpMemTiles.QueryMatchingGeometries(layerName, false, box,
[&](const RTree &rtree) { // indexQuery
vector<IndexValue> results;
rtree.query(geom::index::intersects(box), back_inserter(results));
return results;
},
[&](OutputObject const &oo) { // checkQuery
MultiPolygon tmp;
geom::intersection(geom, shpMemTiles.retrieveMultiPolygon(oo.objectID), tmp);
area += multiPolygonArea(tmp);
return false;
}
);
return area;
}
template <typename GeometryT>
std::vector<uint> OsmLuaProcessing::coveredQuery(const string &layerName, bool once, GeometryT &geom) const {
Box box; geom::envelope(geom, box);
std::vector<uint> ids = shpMemTiles.QueryMatchingGeometries(layerName, once, box,
[&](const RTree &rtree) { // indexQuery
vector<IndexValue> results;
rtree.query(geom::index::intersects(box), back_inserter(results));
return results;
},
[&](OutputObject const &oo) { // checkQuery
if (oo.geomType!=POLYGON_) return false; // can only be covered by a polygon!
return geom::covered_by(geom, shpMemTiles.retrieveMultiPolygon(oo.objectID));
}
);
return ids;
}
// Returns whether it is closed polygon
bool OsmLuaProcessing::IsClosed() const {
if (!isWay) return false; // nonsense: it isn't a way
return isClosed;
}
// Return whether it's a multipolygon
bool OsmLuaProcessing::IsMultiPolygon() const {
return isWay && isRelation;
}
void reverse_project(DegPoint& p) {
geom::set<1>(p, latp2lat(geom::get<1>(p)));
}
// Returns area
double OsmLuaProcessing::Area() {
if (!IsClosed()) return 0;
#if BOOST_VERSION >= 106700
geom::strategy::area::spherical<> sph_strategy(RadiusMeter);
if (isRelation) {
// Boost won't calculate area of a multipolygon, so we just total up the member polygons
return multiPolygonArea(multiPolygonCached());
} else if (isWay) {
// Reproject back into lat/lon and then run Boo
geom::model::polygon<DegPoint> p;
geom::assign(p,polygonCached());
geom::for_each_point(p, reverse_project);
return geom::area(p, sph_strategy);
}
#else
if (isRelation) {
return geom::area(multiPolygonCached());
} else if (isWay) {
return geom::area(polygonCached());
}
#endif
return 0;
}
double OsmLuaProcessing::multiPolygonArea(const MultiPolygon &mp) const {
geom::strategy::area::spherical<> sph_strategy(RadiusMeter);
double totalArea = 0;
for (MultiPolygon::const_iterator it = mp.begin(); it != mp.end(); ++it) {
geom::model::polygon<DegPoint> p;
geom::assign(p,*it);
geom::for_each_point(p, reverse_project);
totalArea += geom::area(p, sph_strategy);
}
return totalArea;
}
// Returns length
double OsmLuaProcessing::Length() {
if (isWay && !isRelation) {
geom::model::linestring<DegPoint> l;
geom::assign(l, linestringCached());
geom::for_each_point(l, reverse_project);
return geom::length(l, geom::strategy::distance::haversine<float>(RadiusMeter));
}
// multi_polygon would be calculated as zero
return 0;
}
// Cached geometries creation
const Linestring &OsmLuaProcessing::linestringCached() {
if (!linestringInited) {
linestringInited = true;
linestringCache = osmStore.llListLinestring(llVecPtr->cbegin(),llVecPtr->cend());
}
return linestringCache;
}
const MultiLinestring &OsmLuaProcessing::multiLinestringCached() {
if (!multiLinestringInited) {
multiLinestringInited = true;
multiLinestringCache = osmStore.wayListMultiLinestring(outerWayVecPtr->cbegin(), outerWayVecPtr->cend());
}
return multiLinestringCache;
}
const Polygon &OsmLuaProcessing::polygonCached() {
if (!polygonInited) {
polygonInited = true;
polygonCache = osmStore.llListPolygon(llVecPtr->cbegin(), llVecPtr->cend());
}
return polygonCache;
}
const MultiPolygon &OsmLuaProcessing::multiPolygonCached() {
if (!multiPolygonInited) {
multiPolygonInited = true;
multiPolygonCache = osmStore.wayListMultiPolygon(
outerWayVecPtr->cbegin(), outerWayVecPtr->cend(), innerWayVecPtr->begin(), innerWayVecPtr->cend());
}
return multiPolygonCache;
}
// ---- Requests from Lua to write this way/node to a vector tile's Layer
// Add object to specified layer from Lua
void OsmLuaProcessing::Layer(const string &layerName, bool area) {
outputKeys.clear();
if (layers.layerMap.count(layerName) == 0) {
throw out_of_range("ERROR: Layer(): a layer named as \"" + layerName + "\" doesn't exist.");
}
uint layerMinZoom = layers.layers[layers.layerMap[layerName]].minzoom;
AttributeSet attributes;
OutputGeometryType geomType = isRelation ? (area ? POLYGON_ : MULTILINESTRING_ ) :
isWay ? (area ? POLYGON_ : LINESTRING_) : POINT_;
try {
// Lua profiles often write the same geometry twice, e.g. a river and its name,
// a highway and its name. Avoid duplicating geometry processing and storage
// when this occurs.
if (lastStoredGeometryId != 0 && lastStoredGeometryType == geomType) {
OutputObject oo(geomType, layers.layerMap[layerName], lastStoredGeometryId, 0, layerMinZoom);
outputs.push_back(std::make_pair(std::move(oo), attributes));
return;
}
if (geomType==POINT_) {
Point p = Point(lon, latp);
if(CorrectGeometry(p) == CorrectGeometryResult::Invalid) return;
NodeID id = USE_NODE_STORE | originalOsmID;
if (materializeGeometries)
id = osmMemTiles.storePoint(p);
OutputObject oo(geomType, layers.layerMap[layerName], id, 0, layerMinZoom);
outputs.push_back(std::make_pair(std::move(oo), attributes));
return;
}
else if (geomType==POLYGON_) {
// polygon
MultiPolygon mp;
if (isRelation) {
try {
mp = multiPolygonCached();
if(CorrectGeometry(mp) == CorrectGeometryResult::Invalid) return;
NodeID id = osmMemTiles.storeMultiPolygon(mp);
OutputObject oo(geomType, layers.layerMap[layerName], id, 0, layerMinZoom);
outputs.push_back(std::make_pair(std::move(oo), attributes));
} catch(std::out_of_range &err) {
cout << "In relation " << originalOsmID << ": " << err.what() << endl;
return;
}
}
else if (isWay) {
//Is there a more efficient way to do this?
Linestring ls = linestringCached();
Polygon p;
geom::assign_points(p, ls);
mp.push_back(p);
auto correctionResult = CorrectGeometry(mp);
if(correctionResult == CorrectGeometryResult::Invalid) return;
NodeID id = 0;
if (!materializeGeometries && correctionResult == CorrectGeometryResult::Valid) {
id = USE_WAY_STORE | originalOsmID;
wayEmitted = true;
} else
id = osmMemTiles.storeMultiPolygon(mp);
OutputObject oo(geomType, layers.layerMap[layerName], id, 0, layerMinZoom);
outputs.push_back(std::make_pair(std::move(oo), attributes));
}
}
else if (geomType==MULTILINESTRING_) {
// multilinestring
MultiLinestring mls;
try {
mls = multiLinestringCached();
} catch(std::out_of_range &err) {
cout << "In relation " << originalOsmID << ": " << err.what() << endl;
return;
}
if (CorrectGeometry(mls) == CorrectGeometryResult::Invalid) return;
NodeID id = osmMemTiles.storeMultiLinestring(mls);
lastStoredGeometryId = id;
lastStoredGeometryType = geomType;
OutputObject oo(geomType, layers.layerMap[layerName], id, 0, layerMinZoom);
outputs.push_back(std::make_pair(std::move(oo), attributes));
}
else if (geomType==LINESTRING_) {
// linestring
Linestring ls = linestringCached();
auto correctionResult = CorrectGeometry(ls);
if(correctionResult == CorrectGeometryResult::Invalid) return;
if (isWay && !isRelation) {
NodeID id = 0;
if (!materializeGeometries && correctionResult == CorrectGeometryResult::Valid) {
id = USE_WAY_STORE | originalOsmID;
wayEmitted = true;
} else
id = osmMemTiles.storeLinestring(ls);
lastStoredGeometryId = id;
lastStoredGeometryType = geomType;
OutputObject oo(geomType, layers.layerMap[layerName], id, 0, layerMinZoom);
outputs.push_back(std::make_pair(std::move(oo), attributes));
} else {
NodeID id = osmMemTiles.storeLinestring(ls);
lastStoredGeometryId = id;
lastStoredGeometryType = geomType;
OutputObject oo(geomType, layers.layerMap[layerName], id, 0, layerMinZoom);
outputs.push_back(std::make_pair(std::move(oo), attributes));
}
}
} catch (std::invalid_argument &err) {
cerr << "Error in OutputObject constructor: " << err.what() << endl;
}
}
// LayerAsCentroid(layerName, [centroid-algorithm, [role, [role, ...]]])
//
// Emit a point. This function can be called for nodes, ways or relations.
//
// When called for a 2D geometry, you can pass a preferred centroid algorithm
// in `centroid-algorithm`. Currently `polylabel` and `centroid` are supported.
//
// When called for a relation, you can pass a list of roles. The point of a node
// with that role will be used if available.
void OsmLuaProcessing::LayerAsCentroid(const string &layerName, kaguya::VariadicArgType varargs) {
outputKeys.clear();
if (layers.layerMap.count(layerName) == 0) {
throw out_of_range("ERROR: LayerAsCentroid(): a layer named as \"" + layerName + "\" doesn't exist.");
}
CentroidAlgorithm algorithm = defaultCentroidAlgorithm();
for (auto needleRef : varargs) {
const std::string needle = needleRef.get<std::string>();
algorithm = parseCentroidAlgorithm(needle);
break;
}
// This will be non-zero if we ultimately used a node from a relation to
// label the point.
NodeID relationNode = 0;
uint layerMinZoom = layers.layers[layers.layerMap[layerName]].minzoom;
AttributeSet attributes;
Point geomp;
bool centroidFound = false;
try {
// If we're a relation, see if the user would prefer we use one of its members
// to label the point.
if (isRelation) {
int i = -1;
for (auto needleRef : varargs) {
i++;
// Skip the first vararg, it's the algorithm.
if (i == 0) continue;
const std::string needle = needleRef.get<std::string>();
// We do a linear search of the relation's members. This is not very efficient
// for relations like Tongass National Park (ID 6535292, 29,000 members),
// but in general, it's probably fine.
//
// I note that relation members seem to be sorted nodes first, then ways,
// then relations. I'm not sure if we can rely on that, so I don't
// short-circuit on the first non-node.
for (int i = 0; i < currentRelation->memids.size(); i++) {
if (currentRelation->types[i] != PbfReader::Relation::MemberType::NODE)
continue;
const protozero::data_view role = stringTable->at(currentRelation->roles_sid[i]);
if (role.size() == needle.size() && 0 == memcmp(role.data(), needle.data(), role.size())) {
relationNode = currentRelation->memids[i];
const auto ll = osmStore.nodes.at(relationNode);
geomp = Point(ll.lon, ll.latp);
centroidFound = true;
break;
}
}
if (relationNode != 0)
break;
}
}
if (!centroidFound)
geomp = calculateCentroid(algorithm);
// TODO: I think geom::is_empty always returns false for Points?
// See https://github.com/boostorg/geometry/blob/fa3623528ea27ba2c3c1327e4b67408a2b567038/include/boost/geometry/algorithms/is_empty.hpp#L103
if(geom::is_empty(geomp)) {
cerr << "Geometry is empty in OsmLuaProcessing::LayerAsCentroid (" << (isRelation ? "relation " : isWay ? "way " : "node ") << originalOsmID << ")" << endl;
return;
}
} catch(std::out_of_range &err) {
cout << "Couldn't find " << (isRelation ? "relation " : isWay ? "way " : "node " ) << originalOsmID << ": " << err.what() << endl;
return;
} catch (geom::centroid_exception &err) {
if (verbose) cerr << "Problem geometry " << (isRelation ? "relation " : isWay ? "way " : "node " ) << originalOsmID << ": " << err.what() << endl;
return;
} catch (std::invalid_argument &err) {
cerr << "Error in OutputObject constructor for " << (isRelation ? "relation " : isWay ? "way " : "node " ) << originalOsmID << ": " << err.what() << endl;
return;
}
NodeID id = 0;
// We don't do lazy geometries for centroids in these cases:
//
// - --materialize-geometries is set
// - the geometry is a relation - calculating their centroid can be quite expensive,
// and there's not as many of them as there are ways
// - when the algorithm chosen is polylabel
// We can extend lazy geometries to this, it just needs some fiddling to
// express it in the ID and measure if there's a runtime impact in computing
// the polylabel twice.
if (materializeGeometries || (isRelation && relationNode == 0) || (isWay && algorithm != CentroidAlgorithm::Centroid)) {
id = osmMemTiles.storePoint(geomp);
} else if (relationNode != 0) {
id = USE_NODE_STORE | relationNode;
} else if (!isRelation && !isWay) {
// Sometimes people call LayerAsCentroid(...) on a node, because they're
// writing a generic handler that doesn't know if it's a node or a way,
// e.g. POIs.
id = USE_NODE_STORE | originalOsmID;
} else {
id = USE_WAY_STORE | originalOsmID;
wayEmitted = true;
}
OutputObject oo(POINT_, layers.layerMap[layerName], id, 0, layerMinZoom);
outputs.push_back(std::make_pair(std::move(oo), attributes));
}
Point OsmLuaProcessing::calculateCentroid(CentroidAlgorithm algorithm) {
Point centroid;
if (isRelation) {
MultiPolygon tmp;
tmp = multiPolygonCached();
if (algorithm == CentroidAlgorithm::Polylabel) {
int index = 0;
// CONSIDER: pick precision intelligently
// Polylabel works on polygons, so for multipolygons we'll label the biggest outer.
double biggestSize = 0;
for (int i = 0; i < tmp.size(); i++) {
double size = geom::area(tmp[i]);
if (size > biggestSize) {
biggestSize = size;
index = i;
}
}
if (tmp.size() == 0)
throw geom::centroid_exception();
centroid = mapbox::polylabel(tmp[index]);
} else {
geom::centroid(tmp, centroid);
}
return Point(centroid.x()*10000000.0, centroid.y()*10000000.0);
} else if (isWay) {
Polygon p;
geom::assign_points(p, linestringCached());
if (algorithm == CentroidAlgorithm::Polylabel) {
// CONSIDER: pick precision intelligently
centroid = mapbox::polylabel(p);
} else {
geom::centroid(p, centroid);
}
return Point(centroid.x()*10000000.0, centroid.y()*10000000.0);
} else {
return Point(lon, latp);
}
}
OsmLuaProcessing::CentroidAlgorithm OsmLuaProcessing::parseCentroidAlgorithm(const std::string& algorithm) const {
if (algorithm == "polylabel") return OsmLuaProcessing::CentroidAlgorithm::Polylabel;
if (algorithm == "centroid") return OsmLuaProcessing::CentroidAlgorithm::Centroid;
throw std::runtime_error("unknown centroid algorithm " + algorithm);
}
kaguya::optional<std::vector<double>> OsmLuaProcessing::Centroid(kaguya::VariadicArgType algorithmArgs) {
CentroidAlgorithm algorithm = defaultCentroidAlgorithm();
for (auto needleRef : algorithmArgs) {
const std::string needle = needleRef.get<std::string>();
algorithm = parseCentroidAlgorithm(needle);
break;
}
try {
Point c = calculateCentroid(algorithm);
return std::vector<double> { latp2lat(c.y()/10000000.0), c.x()/10000000.0 };
} catch (geom::centroid_exception &err) {
if (verbose) cerr << "Problem geometry " << (isRelation ? "relation " : isWay ? "way " : "node " ) << originalOsmID << ": " << err.what() << endl;
return kaguya::optional<std::vector<double>>();
}
}
// Accept a relation in relation_scan phase
void OsmLuaProcessing::Accept() {
relationAccepted = true;
}
// Set a tag in post-scan phase
void OsmLuaProcessing::SetTag(const std::string &key, const std::string &value) {
if (!isPostScanRelation) throw std::runtime_error("SetTag can only be used in relation_postscan_function");
osmStore.scannedRelations.set_relation_tag(originalOsmID, key, value);
}
void OsmLuaProcessing::removeAttributeIfNeeded(const string& key) {
// Does it exist?
for (int i = 0; i < outputKeys.size(); i++) {
if (outputKeys[i] == key) {
AttributeSet& set = outputs.back().second;
set.removePairWithKey(attributeStore.pairStore, attributeStore.keyStore.key2index(key));
return;
}
}
outputKeys.push_back(key);
}
// Force a new ID
void OsmLuaProcessing::ModifyId(const int newId) {
originalOsmID = static_cast<int64_t>(newId);
}
// Set attributes in a vector tile's Attributes table
void OsmLuaProcessing::Attribute(const string &key, const protozero::data_view val, const char minzoom) {
if (outputs.size()==0) { ProcessingError("Can't add Attribute if no Layer set"); return; }
removeAttributeIfNeeded(key);
attributeStore.addAttribute(outputs.back().second, key, val, minzoom);
setVectorLayerMetadata(outputs.back().first.layer, key, 0);
}
void OsmLuaProcessing::AttributeNumeric(const string &key, const double val, const char minzoom) {
if (outputs.size()==0) { ProcessingError("Can't add Attribute if no Layer set"); return; }
removeAttributeIfNeeded(key);
attributeStore.addAttribute(outputs.back().second, key, val, minzoom);
setVectorLayerMetadata(outputs.back().first.layer, key, 1);
}
void OsmLuaProcessing::AttributeBoolean(const string &key, const bool val, const char minzoom) {
if (outputs.size()==0) { ProcessingError("Can't add Attribute if no Layer set"); return; }
removeAttributeIfNeeded(key);
attributeStore.addAttribute(outputs.back().second, key, val, minzoom);
setVectorLayerMetadata(outputs.back().first.layer, key, 2);
}
void OsmLuaProcessing::AttributeInteger(const string &key, const int val, const char minzoom) {
if (outputs.size()==0) { ProcessingError("Can't add Attribute if no Layer set"); return; }
removeAttributeIfNeeded(key);
attributeStore.addAttribute(outputs.back().second, key, val, minzoom);
setVectorLayerMetadata(outputs.back().first.layer, key, 3);
}
// Set minimum zoom
void OsmLuaProcessing::MinZoom(const double z) {
if (outputs.size()==0) { ProcessingError("Can't set minimum zoom if no Layer set"); return; }
outputs.back().first.setMinZoom(z);
}
// Set z_order
void OsmLuaProcessing::ZOrder(const double z) {
if (outputs.size()==0) { ProcessingError("Can't set z_order if no Layer set"); return; }
outputs.back().first.setZOrder(z);
}
// Read scanned relations
// Kaguya doesn't support optional<tuple<int,string>>, so we write a custom serializer
// to either return nil or a tuple.
template<> struct kaguya::lua_type_traits<OsmLuaProcessing::OptionalRelation> {
typedef OsmLuaProcessing::OptionalRelation get_type;
typedef const OsmLuaProcessing::OptionalRelation& push_type;
static bool strictCheckType(lua_State* l, int index)
{
throw std::runtime_error("Lua code doesn't know how to send OptionalRelation");
}
static bool checkType(lua_State* l, int index)
{
throw std::runtime_error("Lua code doesn't know how to send OptionalRelation");
}
static get_type get(lua_State* l, int index)
{
throw std::runtime_error("Lua code doesn't know how to send OptionalRelation");
}
static int push(lua_State* l, push_type s)
{
if (s.done)
return 0;
lua_pushinteger(l, s.id);
lua_pushlstring(l, s.role.data(), s.role.size());
return 2;
}