mln-rs-expression 0.0.14

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. 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
// Integer arithmetic on tile coordinates wraps where the C++ int64_t
// arithmetic could overflow (UB in C++; it wraps on the targets we build
// for). Rings and lines with fewer than two points walk no edges (C++ reads
// out of bounds for an empty one).

use std::f64::consts::PI;

use feature::Value as MbglValue;
use geo_types::{Coord, Geometry, Polygon};
use geojson::GeoJson;
use geometry::Point;
use mln_conversion::{Convertible, Error};
use mln_tile_id::CanonicalTileID;
use mln_tile_id::geometry_tile_data::{GeometryCoordinates, GeometryTileFeature};
use mln_util::constants::{DEGREES_MAX, EXTENT, LONGITUDE_MAX};
use mln_util::geometry::{FeatureType, to_feature_type};
use mln_util::logging::{Event, Log};

use crate::expression::{EvaluationContext, Expr, Node};
use crate::parsing_context::{ParseResult, ParsingContext};

// --- util/geometry_util ---

/// A bounding box: min x, min y, max x, max y.
type WithinBBox = [i64; 4];
const DEFAULT_WITHIN_BBOX: WithinBBox = [i64::MAX, i64::MAX, i64::MIN, i64::MIN];

type TilePolygon = Vec<Vec<Point<i64>>>;

fn update_bbox(bbox: &mut WithinBBox, p: &Point<i64>) {
    bbox[0] = bbox[0].min(p.x);
    bbox[1] = bbox[1].min(p.y);
    bbox[2] = bbox[2].max(p.x);
    bbox[3] = bbox[3].max(p.y);
}

/// Check if bbox1 is within bbox2.
fn box_within_box(bbox1: &WithinBBox, bbox2: &WithinBBox) -> bool {
    if bbox1[0] <= bbox2[0] {
        return false;
    }
    if bbox1[2] >= bbox2[2] {
        return false;
    }
    if bbox1[1] <= bbox2[1] {
        return false;
    }
    if bbox1[3] >= bbox2[3] {
        return false;
    }
    true
}

fn ray_intersect(p: &Point<i64>, p1: &Point<i64>, p2: &Point<i64>) -> bool {
    ((p1.y > p.y) != (p2.y > p.y))
        && (p.x
            < (p2.x.wrapping_sub(p1.x))
                .wrapping_mul(p.y.wrapping_sub(p1.y))
                .wrapping_div(p2.y.wrapping_sub(p1.y))
                .wrapping_add(p1.x))
}

/// Check if point p is on line segment with end points p1 and p2.
fn point_on_boundary(p: &Point<i64>, p1: &Point<i64>, p2: &Point<i64>) -> bool {
    // requirements of point p on line segment:
    // 1. colinear: cross product of vector p->p1(x1, y1) and vector
    //    p->p2(x2, y2) equals to 0
    // 2. p is between p1 and p2
    let x1 = p.x.wrapping_sub(p1.x);
    let y1 = p.y.wrapping_sub(p1.y);
    let x2 = p.x.wrapping_sub(p2.x);
    let y2 = p.y.wrapping_sub(p2.y);
    (x1.wrapping_mul(y2).wrapping_sub(x2.wrapping_mul(y1)) == 0)
        && (x1.wrapping_mul(x2) <= 0)
        && (y1.wrapping_mul(y2) <= 0)
}

fn segment_intersect_segment(a: &Point<i64>, b: &Point<i64>, c: &Point<i64>, d: &Point<i64>) -> bool {
    // a, b are end points for line segment1, c and d are end points for
    // line segment2
    let perp = |v1: &Point<i64>, v2: &Point<i64>| v1.x.wrapping_mul(v2.y).wrapping_sub(v1.y.wrapping_mul(v2.x));

    // check if two segments are parallel or not
    // precondition is end point a, b is inside polygon, if line a->b is
    // parallel to polygon edge c->d, then a->b won't intersect with c->d
    let vector_p = Point::new(b.x.wrapping_sub(a.x), b.y.wrapping_sub(a.y));
    let vector_q = Point::new(d.x.wrapping_sub(c.x), d.y.wrapping_sub(c.y));
    if perp(&vector_q, &vector_p) == 0 {
        return false;
    }

    // check if p1 and p2 are in different sides of line segment q1->q2
    let two_sided = |p1: &Point<i64>, p2: &Point<i64>, q1: &Point<i64>, q2: &Point<i64>| {
        // q1->p1 (x1, y1), q1->p2 (x2, y2), q1->q2 (x3, y3)
        let x1 = p1.x.wrapping_sub(q1.x);
        let y1 = p1.y.wrapping_sub(q1.y);
        let x2 = p2.x.wrapping_sub(q1.x);
        let y2 = p2.y.wrapping_sub(q1.y);
        let x3 = q2.x.wrapping_sub(q1.x);
        let y3 = q2.y.wrapping_sub(q1.y);
        let ret1 = x1.wrapping_mul(y3).wrapping_sub(x3.wrapping_mul(y1));
        let ret2 = x2.wrapping_mul(y3).wrapping_sub(x3.wrapping_mul(y2));
        (ret1 > 0 && ret2 < 0) || (ret1 < 0 && ret2 > 0)
    };

    // If lines are intersecting with each other, the relative location
    // should be: a and b lie in different sides of segment c->d, c and d lie
    // in different sides of segment a->b
    two_sided(a, b, c, d) && two_sided(c, d, a, b)
}

fn line_intersect_polygon(p1: &Point<i64>, p2: &Point<i64>, polygon: &TilePolygon) -> bool {
    // loop through every edge of every ring
    polygon.iter().any(|ring| ring.windows(2).any(|edge| segment_intersect_segment(p1, p2, &edge[0], &edge[1])))
}

/// Ray casting algorithm for detecting if point is in polygon.
fn point_within_polygon(point: &Point<i64>, polygon: &TilePolygon, true_on_boundary: bool) -> bool {
    let mut within = false;
    for ring in polygon {
        // loop through every edge of the ring
        for edge in ring.windows(2) {
            if point_on_boundary(point, &edge[0], &edge[1]) {
                return true_on_boundary;
            }
            if ray_intersect(point, &edge[0], &edge[1]) {
                within = !within;
            }
        }
    }
    within
}

fn point_within_polygons(point: &Point<i64>, polygons: &[TilePolygon], true_on_boundary: bool) -> bool {
    polygons.iter().any(|polygon| point_within_polygon(point, polygon, true_on_boundary))
}

fn line_string_within_polygon(line: &[Point<i64>], polygon: &TilePolygon) -> bool {
    // First, check if geometry points of line segments are all inside polygon
    if !line.iter().all(|point| point_within_polygon(point, polygon, false)) {
        return false;
    }

    // Second, check if there is line segment intersecting polygon edge
    !line.windows(2).any(|segment| line_intersect_polygon(&segment[0], &segment[1], polygon))
}

fn line_string_within_polygons(line: &[Point<i64>], polygons: &[TilePolygon]) -> bool {
    polygons.iter().any(|polygon| line_string_within_polygon(line, polygon))
}

fn lat_lon_to_tile_coordinates(point: &Coord<f64>, canonical: &CanonicalTileID) -> Point<i64> {
    let size = f64::from(EXTENT) * 2f64.powi(i32::from(canonical.z));

    let x = (point.x + LONGITUDE_MAX) * size / DEGREES_MAX;
    let y =
        (LONGITUDE_MAX - mln_math::rad2deg((point.y * PI / DEGREES_MAX + PI / 4.0).tan().ln())) * size / DEGREES_MAX;

    // `util::clamp<int64_t>(static_cast<int64_t>(x), min, max)`; `as`
    // saturates where the C++ cast is UB.
    Point::new(x as i64, y as i64)
}

fn get_tile_polygon(polygon: &Polygon<f64>, canonical: &CanonicalTileID, bbox: &mut WithinBBox) -> TilePolygon {
    let mut result = Vec::with_capacity(1 + polygon.interiors().len());
    for ring in std::iter::once(polygon.exterior()).chain(polygon.interiors()) {
        let mut temp = Vec::with_capacity(ring.0.len());
        for p in &ring.0 {
            let coord = lat_lon_to_tile_coordinates(p, canonical);
            temp.push(coord);
            update_bbox(bbox, &coord);
        }
        result.push(temp);
    }
    result
}

fn get_tile_polygons(
    polygon_geo_set: &Geometry<f64>,
    canonical: &CanonicalTileID,
    bbox: &mut WithinBBox,
) -> Vec<TilePolygon> {
    match polygon_geo_set {
        Geometry::MultiPolygon(polygons) => polygons.iter().map(|pg| get_tile_polygon(pg, canonical, bbox)).collect(),
        Geometry::Polygon(polygon) => vec![get_tile_polygon(polygon, canonical, bbox)],
        Geometry::Rect(rect) => vec![get_tile_polygon(&rect.to_polygon(), canonical, bbox)],
        Geometry::Triangle(triangle) => vec![get_tile_polygon(&triangle.to_polygon(), canonical, bbox)],
        _ => Vec::new(),
    }
}

fn update_point(p: &mut Point<i64>, bbox: &mut WithinBBox, poly_bbox: &WithinBBox, world_size: i64) {
    if p.x < poly_bbox[0] || p.x > poly_bbox[2] {
        let get_shift = |point: &Point<i64>, polygon_side: i64, size: i64| -> i64 {
            if point.x.wrapping_sub(polygon_side) > size / 2 {
                return -size;
            }
            if polygon_side.wrapping_sub(point.x) > size / 2 {
                return size;
            }
            0
        };

        let mut shift = get_shift(p, poly_bbox[0], world_size);
        if shift == 0 {
            shift = get_shift(p, poly_bbox[2], world_size);
        }
        p.x = p.x.wrapping_add(shift);
    }
    update_bbox(bbox, p);
}

/// `EXTENT * coordinate`, wrapping as in C++: an int32 times a uint32 is a
/// uint32 product there, widened to `int64_t` afterwards.
fn tile_shift(coordinate: u32) -> i64 {
    i64::from((EXTENT as u32).wrapping_mul(coordinate))
}

fn world_size(canonical: &CanonicalTileID) -> i64 {
    (f64::from(EXTENT) * 2f64.powi(i32::from(canonical.z))) as i64
}

fn get_tile_points(
    points: &GeometryCoordinates,
    canonical: &CanonicalTileID,
    bbox: &mut WithinBBox,
    poly_bbox: &WithinBBox,
) -> Vec<Point<i64>> {
    let x_shift = tile_shift(canonical.x);
    let y_shift = tile_shift(canonical.y);
    let world_size = world_size(canonical);
    let mut results = Vec::with_capacity(points.len());
    for p in points {
        let mut point = Point::new(i64::from(p.x) + x_shift, i64::from(p.y) + y_shift);
        update_point(&mut point, bbox, poly_bbox, world_size);
        results.push(point);
    }
    results
}

fn get_tile_lines(
    lines: &[GeometryCoordinates],
    canonical: &CanonicalTileID,
    bbox: &mut WithinBBox,
    poly_bbox: &WithinBBox,
) -> Vec<Vec<Point<i64>>> {
    let x_shift = tile_shift(canonical.x);
    let y_shift = tile_shift(canonical.y);
    let mut results = Vec::with_capacity(lines.len());
    for line in lines {
        let mut line_string = Vec::with_capacity(line.len());
        for p in line {
            let point = Point::new(i64::from(p.x) + x_shift, i64::from(p.y) + y_shift);
            update_bbox(bbox, &point);
            line_string.push(point);
        }
        results.push(line_string);
    }

    let world_size = world_size(canonical);
    if bbox[2].wrapping_sub(bbox[0]) <= world_size / 2 {
        *bbox = DEFAULT_WITHIN_BBOX;
        for line in &mut results {
            for p in line.iter_mut() {
                update_point(p, bbox, poly_bbox, world_size);
            }
        }
    }
    results
}

fn feature_within_polygons(
    feature: &dyn GeometryTileFeature,
    canonical: &CanonicalTileID,
    polygon_geo_set: &Geometry<f64>,
) -> bool {
    let mut poly_bbox = DEFAULT_WITHIN_BBOX;
    let polygons = get_tile_polygons(polygon_geo_set, canonical, &mut poly_bbox);
    // An empty multi-polygon contains nothing.
    if polygons.is_empty() {
        return false;
    }
    // A geometry that can't be decoded is within nothing.
    let Ok(geometries) = feature.get_geometries() else {
        return false;
    };
    match feature.get_type() {
        FeatureType::Point => {
            // C++ asserts a geometry and reads `geometries.at(0)` (which
            // throws when there is none).
            let Some(first) = geometries.first() else {
                return false;
            };
            let mut point_bbox = DEFAULT_WITHIN_BBOX;
            let points = get_tile_points(first, canonical, &mut point_bbox, &poly_bbox);
            if !box_within_box(&point_bbox, &poly_bbox) {
                return false;
            }

            points.iter().all(|p| point_within_polygons(p, &polygons, false))
        }
        FeatureType::LineString => {
            let mut line_bbox = DEFAULT_WITHIN_BBOX;
            let multi_line_string = get_tile_lines(&geometries, canonical, &mut line_bbox, &poly_bbox);
            if !box_within_box(&line_bbox, &poly_bbox) {
                return false;
            }

            multi_line_string.iter().all(|line| line_string_within_polygons(line, &polygons))
        }
        _ => false,
    }
}

const POLYGON_REQUIRED: &str = "'within' expression requires valid geojson source that contains polygon geometry type.";

fn parse_value(value: &Convertible<'_>, ctx: &mut ParsingContext) -> Option<GeoJson> {
    if value.is_object() {
        let mut error = Error::default();
        let geojson = value.to_geojson(&mut error);
        if let Some(geojson) = geojson
            && error.message.is_empty()
        {
            return Some(geojson);
        }
        ctx.error(error.message);
    }

    ctx.error(POLYGON_REQUIRED.to_owned());
    None
}

fn get_polygon_info(geometry: Option<&geojson::Geometry>, ctx: &mut ParsingContext) -> Option<Geometry<f64>> {
    match geometry.map(|g| feature::geojson::geometry(g.clone())) {
        Some(Ok(geometry)) if to_feature_type(Some(&geometry)) == FeatureType::Polygon => return Some(geometry),
        Some(Err(e)) => {
            ctx.error(e.to_string());
            return None;
        }
        _ => {}
    }
    ctx.error(POLYGON_REQUIRED.to_owned());
    None
}

/// The geometry of each feature of `geojson`; a bare geometry is one.
pub(crate) fn geojson_geometries(geojson: &GeoJson) -> Vec<Option<&geojson::Geometry>> {
    match geojson {
        GeoJson::Geometry(geometry) => vec![Some(geometry)],
        GeoJson::Feature(feature) => vec![feature.geometry.as_ref()],
        GeoJson::FeatureCollection(collection) => collection.features.iter().map(|f| f.geometry.as_ref()).collect(),
    }
}

/// `["within", geojson]`: whether the feature lies inside the polygons.
#[derive(Clone, PartialEq)]
pub(crate) struct Within {
    geojson_source: GeoJson,
    geometries: Geometry<f64>,
}

/// A `within` crosses as its GeoJSON, as the style writes it, and finds its
/// polygons again.
impl mln_wire::Wire for Within {
    fn write(&self, w: &mut mln_wire::Writer) {
        self.source_value().write(w);
    }

    fn read(r: &mut mln_wire::Reader<'_>) -> Option<Self> {
        Within::try_from(MbglValue::read(r)?).ok().or_else(|| r.fail("invalid within GeoJSON"))
    }
}

impl TryFrom<MbglValue> for Within {
    type Error = String;

    fn try_from(value: MbglValue) -> Result<Self, String> {
        let mut ctx = ParsingContext::without_functions();
        parse_value(&Convertible::from(&value), &mut ctx)
            .and_then(|geojson_source| Within::from_source(geojson_source, &mut ctx))
            .ok_or_else(|| ctx.get_combined_errors())
    }
}

impl From<Within> for MbglValue {
    fn from(within: Within) -> Self {
        within.source_value()
    }
}

impl Within {
    pub(crate) fn parse(value: &Convertible<'_>, ctx: &mut ParsingContext) -> ParseResult {
        // object value, quoted with ["within", value]
        if value.array_length() != 2 {
            ctx.error(format!(
                "'within' expression requires exactly one argument, but found {} instead.",
                value.array_length() - 1
            ));
            return None;
        }

        let parsed_value = parse_value(&value.array_member(1), ctx)?;
        Within::from_source(parsed_value, ctx).map(|within| Expr::new(Node::Within(Box::new(within))))
    }

    /// The `within` of a GeoJSON value: of its first polygon. Every
    /// non-polygon before that reports an error.
    fn from_source(geojson_source: GeoJson, ctx: &mut ParsingContext) -> Option<Within> {
        let candidates = geojson_geometries(&geojson_source);
        let found = candidates.iter().find_map(|geometry| get_polygon_info(*geometry, ctx));
        // An empty collection has no geometry to report an error about.
        if candidates.is_empty() {
            ctx.error(POLYGON_REQUIRED.to_owned());
        }
        Some(Within { geometries: found?, geojson_source })
    }
}

impl Within {
    pub(crate) fn eval_bool(&self, params: &EvaluationContext<'_>) -> bool {
        let (Some(feature), Some(canonical)) = (params.feature, params.canonical) else {
            return false;
        };
        let geometry_type = feature.get_type();
        // Currently only support Point and LineString types in Polygon/Polygons
        if geometry_type == FeatureType::Point || geometry_type == FeatureType::LineString {
            return feature_within_polygons(feature, canonical, &self.geometries);
        }
        Log::warning(Event::General, "within expression currently only support Point/LineString geometry type.");

        false
    }

    pub(crate) fn serialize(&self) -> MbglValue {
        MbglValue::Array(vec![MbglValue::String("within".to_owned()), self.source_value()])
    }

    /// The GeoJSON, as a style value.
    fn source_value(&self) -> MbglValue {
        mln_conversion::json_to_value(&serde_json::to_value(&self.geojson_source).unwrap_or_default())
    }
}

#[cfg(test)]
mod tests {
    use std::borrow::Cow;

    use super::*;
    use crate::Functions;
    use crate::value::Value;

    #[test]
    fn empty_feature_collection_is_a_parse_error() {
        let document = serde_json::json!(["within", {"type": "FeatureCollection", "features": []}]);
        let mut ctx = ParsingContext::new(&Functions::with_builtins());
        assert!(ctx.parse_expression(&Convertible::from(&document), None).is_none());
        assert_eq!(ctx.get_errors().len(), 1);
        assert_eq!(ctx.get_errors()[0].message, POLYGON_REQUIRED);
    }

    struct PointFeature(mln_tile_id::geometry_tile_data::GeometryCollection);

    impl GeometryTileFeature for PointFeature {
        fn get_type(&self) -> FeatureType {
            FeatureType::Point
        }

        fn get_value(&self, _key: &str) -> Option<MbglValue> {
            None
        }

        fn get_geometries(
            &self,
        ) -> Result<Cow<'_, mln_tile_id::geometry_tile_data::GeometryCollection>, mln_util::error::Error> {
            Ok(Cow::Borrowed(&self.0))
        }
    }

    /// An empty multi-polygon parses, and contains nothing.
    #[test]
    fn nothing_is_within_an_empty_multi_polygon() {
        let document = serde_json::json!(["within", {"type": "MultiPolygon", "coordinates": []}]);
        let mut ctx = ParsingContext::new(&Functions::with_builtins());
        let within = ctx.parse_expression(&Convertible::from(&document), None).expect("parses");
        let feature = PointFeature(vec![vec![Point::new(10, 10)]].into());
        let canonical = CanonicalTileID::new(3, 3, 3);
        let params = EvaluationContext::from_feature(Some(&feature)).with_canonical_tile_id(Some(&canonical));
        assert_eq!(within.evaluate(&params), Ok(Value::Bool(false)));
    }
}