|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+use coord::LatLonDeg;
|
|
|
2
|
+use osmpbf::{DenseNode, Node, PrimitiveBlock, Relation, Way};
|
|
|
3
|
+use regex::Regex;
|
|
|
4
|
+
|
|
|
5
|
+
|
|
|
6
|
+pub trait Query {
|
|
|
7
|
+ type BI;
|
|
|
8
|
+ fn create_block_index(&self, &PrimitiveBlock) -> Self::BI;
|
|
|
9
|
+ fn node_matches(&self, &Self::BI, node: &Node) -> bool;
|
|
|
10
|
+ fn dense_node_matches(&self, &Self::BI, dnode: &DenseNode) -> bool;
|
|
|
11
|
+ fn way_matches(&self, &Self::BI, way: &Way) -> bool;
|
|
|
12
|
+ fn relation_matches(&self, &Self::BI, relation: &Relation) -> bool;
|
|
|
13
|
+}
|
|
|
14
|
+
|
|
|
15
|
+#[derive(Debug, Eq, PartialEq)]
|
|
|
16
|
+pub enum QueryArgs {
|
|
|
17
|
+ ValuePattern(String),
|
|
|
18
|
+ KeyValue(String, String),
|
|
|
19
|
+ Intersection(Box<(QueryArgs, QueryArgs)>),
|
|
|
20
|
+}
|
|
|
21
|
+
|
|
|
22
|
+#[derive(Debug)]
|
|
|
23
|
+pub enum QueryKind {
|
|
|
24
|
+ ValuePattern(ValuePatternQuery),
|
|
|
25
|
+ KeyValue(KeyValueQuery),
|
|
|
26
|
+ Intersection(Box<(QueryArgs, QueryArgs)>),
|
|
|
27
|
+}
|
|
|
28
|
+
|
|
|
29
|
+impl QueryArgs {
|
|
|
30
|
+ pub fn compile(self) -> Result<QueryKind, String> {
|
|
|
31
|
+ match self {
|
|
|
32
|
+ QueryArgs::ValuePattern(pattern) => {
|
|
|
33
|
+ Ok(QueryKind::ValuePattern(ValuePatternQuery::new(&pattern)?))
|
|
|
34
|
+ },
|
|
|
35
|
+ QueryArgs::KeyValue(k, v) => {
|
|
|
36
|
+ Ok(QueryKind::KeyValue(KeyValueQuery::new(k, v)))
|
|
|
37
|
+ },
|
|
|
38
|
+ _ => {
|
|
|
39
|
+ //TODO implement
|
|
|
40
|
+ unimplemented!();
|
|
|
41
|
+ },
|
|
|
42
|
+ }
|
|
|
43
|
+ }
|
|
|
44
|
+}
|
|
|
45
|
+
|
|
|
46
|
+#[derive(Debug)]
|
|
|
47
|
+pub struct ValuePatternQuery {
|
|
|
48
|
+ re: Regex,
|
|
|
49
|
+}
|
|
|
50
|
+
|
|
|
51
|
+impl ValuePatternQuery {
|
|
|
52
|
+ pub fn new(pattern: &str) -> Result<Self, String> {
|
|
|
53
|
+ let re = Regex::new(&pattern)
|
|
|
54
|
+ .map_err(|e| format!("{}", e))?;
|
|
|
55
|
+ Ok(ValuePatternQuery { re })
|
|
|
56
|
+ }
|
|
|
57
|
+}
|
|
|
58
|
+
|
|
|
59
|
+impl Query for ValuePatternQuery {
|
|
|
60
|
+ type BI = ();
|
|
|
61
|
+
|
|
|
62
|
+ fn create_block_index(&self, _block: &PrimitiveBlock) -> () {
|
|
|
63
|
+ ()
|
|
|
64
|
+ }
|
|
|
65
|
+
|
|
|
66
|
+ fn node_matches(&self, _: &(), node: &Node) -> bool {
|
|
|
67
|
+ for (_key, val) in node.tags() {
|
|
|
68
|
+ if self.re.is_match(val) {
|
|
|
69
|
+ return true;
|
|
|
70
|
+ }
|
|
|
71
|
+ }
|
|
|
72
|
+ return false;
|
|
|
73
|
+ }
|
|
|
74
|
+
|
|
|
75
|
+ fn dense_node_matches(&self, _: &(), dnode: &DenseNode) -> bool {
|
|
|
76
|
+ for (_key, val) in dnode.tags() {
|
|
|
77
|
+ if self.re.is_match(val) {
|
|
|
78
|
+ return true;
|
|
|
79
|
+ }
|
|
|
80
|
+ }
|
|
|
81
|
+ return false;
|
|
|
82
|
+ }
|
|
|
83
|
+
|
|
|
84
|
+ fn way_matches(&self, _: &(), way: &Way) -> bool {
|
|
|
85
|
+ for (_key, val) in way.tags() {
|
|
|
86
|
+ if self.re.is_match(val) {
|
|
|
87
|
+ return true;
|
|
|
88
|
+ }
|
|
|
89
|
+ }
|
|
|
90
|
+ return false;
|
|
|
91
|
+ }
|
|
|
92
|
+
|
|
|
93
|
+ fn relation_matches(&self, _: &(), relation: &Relation) -> bool {
|
|
|
94
|
+ for (_key, val) in relation.tags() {
|
|
|
95
|
+ if self.re.is_match(val) {
|
|
|
96
|
+ return true;
|
|
|
97
|
+ }
|
|
|
98
|
+ }
|
|
|
99
|
+ return false;
|
|
|
100
|
+ }
|
|
|
101
|
+}
|
|
|
102
|
+
|
|
|
103
|
+#[derive(Debug)]
|
|
|
104
|
+pub struct KeyValueQuery {
|
|
|
105
|
+ key: String,
|
|
|
106
|
+ value: String,
|
|
|
107
|
+}
|
|
|
108
|
+
|
|
|
109
|
+impl KeyValueQuery {
|
|
|
110
|
+ pub fn new<S: Into<String>>(key: S, value: S) -> Self {
|
|
|
111
|
+ KeyValueQuery {
|
|
|
112
|
+ key: key.into(),
|
|
|
113
|
+ value: value.into(),
|
|
|
114
|
+ }
|
|
|
115
|
+ }
|
|
|
116
|
+}
|
|
|
117
|
+
|
|
|
118
|
+impl Query for KeyValueQuery {
|
|
|
119
|
+ type BI = ();
|
|
|
120
|
+
|
|
|
121
|
+ fn create_block_index(&self, _block: &PrimitiveBlock) -> () {
|
|
|
122
|
+ ()
|
|
|
123
|
+ }
|
|
|
124
|
+
|
|
|
125
|
+ fn node_matches(&self, _: &(), node: &Node) -> bool {
|
|
|
126
|
+ for (key, val) in node.tags() {
|
|
|
127
|
+ if key == self.key && val == self.value {
|
|
|
128
|
+ return true;
|
|
|
129
|
+ }
|
|
|
130
|
+ }
|
|
|
131
|
+ return false;
|
|
|
132
|
+ }
|
|
|
133
|
+
|
|
|
134
|
+ fn dense_node_matches(&self, _: &(), dnode: &DenseNode) -> bool {
|
|
|
135
|
+ for (key, val) in dnode.tags() {
|
|
|
136
|
+ if key == self.key && val == self.value {
|
|
|
137
|
+ return true;
|
|
|
138
|
+ }
|
|
|
139
|
+ }
|
|
|
140
|
+ return false;
|
|
|
141
|
+ }
|
|
|
142
|
+
|
|
|
143
|
+ fn way_matches(&self, _: &(), way: &Way) -> bool {
|
|
|
144
|
+ for (key, val) in way.tags() {
|
|
|
145
|
+ if key == self.key && val == self.value {
|
|
|
146
|
+ return true;
|
|
|
147
|
+ }
|
|
|
148
|
+ }
|
|
|
149
|
+ return false;
|
|
|
150
|
+ }
|
|
|
151
|
+
|
|
|
152
|
+ fn relation_matches(&self, _: &(), relation: &Relation) -> bool {
|
|
|
153
|
+ for (key, val) in relation.tags() {
|
|
|
154
|
+ if key == self.key && val == self.value {
|
|
|
155
|
+ return true;
|
|
|
156
|
+ }
|
|
|
157
|
+ }
|
|
|
158
|
+ return false;
|
|
|
159
|
+ }
|
|
|
160
|
+}
|
|
|
161
|
+
|
|
|
162
|
+pub fn find_query_matches<Q: Query>(
|
|
|
163
|
+ block: &PrimitiveBlock,
|
|
|
164
|
+ query: &Q,
|
|
|
165
|
+ matches: &mut Vec<LatLonDeg>,
|
|
|
166
|
+ way_node_ids: &mut Vec<i64>,
|
|
|
167
|
+) {
|
|
|
168
|
+ let block_index = query.create_block_index(block);
|
|
|
169
|
+
|
|
|
170
|
+ for node in block.groups().flat_map(|g| g.nodes()) {
|
|
|
171
|
+ if query.node_matches(&block_index, &node) {
|
|
|
172
|
+ let pos = LatLonDeg::new(node.lat(), node.lon());
|
|
|
173
|
+ matches.push(pos);
|
|
|
174
|
+ }
|
|
|
175
|
+ }
|
|
|
176
|
+
|
|
|
177
|
+ for node in block.groups().flat_map(|g| g.dense_nodes()) {
|
|
|
178
|
+ if query.dense_node_matches(&block_index, &node) {
|
|
|
179
|
+ let pos = LatLonDeg::new(node.lat(), node.lon());
|
|
|
180
|
+ matches.push(pos);
|
|
|
181
|
+ }
|
|
|
182
|
+ }
|
|
|
183
|
+
|
|
|
184
|
+ for way in block.groups().flat_map(|g| g.ways()) {
|
|
|
185
|
+ if query.way_matches(&block_index, &way) {
|
|
|
186
|
+ way_node_ids.push(way.refs_slice()[0]);
|
|
|
187
|
+ }
|
|
|
188
|
+ }
|
|
|
189
|
+}
|