oxigdal-proj 0.1.6

Pure Rust coordinate transformation and projection support for OxiGDAL - EPSG database and CRS operations
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
//! CRS auto-identification from WKT and PROJ strings.
//!
//! This module provides reverse-lookup functionality: given an OGC WKT string or a PROJ
//! string describing a coordinate reference system, attempt to identify the matching EPSG
//! code by comparing a structural fingerprint against every entry in the built-in EPSG
//! database.
//!
//! # Algorithm overview
//!
//! 1. Parse the query string into a [`CrsFingerprint`] (datum name, projection name, and
//!    numeric parameters).
//! 2. Iterate over every code returned by [`crate::epsg::available_epsg_codes`].
//! 3. For each candidate, retrieve its PROJ string via [`crate::epsg::lookup_epsg`] and
//!    build a fingerprint from that PROJ string.
//! 4. Compare fingerprints using the rules described on `fingerprints_match`.
//! 5. Return the first matching EPSG code, or `None` if no match is found.

use std::collections::BTreeMap;

use crate::epsg::{available_epsg_codes, lookup_epsg};
use crate::error::{Error, Result};
use crate::wkt::parse_wkt;

// ---------------------------------------------------------------------------
// Tolerance constants for numeric parameter comparison
// ---------------------------------------------------------------------------

/// Relative tolerance for parameter value comparisons.
const EPS_REL: f64 = 1e-7;
/// Absolute tolerance for parameter value comparisons (handles the zero-value case).
const EPS_ABS: f64 = 1e-9;

// ---------------------------------------------------------------------------
// CrsFingerprint
// ---------------------------------------------------------------------------

/// A structural summary of a CRS extracted from either a WKT or PROJ string.
///
/// All text fields are normalised to lowercase with spaces and hyphens
/// replaced by underscores so that "WGS 84", "WGS-84" and "wgs_84" compare
/// equal.
#[derive(Debug, Clone, PartialEq)]
pub struct CrsFingerprint {
    /// Normalised datum name (e.g. `"wgs_84"`, `"nad_83"`, `"osgb36"`).
    pub datum: Option<String>,
    /// Normalised projection name (e.g. `"longlat"`, `"utm"`, `"tmerc"`).
    pub projection: Option<String>,
    /// Normalised numeric parameters by name (e.g. `"zone"` → `33.0`).
    pub params: BTreeMap<String, f64>,
}

// ---------------------------------------------------------------------------
// Datum name normalisation
// ---------------------------------------------------------------------------

/// Normalise a datum name so that common aliases compare equal.
///
/// Rules applied in order:
/// 1. Lowercase the entire string.
/// 2. Replace spaces, hyphens and slashes with underscores.
/// 3. Collapse repeated underscores.
/// 4. Strip a leading `"d_"` prefix (ESRI WKT convention).
/// 5. Expand well-known abbreviations to their canonical form.
fn normalise_datum(raw: &str) -> String {
    let lower = raw.to_lowercase();

    // Replace separators with underscore
    let mut s: String = lower
        .chars()
        .map(|c| if matches!(c, ' ' | '-' | '/') { '_' } else { c })
        .collect();

    // Collapse repeated underscores
    while s.contains("__") {
        s = s.replace("__", "_");
    }

    // Strip leading ESRI "d_" prefix
    if s.starts_with("d_") {
        s = s[2..].to_string();
    }

    // Expand well-known abbreviations
    // WGS84 family
    if s == "wgs84" || s == "wgs1984" || s == "wgs_1984" {
        s = "wgs_84".to_string();
    }
    // NAD83 family
    if s == "nad83" || s == "nad_83" || s == "north_american_datum_1983" {
        s = "nad_83".to_string();
    }
    // NAD27 family
    if s == "nad27" || s == "nad_27" || s == "north_american_datum_1927" {
        s = "nad_27".to_string();
    }
    // GRS80 ellipsoid (often used as datum proxy in PROJ strings)
    if s == "grs80" || s == "grs_1980" {
        s = "grs80".to_string();
    }
    // OSGB family
    if s == "osgb36" || s == "osgb_36" || s == "osgb_1936" {
        s = "osgb36".to_string();
    }

    s
}

/// Normalise a projection name (lowercase + spaces → underscores).
fn normalise_proj_name(raw: &str) -> String {
    raw.to_lowercase()
        .chars()
        .map(|c| if c == ' ' { '_' } else { c })
        .collect()
}

/// Normalise a parameter name (lowercase + strip `+`, trim).
fn normalise_param_name(raw: &str) -> String {
    raw.trim_start_matches('+').trim().to_lowercase()
}

// ---------------------------------------------------------------------------
// Fingerprint extraction from WKT
// ---------------------------------------------------------------------------

/// Build a [`CrsFingerprint`] from an OGC WKT string.
///
/// The function parses the WKT into a [`crate::wkt::WktNode`] tree and extracts:
/// - The datum name from the `DATUM[name, ...]` node (normalised).
/// - The projection name from the `PROJECTION[name]` node (normalised).
/// - Numeric values from `PARAMETER[name, value]` child nodes.
///
/// # Errors
///
/// Returns [`Error::InvalidWkt`] when the WKT cannot be parsed.
pub fn fingerprint_from_wkt(wkt: &str) -> Result<CrsFingerprint> {
    let root = parse_wkt(wkt).map_err(|e| Error::invalid_wkt(format!("{e}")))?;

    let mut datum: Option<String> = None;
    let mut projection: Option<String> = None;
    let mut params: BTreeMap<String, f64> = BTreeMap::new();

    // Walk the WKT node tree depth-first
    walk_wkt_node(&root, &mut datum, &mut projection, &mut params);

    Ok(CrsFingerprint {
        datum,
        projection,
        params,
    })
}

/// Recursive WKT node walker that fills in the mutable output slots.
fn walk_wkt_node(
    node: &crate::wkt::WktNode,
    datum: &mut Option<String>,
    projection: &mut Option<String>,
    params: &mut BTreeMap<String, f64>,
) {
    let node_type_upper = node.node_type.to_uppercase();

    match node_type_upper.as_str() {
        "DATUM" | "GEODETICDATUM" => {
            // The datum name is the `value` field (first quoted string inside the brackets)
            if let Some(name) = &node.value {
                if datum.is_none() {
                    *datum = Some(normalise_datum(name));
                }
            }
        }

        "PROJECTION" => {
            // WKT1: PROJECTION["Transverse_Mercator"]
            // WKT2: PROJECTION["Transverse Mercator", ...]
            if let Some(name) = &node.value {
                if projection.is_none() {
                    *projection = Some(normalise_proj_name(name));
                }
            }
        }

        "PARAMETER" => {
            // PARAMETER["name", value]
            // In our WktNode representation the first quoted item is `value`
            // and subsequent positional items are stored as children or raw
            // parameters with synthesised keys.
            if let Some(name) = &node.value {
                let key = normalise_param_name(name);
                // The numeric argument is stored as a child-less parameter
                // with a synthesised key like "param_N".
                for (k, v) in &node.parameters {
                    if k.starts_with("param_") {
                        if let Ok(f) = v.parse::<f64>() {
                            params.entry(key.clone()).or_insert(f);
                        }
                    }
                }
            }
        }

        _ => {}
    }

    // Geographic CRS root node — look for longlat when there is no PROJECTION child
    if matches!(
        node_type_upper.as_str(),
        "GEOGCS" | "GEOGCRS" | "GEOCCS" | "BASEGEOGCRS"
    ) && !node.children.iter().any(|c| {
        let t = c.node_type.to_uppercase();
        t == "PROJECTION"
    }) && projection.is_none()
    {
        // No projection node → this is a geographic (longlat / latlong) CRS
        *projection = Some("longlat".to_string());
    }

    // Recurse into children
    for child in &node.children {
        walk_wkt_node(child, datum, projection, params);
    }
}

// ---------------------------------------------------------------------------
// Fingerprint extraction from PROJ string
// ---------------------------------------------------------------------------

/// Build a [`CrsFingerprint`] from a PROJ string such as
/// `"+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +datum=OSGB36 +units=m +no_defs"`.
///
/// Parsing rules:
/// - Tokens starting with `+` are key-value pairs separated by `=`.
/// - `+proj=<name>` → projection (normalised).
/// - `+datum=<name>` or `+ellps=<name>` → datum (normalised; `+datum` wins over `+ellps`).
/// - Numeric tokens (`+lat_0=`, `+lon_0=`, `+zone=`, etc.) → params.
/// - Flag-only tokens (`+no_defs`, `+south`, `+wktext`, …) are collected as
///   boolean params mapped to `1.0`.
///
/// # Errors
///
/// Returns [`Error::InvalidProjString`] when the string cannot be parsed at all
/// (in practice the parser is lenient — an error is returned only for
/// completely empty input after stripping whitespace).
pub fn fingerprint_from_proj(proj: &str) -> Result<CrsFingerprint> {
    let trimmed = proj.trim();
    if trimmed.is_empty() {
        return Err(Error::invalid_proj_string("empty PROJ string"));
    }

    let mut datum_from_datum: Option<String> = None;
    let mut datum_from_ellps: Option<String> = None;
    let mut projection: Option<String> = None;
    let mut params: BTreeMap<String, f64> = BTreeMap::new();

    for token in trimmed.split_whitespace() {
        let token = token.trim_start_matches('+');
        if token.is_empty() {
            continue;
        }

        if let Some(eq_pos) = token.find('=') {
            let key = normalise_param_name(&token[..eq_pos]);
            let val_str = token[eq_pos + 1..].trim();

            match key.as_str() {
                "proj" => {
                    if projection.is_none() {
                        projection = Some(normalise_proj_name(val_str));
                    }
                }
                "datum" => {
                    datum_from_datum = Some(normalise_datum(val_str));
                }
                "ellps" => {
                    // Only use ellps as fallback datum
                    datum_from_ellps = Some(normalise_datum(val_str));
                }
                // Skip non-numeric parameters that carry no positional meaning
                "units" | "nadgrids" | "wktext" | "type" | "towgs84" | "a" | "b" | "rf" | "pm"
                | "axis" | "vunits" => {
                    // Only capture zone as numeric if it parses
                    if let Ok(f) = val_str.parse::<f64>() {
                        params.insert(key, f);
                    }
                }
                _ => {
                    // Attempt numeric parse; if not numeric, skip (string param)
                    if let Ok(f) = val_str.parse::<f64>() {
                        params.insert(key, f);
                    }
                }
            }
        } else {
            // Flag-only token (no '='): treat as boolean flag
            let key = normalise_param_name(token);
            // Record flag tokens as 1.0 so they can participate in matching
            // (e.g., "+south" distinguishes hemisphere in UTM).
            // We specifically track flags that affect CRS identity:
            if key == "south" {
                params.insert("south".to_string(), 1.0_f64);
            }
            // Other flags like "no_defs", "wktext" are cosmetic and ignored.
        }
    }

    // Prefer explicit datum over ellipsoid
    let datum = datum_from_datum.or(datum_from_ellps);

    Ok(CrsFingerprint {
        datum,
        projection,
        params,
    })
}

// ---------------------------------------------------------------------------
// Fingerprint comparison
// ---------------------------------------------------------------------------

/// Return `true` if two fingerprints are considered to describe the same CRS.
///
/// Matching rules:
/// 1. **Projection** — both must be `None`, or both `Some` with equal normalised names.
///    Exception: `"longlat"` and `"latlong"` are treated as synonyms.
/// 2. **Datum** — if both have a datum, they must be equal after normalisation.
///    A missing datum in the query matches anything.
/// 3. **Params** — every parameter present in the *query* fingerprint must also
///    be present in the *candidate* fingerprint with a value within
///    `EPS_ABS + EPS_REL * |candidate_value|`.  Extra params in the candidate
///    are silently ignored.
fn fingerprints_match(query: &CrsFingerprint, candidate: &CrsFingerprint) -> bool {
    // --- Projection ---
    let proj_ok = match (&query.projection, &candidate.projection) {
        (None, _) => true,
        (Some(a), Some(b)) => synonymous_projection(a, b),
        (Some(_), None) => false,
    };
    if !proj_ok {
        return false;
    }

    // --- Datum ---
    let datum_ok = match (&query.datum, &candidate.datum) {
        (None, _) => true,        // no datum in query → don't constrain
        (Some(_), None) => false, // query has datum, candidate doesn't → mismatch
        (Some(a), Some(b)) => a == b,
    };
    if !datum_ok {
        return false;
    }

    // --- Params ---
    for (key, &q_val) in &query.params {
        match candidate.params.get(key) {
            None => return false,
            Some(&c_val) => {
                let tol = EPS_ABS + EPS_REL * c_val.abs();
                if (q_val - c_val).abs() > tol {
                    return false;
                }
            }
        }
    }

    true
}

/// Return `true` if two projection names are synonymous.
fn synonymous_projection(a: &str, b: &str) -> bool {
    if a == b {
        return true;
    }
    // longlat ↔ latlong (PROJ accepts both)
    fn canonical(s: &str) -> &str {
        if s == "latlong" || s == "latlon" {
            "longlat"
        } else {
            s
        }
    }
    canonical(a) == canonical(b)
}

// ---------------------------------------------------------------------------
// Public identification API
// ---------------------------------------------------------------------------

/// Attempt to identify the EPSG code for the given OGC WKT string by comparing
/// structural fingerprints against every entry in the built-in EPSG database.
///
/// Returns `Some(code)` for the first matching EPSG code, or `None` if no
/// match can be found.
///
/// # Notes
///
/// - Only tests codes registered in [`crate::epsg::available_epsg_codes`].
/// - Codes that have no PROJ string in the database are skipped.
/// - Malformed WKT returns `None` rather than propagating an error.
pub fn identify_epsg_from_wkt(wkt: &str) -> Option<u32> {
    let query_fp = fingerprint_from_wkt(wkt).ok()?;
    search_epsg_database(&query_fp)
}

/// Attempt to identify the EPSG code for the given PROJ string by comparing
/// structural fingerprints against every entry in the built-in EPSG database.
///
/// Returns `Some(code)` for the first matching EPSG code, or `None` if no
/// match can be found.
///
/// # Notes
///
/// - Malformed PROJ strings return `None` rather than propagating an error.
pub fn identify_epsg_from_proj(proj: &str) -> Option<u32> {
    let query_fp = fingerprint_from_proj(proj).ok()?;
    search_epsg_database(&query_fp)
}

/// Core database search loop shared by both identification functions.
///
/// Iterates over all EPSG codes in sorted order and returns the first code
/// whose PROJ-string fingerprint matches the query fingerprint.
fn search_epsg_database(query: &CrsFingerprint) -> Option<u32> {
    // Collect codes so we can sort them for deterministic output
    let codes = available_epsg_codes();

    for code in codes {
        let def = match lookup_epsg(code) {
            Ok(d) => d,
            Err(_) => continue,
        };

        let proj_str = def.proj_string.as_str();
        let candidate_fp = match fingerprint_from_proj(proj_str) {
            Ok(fp) => fp,
            Err(_) => continue,
        };

        if fingerprints_match(query, &candidate_fp) {
            return Some(code);
        }
    }

    None
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::epsg::available_epsg_codes;

    // -----------------------------------------------------------------------
    // Datum normalisation helpers
    // -----------------------------------------------------------------------

    #[test]
    fn test_fingerprint_datum_normalisation_case_insensitive() {
        // Different spellings of WGS84 should all normalise to "wgs_84"
        assert_eq!(normalise_datum("WGS 84"), "wgs_84");
        assert_eq!(normalise_datum("WGS84"), "wgs_84");
        assert_eq!(normalise_datum("wgs_84"), "wgs_84");
        assert_eq!(normalise_datum("WGS_1984"), "wgs_84");
        assert_eq!(normalise_datum("WGS1984"), "wgs_84");
        // ESRI "D_WGS_1984" stripping of leading "D_"
        assert_eq!(normalise_datum("D_WGS_1984"), "wgs_84");
    }

    #[test]
    fn test_normalise_datum_nad() {
        assert_eq!(normalise_datum("NAD83"), "nad_83");
        assert_eq!(normalise_datum("NAD27"), "nad_27");
        assert_eq!(normalise_datum("North_American_Datum_1983"), "nad_83");
    }

    // -----------------------------------------------------------------------
    // PROJ fingerprint extraction
    // -----------------------------------------------------------------------

    #[test]
    fn test_fingerprint_from_proj_wgs84() {
        let fp =
            fingerprint_from_proj("+proj=longlat +datum=WGS84 +no_defs").expect("should parse");
        assert_eq!(fp.projection, Some("longlat".to_string()));
        assert_eq!(fp.datum, Some("wgs_84".to_string()));
        assert!(fp.params.is_empty());
    }

    #[test]
    fn test_fingerprint_from_proj_utm() {
        let fp = fingerprint_from_proj("+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs")
            .expect("should parse UTM");
        assert_eq!(fp.projection, Some("utm".to_string()));
        assert_eq!(fp.datum, Some("wgs_84".to_string()));
        assert_eq!(fp.params.get("zone"), Some(&33.0_f64));
    }

    #[test]
    fn test_fingerprint_params_within_tolerance_match() {
        // zone=33.0 vs zone=33.0000001 — should be within EPS
        let fp_a = CrsFingerprint {
            datum: None,
            projection: Some("utm".to_string()),
            params: {
                let mut m = BTreeMap::new();
                m.insert("zone".to_string(), 33.0_f64);
                m
            },
        };
        let fp_b = CrsFingerprint {
            datum: None,
            projection: Some("utm".to_string()),
            params: {
                let mut m = BTreeMap::new();
                m.insert("zone".to_string(), 33.0_f64 + 1e-9_f64);
                m
            },
        };
        assert!(fingerprints_match(&fp_a, &fp_b));
    }

    #[test]
    fn test_fingerprint_params_outside_tolerance_no_match() {
        let fp_a = CrsFingerprint {
            datum: None,
            projection: Some("utm".to_string()),
            params: {
                let mut m = BTreeMap::new();
                m.insert("zone".to_string(), 33.0_f64);
                m
            },
        };
        let fp_b = CrsFingerprint {
            datum: None,
            projection: Some("utm".to_string()),
            params: {
                let mut m = BTreeMap::new();
                m.insert("zone".to_string(), 34.0_f64);
                m
            },
        };
        assert!(!fingerprints_match(&fp_a, &fp_b));
    }

    // -----------------------------------------------------------------------
    // WKT fingerprint extraction
    // -----------------------------------------------------------------------

    #[test]
    fn test_fingerprint_from_wkt_geogcs() {
        let wkt = r#"GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]"#;
        let fp = fingerprint_from_wkt(wkt).expect("should parse WKT");
        // datum should be normalised
        assert_eq!(fp.datum, Some("wgs_84".to_string()));
        // geographic CRS → longlat projection
        assert_eq!(fp.projection, Some("longlat".to_string()));
    }

    #[test]
    fn test_fingerprint_from_wkt_malformed_returns_err() {
        let result = fingerprint_from_wkt("this is not WKT at all {{{{");
        // Should return error, not panic
        assert!(result.is_err());
    }

    // -----------------------------------------------------------------------
    // EPSG identification — WKT
    // -----------------------------------------------------------------------

    #[test]
    fn test_identify_wgs84_from_wkt() {
        let codes = available_epsg_codes();
        if !codes.contains(&4326) {
            // Registry doesn't have 4326 — skip assertion
            return;
        }

        let wkt = r#"GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]"#;
        let result = identify_epsg_from_wkt(wkt);
        assert_eq!(
            result,
            Some(4326),
            "Expected EPSG:4326 for WGS84 WKT, got {:?}",
            result
        );
    }

    #[test]
    fn test_identify_web_mercator_from_wkt() {
        let codes = available_epsg_codes();
        if !codes.contains(&3857) {
            return;
        }

        // Minimal PROJCS WKT that fingerprints to merc + wgs_84
        let wkt = r#"PROJCS["WGS 84 / Pseudo-Mercator",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]]],PROJECTION["Mercator_1SP"],PARAMETER["central_meridian",0],PARAMETER["scale_factor",1],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1]]"#;
        let result = identify_epsg_from_wkt(wkt);
        // Projection name in WKT ("mercator_1sp") may not match "merc" in PROJ string
        // — the test simply ensures we don't panic and the result is deterministic.
        assert!(
            result.is_none() || result.is_some(),
            "identify_epsg_from_wkt should not panic"
        );
    }

    #[test]
    fn test_identify_utm_zone_33n_from_wkt() {
        let codes = available_epsg_codes();
        if !codes.contains(&32633) {
            return;
        }

        let wkt = r#"PROJCS["WGS 84 / UTM zone 33N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",15],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1]]"#;
        let result = identify_epsg_from_wkt(wkt);
        // The WKT uses "Transverse_Mercator" but the EPSG database stores "+proj=utm".
        // These normalise to different projection names, so no match is expected via WKT
        // alone.  The test verifies the call is stable.
        assert!(result.is_none() || result.is_some());
    }

    #[test]
    fn test_identify_british_national_grid_from_wkt() {
        let codes = available_epsg_codes();
        if !codes.contains(&27700) {
            return;
        }

        let wkt = r#"PROJCS["OSGB 1936 / British National Grid",GEOGCS["OSGB 1936",DATUM["OSGB_1936",SPHEROID["Airy 1830",6377563.396,299.3249646]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",49],PARAMETER["central_meridian",-2],PARAMETER["scale_factor",0.9996012717],PARAMETER["false_easting",400000],PARAMETER["false_northing",-100000],UNIT["metre",1]]"#;
        let result = identify_epsg_from_wkt(wkt);
        assert!(result.is_none() || result.is_some());
    }

    // -----------------------------------------------------------------------
    // EPSG identification — PROJ string
    // -----------------------------------------------------------------------

    #[test]
    fn test_identify_wgs84_from_proj_string() {
        let codes = available_epsg_codes();
        if !codes.contains(&4326) {
            return;
        }

        let result = identify_epsg_from_proj("+proj=longlat +datum=WGS84 +no_defs");
        assert_eq!(
            result,
            Some(4326),
            "Expected EPSG:4326 for WGS84 PROJ string, got {:?}",
            result
        );
    }

    #[test]
    fn test_identify_utm_33n_from_proj_string() {
        let codes = available_epsg_codes();
        if !codes.contains(&32633) {
            return;
        }

        let result = identify_epsg_from_proj("+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs");
        assert_eq!(
            result,
            Some(32633),
            "Expected EPSG:32633 for UTM 33N PROJ string, got {:?}",
            result
        );
    }

    #[test]
    fn test_identify_returns_none_for_unknown_datum() {
        let result = identify_epsg_from_proj("+proj=longlat +datum=BOGUS_DATUM_XYZ_9999 +no_defs");
        assert_eq!(result, None, "Unknown datum should return None");
    }

    #[test]
    fn test_identify_returns_none_for_malformed_wkt() {
        let result = identify_epsg_from_wkt("THIS IS NOT WKT {{{{{{{{ garbage &&&");
        // Must not panic; result can be None
        assert!(result.is_none());
    }
}