Skip to content

Commit

Permalink
Merge pull request #583 from openmobilemaps/feature/to-boolean
Browse files Browse the repository at this point in the history
Add to-boolean expression for vector layer style jsons
  • Loading branch information
maurhofer-ubique authored Jan 26, 2024
2 parents b191d91 + f72f1a8 commit f8596f3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
13 changes: 10 additions & 3 deletions shared/public/Tiled2dMapVectorStyleParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Tiled2dMapVectorStyleParser {
static const std::string caseExpression;
static const std::string matchExpression;
static const std::string toStringExpression;
static const std::string toBooleanExpression;
static const std::string toNumberExpression;
static const std::string stopsExpression;
static const std::string stepExpression;
Expand Down Expand Up @@ -209,8 +210,14 @@ class Tiled2dMapVectorStyleParser {

// Example: ["to-number",["get","rank"]]
else if (isExpression(json[0], toNumberExpression)) {
auto toStringValue = std::make_shared<ToNumberValue>(parseValue(json[1]));
return toStringValue;
auto toNumberValue = std::make_shared<ToNumberValue>(parseValue(json[1]));
return toNumberValue;
}

// Example: ["to-boolean",["get","rank"]]
else if (isExpression(json[0], toBooleanExpression)) {
auto toBooleanValue = std::make_shared<ToBooleanValue>(parseValue(json[1]));
return toBooleanValue;
}

// Example: [ "interpolate", ["linear"], [ "zoom" ], 13, 0.3, 15, [ "match", [ "get", "class" ], "river", 0.1, 0.3 ] ]
Expand Down Expand Up @@ -331,7 +338,7 @@ class Tiled2dMapVectorStyleParser {
for (auto it = json.begin() + 1; it != json.end(); it += 1) {
values.push_back(parseValue(*it));
}
return std::make_shared<ToBoolValue>(values);
return std::make_shared<BooleanValue>(values);
}

// Example: [ "coalesce", ["get", "name_de"], ["get", "name_en"], ["get", "name"]]
Expand Down
68 changes: 63 additions & 5 deletions shared/public/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -1525,20 +1525,78 @@ class ToNumberValue: public Value {
}
};

class ToBoolValue: public Value {
class ToBooleanValue: public Value {
const std::shared_ptr<Value> value;

public:
ToBooleanValue(const std::shared_ptr<Value> value): value(value) {}

std::unique_ptr<Value> clone() override {
return std::make_unique<ToBooleanValue>(value->clone());
}

UsedKeysCollection getUsedKeys() const override {
return value->getUsedKeys();
}

ValueVariant evaluate(const EvaluationContext &context) const override {
return std::visit(overloaded {
[](const std::string &val){
return !val.empty();
},
[](double val){
return val != 0.0 && !isnan(val);
},
[](int64_t val){
return val != 0 && !isnan(val);
},
[](bool val){
return val;
},
[](const Color &val){
return true;
},
[](const std::vector<float> &val){
return true;
},
[](const std::vector<std::string> &val){
return true;
},
[](const std::vector<FormattedStringEntry> &val){
return true;
},
[](const std::monostate &val) {
return false;
}
}, value->evaluate(context));
};

bool isEqual(const std::shared_ptr<Value>& other) const override {
if (auto casted = std::dynamic_pointer_cast<ToBooleanValue>(other)) {
// Compare the value member
if (value && casted->value && !value->isEqual(casted->value)) {
return false;
}
return true; // All members are equal
}
return false; // Not the same type or nullptr
}
};

class BooleanValue: public Value {
const std::vector<std::shared_ptr<Value>> values;

public:
ToBoolValue(const std::shared_ptr<Value> value): values({ value }) {}
BooleanValue(const std::shared_ptr<Value> value): values({value }) {}

ToBoolValue(const std::vector<std::shared_ptr<Value>> values): values(values) {}
BooleanValue(const std::vector<std::shared_ptr<Value>> values): values(values) {}

std::unique_ptr<Value> clone() override {
std::vector<std::shared_ptr<Value>> clonedValues;
for (const auto &value: values) {
clonedValues.push_back(value->clone());
}
return std::make_unique<ToBoolValue>(clonedValues);
return std::make_unique<BooleanValue>(clonedValues);
}

UsedKeysCollection getUsedKeys() const override {
Expand All @@ -1561,7 +1619,7 @@ class ToBoolValue: public Value {
};

bool isEqual(const std::shared_ptr<Value>& other) const override {
if (auto casted = std::dynamic_pointer_cast<ToBoolValue>(other)) {
if (auto casted = std::dynamic_pointer_cast<BooleanValue>(other)) {
// Compare the value members
for (const auto &value: values) {
bool found = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const std::string Tiled2dMapVectorStyleParser::anyExpression = "any";
const std::string Tiled2dMapVectorStyleParser::caseExpression = "case";
const std::string Tiled2dMapVectorStyleParser::matchExpression = "match";
const std::string Tiled2dMapVectorStyleParser::toStringExpression = "to-string";
const std::string Tiled2dMapVectorStyleParser::toBooleanExpression = "to-boolean";
const std::string Tiled2dMapVectorStyleParser::toNumberExpression = "to-number";
const std::string Tiled2dMapVectorStyleParser::stopsExpression = "stops";
const std::string Tiled2dMapVectorStyleParser::stepExpression = "step";
Expand Down

0 comments on commit f8596f3

Please sign in to comment.