File size: 1,819 Bytes
2574e86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use serde::{Deserialize, Serialize};
use solverforge::prelude::*;

/// Depot or customer site used by the routing model.
///
/// SolverForge treats a `Location` as read-only problem data. Routes refer to
/// locations by vector index so constraints and map rendering can cheaply look
/// up coordinates without copying place records into every visit.
#[problem_fact]
#[derive(Serialize, Deserialize)]
pub struct Location {
    #[planning_id]
    pub id: String,
    pub name: String,
    pub label: String,
    pub lat_e6: i32,
    pub lng_e6: i32,
    pub kind: String,
}

impl Location {
    /// Builds one location fact from seed data or transport input.
    pub fn new(
        id: impl Into<String>,
        name: impl Into<String>,
        label: String,
        lat_e6: i32,
        lng_e6: i32,
        kind: String,
    ) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            label,
            lat_e6,
            lng_e6,
            kind,
        }
    }

    /// Returns latitude in degrees from the integer microdegree storage format.
    pub fn lat(&self) -> f64 {
        f64::from(self.lat_e6) / 1_000_000.0
    }

    /// Returns longitude in degrees from the integer microdegree storage format.
    pub fn lng(&self) -> f64 {
        f64::from(self.lng_e6) / 1_000_000.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_location_construction() {
        let fact = Location::new(
            "test-id",
            "test",
            "test".to_string(),
            0,
            0,
            "test".to_string(),
        );
        assert_eq!(fact.id, "test-id");
        assert_eq!(fact.name, "test");
        let _ = &fact.label;
        let _ = &fact.lat_e6;
        let _ = &fact.lng_e6;
        let _ = &fact.kind;
    }
}