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
pub mod filter;
pub mod json;

use self::json::PhotonFeatureRaw;

#[derive(Debug)]
pub struct LatLon {
    pub lat: f64,
    pub lon: f64,
}

impl LatLon {
    pub fn new(lat: f64, lon: f64) -> Self {
        LatLon { lat, lon }
    }

    fn from_vec(vec: &[f64]) -> Self {
        assert!(vec.len() >= 2);
        LatLon {
            lat: vec[1], // API format is [lon,lat]
            lon: vec[0],
        }
    }
}

#[derive(Debug)]
pub enum OsmType {
    Relation,
    Way,
    Node,
}

impl From<String> for OsmType {
    fn from(str: String) -> Self {
        match str.as_str() {
            "R" | "r" => Self::Relation,
            "W" | "w" => Self::Way,
            "N" | "n" => Self::Node,
            _ => panic!("Unexpected OSM Type"),
        }
    }
}

/// A bounding box, described by two corner coordinates: south_west (min) and north_east (max).
/// Semantically, south_west's coordinates are always smaller than north_east's, though this
/// constraint is not enforced.
#[derive(Debug)]
pub struct BoundingBox {
    pub south_west: LatLon,
    pub north_east: LatLon,
}

impl From<Vec<f64>> for BoundingBox {
    fn from(vec: Vec<f64>) -> Self {
        assert!(vec.len() >= 4);

        BoundingBox {
            south_west: LatLon::from_vec(&vec[0..2]),
            north_east: LatLon::from_vec(&vec[2..4]),
        }
    }
}

#[derive(Debug)]
pub struct PhotonFeature {
    pub coords: LatLon,

    pub osm_id: u64,
    pub osm_key: String,

    pub osm_type: OsmType,
    pub osm_value: String,
    pub r#type: String,

    pub extent: Option<BoundingBox>,
    pub name: Option<String>,

    pub country: Option<String>,
    pub country_iso_code: Option<String>,
    pub state: Option<String>,
    pub county: Option<String>,
    pub city: Option<String>,
    pub postcode: Option<String>,
    pub district: Option<String>,
    pub street: Option<String>,
    pub house_number: Option<String>,
}

impl From<PhotonFeatureRaw> for PhotonFeature {
    fn from(raw: PhotonFeatureRaw) -> Self {
        PhotonFeature {
            coords: LatLon::from_vec(&raw.geometry.coordinates),
            osm_id: raw.properties.osm_id,
            osm_key: raw.properties.osm_key,
            osm_type: OsmType::from(raw.properties.osm_type),
            osm_value: raw.properties.osm_value,
            r#type: raw.properties.r#type, // raw.r#type always equals "Feature", so property's type is more interesting
            extent: raw.properties.extent.map(BoundingBox::from),
            name: raw.properties.name,
            country: raw.properties.country,
            country_iso_code: raw.properties.countrycode,
            state: raw.properties.state,
            county: raw.properties.county,
            city: raw.properties.city,
            postcode: raw.properties.postcode,
            district: raw.properties.district,
            street: raw.properties.street,
            house_number: raw.properties.housenumber,
        }
    }
}