A simple map viewer

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. use coord::LatLonDeg;
  2. use osmpbf::{DenseNode, Node, PrimitiveBlock, Relation, Way};
  3. use regex::Regex;
  4. use search::MatchItem;
  5. use std::collections::hash_set::HashSet;
  6. pub trait Query {
  7. type BI;
  8. fn create_block_index(&self, &PrimitiveBlock) -> Option<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. #[derive(Debug, Eq, PartialEq)]
  15. pub enum QueryArgs {
  16. ValuePattern(String),
  17. KeyValue(String, String),
  18. KeyValueRegex(String, String),
  19. Intersection(Vec<QueryArgs>),
  20. }
  21. #[derive(Debug)]
  22. pub enum QueryKind {
  23. ValuePattern(ValuePatternQuery),
  24. KeyValue(KeyValueQuery),
  25. KeyValueRegex(KeyValueRegexQuery),
  26. Intersection(Vec<QueryKind>),
  27. }
  28. impl QueryArgs {
  29. pub fn compile(self) -> Result<QueryKind, String> {
  30. match self {
  31. QueryArgs::ValuePattern(pattern) => {
  32. Ok(QueryKind::ValuePattern(ValuePatternQuery::new(&pattern)?))
  33. },
  34. QueryArgs::KeyValue(k, v) => {
  35. Ok(QueryKind::KeyValue(KeyValueQuery::new(k, v)))
  36. },
  37. QueryArgs::KeyValueRegex(k, v) => {
  38. Ok(QueryKind::KeyValueRegex(KeyValueRegexQuery::new(k, &v)?))
  39. },
  40. QueryArgs::Intersection(queries) => {
  41. let mut subqueries = Vec::with_capacity(queries.len());
  42. for q in queries {
  43. subqueries.push(q.compile()?);
  44. }
  45. Ok(QueryKind::Intersection(subqueries))
  46. },
  47. }
  48. }
  49. }
  50. #[derive(Debug)]
  51. pub struct ValuePatternQuery {
  52. re: Regex,
  53. }
  54. impl ValuePatternQuery {
  55. pub fn new(pattern: &str) -> Result<Self, String> {
  56. let re = Regex::new(&pattern)
  57. .map_err(|e| format!("{}", e))?;
  58. Ok(ValuePatternQuery { re })
  59. }
  60. }
  61. impl Query for ValuePatternQuery {
  62. type BI = ();
  63. fn create_block_index(&self, _block: &PrimitiveBlock) -> Option<()> {
  64. Some(())
  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. fn dense_node_matches(&self, _: &(), dnode: &DenseNode) -> bool {
  75. for (_key, val) in dnode.tags() {
  76. if self.re.is_match(val) {
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. fn way_matches(&self, _: &(), way: &Way) -> bool {
  83. for (_key, val) in way.tags() {
  84. if self.re.is_match(val) {
  85. return true;
  86. }
  87. }
  88. return false;
  89. }
  90. fn relation_matches(&self, _: &(), relation: &Relation) -> bool {
  91. for (_key, val) in relation.tags() {
  92. if self.re.is_match(val) {
  93. return true;
  94. }
  95. }
  96. return false;
  97. }
  98. }
  99. #[derive(Debug)]
  100. pub struct KeyValueQuery {
  101. key: String,
  102. value: String,
  103. }
  104. impl KeyValueQuery {
  105. pub fn new<S: Into<String>>(key: S, value: S) -> Self {
  106. KeyValueQuery {
  107. key: key.into(),
  108. value: value.into(),
  109. }
  110. }
  111. }
  112. impl Query for KeyValueQuery {
  113. type BI = (Vec<u32>, Vec<u32>);
  114. fn create_block_index(&self, block: &PrimitiveBlock) -> Option<(Vec<u32>, Vec<u32>)> {
  115. let mut key_indices = vec![];
  116. let mut value_indices = vec![];
  117. let key_bytes = self.key.as_bytes();
  118. let value_bytes = self.value.as_bytes();
  119. for (i, string) in block.raw_stringtable().iter().enumerate() {
  120. if string.as_slice() == key_bytes {
  121. key_indices.push(i as u32);
  122. }
  123. if string.as_slice() == value_bytes {
  124. value_indices.push(i as u32);
  125. }
  126. }
  127. if key_indices.is_empty() || value_indices.is_empty() {
  128. // No matches possible for this block
  129. return None;
  130. }
  131. key_indices.sort();
  132. value_indices.sort();
  133. Some((key_indices, value_indices))
  134. }
  135. fn node_matches(&self, bi: &Self::BI, node: &Node) -> bool {
  136. for (key, val) in node.raw_tags() {
  137. if bi.0.binary_search(&key).is_ok() && bi.1.binary_search(&val).is_ok() {
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. fn dense_node_matches(&self, bi: &Self::BI, dnode: &DenseNode) -> bool {
  144. for (key, val) in dnode.raw_tags() {
  145. if key >= 0 &&
  146. val >= 0 &&
  147. bi.0.binary_search(&(key as u32)).is_ok() &&
  148. bi.1.binary_search(&(val as u32)).is_ok()
  149. {
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. fn way_matches(&self, bi: &Self::BI, way: &Way) -> bool {
  156. for (key, val) in way.raw_tags() {
  157. if bi.0.binary_search(&key).is_ok() && bi.1.binary_search(&val).is_ok() {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. fn relation_matches(&self, bi: &Self::BI, relation: &Relation) -> bool {
  164. for (key, val) in relation.raw_tags() {
  165. if bi.0.binary_search(&key).is_ok() && bi.1.binary_search(&val).is_ok() {
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. }
  172. #[derive(Debug)]
  173. pub struct KeyValueRegexQuery {
  174. key: String,
  175. value_re: Regex,
  176. }
  177. impl KeyValueRegexQuery {
  178. pub fn new<S: Into<String>>(key: S, value_pattern: &str) -> Result<Self, String> {
  179. let value_re = Regex::new(value_pattern)
  180. .map_err(|e| format!("{}", e))?;
  181. Ok(KeyValueRegexQuery {
  182. key: key.into(),
  183. value_re,
  184. })
  185. }
  186. }
  187. impl Query for KeyValueRegexQuery {
  188. type BI = (Vec<u32>, Vec<u32>);
  189. fn create_block_index(&self, block: &PrimitiveBlock) -> Option<(Vec<u32>, Vec<u32>)> {
  190. let mut key_indices = vec![];
  191. let mut value_indices = vec![];
  192. let key_bytes = self.key.as_bytes();
  193. for (i, string) in block.raw_stringtable().iter().enumerate() {
  194. if string.as_slice() == key_bytes {
  195. key_indices.push(i as u32);
  196. }
  197. if let Ok(s) = ::std::str::from_utf8(string) {
  198. if self.value_re.is_match(s) {
  199. value_indices.push(i as u32);
  200. }
  201. }
  202. }
  203. if key_indices.is_empty() || value_indices.is_empty() {
  204. // No matches possible for this block
  205. return None;
  206. }
  207. key_indices.sort();
  208. value_indices.sort();
  209. Some((key_indices, value_indices))
  210. }
  211. fn node_matches(&self, bi: &Self::BI, node: &Node) -> bool {
  212. for (key, val) in node.raw_tags() {
  213. if bi.0.binary_search(&key).is_ok() && bi.1.binary_search(&val).is_ok() {
  214. return true;
  215. }
  216. }
  217. return false;
  218. }
  219. fn dense_node_matches(&self, bi: &Self::BI, dnode: &DenseNode) -> bool {
  220. for (key, val) in dnode.raw_tags() {
  221. if key >= 0 &&
  222. val >= 0 &&
  223. bi.0.binary_search(&(key as u32)).is_ok() &&
  224. bi.1.binary_search(&(val as u32)).is_ok()
  225. {
  226. return true;
  227. }
  228. }
  229. return false;
  230. }
  231. fn way_matches(&self, bi: &Self::BI, way: &Way) -> bool {
  232. for (key, val) in way.raw_tags() {
  233. if bi.0.binary_search(&key).is_ok() && bi.1.binary_search(&val).is_ok() {
  234. return true;
  235. }
  236. }
  237. return false;
  238. }
  239. fn relation_matches(&self, bi: &Self::BI, relation: &Relation) -> bool {
  240. for (key, val) in relation.raw_tags() {
  241. if bi.0.binary_search(&key).is_ok() && bi.1.binary_search(&val).is_ok() {
  242. return true;
  243. }
  244. }
  245. return false;
  246. }
  247. }
  248. pub fn find_query_matches<Q: Query>(
  249. block: &PrimitiveBlock,
  250. query: &Q,
  251. matches: &mut HashSet<MatchItem>,
  252. way_node_ids: &mut HashSet<i64>,
  253. ) {
  254. if let Some(block_index) = query.create_block_index(block) {
  255. for node in block.groups().flat_map(|g| g.nodes()) {
  256. if query.node_matches(&block_index, &node) {
  257. matches.insert(MatchItem::Node{
  258. id: node.id(),
  259. pos: LatLonDeg::new(node.lat(), node.lon()),
  260. });
  261. }
  262. }
  263. for node in block.groups().flat_map(|g| g.dense_nodes()) {
  264. if query.dense_node_matches(&block_index, &node) {
  265. matches.insert(MatchItem::Node{
  266. id: node.id,
  267. pos: LatLonDeg::new(node.lat(), node.lon()),
  268. });
  269. }
  270. }
  271. for way in block.groups().flat_map(|g| g.ways()) {
  272. if query.way_matches(&block_index, &way) {
  273. way_node_ids.extend(way.refs());
  274. matches.insert(MatchItem::Way{
  275. id: way.id(),
  276. nodes: way.refs().collect(),
  277. });
  278. }
  279. }
  280. }
  281. }