Add expression in. (#171)

* Add expression in.

* Add test for expression in.
This commit is contained in:
Kevin Li
2020-02-19 08:52:39 +08:00
committed by GitHub
parent 6109d74b8a
commit 18264df98d
5 changed files with 97 additions and 0 deletions

View File

@@ -1536,6 +1536,42 @@ public class Expression {
return at(literal(number), 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
* @return true if exists.
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-in">Style specification</a>
*/
public static Expression in(@NonNull Expression needle, @NonNull Expression haystack) {
return new Expression("in", needle, haystack);
}
/**
* 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
* @return true if exists.
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-in">Style specification</a>
*/
public static Expression in(@NonNull Number needle, @NonNull Expression haystack) {
return new Expression("in", literal(needle), haystack);
}
/**
* 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
* @return true if exists.
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-in">Style specification</a>
*/
public static Expression in(@NonNull String needle, @NonNull Expression haystack) {
return new Expression("in", literal(needle), haystack);
}
/**
* Retrieves a property value from the current feature's properties,
* or from another object if a second argument is provided.

View File

@@ -48,6 +48,7 @@ import static com.mapbox.mapboxsdk.style.expressions.Expression.gte;
import static com.mapbox.mapboxsdk.style.expressions.Expression.has;
import static com.mapbox.mapboxsdk.style.expressions.Expression.heatmapDensity;
import static com.mapbox.mapboxsdk.style.expressions.Expression.id;
import static com.mapbox.mapboxsdk.style.expressions.Expression.in;
import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate;
import static com.mapbox.mapboxsdk.style.expressions.Expression.isSupportedScript;
import static com.mapbox.mapboxsdk.style.expressions.Expression.length;
@@ -446,6 +447,27 @@ public class ExpressionTest {
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
@Test
public void testInString() throws Exception {
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 testInNumber() throws Exception {
Object[] expected = new Object[] {"in", 1f, new Object[] {"literal", new Object[] {1f, 2f}}};
Object[] actual = in(literal(1f), literal(new Object[] {1f, 2f})).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
@Test
public void testInArray() throws Exception {
Object[] expected = new Object[] {"in", "one", new Object[] {"literal", new Object[] {"one", "two"}}};
Object[] actual = in(literal("one"), literal(new Object[] {"one", "two"})).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
@Test
public void testAtLiteral() throws Exception {
Object[] expected = new Object[] {"at", 3f, new Object[] {"literal", new Object[] {"one", "two"}}};

View File

@@ -43,10 +43,12 @@ import static com.mapbox.mapboxsdk.style.expressions.Expression.eq;
import static com.mapbox.mapboxsdk.style.expressions.Expression.exponential;
import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
import static com.mapbox.mapboxsdk.style.expressions.Expression.gte;
import static com.mapbox.mapboxsdk.style.expressions.Expression.in;
import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate;
import static com.mapbox.mapboxsdk.style.expressions.Expression.literal;
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.zoom;
import static com.mapbox.mapboxsdk.style.layers.Property.FILL_TRANSLATE_ANCHOR_MAP;
@@ -65,6 +67,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.textSize;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility;
/**
@@ -198,6 +201,9 @@ public class RuntimeStyleActivity extends AppCompatActivity {
case R.id.action_fill_filter:
styleFillFilterLayer();
return true;
case R.id.action_textsize_filter:
styleTextSizeFilterLayer();
return true;
case R.id.action_line_filter:
styleLineFilterLayer();
return true;
@@ -524,6 +530,34 @@ public class RuntimeStyleActivity extends AppCompatActivity {
}, 2000);
}
private void styleTextSizeFilterLayer() {
mapboxMap.setStyle(new Style.Builder().fromUri("asset://fill_filter_style.json"));
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(31, -100), 3));
Handler handler = new Handler(getMainLooper());
handler.postDelayed(() -> {
if (mapboxMap == null) {
return;
}
Timber.d("Styling text size fill layer");
SymbolLayer states = (SymbolLayer) mapboxMap.getStyle().getLayer("state-label-lg");
if (states != null) {
states.setProperties(
textSize(switchCase(
in(get("name"), literal("Texas")), literal(25.0f),
in(get("name"), literal(new Object[] {"California","Illinois"})), literal(25.0f),
literal(6.0f) // default value
)
)
);
} else {
Toast.makeText(RuntimeStyleActivity.this, "No states layer in this style", Toast.LENGTH_SHORT).show();
}
}, 2000);
}
private void styleLineFilterLayer() {
mapboxMap.setStyle(new Style.Builder().fromUri("asset://line_filter_style.json"));
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(40, -97), 5));

View File

@@ -58,6 +58,10 @@
android:id="@+id/action_fill_filter"
android:title="@string/apply_filtered_fill"
mapbox:showAsAction="never" />
<item
android:id="@+id/action_textsize_filter"
android:title="@string/apply_textsize_fill"
mapbox:showAsAction="never" />
<item
android:id="@+id/action_line_filter"
android:title="@string/apply_filtered_line"

View File

@@ -77,6 +77,7 @@
<string name="change_the_water_color_on_zoom">Change the water color on zoom</string>
<string name="custom_tiles">Custom tiles</string>
<string name="apply_filtered_fill">Apply filtered fill</string>
<string name="apply_textsize_fill">Apply text size fill</string>
<string name="apply_filtered_line">Apply filtered line</string>
<string name="apply_numeric_fill_filter">Apply numeric fill filter</string>
<string name="bring_water_to_front">Bring water to front</string>