Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[core] Add support for $id key to filters #5708

Merged
merged 1 commit into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/parse/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static void Parse_EvaluateFilter(benchmark::State& state) {
const PropertyMap properties = { { "foo", std::string("bar") } };

while (state.KeepRunning()) {
filter(FeatureType::Unknown, [&] (const std::string& key) -> optional<Value> {
filter(FeatureType::Unknown, {}, [&] (const std::string& key) -> optional<Value> {
auto it = properties.find(key);
if (it == properties.end())
return {};
Expand Down
4 changes: 3 additions & 1 deletion include/mbgl/style/filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ class Filter : public FilterBase {
public:
using FilterBase::FilterBase;

bool operator()(const Feature&) const;

template <class PropertyAccessor>
bool operator()(FeatureType type, PropertyAccessor accessor) const;
bool operator()(FeatureType type, optional<FeatureIdentifier> id, PropertyAccessor accessor) const;
};

} // namespace style
Expand Down
30 changes: 25 additions & 5 deletions include/mbgl/style/filter_evaluator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ template <class PropertyAccessor>
class FilterEvaluator {
public:
const FeatureType featureType;
const optional<FeatureIdentifier> featureIdentifier;
const PropertyAccessor propertyAccessor;

bool operator()(const NullFilter&) const {
Expand Down Expand Up @@ -123,9 +124,19 @@ class FilterEvaluator {

private:
optional<Value> getValue(const std::string& key_) const {
return key_ == "$type"
? optional<Value>(uint64_t(featureType))
: propertyAccessor(key_);
if (key_ == "$type") {
return optional<Value>(uint64_t(featureType));
} else if (key_ == "$id") {
if (featureIdentifier) {
return FeatureIdentifier::visit(*featureIdentifier, [] (auto id) {
return Value(std::move(id));
});
} else {
return optional<Value>();
}
} else {
return propertyAccessor(key_);
}
}

template <class Op>
Expand Down Expand Up @@ -183,9 +194,18 @@ class FilterEvaluator {
}
};

inline bool Filter::operator()(const Feature& feature) const {
return operator()(apply_visitor(ToFeatureType(), feature.geometry), feature.id, [&] (const std::string& key) -> optional<Value> {
auto it = feature.properties.find(key);
if (it == feature.properties.end())
return {};
return it->second;
});
}

template <class PropertyAccessor>
bool Filter::operator()(FeatureType type, PropertyAccessor accessor) const {
return FilterBase::visit(*this, FilterEvaluator<PropertyAccessor> { type, accessor });
bool Filter::operator()(FeatureType type, optional<FeatureIdentifier> id, PropertyAccessor accessor) const {
return FilterBase::visit(*this, FilterEvaluator<PropertyAccessor> { type, id, accessor });
}

} // namespace style
Expand Down
6 changes: 1 addition & 5 deletions include/mbgl/util/feature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ using Value = mapbox::geometry::value;
using NullValue = mapbox::geometry::null_value_t;
using PropertyMap = mapbox::geometry::property_map;
using FeatureIdentifier = mapbox::geometry::identifier;
class Feature : public mapbox::geometry::feature<double> {
public:
Feature(geometry_type&& geometry_)
: mapbox::geometry::feature<double> { std::move(geometry_) } {}
};
using Feature = mapbox::geometry::feature<double>;

} // namespace mbgl
17 changes: 9 additions & 8 deletions platform/darwin/test/MGLFeatureTests.mm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import <Mapbox/Mapbox.h>
#import <XCTest/XCTest.h>

#import <mbgl/util/geometry.hpp>
#import "../../darwin/src/MGLFeature_Private.h"

@interface MGLFeatureTests : XCTestCase
Expand All @@ -12,16 +13,16 @@ @implementation MGLFeatureTests
- (void)testGeometryConversion {
std::vector<mbgl::Feature> features;

mapbox::geometry::point<double> point = { -90.066667, 29.95 };
features.emplace_back(point);
mbgl::Point<double> point = { -90.066667, 29.95 };
features.push_back(mbgl::Feature { point });

mapbox::geometry::line_string<double> lineString = {
mbgl::LineString<double> lineString = {
{ -84.516667, 39.1 },
{ -90.066667, 29.95 },
};
features.emplace_back(lineString);
features.push_back(mbgl::Feature { lineString });

mapbox::geometry::polygon<double> polygon = {
mbgl::Polygon<double> polygon = {
{
{ 1, 1 },
{ 4, 1 },
Expand All @@ -35,7 +36,7 @@ - (void)testGeometryConversion {
{ 2, 3 },
},
};
features.emplace_back(polygon);
features.push_back(mbgl::Feature { polygon });

NS_ARRAY_OF(MGLShape <MGLFeature> *) *shapes = MGLFeaturesFromMBGLFeatures(features);
XCTAssertEqual(shapes.count, 3, @"All features should be converted into shapes");
Expand Down Expand Up @@ -87,8 +88,8 @@ - (void)testGeometryConversion {
- (void)testPropertyConversion {
std::vector<mbgl::Feature> features;

mapbox::geometry::point<double> point = { -90.066667, 29.95 };
mbgl::Feature pointFeature(point);
mbgl::Point<double> point = { -90.066667, 29.95 };
mbgl::Feature pointFeature { point };
pointFeature.id = { UINT64_MAX };
pointFeature.properties["null"] = nullptr;
pointFeature.properties["bool"] = true;
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/annotation/shape_annotation_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void ShapeAnnotationImpl::updateTileData(const CanonicalTileID& tileID, Annotati
if (!shapeTiler) {
mapbox::geometry::feature_collection<double> features;
features.emplace_back(ShapeAnnotationGeometry::visit(geometry(), [] (auto&& geom) {
return Feature(std::move(geom));
return Feature { std::move(geom) };
}));
mapbox::geojsonvt::Options options;
options.maxZoom = util::clamp<uint8_t>(maxZoom, 0, 18);
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/symbol_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void SymbolBucket::parseFeatures(const GeometryTileLayer& layer, const Filter& f
const GLsizei featureCount = static_cast<GLsizei>(layer.featureCount());
for (GLsizei i = 0; i < featureCount; i++) {
auto feature = layer.getFeature(i);
if (!filter(feature->getType(), [&] (const auto& key) { return feature->getValue(key); }))
if (!filter(feature->getType(), feature->getID(), [&] (const auto& key) { return feature->getValue(key); }))
continue;

SymbolFeature ft;
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/style/bucket_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void BucketParameters::eachFilteredFeature(const Filter& filter,
auto name = layer.getName();
for (std::size_t i = 0; !cancelled() && i < layer.featureCount(); i++) {
auto feature = layer.getFeature(i);
if (!filter(feature->getType(), [&] (const auto& key) { return feature->getValue(key); }))
if (!filter(feature->getType(), feature->getID(), [&] (const auto& key) { return feature->getValue(key); }))
continue;
function(*feature, i, name);
}
Expand Down
136 changes: 74 additions & 62 deletions test/style/filter.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <mbgl/test/util.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/util/geometry.hpp>

#include <mbgl/style/filter.hpp>
#include <mbgl/style/filter_evaluator.hpp>
Expand All @@ -17,97 +19,107 @@ Filter parse(const char * expression) {
return *conversion::convert<Filter>(doc);
}

bool evaluate(const Filter& filter, const PropertyMap& properties, FeatureType type = FeatureType::Unknown) {
return filter(type, [&] (const std::string& key) -> optional<Value> {
auto it = properties.find(key);
if (it == properties.end())
return {};
return it->second;
});
Feature feature(const PropertyMap& properties, const Geometry<double>& geometry = Point<double>()) {
Feature result { geometry };
result.properties = properties;
return result;
}

TEST(Filter, EqualsString) {
Filter f = parse("[\"==\", \"foo\", \"bar\"]");
ASSERT_TRUE(evaluate(f, {{ "foo", std::string("bar") }}));
ASSERT_FALSE(evaluate(f, {{ "foo", std::string("baz") }}));
ASSERT_TRUE(f(feature({{ "foo", std::string("bar") }})));
ASSERT_FALSE(f(feature({{ "foo", std::string("baz") }})));
}

TEST(Filter, EqualsNumber) {
Filter f = parse("[\"==\", \"foo\", 0]");
ASSERT_TRUE(evaluate(f, {{ "foo", int64_t(0) }}));
ASSERT_TRUE(evaluate(f, {{ "foo", uint64_t(0) }}));
ASSERT_TRUE(evaluate(f, {{ "foo", double(0) }}));
ASSERT_FALSE(evaluate(f, {{ "foo", int64_t(1) }}));
ASSERT_FALSE(evaluate(f, {{ "foo", uint64_t(1) }}));
ASSERT_FALSE(evaluate(f, {{ "foo", double(1) }}));
ASSERT_FALSE(evaluate(f, {{ "foo", std::string("0") }}));
ASSERT_FALSE(evaluate(f, {{ "foo", false }}));
ASSERT_FALSE(evaluate(f, {{ "foo", true }}));
ASSERT_FALSE(evaluate(f, {{ "foo", nullptr }}));
ASSERT_FALSE(evaluate(f, {{}}));
ASSERT_TRUE(f(feature({{ "foo", int64_t(0) }})));
ASSERT_TRUE(f(feature({{ "foo", uint64_t(0) }})));
ASSERT_TRUE(f(feature({{ "foo", double(0) }})));
ASSERT_FALSE(f(feature({{ "foo", int64_t(1) }})));
ASSERT_FALSE(f(feature({{ "foo", uint64_t(1) }})));
ASSERT_FALSE(f(feature({{ "foo", double(1) }})));
ASSERT_FALSE(f(feature({{ "foo", std::string("0") }})));
ASSERT_FALSE(f(feature({{ "foo", false }})));
ASSERT_FALSE(f(feature({{ "foo", true }})));
ASSERT_FALSE(f(feature({{ "foo", nullptr }})));
ASSERT_FALSE(f(feature({{}})));
}

TEST(Filter, EqualsType) {
Filter f = parse("[\"==\", \"$type\", \"LineString\"]");
ASSERT_FALSE(evaluate(f, {{}}, FeatureType::Point));
ASSERT_TRUE(evaluate(f, {{}}, FeatureType::LineString));
ASSERT_FALSE(f(feature({{}}, Point<double>())));
ASSERT_TRUE(f(feature({{}}, LineString<double>())));
}

TEST(Filter, InType) {
Filter f = parse("[\"in\", \"$type\", \"LineString\", \"Polygon\"]");
ASSERT_FALSE(evaluate(f, {{}}, FeatureType::Point));
ASSERT_TRUE(evaluate(f, {{}}, FeatureType::LineString));
ASSERT_TRUE(evaluate(f, {{}}, FeatureType::Polygon));
ASSERT_FALSE(f(feature({{}}, Point<double>())));
ASSERT_TRUE(f(feature({{}}, LineString<double>())));
ASSERT_TRUE(f(feature({{}}, Polygon<double>())));
}

TEST(Filter, Any) {
ASSERT_FALSE(evaluate(parse("[\"any\"]"), {{}}));
ASSERT_TRUE(evaluate(parse("[\"any\", [\"==\", \"foo\", 1]]"),
{{ std::string("foo"), int64_t(1) }}));
ASSERT_FALSE(evaluate(parse("[\"any\", [\"==\", \"foo\", 0]]"),
{{ std::string("foo"), int64_t(1) }}));
ASSERT_TRUE(evaluate(parse("[\"any\", [\"==\", \"foo\", 0], [\"==\", \"foo\", 1]]"),
{{ std::string("foo"), int64_t(1) }}));
ASSERT_FALSE(parse("[\"any\"]")(feature({{}})));
ASSERT_TRUE(parse("[\"any\", [\"==\", \"foo\", 1]]")(
feature({{ std::string("foo"), int64_t(1) }})));
ASSERT_FALSE(parse("[\"any\", [\"==\", \"foo\", 0]]")(
feature({{ std::string("foo"), int64_t(1) }})));
ASSERT_TRUE(parse("[\"any\", [\"==\", \"foo\", 0], [\"==\", \"foo\", 1]]")(
feature({{ std::string("foo"), int64_t(1) }})));
}

TEST(Filter, All) {
ASSERT_TRUE(evaluate(parse("[\"all\"]"), {{}}));
ASSERT_TRUE(evaluate(parse("[\"all\", [\"==\", \"foo\", 1]]"),
{{ std::string("foo"), int64_t(1) }}));
ASSERT_FALSE(evaluate(parse("[\"all\", [\"==\", \"foo\", 0]]"),
{{ std::string("foo"), int64_t(1) }}));
ASSERT_FALSE(evaluate(parse("[\"all\", [\"==\", \"foo\", 0], [\"==\", \"foo\", 1]]"),
{{ std::string("foo"), int64_t(1) }}));
ASSERT_TRUE(parse("[\"all\"]")(feature({{}})));
ASSERT_TRUE(parse("[\"all\", [\"==\", \"foo\", 1]]")(
feature({{ std::string("foo"), int64_t(1) }})));
ASSERT_FALSE(parse("[\"all\", [\"==\", \"foo\", 0]]")(
feature({{ std::string("foo"), int64_t(1) }})));
ASSERT_FALSE(parse("[\"all\", [\"==\", \"foo\", 0], [\"==\", \"foo\", 1]]")(
feature({{ std::string("foo"), int64_t(1) }})));
}

TEST(Filter, None) {
ASSERT_TRUE(evaluate(parse("[\"none\"]"), {{}}));
ASSERT_FALSE(evaluate(parse("[\"none\", [\"==\", \"foo\", 1]]"),
{{ std::string("foo"), int64_t(1) }}));
ASSERT_TRUE(evaluate(parse("[\"none\", [\"==\", \"foo\", 0]]"),
{{ std::string("foo"), int64_t(1) }}));
ASSERT_FALSE(evaluate(parse("[\"none\", [\"==\", \"foo\", 0], [\"==\", \"foo\", 1]]"),
{{ std::string("foo"), int64_t(1) }}));
ASSERT_TRUE(parse("[\"none\"]")(feature({{}})));
ASSERT_FALSE(parse("[\"none\", [\"==\", \"foo\", 1]]")(
feature({{ std::string("foo"), int64_t(1) }})));
ASSERT_TRUE(parse("[\"none\", [\"==\", \"foo\", 0]]")(
feature({{ std::string("foo"), int64_t(1) }})));
ASSERT_FALSE(parse("[\"none\", [\"==\", \"foo\", 0], [\"==\", \"foo\", 1]]")(
feature({{ std::string("foo"), int64_t(1) }})));
}

TEST(Filter, Has) {
ASSERT_TRUE(evaluate(parse("[\"has\", \"foo\"]"),
{{ std::string("foo"), int64_t(1) }}));
ASSERT_TRUE(evaluate(parse("[\"has\", \"foo\"]"),
{{ std::string("foo"), int64_t(0) }}));
ASSERT_TRUE(evaluate(parse("[\"has\", \"foo\"]"),
{{ std::string("foo"), false }}));
ASSERT_FALSE(evaluate(parse("[\"has\", \"foo\"]"),
{{}}));
ASSERT_TRUE(parse("[\"has\", \"foo\"]")(
feature({{ std::string("foo"), int64_t(1) }})));
ASSERT_TRUE(parse("[\"has\", \"foo\"]")(
feature({{ std::string("foo"), int64_t(0) }})));
ASSERT_TRUE(parse("[\"has\", \"foo\"]")(
feature({{ std::string("foo"), false }})));
ASSERT_FALSE(parse("[\"has\", \"foo\"]")(
feature({{}})));
}

TEST(Filter, NotHas) {
ASSERT_FALSE(evaluate(parse("[\"!has\", \"foo\"]"),
{{ std::string("foo"), int64_t(1) }}));
ASSERT_FALSE(evaluate(parse("[\"!has\", \"foo\"]"),
{{ std::string("foo"), int64_t(0) }}));
ASSERT_FALSE(evaluate(parse("[\"!has\", \"foo\"]"),
{{ std::string("foo"), false }}));
ASSERT_TRUE(evaluate(parse("[\"!has\", \"foo\"]"),
{{}}));
ASSERT_FALSE(parse("[\"!has\", \"foo\"]")(
feature({{ std::string("foo"), int64_t(1) }})));
ASSERT_FALSE(parse("[\"!has\", \"foo\"]")(
feature({{ std::string("foo"), int64_t(0) }})));
ASSERT_FALSE(parse("[\"!has\", \"foo\"]")(
feature({{ std::string("foo"), false }})));
ASSERT_TRUE(parse("[\"!has\", \"foo\"]")(
feature({{}})));
}

TEST(Filter, ID) {
Feature feature1 { Point<double>() };
feature1.id = { 1234 };

ASSERT_TRUE(parse("[\"==\", \"$id\", 1234]")(feature1));
ASSERT_FALSE(parse("[\"==\", \"$id\", \"1234\"]")(feature1));

Feature feature2 { Point<double>() };
feature2.properties["id"] = { 1234 };

ASSERT_FALSE(parse("[\"==\", \"$id\", 1234]")(feature2));
}