mln-rs-style 0.0.21

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
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
use approx::assert_abs_diff_eq;
use geo_types::{Geometry, LineString, MultiLineString, MultiPoint, Point, Polygon};

use super::util::{StubGeometryTileFeature, property_map};
use crate::collections::VecSet;
use crate::expression::dsl::{self, get, get_str, image, linear, literal, literal_str, number, step, zoom};
use crate::expression::parse::create_expression_from_str;
use crate::expression::{Dependency, EvaluationContext, Functions, Image, Value};
use crate::feature::{GeometryCollection, Value as FeatureValue, geojson};
use crate::geometry::FeatureType;
use crate::property::evaluated::Evaluated;
use crate::property::evaluation::PropertyEvaluationParameters;
use crate::property::expression::PropertyExpression;
use crate::property::value::{PropertyValue, StyleValue as _};
use crate::tile_id::CanonicalTileID;

fn one_integer() -> StubGeometryTileFeature {
    StubGeometryTileFeature::new(property_map(&[("property", FeatureValue::Uint(1))]))
}

fn one_double() -> StubGeometryTileFeature {
    StubGeometryTileFeature::new(property_map(&[("property", FeatureValue::Double(1.0))]))
}

fn one_string() -> StubGeometryTileFeature {
    StubGeometryTileFeature::new(property_map(&[("property", FeatureValue::String("1".into()))]))
}

fn one_image() -> StubGeometryTileFeature {
    StubGeometryTileFeature::new(property_map(&[("image_name", FeatureValue::String("maki-11".into()))]))
}

fn empty_tile_feature() -> StubGeometryTileFeature {
    StubGeometryTileFeature::new(property_map(&[]))
}

fn evaluate(value: &PropertyValue<f32>, zoom: f64) -> f32 {
    let parameters = PropertyEvaluationParameters::new(zoom);
    *Evaluated::plain(value, &parameters, 0.0).uniform()
}

fn num(v: f64) -> crate::expression::Expr {
    literal(Value::Number(v))
}

// gtest: PropertyExpression.Constant
#[test]
fn constant() {
    assert_eq!(2.0_f32, evaluate(&PropertyValue::Constant(2.0), 0.0));
    assert_eq!(3.8_f32, evaluate(&PropertyValue::Constant(3.8), 0.0));
    assert_eq!(22.0_f32, evaluate(&PropertyValue::Constant(22.0), 0.0));
    assert_eq!(2.0_f32, evaluate(&PropertyValue::Constant(2.0), 4.0));
    assert_eq!(3.8_f32, evaluate(&PropertyValue::Constant(3.8), 4.0));
    assert_eq!(22.0_f32, evaluate(&PropertyValue::Constant(22.0), 4.0));
    assert_eq!(2.0_f32, evaluate(&PropertyValue::Constant(2.0), 22.0));
    assert_eq!(3.8_f32, evaluate(&PropertyValue::Constant(3.8), 22.0));
    assert_eq!(22.0_f32, evaluate(&PropertyValue::Constant(22.0), 22.0));

    assert_eq!(Dependency::NONE, PropertyValue::Constant(2.0_f32).dependencies());
}

// gtest: PropertyExpression.Expression
#[test]
fn expression() {
    let expression =
        PropertyExpression::<f32>::from_expr(interpolate(linear(), zoom(), vec![(0.0, num(0.0)), (1.0, num(1.0))]));
    assert_eq!(0.0, evaluate(&PropertyValue::Expression(expression.clone()), 0.0));
    assert_eq!(0.5, evaluate(&PropertyValue::Expression(expression.clone()), 0.5));
    assert_eq!(Dependency::ZOOM, expression.dependencies());
}

// gtest: PropertyExpression.Defaults
#[test]
fn defaults() {
    let no_default = PropertyExpression::<f32>::from_expr(number(get_str("property"), None));
    let with_default = PropertyExpression::<f32>::from_expr(number(get_str("property"), Some(num(0.0))));
    assert_eq!(1.0_f32, with_default.evaluate(&EvaluationContext::from_feature(Some(&one_integer())), 2.0));
    assert_eq!(1.0_f32, with_default.evaluate(&EvaluationContext::from_feature(Some(&one_double())), 2.0));
    assert_eq!(0.0_f32, with_default.evaluate(&EvaluationContext::from_feature(Some(&one_string())), 2.0));
    assert_eq!(2.0_f32, no_default.evaluate(&EvaluationContext::from_feature(Some(&one_string())), 2.0));

    assert_eq!(Dependency::FEATURE, no_default.dependencies());
}

fn interpolate(
    interpolator: crate::expression::Interpolator,
    input: crate::expression::Expr,
    stops: Vec<(f64, crate::expression::Expr)>,
) -> crate::expression::Expr {
    dsl::interpolate(interpolator, input, stops).expect("valid stops")
}

/// `["interpolate", ["linear"], ["number", ["get", "property"]], 1, v]`.
fn inner(v: f64) -> crate::expression::Expr {
    interpolate(linear(), number(get_str("property"), None), vec![(1.0, num(v))])
}

// gtest: PropertyExpression.ZoomInterpolation
#[test]
fn zoom_interpolation() {
    assert_eq!(
        40.0_f32,
        PropertyExpression::<f32>::from_expr(interpolate(
            linear(),
            zoom(),
            vec![(0.0, inner(24.0)), (1.5, inner(36.0)), (3.0, inner(48.0))]
        ))
        .evaluate(&EvaluationContext::with_zoom_feature(2.0, Some(&one_integer())), -1.0),
        "Should interpolate between stops"
    );

    assert_eq!(
        33.0_f32,
        PropertyExpression::<f32>::from_expr(interpolate(
            linear(),
            zoom(),
            vec![(5.0, inner(33.0)), (10.0, inner(66.0))]
        ))
        .evaluate(&EvaluationContext::with_zoom_feature(0.0, Some(&one_integer())), -1.0),
        "Use first stop output for input values from -inf to first stop"
    );

    assert_eq!(
        66.0_f32,
        PropertyExpression::<f32>::from_expr(interpolate(
            linear(),
            zoom(),
            vec![(0.0, inner(33.0)), (10.0, inner(66.0))]
        ))
        .evaluate(&EvaluationContext::with_zoom_feature(20.0, Some(&one_integer())), -1.0),
        "Use last stop output for input values from last stop to +inf"
    );

    assert_eq!(
        66.0_f32,
        PropertyExpression::<f32>::from_expr(interpolate(
            linear(),
            zoom(),
            vec![(0.0, inner(33.0)), (10.0, inner(66.0))]
        ))
        .evaluate(&EvaluationContext::with_zoom_feature(10.0, Some(&one_integer())), -1.0),
        "Should interpolate TO the last stop."
    );

    assert_eq!(
        33.0_f32,
        PropertyExpression::<f32>::from_expr(interpolate(
            linear(),
            zoom(),
            vec![(0.0, inner(33.0)), (10.0, inner(66.0))]
        ))
        .evaluate(&EvaluationContext::with_zoom_feature(0.0, Some(&one_integer())), -1.0),
        "Should interpolate TO the first stop"
    );
}

// gtest: PropertyExpression.Issue8460
#[test]
fn issue8460() {
    let fn1 = PropertyExpression::<f32>::from_expr(interpolate(
        linear(),
        zoom(),
        vec![(15.0, inner(0.0)), (15.2, inner(600.0))],
    ));

    assert_abs_diff_eq!(
        0.0,
        fn1.evaluate(&EvaluationContext::with_zoom_feature(15.0, Some(&one_integer())), -1.0),
        epsilon = 0.00
    );
    assert_abs_diff_eq!(
        300.0,
        fn1.evaluate(&EvaluationContext::with_zoom_feature(15.1, Some(&one_integer())), -1.0),
        epsilon = 0.01
    );
    assert_abs_diff_eq!(
        600.0,
        fn1.evaluate(&EvaluationContext::with_zoom_feature(15.2, Some(&one_integer())), -1.0),
        epsilon = 0.00
    );
    assert_abs_diff_eq!(
        600.0,
        fn1.evaluate(&EvaluationContext::with_zoom_feature(16.0, Some(&one_integer())), -1.0),
        epsilon = 0.00
    );

    let fn2 = PropertyExpression::<f32>::from_expr(interpolate(
        linear(),
        zoom(),
        vec![(15.0, inner(0.0)), (15.2, inner(300.0)), (18.0, inner(600.0))],
    ));

    assert_abs_diff_eq!(
        0.0,
        fn2.evaluate(&EvaluationContext::with_zoom_feature(15.0, Some(&one_integer())), -1.0),
        epsilon = 0.00
    );
    assert_abs_diff_eq!(
        150.0,
        fn2.evaluate(&EvaluationContext::with_zoom_feature(15.1, Some(&one_integer())), -1.0),
        epsilon = 0.01
    );
    assert_abs_diff_eq!(
        300.0,
        fn2.evaluate(&EvaluationContext::with_zoom_feature(15.2, Some(&one_integer())), -1.0),
        epsilon = 0.00
    );
    assert_abs_diff_eq!(
        385.71,
        fn2.evaluate(&EvaluationContext::with_zoom_feature(16.0, Some(&one_integer())), -1.0),
        epsilon = 0.01
    );
    assert_abs_diff_eq!(
        600.0,
        fn2.evaluate(&EvaluationContext::with_zoom_feature(18.0, Some(&one_integer())), -1.0),
        epsilon = 0.00
    );
    assert_abs_diff_eq!(
        600.0,
        fn2.evaluate(&EvaluationContext::with_zoom_feature(19.0, Some(&one_integer())), -1.0),
        epsilon = 0.00
    );
}

// gtest: PropertyExpression.ImageExpression
#[test]
fn image_expression() {
    let empty_set = VecSet::new();
    let available_images: VecSet<String> =
        ["maki-11", "airport-11", "bicycle-15"].iter().map(ToString::to_string).collect();

    // evaluation test without available images
    {
        let prop_expr = PropertyExpression::<Image>::from_expr(image(literal_str("airport-11")));
        let evaluated_image = prop_expr.evaluate(
            &EvaluationContext::from_feature(Some(&empty_tile_feature())).with_available_images(Some(&empty_set)),
            Image::default(),
        );
        assert!(!evaluated_image.is_available());
        assert_eq!(evaluated_image.id(), "airport-11");

        assert_eq!(Dependency::IMAGE, prop_expr.dependencies());

        let dd_prop_expr = PropertyExpression::<Image>::from_expr(image(get(literal_str("image_name"))));
        let evaluated_image = dd_prop_expr.evaluate(
            &EvaluationContext::from_feature(Some(&one_image())).with_available_images(Some(&empty_set)),
            Image::default(),
        );
        assert!(!evaluated_image.is_available());
        assert_eq!(evaluated_image.id(), "maki-11");

        let evaluated_image = dd_prop_expr.evaluate(
            &EvaluationContext::from_feature(Some(&empty_tile_feature())).with_available_images(Some(&empty_set)),
            Image::default(),
        );
        assert!(!evaluated_image.is_available());
        assert_eq!(evaluated_image.id(), "");
    }
    // evaluation test with available images
    {
        let prop_expr = PropertyExpression::<Image>::from_expr(image(literal_str("airport-11")));
        let evaluated_image = prop_expr.evaluate(
            &EvaluationContext::from_feature(Some(&empty_tile_feature()))
                .with_available_images(Some(&available_images)),
            Image::default(),
        );
        assert!(evaluated_image.is_available());
        assert_eq!(evaluated_image.id(), "airport-11");
        assert_eq!(Dependency::IMAGE, prop_expr.dependencies());
        let dd_prop_expr = PropertyExpression::<Image>::from_expr(image(get(literal_str("image_name"))));
        let evaluated_image = dd_prop_expr.evaluate(
            &EvaluationContext::from_feature(Some(&one_image())).with_available_images(Some(&available_images)),
            Image::default(),
        );
        assert!(evaluated_image.is_available());
        assert_eq!(evaluated_image.id(), "maki-11");
        assert_eq!(Dependency::IMAGE | Dependency::FEATURE, dd_prop_expr.dependencies());
        let evaluated_image = dd_prop_expr.evaluate(
            &EvaluationContext::from_feature(Some(&empty_tile_feature()))
                .with_available_images(Some(&available_images)),
            Image::default(),
        );
        assert!(!evaluated_image.is_available());
        assert_eq!(evaluated_image.id(), "");
    }
    // evaluation with zoom
    {
        let expr = step(zoom(), image(literal_str("airport-11")), 18.0, image(literal_str("bicycle-15")));
        let prop_expr = PropertyExpression::<Image>::from_expr(expr);
        let evaluated_image = prop_expr.evaluate(
            &EvaluationContext::with_zoom_feature(0.0, Some(&empty_tile_feature()))
                .with_available_images(Some(&available_images)),
            Image::default(),
        );
        assert!(evaluated_image.is_available());
        assert_eq!(evaluated_image.id(), "airport-11");
        let evaluated_image = prop_expr.evaluate(
            &EvaluationContext::with_zoom_feature(18.0, Some(&empty_tile_feature()))
                .with_available_images(Some(&available_images)),
            Image::default(),
        );
        assert!(evaluated_image.is_available());
        assert_eq!(evaluated_image.id(), "bicycle-15");
        assert_eq!(Dependency::IMAGE | Dependency::ZOOM, prop_expr.dependencies());
    }
}

/// Converts `geometry` from lon/lat to tile coordinates.
fn convert_geometry(geometry: &Geometry<f64>, canonical: &CanonicalTileID) -> GeometryCollection {
    crate::feature::convert_geometry_to_tile(geometry, canonical)
}

fn pt(x: f64, y: f64) -> Point<f64> {
    Point::new(x, y)
}

fn pts(points: &[(f64, f64)]) -> LineString<f64> {
    points.iter().copied().collect()
}

fn multi_point(points: LineString<f64>) -> Geometry<f64> {
    Geometry::MultiPoint(MultiPoint(points.into_points()))
}

fn multi_line_string(lines: Vec<LineString<f64>>) -> Geometry<f64> {
    Geometry::MultiLineString(MultiLineString(lines))
}

/// The polygon of `rings`, the exterior first.
fn polygon(rings: Vec<LineString<f64>>) -> Geometry<f64> {
    let mut rings = rings.into_iter();
    Geometry::Polygon(Polygon::new(rings.next().expect("an exterior"), rings.collect()))
}

fn create_geo_expression(op: &str, geojson: &str) -> Option<crate::expression::Expr> {
    create_expression_from_str(&format!(r#"["{op}", {geojson} ]"#), &Functions::with_builtins())
}

// gtest: PropertyExpression.WithinExpression
#[test]
fn within_expression() {
    let canonical_tile_id = CanonicalTileID::new(3, 3, 3);

    // geoJSON source must be Polygon.(Currently only supports Polygon)
    let invalid_geo_source = r#"{
      "type": "LineString",
      "coordinates": [[0, 0], [0, 5], [5, 5], [5, 0]]
    }"#;
    let valid_geo_source = r#"
    {
      "type": "Polygon",
      "coordinates": [
        [
          [-11.689453125, -9.79567758282973],
          [2.021484375, -10.012129557908128],
          [-15.99609375, -17.392579271057766],
          [-5.9765625, -5.659718554577273],
          [-16.259765625, -3.7327083213358336],
          [-17.75390625, -12.897489183755892],
          [-17.138671875, -21.002471054356715],
          [4.482421875, -16.8886597873816],
          [3.076171875, -7.01366792756663],
          [ -5.9326171875, 0.6591651462894632],
          [-16.1279296875, 1.4939713066293239],
          [-11.689453125, -9.79567758282973]
        ]
      ]
        }"#;

    // evaluation test with invalid geojson source
    {
        let expression = create_geo_expression("within", invalid_geo_source);
        assert!(expression.is_none());
    }

    // evaluation test with valid geojson source
    let expression = create_geo_expression("within", valid_geo_source);
    assert!(expression.is_some());
    let prop_expr = PropertyExpression::<bool>::from_expr(expression.unwrap());

    assert_eq!(Dependency::FEATURE, prop_expr.dependencies());

    let eval = |feature: &StubGeometryTileFeature| {
        prop_expr.evaluate(
            &EvaluationContext::from_feature(Some(feature)).with_canonical_tile_id(Some(&canonical_tile_id)),
            false,
        )
    };

    // evaluation test with valid geojson source but FeatureType is not Point/LineString
    // (currently only support FeatureType::Point and FeatureType::LineString)
    {
        // testPoly is inside polygon, but will return false
        let test_ring = vec![pts(&[
            (-9.228_515_625, -17.560_246_503_294_888),
            (-2.460_937_5, -16.045_813_453_752_17),
            (-9.228_515_625, -17.560_246_503_294_888),
        ])];
        let geo_poly = convert_geometry(&polygon(test_ring), &canonical_tile_id);
        let poly_feature = StubGeometryTileFeature::with_geometry(FeatureType::Polygon, geo_poly);

        assert!(!eval(&poly_feature));
    }
    // evaluation test with valid geojson source and valid linestring features
    {
        // testLine is inside polygon
        let test_line0 = pts(&[(-9.228_515_625, -17.560_246_503_294_888), (-2.460_937_5, -16.045_813_453_752_17)]);
        let geo_line0 = convert_geometry(&Geometry::LineString(test_line0), &canonical_tile_id);
        let line_feature0 = StubGeometryTileFeature::with_geometry(FeatureType::LineString, geo_line0);

        // testLine is intersecting polygon even though end points are all inside polygon, will return false
        let test_line1 =
            pts(&[(-10.415_039_062_5, -10.082_445_532_162_465), (-8.827_514_648_437_5, -9.194_292_714_912_638)]);
        let geo_line1 = convert_geometry(&Geometry::LineString(test_line1), &canonical_tile_id);
        let line_feature1 = StubGeometryTileFeature::with_geometry(FeatureType::LineString, geo_line1);

        assert!(eval(&line_feature0));
        assert!(!eval(&line_feature1));
    }

    // evaluation test with valid geojson source and valid point features
    {
        let get_point_feature = |x: f64, y: f64| {
            let geo_point = convert_geometry(&Geometry::Point(pt(x, y)), &canonical_tile_id);
            StubGeometryTileFeature::with_geometry(FeatureType::Point, geo_point)
        };

        // check `within` algorithm
        assert!(!eval(&get_point_feature(-10.722_656_25, -7.275_292_336_372_17)));
        assert!(!eval(&get_point_feature(-7.646_484_374_999_999, -12.382_928_338_487_396)));
        assert!(!eval(&get_point_feature(-15.249_023_437_5, -2.986_927_393_334_863)));
        assert!(!eval(&get_point_feature(-10.590_820_312_499_998, 2.460_181_181_021_005_2)));
        assert!(eval(&get_point_feature(-3.999_023_437_5, -4.915_832_801_313_164)));
        assert!(eval(&get_point_feature(-1.186_523_437_5, -16.636_191_878_397_65)));
        assert!(eval(&get_point_feature(-15.512_695_312_5, -11.738_302_371_436_84)));

        // On the boundary
        assert!(!eval(&get_point_feature(3.076_171_875, -7.013_667_927_566_63)));
        assert!(!eval(&get_point_feature(-11.689_453_125, -9.795_677_582_829_73)));
        assert!(!eval(&get_point_feature(2.021_484_375, -10.012_129_557_908_128)));
        assert!(!eval(&get_point_feature(-15.996_093_75, -17.392_579_271_057_766)));
        assert!(!eval(&get_point_feature(-5.976_562_5, -5.659_718_554_577_273)));
        assert!(!eval(&get_point_feature(-16.259_765_625, -3.732_708_321_335_833_6)));
        assert!(!eval(&get_point_feature(-17.753_906_25, -12.897_489_183_755_892)));
        assert!(!eval(&get_point_feature(-17.138_671_875, -21.002_471_054_356_715)));
        assert!(!eval(&get_point_feature(4.482_421_875, -16.888_659_787_381_6)));
        assert!(!eval(&get_point_feature(3.076_171_875, -7.013_667_927_566_63)));
        assert!(!eval(&get_point_feature(-5.932_617_187_5, 0.659_165_146_289_463_2)));
        assert!(!eval(&get_point_feature(-16.127_929_687_5, 1.493_971_306_629_323_9)));
        assert!(!eval(&get_point_feature(-11.689_453_125, -9.795_677_582_829_73)));
    }
}

// gtest: PropertyExpression.WithinExpressionAntiMeridian
#[test]
fn within_expression_anti_meridian() {
    let canonical_tile_id = CanonicalTileID::new(0, 0, 0);
    let eval = |prop_expr: &PropertyExpression<bool>, feature: &StubGeometryTileFeature| {
        prop_expr.evaluate(
            &EvaluationContext::from_feature(Some(feature)).with_canonical_tile_id(Some(&canonical_tile_id)),
            false,
        )
    };

    // evaluation test with line geometries
    {
        let polygon1 = r#"
       {
         "type": "Polygon",
         "coordinates": [[[-190, 0], [-178, 0], [-178, 10], [-190, 10], [-190, 0]]]
       }"#;
        // evaluation test with valid geojson source
        let expression = create_geo_expression("within", polygon1);
        assert!(expression.is_some());
        let prop_expr = PropertyExpression::<bool>::from_expr(expression.unwrap());

        let geo_line0 =
            convert_geometry(&Geometry::LineString(pts(&[(-183.0, 5.0), (-179.0, 1.0)])), &canonical_tile_id);
        let line_feature0 = StubGeometryTileFeature::with_geometry(FeatureType::LineString, geo_line0);

        let geo_line1 = convert_geometry(&Geometry::LineString(pts(&[(183.0, 5.0), (181.0, 1.0)])), &canonical_tile_id);
        let line_feature1 = StubGeometryTileFeature::with_geometry(FeatureType::LineString, geo_line1);

        assert!(eval(&prop_expr, &line_feature0));
        assert!(!eval(&prop_expr, &line_feature1));
    }

    // evaluation test with point geometries
    {
        let polygon2 = r#"
       {
         "type": "Polygon",
         "coordinates":  [[[-185.0, 60.0], [-175.0, 60.0], [-175.0, 65.0], [-185.0, 65.0], [-185.0, 60.0]]]
       }"#;
        // evaluation test with valid geojson source
        let expression = create_geo_expression("within", polygon2);
        assert!(expression.is_some());
        let prop_expr = PropertyExpression::<bool>::from_expr(expression.unwrap());

        let get_point_feature = |x: f64, y: f64| {
            let geo_point = convert_geometry(&Geometry::Point(pt(x, y)), &canonical_tile_id);
            StubGeometryTileFeature::with_geometry(FeatureType::Point, geo_point)
        };

        // check `within` algorithm
        assert!(eval(&prop_expr, &get_point_feature(177.0, 62.5)));
        assert!(eval(&prop_expr, &get_point_feature(180.0, 62.5)));
        assert!(eval(&prop_expr, &get_point_feature(-180.0, 62.5)));
        assert!(!eval(&prop_expr, &get_point_feature(-190.0, 62.5)));
    }
}

fn read_geometry_data(name: &str) -> String {
    super::util::read_file(&format!("test/fixtures/geometry_data/{name}"))
}

// gtest: PropertyExpression.DistanceExpression
#[test]
fn distance_expression() {
    let invalid_result = f64::NAN;
    let canonical_tile_id = CanonicalTileID::new(15, 18653, 9484);
    let eval = |prop_expr: &PropertyExpression<f64>, feature: &StubGeometryTileFeature| {
        prop_expr.evaluate(
            &EvaluationContext::from_feature(Some(feature)).with_canonical_tile_id(Some(&canonical_tile_id)),
            invalid_result,
        )
    };
    let distance_expr = |geojson: &str| {
        let expression = create_geo_expression("distance", geojson);
        assert!(expression.is_some(), "{geojson}");
        PropertyExpression::<f64>::from_expr(expression.unwrap())
    };
    let feature = |type_: FeatureType, geometry: Geometry<f64>| {
        StubGeometryTileFeature::with_geometry(type_, convert_geometry(&geometry, &canonical_tile_id))
    };
    let get_feature = |name: &str, type_: FeatureType| {
        let geometry_input = read_geometry_data(name);
        let features = geojson::features(geometry_input.parse().expect("fixture GeoJSON")).expect("fixture geometry");
        let geometry = features[0].geometry.clone().expect("a geometry");
        feature(type_, geometry)
    };

    // Parsing error with invalid geojson source
    {
        // geoJSON source must be Point or LineString.
        let invalid_geo_source = r#"{
            "type": "Unknown",
            "coordinates": [[[0, 0], [0, 5], [5, 5], [5, 0], [0, 0]]]
            }"#;
        assert!(create_geo_expression("distance", invalid_geo_source).is_none());
    }

    // Parsing error with extra arguments
    {
        let invalid_geo_source = r#"{
            "type": "Point",
            "coordinates": [0, 0],
            }"#;
        let expression = create_expression_from_str(
            &format!(r#"["distance", {invalid_geo_source}, "ExtraArgument"]"#),
            &Functions::with_builtins(),
        );
        assert!(expression.is_none());
    }

    let point_geo_source = r#"{
             "type": "Point",
             "coordinates":
               [
                   24.938492774963375,
                   60.16980226227959
               ]
              }"#;

    // Evaluation test with Point to Point distance by using different units
    {
        let point_feature =
            feature(FeatureType::Point, Geometry::Point(pt(24.931_159_615_516_663, 60.167_333_872_589_23)));

        // Default Unit: meters
        let prop_expr = distance_expr(point_geo_source);
        assert_eq!(Dependency::FEATURE, prop_expr.dependencies());

        assert_abs_diff_eq!(491.307, eval(&prop_expr, &point_feature), epsilon = 0.01);
    }

    // Evaluation test with Point to MultiPoint distance
    {
        let prop_expr = distance_expr(point_geo_source);
        let multi_point = feature(
            FeatureType::Point,
            multi_point(pts(&[
                (24.931_159_615_516_663, 60.167_333_872_589_23),
                (24.936_089_515_686_035, 60.169_033_745_694_826),
            ])),
        );
        assert_abs_diff_eq!(158.541, eval(&prop_expr, &multi_point), epsilon = 0.01);
    }

    // Evaluation test with Point to LineString distance
    {
        let prop_expr = distance_expr(point_geo_source);
        let line_feature = feature(
            FeatureType::LineString,
            Geometry::LineString(pts(&[
                (24.936_218_261_718_75, 60.171_317_895_077_54),
                (24.941_453_933_715_817, 60.170_229_208_170_014),
                (24.941_110_610_961_914, 60.168_500_043_045_34),
            ])),
        );
        assert_abs_diff_eq!(107.211, eval(&prop_expr, &line_feature), epsilon = 0.01);
    }

    // Evaluation test with Point to MultiLineString distance
    {
        let prop_expr = distance_expr(point_geo_source);
        let line_feature = feature(
            FeatureType::LineString,
            multi_line_string(vec![
                pts(&[
                    (24.936_218_261_718_75, 60.171_317_895_077_54),
                    (24.941_453_933_715_817, 60.170_229_208_170_014),
                    (24.941_110_610_961_914, 60.168_500_043_045_34),
                ]),
                pts(&[
                    (24.937_773_942_947_388, 60.169_401_995_468_24),
                    (24.933_900_833_129_883, 60.170_079_777_739_49),
                ]),
            ]),
        );
        assert_abs_diff_eq!(59.845, eval(&prop_expr, &line_feature), epsilon = 0.01);
    }

    // Evaluation test with MultiPoint to MultiPoint distance
    {
        let multi_points1 = read_geometry_data("multi_point_1.geojson");
        let multi_points_feature = get_feature("multi_point_2.geojson", FeatureType::Point);
        let prop_expr = distance_expr(&multi_points1);
        assert_abs_diff_eq!(283.60, eval(&prop_expr, &multi_points_feature), epsilon = 0.1);
    }

    // Evaluation test with MultiPoint to LineString distance
    {
        let multi_points1 = read_geometry_data("multi_point_1.geojson");
        let multi_line_feature = get_feature("line_string_2.geojson", FeatureType::LineString);
        let prop_expr = distance_expr(&multi_points1);
        assert_abs_diff_eq!(258.691, eval(&prop_expr, &multi_line_feature), epsilon = 0.01);

        // ----------------- switch feature and argument -----------------
        let multi_line2 = read_geometry_data("line_string_2.geojson");
        let multi_points_feature = get_feature("multi_point_1.geojson", FeatureType::Point);
        let prop_expr1 = distance_expr(&multi_line2);
        assert_abs_diff_eq!(258.691, eval(&prop_expr1, &multi_points_feature), epsilon = 0.01);
    }

    // Evaluation test with LineString to LineString distance
    {
        let line_string1 = read_geometry_data("line_string_1.geojson");
        let line_feature = get_feature("line_string_2.geojson", FeatureType::LineString);
        let prop_expr = distance_expr(&line_string1);
        assert_abs_diff_eq!(180.408, eval(&prop_expr, &line_feature), epsilon = 0.01);
    }

    // Evaluation test with MultiPoints to MutltiLineString distance
    {
        let multi_points = read_geometry_data("multi_point_2.geojson");
        let multi_line_feature = get_feature("multi_line_string_1.geojson", FeatureType::LineString);
        let prop_expr = distance_expr(&multi_points);
        assert_abs_diff_eq!(937.632, eval(&prop_expr, &multi_line_feature), epsilon = 0.01);
    }

    // Evaluation test with LineString to MutltiLineString distance
    {
        let line_string1 = read_geometry_data("line_string_1.geojson");
        let multi_line_feature = get_feature("multi_line_string_2.geojson", FeatureType::LineString);
        let prop_expr = distance_expr(&line_string1);
        assert_abs_diff_eq!(17.511, eval(&prop_expr, &multi_line_feature), epsilon = 0.01);
    }

    // Evaluation test with MultiLineString to MutltiLineString distance
    {
        let multi_line_string1 = read_geometry_data("multi_line_string_1.geojson");
        let multi_line_feature = get_feature("multi_line_string_2.geojson", FeatureType::LineString);
        let prop_expr = distance_expr(&multi_line_string1);
        assert_abs_diff_eq!(384.109, eval(&prop_expr, &multi_line_feature), epsilon = 0.01);
    }

    // Evaluation test with MultiPoint to MultiPolygon distance
    {
        let multi_polygon = read_geometry_data("multi_polygon_1.geojson");
        let prop_expr = distance_expr(&multi_polygon);

        let multi_point_feature1 = get_feature("multi_point_3.geojson", FeatureType::Point);
        assert_abs_diff_eq!(27.917, eval(&prop_expr, &multi_point_feature1), epsilon = 0.01);

        let multi_point_feature2 = get_feature("multi_point_2.geojson", FeatureType::Point);
        assert_abs_diff_eq!(0.0, eval(&prop_expr, &multi_point_feature2), epsilon = 0.01);
    }

    // Evaluation test with MultiLine to MultiPolygon distance
    {
        let multi_polygon = read_geometry_data("multi_polygon_1.geojson");
        let prop_expr = distance_expr(&multi_polygon);

        let multi_line_feature1 = get_feature("multi_line_string_1.geojson", FeatureType::LineString);
        assert_abs_diff_eq!(193.450, eval(&prop_expr, &multi_line_feature1), epsilon = 0.01);

        let multi_line_feature2 = get_feature("multi_line_string_2.geojson", FeatureType::LineString);
        assert_abs_diff_eq!(0.0, eval(&prop_expr, &multi_line_feature2), epsilon = 0.01);
    }

    // Evaluation test with Polygon to MultiPolygon distance
    {
        let multi_polygon = read_geometry_data("multi_polygon_1.geojson");
        let prop_expr = distance_expr(&multi_polygon);

        let polygon_feature = feature(
            FeatureType::Polygon,
            polygon(vec![pts(&[
                (24.941_711_425_781_25, 60.167_774_193_529_04),
                (24.948_577_880_859_375, 60.167_774_193_529_04),
                (24.948_577_880_859_375, 60.170_122_472_217_564),
                (24.941_711_425_781_25, 60.170_122_472_217_564),
                (24.941_711_425_781_25, 60.167_774_193_529_04),
            ])]),
        );
        assert_abs_diff_eq!(203.876, eval(&prop_expr, &polygon_feature), epsilon = 0.01);
    }

    // Evaluation test with MultiPolygon to MultiPolygon distance
    {
        let multi_polygon = read_geometry_data("multi_polygon_1.geojson");
        let prop_expr = distance_expr(&multi_polygon);

        let multi_polygon_feature = get_feature("multi_polygon_2.geojson", FeatureType::Polygon);
        assert_abs_diff_eq!(21.446, eval(&prop_expr, &multi_polygon_feature), epsilon = 0.01);
    }

    // Evaluation test with LineString/Polygon to Polygon distance with special cases
    {
        let multi_polygon = r#"{
                    "type": "Polygon",
                          "coordinates": [[[24.937891960144043, 60.16730451765011],
                                           [24.94892120361328, 60.16730451765011],
                                           [24.94892120361328, 60.17010112498546],
                                           [24.937891960144043, 60.17010112498546],
                                           [24.937891960144043, 60.16730451765011]]]}"#;
        let prop_expr = distance_expr(multi_polygon);

        // polygon1 is fully inside polygon
        let polygon_feature1 = feature(
            FeatureType::Polygon,
            polygon(vec![pts(&[
                (24.941_797_256_469_727, 60.168_500_043_045_34),
                (24.942_355_155_944_82, 60.168_500_043_045_34),
                (24.942_355_155_944_82, 60.169_439_353_910_285),
                (24.941_797_256_469_727, 60.169_439_353_910_285),
                (24.941_797_256_469_727, 60.168_500_043_045_34),
            ])]),
        );
        assert_abs_diff_eq!(0.0, eval(&prop_expr, &polygon_feature1), epsilon = 0.01);

        // polygon2's line is intersecting polygon, but no point is inside polygon
        let polygon_feature2 = feature(
            FeatureType::Polygon,
            polygon(vec![pts(&[
                (24.943_385_124_206_543, 60.166_066_249_059_554),
                (24.945_960_044_860_84, 60.166_066_249_059_554),
                (24.945_960_044_860_84, 60.171_424_627_146_2),
                (24.943_385_124_206_543, 60.171_424_627_146_2),
                (24.943_385_124_206_543, 60.166_066_249_059_554),
            ])]),
        );
        assert_abs_diff_eq!(0.0, eval(&prop_expr, &polygon_feature2), epsilon = 0.01);

        // one of polygon3's point is inside polygon
        let polygon_feature3 = feature(
            FeatureType::Polygon,
            polygon(vec![pts(&[
                (24.947_977_066_040_04, 60.169_460_701_572_4),
                (24.952_354_431_152_344, 60.169_460_701_572_4),
                (24.952_354_431_152_344, 60.170_826_923_095_42),
                (24.947_977_066_040_04, 60.170_826_923_095_42),
                (24.947_977_066_040_04, 60.169_460_701_572_4),
            ])]),
        );
        assert_abs_diff_eq!(0.0, eval(&prop_expr, &polygon_feature3), epsilon = 0.01);

        // lineString1 is within polygon
        let line_feature1 = feature(
            FeatureType::LineString,
            Geometry::LineString(pts(&[
                (24.947_097_301_483_154, 60.169_215_202_620_75),
                (24.947_311_878_204_346, 60.168_019_703_248_3),
            ])),
        );
        assert_abs_diff_eq!(0.0, eval(&prop_expr, &line_feature1), epsilon = 0.01);

        // lineString2 is intersecting the polygon but its points are outside the polygon
        let line_feature2 = feature(
            FeatureType::LineString,
            Geometry::LineString(pts(&[
                (24.939_994_812_011_72, 60.171_168_469_598_88),
                (24.940_037_727_355_957, 60.165_425_746_994_84),
            ])),
        );
        assert_abs_diff_eq!(0.0, eval(&prop_expr, &line_feature2), epsilon = 0.01);
    }
}