gistools/proj/parse/
util.rs

1use crate::proj::{BaseUnit, Unit};
2use alloc::string::String;
3
4/// Convert a string to camelCase
5pub fn to_camel_case(s: &str) -> String {
6    let mut result = String::new();
7    let mut capitalize_next = false;
8
9    for (i, c) in s.trim().chars().enumerate() {
10        if c == ' ' || c == '_' || c == '-' {
11            capitalize_next = true;
12        } else if i == 0 {
13            result.push(c.to_ascii_lowercase());
14        } else if capitalize_next {
15            result.push(c.to_ascii_uppercase());
16            capitalize_next = false;
17        } else {
18            result.push(c.to_ascii_lowercase());
19        }
20    }
21
22    result
23}
24
25/// Convert a string to PascalCase
26pub fn to_pascal_case(s: &str) -> String {
27    let mut result = String::new();
28    let mut capitalize_next = true;
29
30    for c in s.trim().chars() {
31        if c == ' ' || c == '_' || c == '-' {
32            capitalize_next = true;
33        } else if capitalize_next {
34            result.push(c.to_ascii_uppercase());
35            capitalize_next = false;
36        } else {
37            result.push(c);
38        }
39    }
40
41    result
42}
43
44/// Returns the prime meridian in degrees for the given name. Falls back to 0.0 (greenwich)
45/// if the name is not found
46pub fn get_prime_meridian(name: &str) -> f64 {
47    match name.to_lowercase().as_str() {
48        "copenhagen" => 12.0,           // 1026 - "12d34'39.9\"E",
49        "greenwich" => 0.0,             // 8901 - "0dE",
50        "lisbon" => -9.131906111111,    // 8902 - "9d07'54.862\"W",
51        "paris" => 2.337229166667,      // 8903 - "2d20'14.025\"E",
52        "bogota" => -74.080916666667,   // 8904 - "74d04'51.3\"W",
53        "madrid" => -3.687938888889,    // 8905 - "3d41'16.58\"W",
54        "rome" => 12.452333333333,      // 8906 - "12d27'8.4\"E",
55        "bern" => 7.439583333333,       // 8907 - "7d26'22.5\"E",
56        "jakarta" => 106.807719444444,  // 8908 - "106d48'27.79\"E",
57        "ferro" => -17.666666666667,    // 8909 - "17d40'W",
58        "brussels" => 4.367975,         // 8910 - "4d22'4.71\"E",
59        "stockholm" => 18.058277777778, // 8911 - "18d3'29.8\"E",
60        "athens" => 23.7163375,         // 8912 - "23d42'58.815\"E",
61        "budapest" => 19.040236111111,  // "19d0'36.9\"E",
62        "oslo" => 10.722916666667,      // 8913 - "10d55'39.5\"E",
63        // "paris_rgs" => // 8914 (UNUSED / REPLACED by paris) - "2d20'13.95\"E",
64        _ => 0.0,
65    }
66}
67
68/// Convert a linear unit name to a multiplier to convert input units to meters
69pub fn linear_unit_to_meters(unit: &str) -> f64 {
70    match unit.trim().to_lowercase().as_str() {
71        "km" | "kilometer" => 1000.0,
72        "metre" | "meter" | "m" => 1.0,
73        "dm" | "decimeter" => 0.1,
74        "cm" | "centimeter" => 0.01,
75        "mm" | "millimeter" => 0.001,
76        "kmi" | "internationalnauticalmile" => 1852.0,
77        "in" | "internationalinch" => 0.0254,
78        "ft" | "internationalfoot" => 0.3048,
79        "yd" | "internationalyard" => 0.9144,
80        "mi" | "internationalstatutemile" => 1609.344,
81        "fath" | "internationalfathom" => 1.8288,
82        "ch" | "internationalchain" => 20.1168,
83        "link" | "internationallink" => 0.201168,
84        "us-in" | "ussurveyorinch" => 100.0 / 3937.0,
85        "us-ft" | "ussurveyorfoot" => 1200.0 / 3937.0,
86        "us-yd" | "ussurveyoryard" => 3600.0 / 3937.0,
87        "us-ch" | "ussurveyorchain" => 79200.0 / 3937.0,
88        "us-mi" | "ussurveyorstatutemile" => 6336000.0 / 3937.0,
89        "ind-yd" | "indianyard" => 0.91439523,
90        "ind-ft" | "indianfoot" => 0.30479841,
91        "ind-ch" | "indianchain" => 20.11669506,
92        _ => 0.0,
93    }
94}
95
96/// Convert an angular unit name to a multiplier to convert input units to degrees
97pub fn angular_unit_to_degrees(unit: &str) -> f64 {
98    match unit.trim().to_lowercase().as_str() {
99        "rad" | "radian" => 180.0 / core::f64::consts::PI,
100        "grad" | "grade" => 1.0 / 200.0 * 180.0,
101        "degree" => 1.0,
102        _ => 0.0,
103    }
104}
105
106/// Convert a parameter name to a unit
107pub fn name_to_unit(name: &str) -> Unit {
108    match name.to_lowercase().as_str() {
109        "false_easting" | "false_northing" => Unit::BaseUnit(BaseUnit::Metre),
110        "scale_factor" => Unit::BaseUnit(BaseUnit::Unity),
111        _ => Unit::BaseUnit(BaseUnit::Degree),
112    }
113}
114
115/// Convert common axis names to order number
116pub fn axis_name_to_order(name: &str) -> u8 {
117    match name.to_lowercase().as_str() {
118        "x" | "(x)" | "east" | "east (x)" | "(e)" | "easting" | "easting (x)" => 0,
119        "y" | "(y)" | "north" | "north (y)" | "(n)" | "northing" | "northing (y)" => 1,
120        "z" | "(z)" => 2,
121        "t" | "(t)" => 3,
122        _ => 0,
123    }
124}