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
#[derive(Clone,PartialEq,Debug)]
pub enum Dataset {
  Node(Node),
  Way(Way),
  Relation(Relation),
  BBox(BBox),
  Timestamp(Timestamp),
}
impl Dataset {
  pub fn get_id(&self) -> Option<u64> {
    match self {
      Self::Node(node) => Some(node.id),
      Self::Way(way) => Some(way.id),
      Self::Relation(relation) => Some(relation.id),
      _ => None,
    }
  }
  pub fn get_info(&self) -> Option<Info> {
    match self {
      Self::Node(node) => node.info.clone(),
      Self::Way(way) => way.info.clone(),
      Self::Relation(relation) => relation.info.clone(),
      _ => None,
    }
  }
}

#[derive(Clone,PartialEq,Debug)]
pub enum DatasetType {
  Node(), Way(), Relation(), BBox(), Timestamp(),
  Header(), Sync(), Jump(), Reset(),
}

#[derive(Clone,PartialEq,Debug)]
pub enum ElementType {
  Node(), Way(), Relation(),
}

pub type Tags = std::collections::HashMap<String,String>;

pub trait Element {
  fn get_id(&self) -> u64;
  fn get_info<'a>(&'a self) -> Option<&'a Info>;
  fn get_type(&self) -> ElementType;
  fn get_tags<'a>(&'a self) -> &'a Tags;
}

#[derive(Clone,PartialEq,Debug)]
pub struct Info {
  pub version: Option<u64>,
  pub timestamp: Option<i64>,
  pub changeset: Option<u64>,
  pub uid: Option<u64>,
  pub user: Option<String>,
}
impl Info {
  pub fn new() -> Self {
    Self {
      version: None,
      timestamp: None,
      changeset: None,
      uid: None,
      user: None
    }
  }
}

#[derive(Clone,PartialEq,Debug)]
pub struct Node {
  pub id: u64,
  pub info: Option<Info>,
  pub data: Option<NodeData>,
  pub tags: Tags,
}
#[derive(Clone,PartialEq,Debug)]
pub struct NodeData {
  pub longitude: i32,
  pub latitude: i32,
}
impl NodeData {
  pub fn get_longitude(&self) -> f32 {
    (self.longitude as f32) / 1.0e7
  }
  pub fn get_latitude(&self) -> f32 {
    (self.latitude as f32) / 1.0e7
  }
}
impl Element for Node {
  fn get_id(&self) -> u64 { self.id }
  fn get_info<'a>(&'a self) -> Option<&'a Info> {
    self.info.as_ref()
  }
  fn get_type(&self) -> ElementType { ElementType::Node() }
  fn get_tags<'a>(&'a self) -> &'a Tags { &self.tags }
}

#[derive(Clone,PartialEq,Debug)]
pub struct Way {
  pub id: u64,
  pub info: Option<Info>,
  pub data: Option<WayData>,
  pub tags: Tags,
}
#[derive(Clone,PartialEq,Debug)]
pub struct WayData {
  pub refs: Vec<u64>
}
impl Element for Way {
  fn get_id(&self) -> u64 { self.id }
  fn get_info<'a>(&'a self) -> Option<&'a Info> {
    self.info.as_ref()
  }
  fn get_type(&self) -> ElementType { ElementType::Way() }
  fn get_tags<'a>(&'a self) -> &'a Tags { &self.tags }
}

#[derive(Clone,PartialEq,Debug)]
pub struct Relation {
  pub id: u64,
  pub info: Option<Info>,
  pub data: Option<RelationData>,
  pub tags: Tags,
}
#[derive(Clone,PartialEq,Debug)]
pub struct RelationData {
  pub members: Vec<RelationMember>
}
#[derive(Clone,PartialEq,Debug)]
pub struct RelationMember {
  pub id: u64,
  pub element_type: ElementType,
  pub role: String,
}
impl Element for Relation {
  fn get_id(&self) -> u64 { self.id }
  fn get_info<'a>(&'a self) -> Option<&'a Info> {
    self.info.as_ref()
  }
  fn get_type(&self) -> ElementType { ElementType::Relation() }
  fn get_tags<'a>(&'a self) -> &'a Tags { &self.tags }
}

#[derive(Clone,PartialEq,Debug)]
pub struct BBox {
  pub x1: i32,
  pub y1: i32,
  pub x2: i32,
  pub y2: i32,
}
impl BBox {
  pub fn get_x1(&self) -> f32 { self.x1 as f32 / 1.0e7 }
  pub fn get_y1(&self) -> f32 { self.y1 as f32 / 1.0e7 }
  pub fn get_x2(&self) -> f32 { self.x2 as f32 / 1.0e7 }
  pub fn get_y2(&self) -> f32 { self.y2 as f32 / 1.0e7 }
  pub fn get_bounds(&self) -> (f32,f32,f32,f32) {
    (self.get_x1(),self.get_y1(),self.get_x2(),self.get_y2())
  }
}

#[derive(Clone,PartialEq,Debug)]
pub struct Timestamp {
  pub time: i64,
}