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}}};

View File

@@ -3,12 +3,14 @@ package com.mapbox.mapboxsdk.testapp.activity.style;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.Polygon;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
@@ -30,13 +32,16 @@ import com.mapbox.mapboxsdk.style.sources.TileSet;
import com.mapbox.mapboxsdk.style.sources.VectorSource;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils;
import timber.log.Timber;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import androidx.appcompat.app.AppCompatActivity;
import timber.log.Timber;
import static com.mapbox.mapboxsdk.style.expressions.Expression.all;
import static com.mapbox.mapboxsdk.style.expressions.Expression.color;
import static com.mapbox.mapboxsdk.style.expressions.Expression.eq;
@@ -50,6 +55,7 @@ import static com.mapbox.mapboxsdk.style.expressions.Expression.lt;
import static com.mapbox.mapboxsdk.style.expressions.Expression.stop;
import static com.mapbox.mapboxsdk.style.expressions.Expression.switchCase;
import static com.mapbox.mapboxsdk.style.expressions.Expression.toNumber;
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.Property.FILL_TRANSLATE_ANCHOR_MAP;
import static com.mapbox.mapboxsdk.style.layers.Property.NONE;
@@ -67,6 +73,7 @@ import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineOpacity;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.symbolPlacement;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textOpacity;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textSize;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility;
@@ -79,6 +86,23 @@ public class RuntimeStyleActivity extends AppCompatActivity {
private MapboxMap mapboxMap;
private boolean styleLoaded;
List<List<Point>> lngLats = Collections.singletonList(
Arrays.asList(
Point.fromLngLat(-15.468749999999998,
41.77131167976407),
Point.fromLngLat(15.468749999999998,
41.77131167976407),
Point.fromLngLat(15.468749999999998,
58.26328705248601),
Point.fromLngLat(-15.468749999999998,
58.26328705248601),
Point.fromLngLat(-15.468749999999998,
41.77131167976407)
)
);
Polygon polygon = Polygon.fromLngLats(lngLats);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -93,13 +117,19 @@ public class RuntimeStyleActivity extends AppCompatActivity {
mapboxMap = map;
// Center and Zoom (Amsterdam, zoomed to streets)
mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(52.379189, 4.899431), 14));
mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(52.379189, 4.899431), 1));
mapboxMap.setStyle(
new Style.Builder()
.fromUri(Style.MAPBOX_STREETS)
// set custom transition
.withTransition(new TransitionOptions(250, 50)), style -> styleLoaded = true
.withTransition(new TransitionOptions(250, 50)), style -> {
styleLoaded = true;
SymbolLayer laber = (SymbolLayer) style.getLayer("country-label");
laber.setProperties(
textOpacity(switchCase(within(polygon), literal(1.0f), literal(0.5f)))
);
}
);
});
}
@@ -547,7 +577,7 @@ public class RuntimeStyleActivity extends AppCompatActivity {
states.setProperties(
textSize(switchCase(
in(get("name"), literal("Texas")), literal(25.0f),
in(get("name"), literal(new Object[] {"California","Illinois"})), literal(25.0f),
in(get("name"), literal(new Object[] {"California", "Illinois"})), literal(25.0f),
literal(6.0f) // default value
)
)