|
24 | 24 |
|
25 | 25 | namespace nucleus::vector_layer {
|
26 | 26 |
|
| 27 | +QJsonValue StyleExpressionBase::extract_literal(QJsonValue expression) |
| 28 | +{ |
| 29 | + assert(expression.isArray()); |
| 30 | + assert(expression.toArray()[0].isString()); |
| 31 | + assert(expression.toArray()[0].toString() == "literal"); |
| 32 | + assert(expression.toArray().size() == 2); |
| 33 | + |
| 34 | + return expression.toArray()[1]; |
| 35 | +} |
| 36 | + |
27 | 37 | std::unique_ptr<StyleExpressionBase> StyleExpressionBase::create_filter_expression(QJsonArray data)
|
28 | 38 | {
|
29 | 39 | if (data.isEmpty())
|
@@ -73,9 +83,28 @@ StyleExpression::StyleExpression(QJsonArray data)
|
73 | 83 | // values
|
74 | 84 | if (m_comparator == "in" || m_comparator == "!in") {
|
75 | 85 | // multiple values
|
76 |
| - for (qsizetype i = 2; i < data.size(); ++i) { |
77 |
| - m_values.push_back(data[i].toString().toStdString()); |
| 86 | + if (data.size() == 3 && data[2].isArray()) { |
| 87 | + // we most likely have a "literal" sub array |
| 88 | + const auto values = extract_literal(data[2]).toArray(); |
| 89 | + |
| 90 | + for (qsizetype i = 0; i < values.size(); ++i) { |
| 91 | + assert(values[i].isString()); |
| 92 | + m_values.push_back(values[i].toString().toStdString()); |
| 93 | + } |
| 94 | + |
| 95 | + } else { |
| 96 | + for (qsizetype i = 2; i < data.size(); ++i) { |
| 97 | + if (data[i].isDouble()) { |
| 98 | + m_values.push_back(std::to_string(data[i].toDouble())); |
| 99 | + } else if (data[i].isString()) { |
| 100 | + m_values.push_back(data[i].toString().toStdString()); |
| 101 | + } else { |
| 102 | + qDebug() << "invalid data: " << data[i]; |
| 103 | + assert(false); |
| 104 | + } |
| 105 | + } |
78 | 106 | }
|
| 107 | + |
79 | 108 | } else if (m_comparator == "has" || m_comparator == "!has") {
|
80 | 109 | // no values for has expressions -> we only check if key is present
|
81 | 110 | if (data.size() > 2) {
|
@@ -103,8 +132,8 @@ bool compare_equal(int64_t) { return true; }
|
103 | 132 |
|
104 | 133 | bool StyleExpression::matches(const mapbox::vector_tile::GeomType& type, const mapbox::feature::properties_type& properties)
|
105 | 134 | {
|
106 |
| - mapbox::feature::value value = extract_value(type, properties); |
107 | 135 | // qDebug() << "match: " << m_key;
|
| 136 | + mapbox::feature::value value = extract_value(type, properties); |
108 | 137 |
|
109 | 138 | if (m_comparator == "in" || m_comparator == "!in") {
|
110 | 139 | const auto string_value = std::visit(vector_tile::util::string_print_visitor, value).toStdString(); // we are only using the value to see if it is in a list -> string is sufficient
|
@@ -161,21 +190,29 @@ StyleExpressionCollection::StyleExpressionCollection(QJsonArray data)
|
161 | 190 | m_subFilters = std::vector<std::unique_ptr<StyleExpressionBase>>();
|
162 | 191 |
|
163 | 192 | for (qsizetype i = 1; i < data.size(); ++i) {
|
164 |
| - if (data[i].isArray()) |
| 193 | + if (data[i].isArray()) { |
| 194 | + if (data[i].toArray().isEmpty()) { |
| 195 | + qDebug() << "empty array"; |
| 196 | + assert(false); |
| 197 | + } |
165 | 198 | m_subFilters.push_back(create_filter_expression(data[i].toArray()));
|
166 |
| - else |
| 199 | + } else { |
167 | 200 | qDebug() << "not an array??";
|
| 201 | + assert(false); |
| 202 | + } |
168 | 203 | }
|
169 | 204 | }
|
170 | 205 |
|
171 | 206 | bool StyleExpressionCollection::matches(const mapbox::vector_tile::GeomType& type, const mapbox::feature::properties_type& properties)
|
172 | 207 | {
|
173 | 208 | if (m_negate) {
|
| 209 | + assert(m_subFilters.size() == 1); |
174 | 210 | // negate should only handle one subfilter -> get the match and negate it
|
175 | 211 | return !m_subFilters[0]->matches(type, properties);
|
176 | 212 | }
|
177 | 213 |
|
178 | 214 | for (size_t i = 0; i < m_subFilters.size(); ++i) {
|
| 215 | + assert(m_subFilters[i] != nullptr); |
179 | 216 | bool result = m_subFilters[i]->matches(type, properties);
|
180 | 217 | if (m_all && !result) {
|
181 | 218 | return false; // the current match was false but all had to be true
|
|
0 commit comments