photon_geocoding/
data.rs

1pub mod filter;
2pub mod json;
3
4use self::json::PhotonFeatureRaw;
5
6#[derive(Debug)]
7pub struct LatLon {
8    pub lat: f64,
9    pub lon: f64,
10}
11
12impl LatLon {
13    pub fn new(lat: f64, lon: f64) -> Self {
14        LatLon { lat, lon }
15    }
16
17    fn from_vec(vec: &[f64]) -> Self {
18        assert!(vec.len() >= 2);
19        LatLon {
20            lat: vec[1], // API format is [lon,lat]
21            lon: vec[0],
22        }
23    }
24}
25
26#[derive(Debug)]
27pub enum OsmType {
28    Relation,
29    Way,
30    Node,
31}
32
33impl From<String> for OsmType {
34    fn from(str: String) -> Self {
35        match str.as_str() {
36            "R" | "r" => Self::Relation,
37            "W" | "w" => Self::Way,
38            "N" | "n" => Self::Node,
39            _ => panic!("Unexpected OSM Type"),
40        }
41    }
42}
43
44/// A bounding box, described by two corner coordinates: south_west (min) and north_east (max).
45/// Semantically, south_west's coordinates are always smaller than north_east's, though this
46/// constraint is not enforced.
47#[derive(Debug)]
48pub struct BoundingBox {
49    pub south_west: LatLon,
50    pub north_east: LatLon,
51}
52
53impl From<Vec<f64>> for BoundingBox {
54    fn from(vec: Vec<f64>) -> Self {
55        assert!(vec.len() >= 4);
56
57        BoundingBox {
58            south_west: LatLon::from_vec(&vec[0..2]),
59            north_east: LatLon::from_vec(&vec[2..4]),
60        }
61    }
62}
63
64#[derive(Debug)]
65pub struct PhotonFeature {
66    pub coords: LatLon,
67
68    pub osm_id: u64,
69    pub osm_key: String,
70
71    pub osm_type: OsmType,
72    pub osm_value: String,
73    pub r#type: String,
74
75    pub extent: Option<BoundingBox>,
76    pub name: Option<String>,
77
78    pub country: Option<String>,
79    pub country_iso_code: Option<String>,
80    pub state: Option<String>,
81    pub county: Option<String>,
82    pub city: Option<String>,
83    pub postcode: Option<String>,
84    pub district: Option<String>,
85    pub street: Option<String>,
86    pub house_number: Option<String>,
87}
88
89impl From<PhotonFeatureRaw> for PhotonFeature {
90    fn from(raw: PhotonFeatureRaw) -> Self {
91        PhotonFeature {
92            coords: LatLon::from_vec(&raw.geometry.coordinates),
93            osm_id: raw.properties.osm_id,
94            osm_key: raw.properties.osm_key,
95            osm_type: OsmType::from(raw.properties.osm_type),
96            osm_value: raw.properties.osm_value,
97            r#type: raw.properties.r#type, // raw.r#type always equals "Feature", so property's type is more interesting
98            extent: raw.properties.extent.map(BoundingBox::from),
99            name: raw.properties.name,
100            country: raw.properties.country,
101            country_iso_code: raw.properties.countrycode,
102            state: raw.properties.state,
103            county: raw.properties.county,
104            city: raw.properties.city,
105            postcode: raw.properties.postcode,
106            district: raw.properties.district,
107            street: raw.properties.street,
108            house_number: raw.properties.housenumber,
109        }
110    }
111}