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

MGLStyleLayer predicates (filters) #6049

Merged
merged 2 commits into from
Sep 2, 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: 2 additions & 0 deletions platform/darwin/src/MGLBaseStyleLayer_Private.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import "MGLBaseStyleLayer.h"

#import "NSPredicate+MGLAdditions.h"

@interface MGLBaseStyleLayer (MGLBaseStyleLayer_Private)

- (void)update;
Expand Down
8 changes: 8 additions & 0 deletions platform/darwin/src/MGLCircleStyleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ typedef NS_ENUM(NSUInteger, MGLCircleStyleLayerCirclePitchScale) {

@interface MGLCircleStyleLayer : MGLBaseStyleLayer <MGLStyleLayer>

/**
A predicate that corresponds to the layer's <a href='https://www.mapbox.com/mapbox-gl-style-spec/#types-filter'>filter</a>.

The predicate's left expression must be a string that identifies a feature
property, or one of the special keys.
*/
@property (nonatomic, nullable) NSPredicate *predicate;

#pragma mark - Accessing the Paint Attributes

/**
Expand Down
10 changes: 10 additions & 0 deletions platform/darwin/src/MGLCircleStyleLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ - (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdenti
return self;
}

- (void)setPredicate:(NSPredicate *)predicate
{
self.layer->setFilter(predicate.mgl_filter);
}

- (NSPredicate *)predicate
{
return [NSPredicate mgl_predicateWithFilter:self.layer->getFilter()];
}

#pragma mark - Accessing the Paint Attributes

- (void)setCircleRadius:(id <MGLStyleAttributeValue, MGLStyleAttributeValue_Private>)circleRadius {
Expand Down
51 changes: 4 additions & 47 deletions platform/darwin/src/MGLFeature.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#import "MGLPointAnnotation.h"
#import "MGLPolyline.h"
#import "MGLPolygon.h"
#import "MGLValueEvaluator.h"

#import "MGLMultiPoint_Private.h"

Expand Down Expand Up @@ -113,51 +114,7 @@ - (id)attributeForKey:(NSString *)key {

@end

/**
Recursively transforms a C++ type into the corresponding Foundation type.
*/
class PropertyValueEvaluator {
public:
id operator()(const mbgl::NullValue &) const {
return [NSNull null];
}

id operator()(const bool &value) const {
return value ? @YES : @NO;
}

id operator()(const uint64_t &value) const {
return @(value);
}

id operator()(const int64_t &value) const {
return @(value);
}

id operator()(const double &value) const {
return @(value);
}

id operator()(const std::string &value) const {
return @(value.c_str());
}

id operator()(const std::vector<mbgl::Value> &values) const {
NSMutableArray *objects = [NSMutableArray arrayWithCapacity:values.size()];
for (const auto &v : values) {
[objects addObject:mbgl::Value::visit(v, *this)];
}
return objects;
}

id operator()(const std::unordered_map<std::string, mbgl::Value> &items) const {
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:items.size()];
for (auto &item : items) {
attributes[@(item.first.c_str())] = mbgl::Value::visit(item.second, *this);
}
return attributes;
}
};


/**
Transforms an `mbgl::geometry::geometry` type into an instance of the
Expand Down Expand Up @@ -253,14 +210,14 @@ static CLLocationCoordinate2D toLocationCoordinate2D(const mbgl::Point<T> &point
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:feature.properties.size()];
for (auto &pair : feature.properties) {
auto &value = pair.second;
PropertyValueEvaluator evaluator;
ValueEvaluator evaluator;
attributes[@(pair.first.c_str())] = mbgl::Value::visit(value, evaluator);
}

GeometryEvaluator<double> evaluator;
MGLShape <MGLFeaturePrivate> *shape = mapbox::geometry::geometry<double>::visit(feature.geometry, evaluator);
if (feature.id) {
shape.identifier = mbgl::FeatureIdentifier::visit(*feature.id, PropertyValueEvaluator());
shape.identifier = mbgl::FeatureIdentifier::visit(*feature.id, ValueEvaluator());
}
shape.attributes = attributes;
[shapes addObject:shape];
Expand Down
8 changes: 8 additions & 0 deletions platform/darwin/src/MGLFillStyleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ typedef NS_ENUM(NSUInteger, MGLFillStyleLayerFillTranslateAnchor) {

@interface MGLFillStyleLayer : MGLBaseStyleLayer <MGLStyleLayer>

/**
A predicate that corresponds to the layer's <a href='https://www.mapbox.com/mapbox-gl-style-spec/#types-filter'>filter</a>.

The predicate's left expression must be a string that identifies a feature
property, or one of the special keys.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly tail work, but we need to document inline which NSExpression operator types we support, as well as the valid values of $type and $id. Linking to the style specification will suffice for now, but it’s suboptimal because the JavaScript syntax and terminology is very foreign to the NSPredicate-based interface we ended up exposing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I’m seeing now that this documentation was originally inline but was removed in response to #6049 (comment). I meant that we should describe the $type and $id keys but do so in an HTML definition list.

*/
@property (nonatomic, nullable) NSPredicate *predicate;

#pragma mark - Accessing the Paint Attributes

/**
Expand Down
10 changes: 10 additions & 0 deletions platform/darwin/src/MGLFillStyleLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ - (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdenti
return self;
}

- (void)setPredicate:(NSPredicate *)predicate
{
self.layer->setFilter(predicate.mgl_filter);
}

- (NSPredicate *)predicate
{
return [NSPredicate mgl_predicateWithFilter:self.layer->getFilter()];
}

#pragma mark - Accessing the Paint Attributes

- (void)setFillAntialias:(id <MGLStyleAttributeValue, MGLStyleAttributeValue_Private>)fillAntialias {
Expand Down
8 changes: 8 additions & 0 deletions platform/darwin/src/MGLLineStyleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ typedef NS_ENUM(NSUInteger, MGLLineStyleLayerLineTranslateAnchor) {

@interface MGLLineStyleLayer : MGLBaseStyleLayer <MGLStyleLayer>

/**
A predicate that corresponds to the layer's <a href='https://www.mapbox.com/mapbox-gl-style-spec/#types-filter'>filter</a>.

The predicate's left expression must be a string that identifies a feature
property, or one of the special keys.
*/
@property (nonatomic, nullable) NSPredicate *predicate;

#pragma mark - Accessing the Layout Attributes

/**
Expand Down
10 changes: 10 additions & 0 deletions platform/darwin/src/MGLLineStyleLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ - (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdenti
return self;
}

- (void)setPredicate:(NSPredicate *)predicate
{
self.layer->setFilter(predicate.mgl_filter);
}

- (NSPredicate *)predicate
{
return [NSPredicate mgl_predicateWithFilter:self.layer->getFilter()];
}

#pragma mark - Accessing the Layout Attributes

- (void)setLineCap:(id <MGLStyleAttributeValue, MGLStyleAttributeValue_Private>)lineCap {
Expand Down
12 changes: 12 additions & 0 deletions platform/darwin/src/MGLStyleLayer.h.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ typedef NS_ENUM(NSUInteger, MGL<%- camelize(type) %>StyleLayer<%- camelize(prope
<% } -%>
@interface MGL<%- camelize(type) %>StyleLayer : MGLBaseStyleLayer <MGLStyleLayer>

<% if (type !== 'background' && type !== 'raster') { -%>
/**
A predicate that corresponds to the layer's <a href='https://www.mapbox.com/mapbox-gl-style-spec/#types-filter'>filter</a>.

The predicate's left expression must be a string that identifies a feature
property, or one of the special keys.
*/
@property (nonatomic, nullable) NSPredicate *predicate;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs documentation.


<% } -%>
<% if (layoutProperties.length) { -%>
#pragma mark - Accessing the Layout Attributes

Expand All @@ -53,6 +63,7 @@ typedef NS_ENUM(NSUInteger, MGL<%- camelize(type) %>StyleLayer<%- camelize(prope

<% } -%>
<% } -%>
<% if (paintProperties.length) { -%>
#pragma mark - Accessing the Paint Attributes

<% for (const property of paintProperties) { -%>
Expand All @@ -68,6 +79,7 @@ typedef NS_ENUM(NSUInteger, MGL<%- camelize(type) %>StyleLayer<%- camelize(prope
*/
@property (nonatomic<% if (!property.required) { %>, null_resettable<% } %>) <%- propertyType(property, false, type) %> <%- camelizeWithLeadingLowercase(property.name) %>;

<% } -%>
<% } -%>
@end

Expand Down
12 changes: 12 additions & 0 deletions platform/darwin/src/MGLStyleLayer.mm.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@
return self;
}

<% if (type !== 'background' && type !== 'raster') { -%>
- (void)setPredicate:(NSPredicate *)predicate
{
self.layer->setFilter(predicate.mgl_filter);
}

- (NSPredicate *)predicate
{
return [NSPredicate mgl_predicateWithFilter:self.layer->getFilter()];
}

<% } -%>
<% if (layoutProperties.length) { -%>
#pragma mark - Accessing the Layout Attributes

Expand Down
8 changes: 8 additions & 0 deletions platform/darwin/src/MGLSymbolStyleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ typedef NS_ENUM(NSUInteger, MGLSymbolStyleLayerTextTranslateAnchor) {

@interface MGLSymbolStyleLayer : MGLBaseStyleLayer <MGLStyleLayer>

/**
A predicate that corresponds to the layer's <a href='https://www.mapbox.com/mapbox-gl-style-spec/#types-filter'>filter</a>.

The predicate's left expression must be a string that identifies a feature
property, or one of the special keys.
*/
@property (nonatomic, nullable) NSPredicate *predicate;

#pragma mark - Accessing the Layout Attributes

/**
Expand Down
10 changes: 10 additions & 0 deletions platform/darwin/src/MGLSymbolStyleLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ - (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdenti
return self;
}

- (void)setPredicate:(NSPredicate *)predicate
{
self.layer->setFilter(predicate.mgl_filter);
}

- (NSPredicate *)predicate
{
return [NSPredicate mgl_predicateWithFilter:self.layer->getFilter()];
}

#pragma mark - Accessing the Layout Attributes

- (void)setSymbolPlacement:(id <MGLStyleAttributeValue, MGLStyleAttributeValue_Private>)symbolPlacement {
Expand Down
49 changes: 49 additions & 0 deletions platform/darwin/src/MGLValueEvaluator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#import <Foundation/Foundation.h>

#import <mbgl/util/geometry.hpp>

/**
Recursively transforms a C++ type into the corresponding Foundation type.
*/
class ValueEvaluator {
public:
id operator()(const mbgl::NullValue &) const {
return [NSNull null];
}

id operator()(const bool &value) const {
return value ? @YES : @NO;
}

id operator()(const uint64_t &value) const {
return @(value);
}

id operator()(const int64_t &value) const {
return @(value);
}

id operator()(const double &value) const {
return @(value);
}

id operator()(const std::string &value) const {
return @(value.c_str());
}

id operator()(const std::vector<mbgl::Value> &values) const {
NSMutableArray *objects = [NSMutableArray arrayWithCapacity:values.size()];
for (const auto &v : values) {
[objects addObject:mbgl::Value::visit(v, *this)];
}
return objects;
}

id operator()(const std::unordered_map<std::string, mbgl::Value> &items) const {
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:items.size()];
for (auto &item : items) {
attributes[@(item.first.c_str())] = mbgl::Value::visit(item.second, *this);
}
return attributes;
}
};
7 changes: 7 additions & 0 deletions platform/darwin/src/NSComparisonPredicate+MGLAdditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import <Foundation/Foundation.h>

#include <mbgl/style/filter.hpp>

@interface NSComparisonPredicate (MGLAdditions)

@end
67 changes: 67 additions & 0 deletions platform/darwin/src/NSComparisonPredicate+MGLAdditions.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#import "NSComparisonPredicate+MGLAdditions.h"

#import "NSPredicate+MGLAdditions.h"
#import "NSExpression+MGLAdditions.h"

@implementation NSComparisonPredicate (MGLAdditions)

- (mbgl::style::Filter)mgl_filter
{
switch (self.predicateOperatorType) {
case NSEqualToPredicateOperatorType: {
auto filter = mbgl::style::EqualsFilter();
filter.key = self.leftExpression.keyPath.UTF8String;
filter.value = self.rightExpression.mgl_filterValue;
return filter;
}
case NSNotEqualToPredicateOperatorType: {
auto filter = mbgl::style::NotEqualsFilter();
filter.key = self.leftExpression.keyPath.UTF8String;
filter.value = self.rightExpression.mgl_filterValue;
return filter;
}
case NSGreaterThanPredicateOperatorType: {
auto filter = mbgl::style::GreaterThanFilter();
filter.key = self.leftExpression.keyPath.UTF8String;
filter.value = self.rightExpression.mgl_filterValue;
return filter;
}
case NSGreaterThanOrEqualToPredicateOperatorType: {
auto filter = mbgl::style::GreaterThanEqualsFilter();
filter.key = self.leftExpression.keyPath.UTF8String;
filter.value = self.rightExpression.mgl_filterValue;
return filter;
}
case NSLessThanPredicateOperatorType: {
auto filter = mbgl::style::LessThanFilter();
filter.key = self.leftExpression.keyPath.UTF8String;
filter.value = self.rightExpression.mgl_filterValue;
return filter;
}
case NSLessThanOrEqualToPredicateOperatorType: {
auto filter = mbgl::style::LessThanEqualsFilter();
filter.key = self.leftExpression.keyPath.UTF8String;
filter.value = self.rightExpression.mgl_filterValue;
return filter;
}
case NSInPredicateOperatorType: {
auto filter = mbgl::style::InFilter();
filter.key = self.leftExpression.keyPath.UTF8String;
filter.values = self.rightExpression.mgl_filterValues;
return filter;
}
case NSMatchesPredicateOperatorType:
case NSLikePredicateOperatorType:
case NSBeginsWithPredicateOperatorType:
case NSEndsWithPredicateOperatorType:
case NSCustomSelectorPredicateOperatorType:
case NSContainsPredicateOperatorType:
case NSBetweenPredicateOperatorType:
[NSException raise:@"Unsupported operator type"
format:@"NSPredicateOperatorType:%lu is not supported.", (unsigned long)self.predicateOperatorType];
}

return {};
}

@end
7 changes: 7 additions & 0 deletions platform/darwin/src/NSCompoundPredicate+MGLAdditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import <Foundation/Foundation.h>

#include <mbgl/style/filter.hpp>

@interface NSCompoundPredicate (MGLAdditions)

@end
Loading