ggsql 0.4.0

A declarative visualization language that extends SQL with powerful data visualization capabilities.
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
//! Coordinate system resolution
//!
//! Resolves the default coordinate system by inspecting aesthetic mappings,
//! and post-scale resolution of projection properties like `radar`.

use super::coord::{Coord, CoordKind};
use super::Projection;
use crate::plot::aesthetic::{MATERIAL_AESTHETICS, POSITION_SUFFIXES};
use crate::plot::layer::GeomType;
use crate::plot::scale::ScaleTypeKind;
use crate::plot::Parameters;
use crate::plot::{Mappings, ParameterValue, Scale};
use crate::GgsqlError;

/// Cartesian primary aesthetic names
const CARTESIAN_PRIMARIES: &[&str] = &["x", "y"];

/// Polar primary aesthetic names
const POLAR_PRIMARIES: &[&str] = &["angle", "radius"];

/// Map primary aesthetic names
const MAP_PRIMARIES: &[&str] = &["lon", "lat"];

/// Resolve coordinate system for a Plot
///
/// If `project` is `Some`, returns `Ok(None)` (keep existing, no changes needed).
/// If `project` is `None`, infers coord from aesthetic mappings and layer types:
/// - x/y/xmin/xmax/ymin/ymax → Cartesian
/// - angle/radius/anglemin/... → Polar
/// - Any Spatial layer → Map
/// - Both cartesian+polar → Error
/// - Neither → Ok(None) (caller should use default Cartesian)
///
/// Called early in the pipeline, before AestheticContext construction.
pub fn resolve_coord(
    project: Option<&Projection>,
    global_mappings: &Mappings,
    layer_mappings: &[&Mappings],
    layer_geom_types: &[GeomType],
) -> Result<Option<Projection>, String> {
    // If project is explicitly specified, keep it as-is
    if project.is_some() {
        return Ok(None);
    }

    // Check if any layer is spatial
    let mut found_map = layer_geom_types.contains(&GeomType::Spatial);

    // Collect all explicit aesthetic keys from global and layer mappings
    let mut found_cartesian = false;
    let mut found_polar = false;

    // Check global mappings
    for aesthetic in global_mappings.aesthetics.keys() {
        check_aesthetic(
            aesthetic,
            &mut found_cartesian,
            &mut found_polar,
            &mut found_map,
        );
    }

    // Check layer mappings
    for layer_map in layer_mappings {
        for aesthetic in layer_map.aesthetics.keys() {
            check_aesthetic(
                aesthetic,
                &mut found_cartesian,
                &mut found_polar,
                &mut found_map,
            );
        }
    }

    // Determine result — check for conflicts
    let conflict_count = [found_cartesian, found_polar, found_map]
        .iter()
        .filter(|&&v| v)
        .count();
    if conflict_count > 1 {
        let mut systems = Vec::new();
        if found_cartesian {
            systems.push("cartesian (x/y)");
        }
        if found_polar {
            systems.push("polar (angle/radius)");
        }
        if found_map {
            systems.push("map (lon/lat)");
        }
        return Err(format!(
            "Conflicting aesthetics: cannot mix {} aesthetics in the same plot. \
             Use PROJECT TO specify the coordinate system explicitly.",
            systems.join(" and "),
        ));
    }

    if found_map {
        let coord = Coord::from_kind(CoordKind::Map);
        let aesthetics = coord
            .position_aesthetic_names()
            .iter()
            .map(|s| s.to_string())
            .collect();
        return Ok(Some(Projection {
            coord,
            aesthetics,
            properties: Parameters::new(),
            computed: Parameters::new(),
        }));
    }

    if found_polar {
        // Infer polar coordinate system
        let coord = Coord::from_kind(CoordKind::Polar);
        let aesthetics = coord
            .position_aesthetic_names()
            .iter()
            .map(|s| s.to_string())
            .collect();
        return Ok(Some(Projection {
            coord,
            aesthetics,
            properties: Parameters::new(),
            computed: Parameters::new(),
        }));
    }

    if found_cartesian {
        // Infer cartesian coordinate system
        let coord = Coord::from_kind(CoordKind::Cartesian);
        let aesthetics = coord
            .position_aesthetic_names()
            .iter()
            .map(|s| s.to_string())
            .collect();
        return Ok(Some(Projection {
            coord,
            aesthetics,
            properties: Parameters::new(),
            computed: Parameters::new(),
        }));
    }

    // Neither found - return None (caller uses default)
    Ok(None)
}

/// Check if an aesthetic name indicates a coordinate system.
/// Updates the found flags accordingly.
fn check_aesthetic(
    aesthetic: &str,
    found_cartesian: &mut bool,
    found_polar: &mut bool,
    found_map: &mut bool,
) {
    // Skip material aesthetics (color, size, etc.)
    if MATERIAL_AESTHETICS.contains(&aesthetic) {
        return;
    }

    // Strip position suffix if present (xmin -> x, anglemax -> angle)
    let primary = strip_position_suffix(aesthetic);

    // Check against cartesian primaries
    if CARTESIAN_PRIMARIES.contains(&primary) {
        *found_cartesian = true;
    }

    // Check against polar primaries
    if POLAR_PRIMARIES.contains(&primary) {
        *found_polar = true;
    }

    // Check against map primaries
    if MAP_PRIMARIES.contains(&primary) {
        *found_map = true;
    }
}

/// Strip position suffix from an aesthetic name.
/// e.g., "xmin" -> "x", "anglemax" -> "angle", "y" -> "y"
fn strip_position_suffix(name: &str) -> &str {
    for suffix in POSITION_SUFFIXES {
        if let Some(base) = name.strip_suffix(suffix) {
            return base;
        }
    }
    name
}

/// Resolve projection properties that depend on scale types.
///
/// Called after `resolve_scales()`. Currently handles:
/// - **`radar`** (polar only): When null (auto), sets to `true` if the theta
///   (pos2) scale is discrete/ordinal. When explicitly `true`, validates that
///   the theta scale is indeed discrete.
/// - **clip boundary** (map only): Computes the visible-area WKT polygon for
///   azimuthal projections and stores it in `computed`.
pub fn resolve_projection_properties(
    project: &mut Projection,
    scales: &[Scale],
) -> crate::Result<()> {
    if project.coord.coord_kind() != CoordKind::Polar {
        return Ok(());
    }

    let theta_scale = scales.iter().find(|s| s.aesthetic == "pos2");

    let theta_is_discrete = theta_scale
        .and_then(|s| s.scale_type.as_ref())
        .is_some_and(|st| {
            matches!(
                st.scale_type_kind(),
                ScaleTypeKind::Discrete | ScaleTypeKind::Ordinal
            )
        });

    let too_few_categories = theta_scale
        .and_then(|s| s.input_range.as_ref())
        .is_some_and(|r| r.len() <= 2);

    match project.properties.get("radar") {
        Some(ParameterValue::Boolean(true)) if !theta_is_discrete => {
            return Err(GgsqlError::ValidationError(
                "SETTING radar => true requires a discrete angle scale, \
                 but the angle aesthetic is continuous"
                    .to_string(),
            ));
        }
        Some(ParameterValue::Boolean(true)) if too_few_categories => {
            return Err(GgsqlError::ValidationError(
                "SETTING radar => true requires more than 2 categories \
                 on the angle aesthetic"
                    .to_string(),
            ));
        }
        Some(ParameterValue::Boolean(_)) => {
            // Explicit true (valid) or false — keep as-is
        }
        _ => {
            // Null / absent — auto-detect: discrete with >2 categories
            let use_radar = theta_is_discrete && !too_few_categories;
            project
                .properties
                .insert("radar".to_string(), ParameterValue::Boolean(use_radar));
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::plot::{AestheticValue, ArrayElement, ScaleType};

    /// Helper to create Mappings with given aesthetic names
    fn mappings_with(aesthetics: &[&str]) -> Mappings {
        let mut m = Mappings::new();
        for aes in aesthetics {
            m.insert(aes.to_string(), AestheticValue::standard_column("col"));
        }
        m
    }

    // ========================================
    // Test: Explicit project is preserved
    // ========================================

    #[test]
    fn test_resolve_keeps_explicit_project() {
        let project = Projection::cartesian();
        let global = mappings_with(&["angle", "radius"]); // Would infer polar
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(Some(&project), &global, &layers, &[]);
        assert!(result.is_ok());
        assert!(result.unwrap().is_none()); // None means keep existing
    }

    // ========================================
    // Test: Infer Cartesian
    // ========================================

    #[test]
    fn test_infer_cartesian_from_x_y() {
        let global = mappings_with(&["x", "y"]);
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_ok());
        let inferred = result.unwrap();
        assert!(inferred.is_some());
        let proj = inferred.unwrap();
        assert_eq!(proj.coord.coord_kind(), CoordKind::Cartesian);
        assert_eq!(proj.aesthetics, vec!["x", "y"]);
    }

    #[test]
    fn test_infer_cartesian_from_variants() {
        let global = mappings_with(&["xmin", "ymax"]);
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_ok());
        let inferred = result.unwrap();
        assert!(inferred.is_some());
        let proj = inferred.unwrap();
        assert_eq!(proj.coord.coord_kind(), CoordKind::Cartesian);
    }

    #[test]
    fn test_infer_cartesian_from_layer() {
        let global = Mappings::new();
        let layer = mappings_with(&["x", "y"]);
        let layers: Vec<&Mappings> = vec![&layer];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_ok());
        let inferred = result.unwrap();
        assert!(inferred.is_some());
        let proj = inferred.unwrap();
        assert_eq!(proj.coord.coord_kind(), CoordKind::Cartesian);
    }

    // ========================================
    // Test: Infer Polar
    // ========================================

    #[test]
    fn test_infer_polar_from_angle_radius() {
        let global = mappings_with(&["angle", "radius"]);
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_ok());
        let inferred = result.unwrap();
        assert!(inferred.is_some());
        let proj = inferred.unwrap();
        assert_eq!(proj.coord.coord_kind(), CoordKind::Polar);
        assert_eq!(proj.aesthetics, vec!["radius", "angle"]);
    }

    #[test]
    fn test_infer_polar_from_variants() {
        let global = mappings_with(&["anglemin", "radiusmax"]);
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_ok());
        let inferred = result.unwrap();
        assert!(inferred.is_some());
        let proj = inferred.unwrap();
        assert_eq!(proj.coord.coord_kind(), CoordKind::Polar);
    }

    #[test]
    fn test_infer_polar_from_layer() {
        let global = Mappings::new();
        let layer = mappings_with(&["angle", "radius"]);
        let layers: Vec<&Mappings> = vec![&layer];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_ok());
        let inferred = result.unwrap();
        assert!(inferred.is_some());
        let proj = inferred.unwrap();
        assert_eq!(proj.coord.coord_kind(), CoordKind::Polar);
    }

    // ========================================
    // Test: Material aesthetics ignored
    // ========================================

    #[test]
    fn test_ignore_material() {
        let global = mappings_with(&["color", "size", "fill", "opacity"]);
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_ok());
        assert!(result.unwrap().is_none()); // Neither cartesian nor polar
    }

    #[test]
    fn test_material_with_cartesian() {
        let global = mappings_with(&["x", "y", "color", "size"]);
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_ok());
        let inferred = result.unwrap();
        assert!(inferred.is_some());
        let proj = inferred.unwrap();
        assert_eq!(proj.coord.coord_kind(), CoordKind::Cartesian);
    }

    // ========================================
    // Test: Conflict error
    // ========================================

    #[test]
    fn test_conflict_error() {
        let global = mappings_with(&["x", "angle"]);
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.contains("Conflicting"));
        assert!(err.contains("cartesian"));
        assert!(err.contains("polar"));
    }

    #[test]
    fn test_conflict_across_global_and_layer() {
        let global = mappings_with(&["x", "y"]);
        let layer = mappings_with(&["angle"]);
        let layers: Vec<&Mappings> = vec![&layer];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.contains("Conflicting"));
    }

    #[test]
    fn test_conflict_cartesian_and_map() {
        let global = mappings_with(&["x", "lon"]);
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.contains("Conflicting"));
        assert!(err.contains("cartesian"));
        assert!(err.contains("map"));
    }

    #[test]
    fn test_conflict_polar_and_map() {
        let global = mappings_with(&["angle", "lat"]);
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.contains("Conflicting"));
        assert!(err.contains("polar"));
        assert!(err.contains("map"));
    }

    #[test]
    fn test_conflict_cartesian_and_spatial_layer() {
        let global = mappings_with(&["x", "y"]);
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(None, &global, &layers, &[GeomType::Spatial]);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.contains("Conflicting"));
        assert!(err.contains("cartesian"));
        assert!(err.contains("map"));
    }

    // ========================================
    // Test: Empty returns None (default)
    // ========================================

    #[test]
    fn test_empty_returns_none() {
        let global = Mappings::new();
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_ok());
        assert!(result.unwrap().is_none());
    }

    // ========================================
    // Test: Wildcard doesn't affect inference
    // ========================================

    #[test]
    fn test_wildcard_with_polar() {
        let mut global = Mappings::with_wildcard();
        global.insert("angle", AestheticValue::standard_column("cat"));
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_ok());
        let inferred = result.unwrap();
        assert!(inferred.is_some());
        let proj = inferred.unwrap();
        assert_eq!(proj.coord.coord_kind(), CoordKind::Polar);
    }

    #[test]
    fn test_wildcard_alone_returns_none() {
        let global = Mappings::with_wildcard();
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_ok());
        assert!(result.unwrap().is_none()); // Wildcard alone doesn't infer coord
    }

    // ========================================
    // Test: Spatial layer infers Map
    // ========================================

    #[test]
    fn test_infer_map_from_spatial_layer() {
        let global = Mappings::new();
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(None, &global, &layers, &[GeomType::Spatial]);
        assert!(result.is_ok());
        let inferred = result.unwrap();
        assert!(inferred.is_some());
        let proj = inferred.unwrap();
        assert_eq!(proj.coord.coord_kind(), CoordKind::Map);
        assert_eq!(proj.aesthetics, vec!["lon", "lat"]);
    }

    #[test]
    fn test_infer_map_from_spatial_among_other_layers() {
        let global = Mappings::new();
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(
            None,
            &global,
            &layers,
            &[GeomType::Spatial, GeomType::Point],
        );
        assert!(result.is_ok());
        let inferred = result.unwrap();
        assert!(inferred.is_some());
        let proj = inferred.unwrap();
        assert_eq!(proj.coord.coord_kind(), CoordKind::Map);
    }

    #[test]
    fn test_infer_map_from_lon_lat_aesthetics() {
        let global = mappings_with(&["lon", "lat"]);
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(None, &global, &layers, &[]);
        assert!(result.is_ok());
        let inferred = result.unwrap();
        assert!(inferred.is_some());
        let proj = inferred.unwrap();
        assert_eq!(proj.coord.coord_kind(), CoordKind::Map);
    }

    #[test]
    fn test_explicit_project_overrides_spatial() {
        let project = Projection::cartesian();
        let global = Mappings::new();
        let layers: Vec<&Mappings> = vec![];

        let result = resolve_coord(Some(&project), &global, &layers, &[GeomType::Spatial]);
        assert!(result.is_ok());
        assert!(result.unwrap().is_none());
    }

    // ========================================
    // Test: Helper functions
    // ========================================

    // ========================================
    // Test: resolve_projection_properties
    // ========================================

    fn scale_with_type(aesthetic: &str, discrete: bool) -> Scale {
        let mut s = Scale::new(aesthetic);
        s.scale_type = Some(if discrete {
            ScaleType::discrete()
        } else {
            ScaleType::continuous()
        });
        s
    }

    fn discrete_scale_with_n(aesthetic: &str, n: usize) -> Scale {
        let mut s = Scale::new(aesthetic);
        s.scale_type = Some(ScaleType::discrete());
        s.input_range = Some(
            (0..n)
                .map(|i| ArrayElement::String(format!("cat{i}")))
                .collect(),
        );
        s
    }

    #[test]
    fn test_radar_auto_true_for_discrete_theta() {
        let mut proj = Projection::polar();
        let scales = vec![discrete_scale_with_n("pos2", 5)];
        resolve_projection_properties(&mut proj, &scales).unwrap();
        assert_eq!(
            proj.properties.get("radar"),
            Some(&ParameterValue::Boolean(true))
        );
    }

    #[test]
    fn test_radar_auto_false_for_continuous_theta() {
        let mut proj = Projection::polar();
        let scales = vec![scale_with_type("pos2", false)];
        resolve_projection_properties(&mut proj, &scales).unwrap();
        assert_eq!(
            proj.properties.get("radar"),
            Some(&ParameterValue::Boolean(false))
        );
    }

    #[test]
    fn test_radar_auto_true_for_discrete_theta_no_range() {
        let mut proj = Projection::polar();
        let scales = vec![scale_with_type("pos2", true)];
        resolve_projection_properties(&mut proj, &scales).unwrap();
        assert_eq!(
            proj.properties.get("radar"),
            Some(&ParameterValue::Boolean(true))
        );
    }

    #[test]
    fn test_radar_auto_false_for_discrete_theta_2_categories() {
        let mut proj = Projection::polar();
        let scales = vec![discrete_scale_with_n("pos2", 2)];
        resolve_projection_properties(&mut proj, &scales).unwrap();
        assert_eq!(
            proj.properties.get("radar"),
            Some(&ParameterValue::Boolean(false))
        );
    }

    #[test]
    fn test_radar_auto_false_for_discrete_theta_1_category() {
        let mut proj = Projection::polar();
        let scales = vec![discrete_scale_with_n("pos2", 1)];
        resolve_projection_properties(&mut proj, &scales).unwrap();
        assert_eq!(
            proj.properties.get("radar"),
            Some(&ParameterValue::Boolean(false))
        );
    }

    #[test]
    fn test_radar_auto_true_for_discrete_theta_3_categories() {
        let mut proj = Projection::polar();
        let scales = vec![discrete_scale_with_n("pos2", 3)];
        resolve_projection_properties(&mut proj, &scales).unwrap();
        assert_eq!(
            proj.properties.get("radar"),
            Some(&ParameterValue::Boolean(true))
        );
    }

    #[test]
    fn test_radar_explicit_true_with_discrete_ok() {
        let mut proj = Projection::polar();
        proj.properties
            .insert("radar".to_string(), ParameterValue::Boolean(true));
        let scales = vec![discrete_scale_with_n("pos2", 4)];
        resolve_projection_properties(&mut proj, &scales).unwrap();
        assert_eq!(
            proj.properties.get("radar"),
            Some(&ParameterValue::Boolean(true))
        );
    }

    #[test]
    fn test_radar_explicit_true_with_2_categories_errors() {
        let mut proj = Projection::polar();
        proj.properties
            .insert("radar".to_string(), ParameterValue::Boolean(true));
        let scales = vec![discrete_scale_with_n("pos2", 2)];
        let result = resolve_projection_properties(&mut proj, &scales);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("more than 2"),
            "error should mention 'more than 2': {err}"
        );
    }

    #[test]
    fn test_radar_explicit_true_with_continuous_errors() {
        let mut proj = Projection::polar();
        proj.properties
            .insert("radar".to_string(), ParameterValue::Boolean(true));
        let scales = vec![scale_with_type("pos2", false)];
        let result = resolve_projection_properties(&mut proj, &scales);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("discrete"),
            "error should mention discrete: {err}"
        );
    }

    #[test]
    fn test_radar_explicit_false_with_discrete_preserved() {
        let mut proj = Projection::polar();
        proj.properties
            .insert("radar".to_string(), ParameterValue::Boolean(false));
        let scales = vec![scale_with_type("pos2", true)];
        resolve_projection_properties(&mut proj, &scales).unwrap();
        assert_eq!(
            proj.properties.get("radar"),
            Some(&ParameterValue::Boolean(false))
        );
    }

    #[test]
    fn test_radar_noop_for_cartesian() {
        let mut proj = Projection::cartesian();
        let scales = vec![scale_with_type("pos2", true)];
        resolve_projection_properties(&mut proj, &scales).unwrap();
        assert!(!proj.properties.contains_key("radar"));
    }

    #[test]
    fn test_strip_position_suffix() {
        assert_eq!(strip_position_suffix("x"), "x");
        assert_eq!(strip_position_suffix("y"), "y");
        assert_eq!(strip_position_suffix("xmin"), "x");
        assert_eq!(strip_position_suffix("xmax"), "x");
        assert_eq!(strip_position_suffix("xend"), "x");
        assert_eq!(strip_position_suffix("ymin"), "y");
        assert_eq!(strip_position_suffix("ymax"), "y");
        assert_eq!(strip_position_suffix("angle"), "angle");
        assert_eq!(strip_position_suffix("anglemin"), "angle");
        assert_eq!(strip_position_suffix("radiusmax"), "radius");
    }
}