Repository navigation
Expand file tree
/
Copy pathosmsearch.cpp
More file actions
301 lines (259 loc) · 9.61 KB
/
Copy pathosmsearch.cpp
File metadata and controls
301 lines (259 loc) · 9.61 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "osmsearch.h"
#include "osmmodel.h"
#include <QRegularExpression>
namespace {
class TagQuery : public OsmSearchQuery
{
public:
QString key, value;
bool hasValue;
bool negated;
TagQuery(const QString &k, const QString &v, bool hasVal, bool neg)
: key(k), value(v), hasValue(hasVal), negated(neg) {}
bool matchWay(const OsmModel *model, int wayIndex) const override {
const OsmWay &way = model->wayAt(wayIndex);
bool result = false;
if (!way.hasTags()) result = !hasValue;
else {
int idx = way.tagHead();
while (idx >= 0) {
const OsmTag &tag = model->tagAt(idx);
QString k = model->keyName(tag.keyIndex);
if (k == key) {
if (!hasValue) result = true;
else if (value == "*" || tag.value == value) result = true;
break;
}
idx = tag.nextIndex;
}
}
return negated ? !result : result;
}
bool matchNode(const OsmModel *model, int nodeIndex) const override {
const OsmNode &node = model->nodeAt(nodeIndex);
bool result = false;
if (!node.hasTags()) result = !hasValue;
else {
int idx = node.tagHead();
while (idx >= 0) {
const OsmTag &tag = model->tagAt(idx);
QString k = model->keyName(tag.keyIndex);
if (k == key) {
if (!hasValue) result = true;
else if (value == "*" || tag.value == value) result = true;
break;
}
idx = tag.nextIndex;
}
}
return negated ? !result : result;
}
};
class TypeQuery : public OsmSearchQuery
{
public:
enum ElementType { NODE, WAY, AREA, LINE, ANY };
ElementType type;
TypeQuery(ElementType t) : type(t) {}
bool matchWay(const OsmModel *model, int wayIndex) const override {
const OsmWay &way = model->wayAt(wayIndex);
if (type == ANY || type == WAY) return true;
if (type == AREA) return way.isClosed();
if (type == LINE) return !way.isClosed();
return false;
}
bool matchNode(const OsmModel *model, int nodeIndex) const override {
Q_UNUSED(model);
Q_UNUSED(nodeIndex);
return type == ANY || type == NODE;
}
};
class NameQuery : public OsmSearchQuery
{
public:
QString pattern;
bool caseSensitive;
NameQuery(const QString &p, bool cs = false) : pattern(p), caseSensitive(cs) {}
bool matchName(const OsmModel *model, int tagHead) const {
if (tagHead < 0) return false;
int idx = tagHead;
while (idx >= 0) {
const OsmTag &tag = model->tagAt(idx);
QString k = model->keyName(tag.keyIndex);
if (k == "name" || k.startsWith("name:")) {
if (caseSensitive) {
if (tag.value.contains(pattern)) return true;
} else {
if (tag.value.contains(pattern, Qt::CaseInsensitive)) return true;
}
}
idx = tag.nextIndex;
}
return false;
}
bool matchWay(const OsmModel *model, int wayIndex) const override {
return matchName(model, model->wayAt(wayIndex).tagHead());
}
bool matchNode(const OsmModel *model, int nodeIndex) const override {
return matchName(model, model->nodeAt(nodeIndex).tagHead());
}
};
class AndQuery : public OsmSearchQuery
{
public:
std::unique_ptr<OsmSearchQuery> left, right;
AndQuery(std::unique_ptr<OsmSearchQuery> l, std::unique_ptr<OsmSearchQuery> r)
: left(std::move(l)), right(std::move(r)) {}
bool matchWay(const OsmModel *model, int wayIndex) const override {
return left->matchWay(model, wayIndex) && right->matchWay(model, wayIndex);
}
bool matchNode(const OsmModel *model, int nodeIndex) const override {
return left->matchNode(model, nodeIndex) && right->matchNode(model, nodeIndex);
}
};
class OrQuery : public OsmSearchQuery
{
public:
std::unique_ptr<OsmSearchQuery> left, right;
OrQuery(std::unique_ptr<OsmSearchQuery> l, std::unique_ptr<OsmSearchQuery> r)
: left(std::move(l)), right(std::move(r)) {}
bool matchWay(const OsmModel *model, int wayIndex) const override {
return left->matchWay(model, wayIndex) || right->matchWay(model, wayIndex);
}
bool matchNode(const OsmModel *model, int nodeIndex) const override {
return left->matchNode(model, nodeIndex) || right->matchNode(model, nodeIndex);
}
};
class NotQuery : public OsmSearchQuery
{
public:
std::unique_ptr<OsmSearchQuery> child;
NotQuery(std::unique_ptr<OsmSearchQuery> c) : child(std::move(c)) {}
bool matchWay(const OsmModel *model, int wayIndex) const override {
return !child->matchWay(model, wayIndex);
}
bool matchNode(const OsmModel *model, int nodeIndex) const override {
return !child->matchNode(model, nodeIndex);
}
};
class AllQuery : public OsmSearchQuery
{
public:
bool matchWay(const OsmModel *model, int wayIndex) const override { Q_UNUSED(model); Q_UNUSED(wayIndex); return true; }
bool matchNode(const OsmModel *model, int nodeIndex) const override { Q_UNUSED(model); Q_UNUSED(nodeIndex); return true; }
};
}
OsmSearchParser::OsmSearchParser() {}
void OsmSearchParser::skipWhitespace(const QString &s, int &pos)
{
while (pos < s.length() && s[pos].isSpace()) pos++;
}
std::unique_ptr<OsmSearchQuery> OsmSearchParser::parseOr(const QString &s, int &pos)
{
auto left = parseAnd(s, pos);
skipWhitespace(s, pos);
while (pos < s.length() && (s[pos] == '|' || (s[pos] == 'O' && pos + 1 < s.length() && s[pos+1] == 'R' && s[pos+1].isSpace()))) {
if (s[pos] == '|') pos++;
else pos += 2;
skipWhitespace(s, pos);
auto right = parseAnd(s, pos);
left = std::make_unique<OrQuery>(std::move(left), std::move(right));
skipWhitespace(s, pos);
}
return left;
}
std::unique_ptr<OsmSearchQuery> OsmSearchParser::parseAnd(const QString &s, int &pos)
{
auto left = parsePrimary(s, pos);
skipWhitespace(s, pos);
while (pos < s.length() && s[pos] != '|' && s[pos] != ')' && s[pos] != '(') {
if (s[pos] == '&') { pos++; skipWhitespace(s, pos); }
else if (s.mid(pos, 3) == "AND" && (pos + 3 >= s.length() || s[pos+3].isSpace())) { pos += 3; skipWhitespace(s, pos); }
auto right = parsePrimary(s, pos);
if (!right) break;
left = std::make_unique<AndQuery>(std::move(left), std::move(right));
skipWhitespace(s, pos);
}
return left;
}
std::unique_ptr<OsmSearchQuery> OsmSearchParser::parsePrimary(const QString &s, int &pos)
{
skipWhitespace(s, pos);
if (pos >= s.length()) return std::make_unique<AllQuery>();
if (s[pos] == '(') {
pos++;
auto q = parseOr(s, pos);
skipWhitespace(s, pos);
if (pos < s.length() && s[pos] == ')') pos++;
return q;
}
if (s[pos] == '-' || s[pos] == '!') {
pos++;
auto q = parsePrimary(s, pos);
return std::make_unique<NotQuery>(std::move(q));
}
static QRegularExpression re("^(type|user|changeset|version|id|name):([^\\s&|()]+)");
auto m = re.match(s.mid(pos));
if (m.hasMatch()) {
QString type = m.captured(1);
QString val = m.captured(2);
pos += m.capturedLength();
if (type == "type") {
TypeQuery::ElementType t = TypeQuery::ANY;
if (val == "node") t = TypeQuery::NODE;
else if (val == "way") t = TypeQuery::WAY;
else if (val == "area") t = TypeQuery::AREA;
else if (val == "line") t = TypeQuery::LINE;
return std::make_unique<TypeQuery>(t);
}
if (type == "name") {
return std::make_unique<NameQuery>(val, false);
}
}
static QRegularExpression tagRe("^\"?([^=!~<>\\s\"]+)\"?\\s*(!=|=|~=|~|!~)?\\s*(\"[^\"]*\"|[^\\s&|()]+)?");
auto tm = tagRe.match(s.mid(pos));
if (tm.hasMatch()) {
QString key = tm.captured(1);
QString op = tm.captured(2);
QString val = tm.captured(3);
pos += tm.capturedLength();
bool negated = false;
if (op == "!=" || op == "!~") negated = true;
if (val.startsWith('"') && val.endsWith('"')) val = val.mid(1, val.length() - 2);
bool hasVal = !op.isEmpty() && !val.isEmpty();
return std::make_unique<TagQuery>(key, val.isEmpty() ? "*" : val, hasVal, negated);
}
QString word;
while (pos < s.length() && !s[pos].isSpace() && s[pos] != '&' && s[pos] != '|' && s[pos] != '(' && s[pos] != ')') {
word += s[pos++];
}
if (!word.isEmpty()) {
return std::make_unique<NameQuery>(word, false);
}
return std::make_unique<AllQuery>();
}
std::unique_ptr<OsmSearchQuery> OsmSearchParser::parse(const QString &query)
{
int pos = 0;
return parseOr(query, pos);
}
QVector<SearchResult> OsmSearchParser::search(const OsmModel *model, const QString &query, int maxResults)
{
QVector<SearchResult> results;
if (!model || query.trimmed().isEmpty()) return results;
auto q = parse(query);
if (!q) return results;
for (int i = 0; i < model->wayCount() && results.size() < maxResults; i++) {
if (q->matchWay(model, i)) {
const OsmWay &way = model->wayAt(i);
SearchResult r;
r.type = SearchResult::WAY;
r.index = i;
r.id = way.id();
r.name = model->getTagValue(way, "name");
r.coord = way.bbox().center();
results.append(r);
}
}
return results;
}