Add expression within (#198)

* Add expression within

* Update expression within and runtime demo

* Update Runtime demo

* Fix android check
This commit is contained in:
Kevin Li
2020-03-04 18:18:53 +08:00
committed by GitHub
parent c217a61170
commit b976545181
4 changed files with 84 additions and 17 deletions

View File

@@ -1,10 +1,6 @@
package com.mapbox.mapboxsdk.style.expressions;
import android.annotation.SuppressLint;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Size;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
@@ -12,6 +8,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.mapbox.geojson.Polygon;
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
import com.mapbox.mapboxsdk.style.layers.PropertyValue;
@@ -22,6 +19,11 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Size;
import static com.mapbox.mapboxsdk.utils.ColorUtils.colorToRgbaArray;
/**
@@ -1539,8 +1541,8 @@ public class Expression {
/**
* Retrieves whether an item exists in an array or a substring exists in a string.
*
* @param needle the item expression
* @param haystack the array or string expression
* @param needle the item expression
* @param haystack the array or string expression
* @return true if exists.
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-in">Style specification</a>
*/
@@ -1551,8 +1553,8 @@ public class Expression {
/**
* Retrieves whether an item exists in an array or a substring exists in a string.
*
* @param needle the item expression
* @param haystack the array or string expression
* @param needle the item expression
* @param haystack the array or string expression
* @return true if exists.
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-in">Style specification</a>
*/
@@ -1563,8 +1565,8 @@ public class Expression {
/**
* Retrieves whether an item exists in an array or a substring exists in a string.
*
* @param needle the item expression
* @param haystack the array or string expression
* @param needle the item expression
* @param haystack the array or string expression
* @return true if exists.
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-in">Style specification</a>
*/
@@ -1572,6 +1574,15 @@ public class Expression {
return new Expression("in", literal(needle), haystack);
}
public static Expression within(@NonNull Polygon polygon) {
Map<String, Expression> map = new HashMap<>();
map.put("type", literal(polygon.type()));
map.put("json", literal(polygon.toJson()));
return new Expression("within", new ExpressionMap(map));
}
/**
* Retrieves a property value from the current feature's properties,
* or from another object if a second argument is provided.

View File

@@ -2,6 +2,8 @@ package com.mapbox.mapboxsdk.style.expressions;
import android.graphics.Color;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.Polygon;
import com.mapbox.mapboxsdk.style.layers.PropertyValue;
import com.mapbox.mapboxsdk.utils.ColorUtils;
@@ -10,7 +12,9 @@ import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -94,6 +98,7 @@ import static com.mapbox.mapboxsdk.style.expressions.Expression.toRgba;
import static com.mapbox.mapboxsdk.style.expressions.Expression.typeOf;
import static com.mapbox.mapboxsdk.style.expressions.Expression.upcase;
import static com.mapbox.mapboxsdk.style.expressions.Expression.var;
import static com.mapbox.mapboxsdk.style.expressions.Expression.within;
import static com.mapbox.mapboxsdk.style.expressions.Expression.zoom;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineOpacity;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth;
@@ -449,11 +454,32 @@ public class ExpressionTest {
@Test
public void testInString() throws Exception {
Object[] expected = new Object[] {"in", "one", "onetwo"};
Object[] expected = new Object[] {"in", "one", "onetwo"};
Object[] actual = in(literal("one"), literal("onetwo")).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
@Test
public void testWithIn() throws Exception {
List<List<Point>> lngLats = Collections.singletonList(
Arrays.asList(
Point.fromLngLat(0, 0),
Point.fromLngLat(0, 5),
Point.fromLngLat(5, 5),
Point.fromLngLat(5, 0),
Point.fromLngLat(0, 0)
)
);
Polygon polygon = Polygon.fromLngLats(lngLats);
HashMap<String, String> map = new HashMap<>();
map.put("type", "Polygon");
map.put("json", polygon.toJson());
Object[] expected = new Object[] {"within", map};
Object[] actual = within(polygon).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
@Test
public void testInNumber() throws Exception {
Object[] expected = new Object[] {"in", 1f, new Object[] {"literal", new Object[] {1f, 2f}}};