Expression filters (#11429)

* [android] - add expression filter integration
This commit is contained in:
Tobrun
2018-03-19 15:11:17 +01:00
committed by GitHub
parent c5075e33ec
commit f011035017
36 changed files with 436 additions and 426 deletions

View File

@@ -40,7 +40,7 @@ import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.style.layers.Filter;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.light.Light;
import com.mapbox.mapboxsdk.style.sources.Source;
@@ -2079,13 +2079,13 @@ public final class MapboxMap {
* Queries the map for rendered features
*
* @param coordinates the point to query
* @param filter filters the returned features
* @param filter filters the returned features with an expression
* @param layerIds optionally - only query these layers
* @return the list of feature
*/
@NonNull
public List<Feature> queryRenderedFeatures(@NonNull PointF coordinates,
@Nullable Filter.Statement filter,
@Nullable Expression filter,
@Nullable String... layerIds) {
return nativeMapView.queryRenderedFeatures(coordinates, layerIds, filter);
}
@@ -2107,13 +2107,13 @@ public final class MapboxMap {
* Queries the map for rendered features
*
* @param coordinates the box to query
* @param filter filters the returned features
* @param filter filters the returned features with an expression
* @param layerIds optionally - only query these layers
* @return the list of feature
*/
@NonNull
public List<Feature> queryRenderedFeatures(@NonNull RectF coordinates,
@Nullable Filter.Statement filter,
@Nullable Expression filter,
@Nullable String... layerIds) {
return nativeMapView.queryRenderedFeatures(coordinates, layerIds, filter);
}

View File

@@ -24,8 +24,8 @@ import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.geometry.ProjectedMeters;
import com.mapbox.mapboxsdk.maps.renderer.MapRenderer;
import com.mapbox.mapboxsdk.storage.FileSource;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.CannotAddLayerException;
import com.mapbox.mapboxsdk.style.layers.Filter;
import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.light.Light;
import com.mapbox.mapboxsdk.style.sources.CannotAddSourceException;
@@ -799,7 +799,7 @@ final class NativeMapView {
@NonNull
public List<Feature> queryRenderedFeatures(@NonNull PointF coordinates,
@Nullable String[] layerIds,
@Nullable Filter.Statement filter) {
@Nullable Expression filter) {
if (isDestroyedOn("queryRenderedFeatures")) {
return new ArrayList<>();
}
@@ -811,7 +811,7 @@ final class NativeMapView {
@NonNull
public List<Feature> queryRenderedFeatures(@NonNull RectF coordinates,
@Nullable String[] layerIds,
@Nullable Filter.Statement filter) {
@Nullable Expression filter) {
if (isDestroyedOn("queryRenderedFeatures")) {
return new ArrayList<>();
}

View File

@@ -4,10 +4,13 @@ package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import static com.mapbox.mapboxsdk.utils.ColorUtils.rgbaToColor;
import com.google.gson.JsonArray;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
/**

View File

@@ -4,10 +4,13 @@ package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import static com.mapbox.mapboxsdk.utils.ColorUtils.rgbaToColor;
import com.google.gson.JsonArray;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
/**
@@ -69,25 +72,40 @@ public class CircleLayer extends Layer {
}
/**
* Set a single filter.
* Set a single expression filter.
*
* @param filter the filter to set
* @param filter the expression filter to set
*/
public void setFilter(Filter.Statement filter) {
public void setFilter(Expression filter) {
nativeSetFilter(filter.toArray());
}
/**
* Set a single filter.
* Set a single expression filter.
*
* @param filter the filter to set
* @param filter the expression filter to set
* @return This
*/
public CircleLayer withFilter(Filter.Statement filter) {
public CircleLayer withFilter(Expression filter) {
setFilter(filter);
return this;
}
/**
* Get a single expression filter.
*
* @return the expression filter to get
*/
@Nullable
public Expression getFilter() {
Expression expression = null;
JsonArray array = nativeGetFilter();
if (array != null) {
expression = Expression.Converter.convert(array);
}
return expression;
}
/**
* Set a property or properties.
*

View File

@@ -4,10 +4,13 @@ package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import static com.mapbox.mapboxsdk.utils.ColorUtils.rgbaToColor;
import com.google.gson.JsonArray;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
/**
@@ -69,25 +72,40 @@ public class FillExtrusionLayer extends Layer {
}
/**
* Set a single filter.
* Set a single expression filter.
*
* @param filter the filter to set
* @param filter the expression filter to set
*/
public void setFilter(Filter.Statement filter) {
public void setFilter(Expression filter) {
nativeSetFilter(filter.toArray());
}
/**
* Set a single filter.
* Set a single expression filter.
*
* @param filter the filter to set
* @param filter the expression filter to set
* @return This
*/
public FillExtrusionLayer withFilter(Filter.Statement filter) {
public FillExtrusionLayer withFilter(Expression filter) {
setFilter(filter);
return this;
}
/**
* Get a single expression filter.
*
* @return the expression filter to get
*/
@Nullable
public Expression getFilter() {
Expression expression = null;
JsonArray array = nativeGetFilter();
if (array != null) {
expression = Expression.Converter.convert(array);
}
return expression;
}
/**
* Set a property or properties.
*

View File

@@ -4,10 +4,13 @@ package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import static com.mapbox.mapboxsdk.utils.ColorUtils.rgbaToColor;
import com.google.gson.JsonArray;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
/**
@@ -69,25 +72,40 @@ public class FillLayer extends Layer {
}
/**
* Set a single filter.
* Set a single expression filter.
*
* @param filter the filter to set
* @param filter the expression filter to set
*/
public void setFilter(Filter.Statement filter) {
public void setFilter(Expression filter) {
nativeSetFilter(filter.toArray());
}
/**
* Set a single filter.
* Set a single expression filter.
*
* @param filter the filter to set
* @param filter the expression filter to set
* @return This
*/
public FillLayer withFilter(Filter.Statement filter) {
public FillLayer withFilter(Expression filter) {
setFilter(filter);
return this;
}
/**
* Get a single expression filter.
*
* @return the expression filter to get
*/
@Nullable
public Expression getFilter() {
Expression expression = null;
JsonArray array = nativeGetFilter();
if (array != null) {
expression = Expression.Converter.convert(array);
}
return expression;
}
/**
* Set a property or properties.
*

View File

@@ -1,230 +0,0 @@
package com.mapbox.mapboxsdk.style.layers;
import java.util.ArrayList;
import java.util.Collections;
/**
* Utility to build filter expressions more easily.
*
* @see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#types-filter">The online documentation</a>
*/
public class Filter {
/**
* Base Filter statement. Subclassed to provide concrete statements.
*/
public abstract static class Statement {
protected final String operator;
public Statement(String operator) {
this.operator = operator;
}
/**
* Generate a raw array representation of the filter
*
* @return the filter represented as an array
*/
public abstract Object[] toArray();
}
/**
* Represents a {@link Filter} statement. Can be unary (eg `has()`, etc) or take any number of values.
*/
private static class SimpleStatement extends Statement {
private final String key;
private final Object[] values;
/**
* @param operator the operator (eg `=`, etc)
* @param key the property key
* @param values the values to operate on, if any
*/
SimpleStatement(String operator, String key, Object... values) {
super(operator);
this.key = key;
this.values = values;
}
/**
* {@inheritDoc}
*/
@Override
public Object[] toArray() {
ArrayList<Object> array = new ArrayList<>(2 + values.length);
array.add(operator);
array.add(key);
Collections.addAll(array, values);
return array.toArray();
}
}
/**
* Represents a collection of {@link Statement}s with an operator that describes their relationship
*/
private static class CompoundStatement extends Statement {
private final Statement[] statements;
/**
* @param operator the relationship operator
* @param statements the statements to compound
*/
CompoundStatement(String operator, Statement... statements) {
super(operator);
this.statements = statements;
}
/**
* {@inheritDoc}
*/
@Override
public Object[] toArray() {
ArrayList<Object> array = new ArrayList<>(1 + statements.length);
array.add(operator);
for (Statement statement : statements) {
array.add(statement.toArray());
}
return array.toArray();
}
}
/**
* Groups a collection of statements in an 'all' relationship
*
* @param statements the collection of statements
* @return the statements compounded
*/
public static Statement all(Statement... statements) {
return new CompoundStatement("all", statements);
}
/**
* Groups a collection of statements in an 'any' relationship
*
* @param statements the collection of statements
* @return the statements compounded
*/
public static Statement any(Statement... statements) {
return new CompoundStatement("any", statements);
}
/**
* Groups a collection of statements in an 'none' relationship
*
* @param statements the collection of statements
* @return the statements compounded
*/
public static Statement none(Statement... statements) {
return new CompoundStatement("none", statements);
}
/**
* Check the property's existence
*
* @param key the property key
* @return the statement
*/
public static Statement has(String key) {
return new SimpleStatement("has", key);
}
/**
* Check the property's existence, negated
*
* @param key the property key
* @return the statement
*/
public static Statement notHas(String key) {
return new SimpleStatement("!has", key);
}
/**
* Check the property equals the given value
*
* @param key the property key
* @param value the value to check against
* @return the statement
*/
public static Statement eq(String key, Object value) {
return new SimpleStatement("==", key, value);
}
/**
* Check the property does not equals the given value
*
* @param key the property key
* @param value the value to check against
* @return the statement
*/
public static Statement neq(String key, Object value) {
return new SimpleStatement("!=", key, value);
}
/**
* Check the property exceeds the given value
*
* @param key the property key
* @param value the value to check against
* @return the statement
*/
public static Statement gt(String key, Object value) {
return new SimpleStatement(">", key, value);
}
/**
* Check the property exceeds or equals the given value
*
* @param key the property key
* @param value the value to check against
* @return the statement
*/
public static Statement gte(String key, Object value) {
return new SimpleStatement(">=", key, value);
}
/**
* Check the property does not exceeds the given value
*
* @param key the property key
* @param value the value to check against
* @return the statement
*/
public static Statement lt(String key, Object value) {
return new SimpleStatement("<", key, value);
}
/**
* Check the property equals or does not exceeds the given value
*
* @param key the property key
* @param value the value to check against
* @return the statement
*/
public static Statement lte(String key, Object value) {
return new SimpleStatement("<=", key, value);
}
/**
* Check the property is within the given set
*
* @param key the property key
* @param values the set of values to check against
* @return the statement
*/
public static Statement in(String key, Object... values) {
return new SimpleStatement("in", key, values);
}
/**
* Check the property is not within the given set
*
* @param key the property key
* @param values the set of values to check against
* @return the statement
*/
public static Statement notIn(String key, Object... values) {
return new SimpleStatement("!in", key, values);
}
}

View File

@@ -4,10 +4,13 @@ package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import static com.mapbox.mapboxsdk.utils.ColorUtils.rgbaToColor;
import com.google.gson.JsonArray;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
/**
@@ -69,25 +72,40 @@ public class HeatmapLayer extends Layer {
}
/**
* Set a single filter.
* Set a single expression filter.
*
* @param filter the filter to set
* @param filter the expression filter to set
*/
public void setFilter(Filter.Statement filter) {
public void setFilter(Expression filter) {
nativeSetFilter(filter.toArray());
}
/**
* Set a single filter.
* Set a single expression filter.
*
* @param filter the filter to set
* @param filter the expression filter to set
* @return This
*/
public HeatmapLayer withFilter(Filter.Statement filter) {
public HeatmapLayer withFilter(Expression filter) {
setFilter(filter);
return this;
}
/**
* Get a single expression filter.
*
* @return the expression filter to get
*/
@Nullable
public Expression getFilter() {
Expression expression = null;
JsonArray array = nativeGetFilter();
if (array != null) {
expression = Expression.Converter.convert(array);
}
return expression;
}
/**
* Set a property or properties.
*

View File

@@ -4,10 +4,13 @@ package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import static com.mapbox.mapboxsdk.utils.ColorUtils.rgbaToColor;
import com.google.gson.JsonArray;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
/**

View File

@@ -2,6 +2,7 @@ package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.NonNull;
import com.google.gson.JsonArray;
import com.mapbox.mapboxsdk.style.expressions.Expression;
/**
@@ -71,6 +72,8 @@ public abstract class Layer {
protected native void nativeSetFilter(Object[] filter);
protected native JsonArray nativeGetFilter();
protected native void nativeSetSourceLayer(String sourceLayer);
protected native String nativeGetSourceLayer();

View File

@@ -4,10 +4,13 @@ package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import static com.mapbox.mapboxsdk.utils.ColorUtils.rgbaToColor;
import com.google.gson.JsonArray;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
/**
@@ -69,25 +72,40 @@ public class LineLayer extends Layer {
}
/**
* Set a single filter.
* Set a single expression filter.
*
* @param filter the filter to set
* @param filter the expression filter to set
*/
public void setFilter(Filter.Statement filter) {
public void setFilter(Expression filter) {
nativeSetFilter(filter.toArray());
}
/**
* Set a single filter.
* Set a single expression filter.
*
* @param filter the filter to set
* @param filter the expression filter to set
* @return This
*/
public LineLayer withFilter(Filter.Statement filter) {
public LineLayer withFilter(Expression filter) {
setFilter(filter);
return this;
}
/**
* Get a single expression filter.
*
* @return the expression filter to get
*/
@Nullable
public Expression getFilter() {
Expression expression = null;
JsonArray array = nativeGetFilter();
if (array != null) {
expression = Expression.Converter.convert(array);
}
return expression;
}
/**
* Set a property or properties.
*

View File

@@ -4,10 +4,13 @@ package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import static com.mapbox.mapboxsdk.utils.ColorUtils.rgbaToColor;
import com.google.gson.JsonArray;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
/**

View File

@@ -4,10 +4,13 @@ package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import static com.mapbox.mapboxsdk.utils.ColorUtils.rgbaToColor;
import com.google.gson.JsonArray;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
/**
@@ -69,25 +72,40 @@ public class SymbolLayer extends Layer {
}
/**
* Set a single filter.
* Set a single expression filter.
*
* @param filter the filter to set
* @param filter the expression filter to set
*/
public void setFilter(Filter.Statement filter) {
public void setFilter(Expression filter) {
nativeSetFilter(filter.toArray());
}
/**
* Set a single filter.
* Set a single expression filter.
*
* @param filter the filter to set
* @param filter the expression filter to set
* @return This
*/
public SymbolLayer withFilter(Filter.Statement filter) {
public SymbolLayer withFilter(Expression filter) {
setFilter(filter);
return this;
}
/**
* Get a single expression filter.
*
* @return the expression filter to get
*/
@Nullable
public Expression getFilter() {
Expression expression = null;
JsonArray array = nativeGetFilter();
if (array != null) {
expression = Expression.Converter.convert(array);
}
return expression;
}
/**
* Set a property or properties.
*

View File

@@ -9,10 +9,13 @@ package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import static com.mapbox.mapboxsdk.utils.ColorUtils.rgbaToColor;
import com.google.gson.JsonArray;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
/**
@@ -89,25 +92,40 @@ public class <%- camelize(type) %>Layer extends Layer {
}
/**
* Set a single filter.
* Set a single expression filter.
*
* @param filter the filter to set
* @param filter the expression filter to set
*/
public void setFilter(Filter.Statement filter) {
public void setFilter(Expression filter) {
nativeSetFilter(filter.toArray());
}
/**
* Set a single filter.
* Set a single expression filter.
*
* @param filter the filter to set
* @param filter the expression filter to set
* @return This
*/
public <%- camelize(type) %>Layer withFilter(Filter.Statement filter) {
public <%- camelize(type) %>Layer withFilter(Expression filter) {
setFilter(filter);
return this;
}
/**
* Get a single expression filter.
*
* @return the expression filter to get
*/
@Nullable
public Expression getFilter() {
Expression expression = null;
JsonArray array = nativeGetFilter();
if (array != null) {
expression = Expression.Converter.convert(array);
}
return expression;
}
<% } -%>
/**
* Set a property or properties.

View File

@@ -8,7 +8,7 @@ import android.support.annotation.WorkerThread;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.style.layers.Filter;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
@@ -94,11 +94,11 @@ public class CustomGeometrySource extends Source {
/**
* Queries the source for features.
*
* @param filter an optional filter statement to filter the returned Features
* @param filter an optional filter expression to filter the returned Features
* @return the features
*/
@NonNull
public List<Feature> querySourceFeatures(@Nullable Filter.Statement filter) {
public List<Feature> querySourceFeatures(@Nullable Expression filter) {
Feature[] features = querySourceFeatures(filter != null ? filter.toArray() : null);
return features != null ? Arrays.asList(features) : new ArrayList<Feature>();
}

View File

@@ -7,7 +7,7 @@ import android.support.annotation.UiThread;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.Geometry;
import com.mapbox.mapboxsdk.style.layers.Filter;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import java.net.URL;
import java.util.ArrayList;
@@ -238,11 +238,11 @@ public class GeoJsonSource extends Source {
/**
* Queries the source for features.
*
* @param filter an optional filter statement to filter the returned Features
* @param filter an optional filter expression to filter the returned Features
* @return the features
*/
@NonNull
public List<Feature> querySourceFeatures(@Nullable Filter.Statement filter) {
public List<Feature> querySourceFeatures(@Nullable Expression filter) {
Feature[] features = querySourceFeatures(filter != null ? filter.toArray() : null);
return features != null ? Arrays.asList(features) : new ArrayList<Feature>();
}

View File

@@ -6,7 +6,7 @@ import android.support.annotation.Size;
import android.support.annotation.UiThread;
import com.mapbox.geojson.Feature;
import com.mapbox.mapboxsdk.style.layers.Filter;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import java.net.URL;
import java.util.ArrayList;
@@ -64,12 +64,12 @@ public class VectorSource extends Source {
* Queries the source for features.
*
* @param sourceLayerIds the source layer identifiers. At least one must be specified.
* @param filter an optional filter statement to filter the returned Features
* @param filter an optional filter expression to filter the returned Features
* @return the features
*/
@NonNull
public List<Feature> querySourceFeatures(@Size(min = 1) String[] sourceLayerIds,
@Nullable Filter.Statement filter) {
@Nullable Expression filter) {
Feature[] features = querySourceFeatures(
sourceLayerIds,
filter != null ? filter.toArray() : null);