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

Commit

Permalink
Add expression within (#198)
Browse files Browse the repository at this point in the history
* Add expression within

* Update expression within and runtime demo

* Update Runtime demo

* Fix android check
  • Loading branch information
Kevin Li authored Mar 4, 2020
1 parent c217a61 commit b976545
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
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;
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;

Expand All @@ -22,6 +19,11 @@
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;

/**
Expand Down Expand Up @@ -1539,8 +1541,8 @@ public static Expression at(@NonNull Number number, @NonNull Expression expressi
/**
* 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>
*/
Expand All @@ -1551,8 +1553,8 @@ public static Expression in(@NonNull Expression needle, @NonNull Expression hays
/**
* 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>
*/
Expand All @@ -1563,15 +1565,24 @@ public static Expression in(@NonNull Number needle, @NonNull Expression 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
* @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);
}

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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;

Expand All @@ -10,7 +12,9 @@
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;

Expand Down Expand Up @@ -94,6 +98,7 @@
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;
Expand Down Expand Up @@ -449,11 +454,32 @@ public void testAt() throws Exception {

@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}}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
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;
Expand All @@ -30,13 +32,16 @@
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;
Expand All @@ -50,6 +55,7 @@
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;
Expand All @@ -67,6 +73,7 @@
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;

Expand All @@ -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);
Expand All @@ -93,13 +117,19 @@ protected void onCreate(Bundle savedInstanceState) {
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)))
);
}
);
});
}
Expand Down Expand Up @@ -547,7 +577,7 @@ private void styleTextSizeFilterLayer() {
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
)
)
Expand Down

0 comments on commit b976545

Please sign in to comment.