-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathmapnik_vector_tile_simple_valid.cpp
More file actions
838 lines (792 loc) · 30.1 KB
/
mapnik_vector_tile_simple_valid.cpp
File metadata and controls
838 lines (792 loc) · 30.1 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
#include "mapnik_vector_tile.hpp"
#if BOOST_VERSION >= 105800
// mapnik
#include <mapnik/feature.hpp>
#include <mapnik/feature_factory.hpp>
#include <mapnik/featureset.hpp>
#include <mapnik/feature_kv_iterator.hpp>
#include <mapnik/geometry/is_simple.hpp>
#include <mapnik/geometry/is_valid.hpp>
#include <mapnik/geometry/reprojection.hpp>
#include <mapnik/util/feature_to_geojson.hpp>
#include <mapnik/projection.hpp>
// mapnik-vector-tile
#include "mapnik_vector_tile.hpp"
#include "vector_tile_compression.hpp"
#include "vector_tile_composite.hpp"
#include "vector_tile_processor.hpp"
#include "vector_tile_projection.hpp"
#include "vector_tile_datasource_pbf.hpp"
#include "vector_tile_geometry_decoder.hpp"
#include "vector_tile_load_tile.hpp"
#include "object_to_container.hpp"
namespace {
// LCOV_EXCL_START
struct not_simple_feature
{
not_simple_feature(std::string const& layer_,
std::int64_t feature_id_)
: layer(layer_),
feature_id(feature_id_) {}
std::string const layer;
std::int64_t const feature_id;
};
// LCOV_EXCL_STOP
struct not_valid_feature
{
not_valid_feature(std::string const& message_,
std::string const& layer_,
std::int64_t feature_id_,
std::string const& geojson_)
: message(message_),
layer(layer_),
feature_id(feature_id_),
geojson(geojson_) {}
std::string const message;
std::string const layer;
std::int64_t const feature_id;
std::string const geojson;
};
void layer_not_simple(protozero::pbf_reader const& layer_msg,
unsigned x,
unsigned y,
unsigned z,
std::vector<not_simple_feature>& errors)
{
mapnik::vector_tile_impl::tile_datasource_pbf ds(layer_msg, x, y, z);
mapnik::query q(mapnik::box2d<double>(std::numeric_limits<double>::lowest(),
std::numeric_limits<double>::lowest(),
std::numeric_limits<double>::max(),
std::numeric_limits<double>::max()));
mapnik::layer_descriptor ld = ds.get_descriptor();
for (auto const& item : ld.get_descriptors())
{
q.add_property_name(item.get_name());
}
mapnik::featureset_ptr fs = ds.features(q);
if (fs && !mapnik::is_empty(fs))
{
mapnik::feature_ptr feature;
while ((feature = fs->next()))
{
if (!mapnik::geometry::is_simple(feature->get_geometry())) // NOLINT
{
// Right now we don't have an obvious way of bypassing our validation
// process in JS, so let's skip testing this line
// LCOV_EXCL_START
errors.emplace_back(ds.get_name(), feature->id());
// LCOV_EXCL_STOP
}
}
}
}
struct visitor_geom_valid
{
std::vector<not_valid_feature>& errors;
mapnik::feature_ptr& feature;
std::string const& layer_name;
bool split_multi_features;
visitor_geom_valid(std::vector<not_valid_feature>& errors_,
mapnik::feature_ptr& feature_,
std::string const& layer_name_,
bool split_multi_features_)
: errors(errors_),
feature(feature_),
layer_name(layer_name_),
split_multi_features(split_multi_features_) {}
void operator()(mapnik::geometry::geometry_empty const&) {}
template <typename T>
void operator()(mapnik::geometry::point<T> const& geom)
{
std::string message;
if (!mapnik::geometry::is_valid(geom, message)) // NOLINT
{
mapnik::feature_impl feature_new(feature->context(), feature->id());
std::string result;
std::string feature_str;
result += "{\"type\":\"FeatureCollection\",\"features\":[";
feature_new.set_data(feature->get_data());
feature_new.set_geometry(mapnik::geometry::geometry<T>(geom));
if (!mapnik::util::to_geojson(feature_str, feature_new))
{
// LCOV_EXCL_START
throw std::runtime_error("Failed to generate GeoJSON geometry");
// LCOV_EXCL_STOP
}
result += feature_str;
result += "]}";
errors.emplace_back(message,
layer_name,
feature->id(),
result);
}
}
template <typename T>
void operator()(mapnik::geometry::multi_point<T> const& geom)
{
std::string message;
if (!mapnik::geometry::is_valid(geom, message)) // NOLINT
{
mapnik::feature_impl feature_new(feature->context(), feature->id());
std::string result;
std::string feature_str;
result += "{\"type\":\"FeatureCollection\",\"features\":[";
feature_new.set_data(feature->get_data());
feature_new.set_geometry(mapnik::geometry::geometry<T>(geom));
if (!mapnik::util::to_geojson(feature_str, feature_new))
{
// LCOV_EXCL_START
throw std::runtime_error("Failed to generate GeoJSON geometry");
// LCOV_EXCL_STOP
}
result += feature_str;
result += "]}";
errors.emplace_back(message,
layer_name,
feature->id(),
result);
}
}
template <typename T>
void operator()(mapnik::geometry::line_string<T> const& geom)
{
std::string message;
if (!mapnik::geometry::is_valid(geom, message)) // NOLINT
{
mapnik::feature_impl feature_new(feature->context(), feature->id());
std::string result;
std::string feature_str;
result += "{\"type\":\"FeatureCollection\",\"features\":[";
feature_new.set_data(feature->get_data());
feature_new.set_geometry(mapnik::geometry::geometry<T>(geom));
if (!mapnik::util::to_geojson(feature_str, feature_new))
{
// LCOV_EXCL_START
throw std::runtime_error("Failed to generate GeoJSON geometry");
// LCOV_EXCL_STOP
}
result += feature_str;
result += "]}";
errors.emplace_back(message,
layer_name,
feature->id(),
result);
}
}
template <typename T>
void operator()(mapnik::geometry::multi_line_string<T> const& geom)
{
if (split_multi_features)
{
for (auto const& ls : geom)
{
std::string message;
if (!mapnik::geometry::is_valid(ls, message)) // NOLINT
{
mapnik::feature_impl feature_new(feature->context(), feature->id());
std::string result;
std::string feature_str;
result += "{\"type\":\"FeatureCollection\",\"features\":[";
feature_new.set_data(feature->get_data());
feature_new.set_geometry(mapnik::geometry::geometry<T>(ls));
if (!mapnik::util::to_geojson(feature_str, feature_new))
{
// LCOV_EXCL_START
throw std::runtime_error("Failed to generate GeoJSON geometry");
// LCOV_EXCL_STOP
}
result += feature_str;
result += "]}";
errors.emplace_back(message,
layer_name,
feature->id(),
result);
}
}
}
else
{
std::string message;
if (!mapnik::geometry::is_valid(geom, message)) // NOLINT
{
mapnik::feature_impl feature_new(feature->context(), feature->id());
std::string result;
std::string feature_str;
result += "{\"type\":\"FeatureCollection\",\"features\":[";
feature_new.set_data(feature->get_data());
feature_new.set_geometry(mapnik::geometry::geometry<T>(geom));
if (!mapnik::util::to_geojson(feature_str, feature_new))
{
// LCOV_EXCL_START
throw std::runtime_error("Failed to generate GeoJSON geometry");
// LCOV_EXCL_STOP
}
result += feature_str;
result += "]}";
errors.emplace_back(message,
layer_name,
feature->id(),
result);
}
}
}
template <typename T>
void operator()(mapnik::geometry::polygon<T> const& geom)
{
std::string message;
if (!mapnik::geometry::is_valid(geom, message)) // NOLINT
{
mapnik::feature_impl feature_new(feature->context(), feature->id());
std::string result;
std::string feature_str;
result += "{\"type\":\"FeatureCollection\",\"features\":[";
feature_new.set_data(feature->get_data());
feature_new.set_geometry(mapnik::geometry::geometry<T>(geom));
if (!mapnik::util::to_geojson(feature_str, feature_new))
{
// LCOV_EXCL_START
throw std::runtime_error("Failed to generate GeoJSON geometry");
// LCOV_EXCL_STOP
}
result += feature_str;
result += "]}";
errors.emplace_back(message,
layer_name,
feature->id(),
result);
}
}
template <typename T>
void operator()(mapnik::geometry::multi_polygon<T> const& geom)
{
if (split_multi_features)
{
for (auto const& poly : geom)
{
std::string message;
if (!mapnik::geometry::is_valid(poly, message))
{
mapnik::feature_impl feature_new(feature->context(), feature->id());
std::string result;
std::string feature_str;
result += "{\"type\":\"FeatureCollection\",\"features\":[";
feature_new.set_data(feature->get_data());
feature_new.set_geometry(mapnik::geometry::geometry<T>(poly));
if (!mapnik::util::to_geojson(feature_str, feature_new))
{
// LCOV_EXCL_START
throw std::runtime_error("Failed to generate GeoJSON geometry");
// LCOV_EXCL_STOP
}
result += feature_str;
result += "]}";
errors.emplace_back(message,
layer_name,
feature->id(),
result);
}
}
}
else
{
std::string message;
if (!mapnik::geometry::is_valid(geom, message))
{
mapnik::feature_impl feature_new(feature->context(), feature->id());
std::string result;
std::string feature_str;
result += "{\"type\":\"FeatureCollection\",\"features\":[";
feature_new.set_data(feature->get_data());
feature_new.set_geometry(mapnik::geometry::geometry<T>(geom));
if (!mapnik::util::to_geojson(feature_str, feature_new))
{
// LCOV_EXCL_START
throw std::runtime_error("Failed to generate GeoJSON geometry");
// LCOV_EXCL_STOP
}
result += feature_str;
result += "]}";
errors.emplace_back(message,
layer_name,
feature->id(),
result);
}
}
}
template <typename T>
void operator()(mapnik::geometry::geometry_collection<T> const& geom)
{
// This should never be able to be reached.
// LCOV_EXCL_START
for (auto const& g : geom)
{
mapnik::util::apply_visitor((*this), g);
}
// LCOV_EXCL_STOP
}
};
void layer_not_valid(protozero::pbf_reader& layer_msg,
unsigned x,
unsigned y,
unsigned z,
std::vector<not_valid_feature>& errors,
bool split_multi_features = false,
bool lat_lon = false,
bool web_merc = false)
{
if (web_merc || lat_lon)
{
mapnik::vector_tile_impl::tile_datasource_pbf ds(layer_msg, x, y, z);
mapnik::query q(mapnik::box2d<double>(std::numeric_limits<double>::lowest(),
std::numeric_limits<double>::lowest(),
std::numeric_limits<double>::max(),
std::numeric_limits<double>::max()));
mapnik::layer_descriptor ld = ds.get_descriptor();
for (auto const& item : ld.get_descriptors())
{
q.add_property_name(item.get_name());
}
mapnik::featureset_ptr fs = ds.features(q);
if (fs && !mapnik::is_empty(fs))
{
mapnik::feature_ptr feature;
while ((feature = fs->next()))
{
if (lat_lon)
{
mapnik::projection wgs84("epsg:4326", true);
mapnik::projection merc("epsg:3857", true);
mapnik::proj_transform prj_trans(merc, wgs84);
unsigned int n_err = 0;
mapnik::util::apply_visitor(
visitor_geom_valid(errors, feature, ds.get_name(), split_multi_features),
mapnik::geometry::reproject_copy(feature->get_geometry(), prj_trans, n_err));
}
else
{
mapnik::util::apply_visitor(
visitor_geom_valid(errors, feature, ds.get_name(), split_multi_features),
feature->get_geometry());
}
}
}
}
else
{
std::vector<protozero::pbf_reader> layer_features;
std::uint32_t version = 1;
std::string layer_name;
while (layer_msg.next())
{
switch (layer_msg.tag())
{
case mapnik::vector_tile_impl::Layer_Encoding::NAME:
layer_name = layer_msg.get_string();
break;
case mapnik::vector_tile_impl::Layer_Encoding::FEATURES:
layer_features.push_back(layer_msg.get_message());
break;
case mapnik::vector_tile_impl::Layer_Encoding::VERSION:
version = layer_msg.get_uint32();
break;
default:
layer_msg.skip();
break;
}
}
for (auto feature_msg : layer_features)
{
mapnik::vector_tile_impl::GeometryPBF::pbf_itr geom_itr;
bool has_geom = false;
bool has_geom_type = false;
std::int32_t geom_type_enum = 0;
//std::uint64_t feature_id = 0;
while (feature_msg.next())
{
switch (feature_msg.tag())
{
case mapnik::vector_tile_impl::Feature_Encoding::TYPE:
geom_type_enum = feature_msg.get_enum();
has_geom_type = true;
break;
case mapnik::vector_tile_impl::Feature_Encoding::GEOMETRY:
geom_itr = feature_msg.get_packed_uint32();
has_geom = true;
break;
default:
feature_msg.skip();
break;
}
}
if (has_geom && has_geom_type)
{
// Decode the geometry first into an int64_t mapnik geometry
mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, 1));
mapnik::vector_tile_impl::GeometryPBF geoms(geom_itr);
feature->set_geometry(mapnik::vector_tile_impl::decode_geometry<double>(geoms, geom_type_enum, version, 0.0, 0.0, 1.0, 1.0));
mapnik::util::apply_visitor(
visitor_geom_valid(errors, feature, layer_name, split_multi_features),
feature->get_geometry());
}
}
}
}
void vector_tile_not_simple(mapnik::vector_tile_impl::merc_tile_ptr const& tile,
std::vector<not_simple_feature>& errors)
{
protozero::pbf_reader tile_msg(tile->get_reader());
while (tile_msg.next(mapnik::vector_tile_impl::Tile_Encoding::LAYERS))
{
protozero::pbf_reader layer_msg(tile_msg.get_message());
layer_not_simple(layer_msg,
tile->x(),
tile->y(),
tile->z(),
errors);
}
}
Napi::Array make_not_simple_array(Napi::Env env, std::vector<not_simple_feature>& errors)
{
Napi::Array array = Napi::Array::New(env, errors.size());
Napi::String layer_key = Napi::String::New(env, "layer");
Napi::String feature_id_key = Napi::String::New(env, "featureId");
std::uint32_t idx = 0;
for (auto const& error : errors)
{
// LCOV_EXCL_START
Napi::Object obj = Napi::Object::New(env);
obj.Set(layer_key, Napi::String::New(env, error.layer));
obj.Set(feature_id_key, Napi::Number::New(env, error.feature_id));
array.Set(idx++, obj);
// LCOV_EXCL_STOP
}
return array;
}
void vector_tile_not_valid(mapnik::vector_tile_impl::merc_tile_ptr const& tile,
std::vector<not_valid_feature>& errors,
bool split_multi_features = false,
bool lat_lon = false,
bool web_merc = false)
{
protozero::pbf_reader tile_msg(tile->get_reader());
while (tile_msg.next(mapnik::vector_tile_impl::Tile_Encoding::LAYERS))
{
protozero::pbf_reader layer_msg(tile_msg.get_message());
layer_not_valid(layer_msg,
tile->x(),
tile->y(),
tile->z(),
errors,
split_multi_features,
lat_lon,
web_merc);
}
}
Napi::Array make_not_valid_array(Napi::Env env, std::vector<not_valid_feature>& errors)
{
Napi::Array array = Napi::Array::New(env, errors.size());
Napi::String layer_key = Napi::String::New(env, "layer");
Napi::String feature_id_key = Napi::String::New(env, "featureId");
Napi::String message_key = Napi::String::New(env, "message");
Napi::String geojson_key = Napi::String::New(env, "geojson");
std::size_t idx = 0;
for (auto const& error : errors)
{
Napi::Object obj = Napi::Object::New(env);
obj.Set(layer_key, Napi::String::New(env, error.layer));
obj.Set(message_key, Napi::String::New(env, error.message));
obj.Set(feature_id_key, Napi::Number::New(env, error.feature_id));
obj.Set(geojson_key, Napi::String::New(env, error.geojson));
array.Set(idx++, obj);
}
return array;
}
struct AsyncGeometrySimple : Napi::AsyncWorker
{
using Base = Napi::AsyncWorker;
AsyncGeometrySimple(mapnik::vector_tile_impl::merc_tile_ptr const& tile, Napi::Function const& callback)
: Base(callback),
tile_(tile) {}
void Execute() override
{
try
{
vector_tile_not_simple(tile_, result_);
}
catch (std::exception const& ex)
{
// LCOV_EXCL_START
SetError(ex.what());
// LCOV_EXCL_STOP
}
}
std::vector<napi_value> GetResult(Napi::Env env) override
{
Napi::Array array = make_not_simple_array(env, result_);
return {env.Undefined(), array};
}
private:
mapnik::vector_tile_impl::merc_tile_ptr tile_;
std::vector<not_simple_feature> result_;
};
struct AsyncGeometryValid : Napi::AsyncWorker
{
using Base = Napi::AsyncWorker;
AsyncGeometryValid(mapnik::vector_tile_impl::merc_tile_ptr const& tile,
bool split_multi_features, bool lat_lon, bool web_merc,
Napi::Function const& callback)
: Base(callback),
tile_(tile),
split_multi_features_(split_multi_features),
lat_lon_(lat_lon),
web_merc_(web_merc)
{
}
void Execute() override
{
try
{
vector_tile_not_valid(tile_, result_, split_multi_features_, lat_lon_, web_merc_);
}
catch (std::exception const& ex)
{
SetError(ex.what());
}
}
std::vector<napi_value> GetResult(Napi::Env env) override
{
Napi::Array array = make_not_valid_array(env, result_);
return {env.Undefined(), array};
}
private:
mapnik::vector_tile_impl::merc_tile_ptr tile_;
bool split_multi_features_;
bool lat_lon_;
bool web_merc_;
std::vector<not_valid_feature> result_;
};
} // namespace
/**
* Count the number of geometries that are not [OGC simple]{@link http://www.iso.org/iso/catalogue_detail.htm?csnumber=40114}
*
* @memberof VectorTile
* @instance
* @name reportGeometrySimplicitySync
* @returns {number} number of features that are not simple
* @example
* var simple = vectorTile.reportGeometrySimplicitySync();
* console.log(simple); // array of non-simple geometries and their layer info
* console.log(simple.length); // number
*/
Napi::Value VectorTile::reportGeometrySimplicitySync(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
Napi::EscapableHandleScope scope(env);
try
{
std::vector<not_simple_feature> errors;
vector_tile_not_simple(tile_, errors);
return scope.Escape(make_not_simple_array(env, errors));
}
catch (std::exception const& ex)
{
// LCOV_EXCL_START
Napi::Error::New(env, ex.what()).ThrowAsJavaScriptException();
// LCOV_EXCL_STOP
}
// LCOV_EXCL_START
return env.Undefined();
// LCOV_EXCL_STOP
}
/**
* Count the number of geometries that are not [OGC valid]{@link http://postgis.net/docs/using_postgis_dbmanagement.html#OGC_Validity}
*
* @memberof VectorTile
* @instance
* @name reportGeometryValiditySync
* @param {object} [options]
* @param {boolean} [options.split_multi_features=false] - If true does validity checks on multi geometries part by part
* Normally the validity of multipolygons and multilinestrings is done together against
* all the parts of the geometries. Changing this to true checks the validity of multipolygons
* and multilinestrings for each part they contain, rather then as a group.
* @param {boolean} [options.lat_lon=false] - If true results in EPSG:4326
* @param {boolean} [options.web_merc=false] - If true results in EPSG:3857
* @returns {number} number of features that are not valid
* @example
* var valid = vectorTile.reportGeometryValiditySync();
* console.log(valid); // array of invalid geometries and their layer info
* console.log(valid.length); // number
*/
Napi::Value VectorTile::reportGeometryValiditySync(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
Napi::EscapableHandleScope scope(env);
bool split_multi_features = false;
bool lat_lon = false;
bool web_merc = false;
if (info.Length() >= 1)
{
if (!info[0].IsObject())
{
Napi::Error::New(env, "The first argument must be an object").ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Object options = info[0].As<Napi::Object>();
if (options.Has("split_multi_features"))
{
Napi::Value param_val = options.Get("split_multi_features");
if (!param_val.IsBoolean())
{
Napi::Error::New(env, "option 'split_multi_features' must be a boolean").ThrowAsJavaScriptException();
return env.Undefined();
}
split_multi_features = param_val.As<Napi::Boolean>();
}
if (options.Has("lat_lon"))
{
Napi::Value param_val = options.Get("lat_lon");
if (!param_val.IsBoolean())
{
Napi::Error::New(env, "option 'lat_lon' must be a boolean").ThrowAsJavaScriptException();
return env.Undefined();
}
lat_lon = param_val.As<Napi::Boolean>();
}
if (options.Has("web_merc"))
{
Napi::Value param_val = options.Get("web_merc");
if (!param_val.IsBoolean())
{
Napi::Error::New(env, "option 'web_merc' must be a boolean").ThrowAsJavaScriptException();
return env.Undefined();
}
web_merc = param_val.As<Napi::Boolean>();
}
}
try
{
std::vector<not_valid_feature> errors;
vector_tile_not_valid(tile_, errors, split_multi_features, lat_lon, web_merc);
return scope.Escape(make_not_valid_array(env, errors));
}
catch (std::exception const& ex)
{
Napi::Error::New(env, ex.what()).ThrowAsJavaScriptException();
}
return env.Undefined();
}
/**
* Count the number of geometries that are not [OGC simple]{@link http://www.iso.org/iso/catalogue_detail.htm?csnumber=40114}
*
* @memberof VectorTile
* @instance
* @name reportGeometrySimplicity
* @param {Function} callback
* @example
* vectorTile.reportGeometrySimplicity(function(err, simple) {
* if (err) throw err;
* console.log(simple); // array of non-simple geometries and their layer info
* console.log(simple.length); // number
* });
*/
Napi::Value VectorTile::reportGeometrySimplicity(Napi::CallbackInfo const& info)
{
if (info.Length() == 0)
{
return reportGeometrySimplicitySync(info);
}
Napi::Env env = info.Env();
// ensure callback is a function
Napi::Value callback = info[info.Length() - 1];
if (!callback.IsFunction())
{
Napi::TypeError::New(env, "last argument must be a callback function").ThrowAsJavaScriptException();
return env.Undefined();
}
auto* worker = new AsyncGeometrySimple(tile_, callback.As<Napi::Function>());
worker->Queue();
return env.Undefined();
}
/**
* Count the number of geometries that are not [OGC valid]{@link http://postgis.net/docs/using_postgis_dbmanagement.html#OGC_Validity}
*
* @memberof VectorTile
* @instance
* @name reportGeometryValidity
* @param {object} [options]
* @param {boolean} [options.split_multi_features=false] - If true does validity checks on multi geometries part by part
* Normally the validity of multipolygons and multilinestrings is done together against
* all the parts of the geometries. Changing this to true checks the validity of multipolygons
* and multilinestrings for each part they contain, rather then as a group.
* @param {boolean} [options.lat_lon=false] - If true results in EPSG:4326
* @param {boolean} [options.web_merc=false] - If true results in EPSG:3857
* @param {Function} callback
* @example
* vectorTile.reportGeometryValidity(function(err, valid) {
* console.log(valid); // array of invalid geometries and their layer info
* console.log(valid.length); // number
* });
*/
Napi::Value VectorTile::reportGeometryValidity(Napi::CallbackInfo const& info)
{
if (info.Length() == 0 || (info.Length() == 1 && !info[0].IsFunction()))
{
return reportGeometryValiditySync(info);
}
Napi::Env env = info.Env();
Napi::EscapableHandleScope scope(env);
bool split_multi_features = false;
bool lat_lon = false;
bool web_merc = false;
if (info.Length() >= 2)
{
if (!info[0].IsObject())
{
Napi::Error::New(env, "The first argument must be an object").ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Object options = info[0].As<Napi::Object>();
if (options.Has("split_multi_features"))
{
Napi::Value param_val = options.Get("split_multi_features");
if (!param_val.IsBoolean())
{
Napi::Error::New(env, "option 'split_multi_features' must be a boolean").ThrowAsJavaScriptException();
return env.Undefined();
}
split_multi_features = param_val.As<Napi::Boolean>();
}
if (options.Has("lat_lon"))
{
Napi::Value param_val = options.Get("lat_lon");
if (!param_val.IsBoolean())
{
Napi::Error::New(env, "option 'lat_lon' must be a boolean").ThrowAsJavaScriptException();
return env.Undefined();
}
lat_lon = param_val.As<Napi::Boolean>();
}
if (options.Has("web_merc"))
{
Napi::Value param_val = options.Get("web_merc");
if (!param_val.IsBoolean())
{
Napi::Error::New(env, "option 'web_merc' must be a boolean").ThrowAsJavaScriptException();
return env.Undefined();
}
web_merc = param_val.As<Napi::Boolean>();
}
}
// ensure callback is a function
Napi::Value callback = info[info.Length() - 1];
if (!callback.IsFunction())
{
Napi::TypeError::New(env, "last argument must be a callback function").ThrowAsJavaScriptException();
return env.Undefined();
}
auto* worker = new AsyncGeometryValid(tile_, split_multi_features, lat_lon, web_merc, callback.As<Napi::Function>());
worker->Queue();
return env.Undefined();
}
#endif // BOOST_VERSION >= 1.58