MapboxMap 之 Expression(三)

系列文章目录

Types

Lookup

Decision

Math

String
Color
Zoom

Heatmap

Feature data
Variable binding
Ramps, scales, curves


 

 


前言

在上一篇中介绍了Expression的数组操作,本篇是将的按照 Decision区分,也可以理解为一些比较符,都是高频使用的。


 

Expression可以按Decision分类

 

一.常规理解的比较符

1. ! 

/**
* !
* 逻辑否定。如果输入为false,则返回true;如果输入为true,则返回false。
*
* ["!", boolean]: boolean
*/
Expression not(@NonNull Expression input) 
Expression not(boolean input)

eg:

FillLayer fillLayer = new FillLayer("layer-id", "source-id");
fillLayer.setFilter(
   not(get("keyToValue"))
);

2. != 

/**
 *!=
 * 如果输入值不相等,则返回true,否则返回false。比较是严格类型化的:不同运行时类型的值始终    
 *被认为是不相等的。
 * 在解析时已知类型不同的情况将被视为无效,并会产生解析错误。
 * 接受可选的排序规则参数来控制与语言环境有关的字符串比较。
 *
 * ["!=", value, value]: boolean
 * ["!=", value, value, collator]: boolean
*/

Expression neq(@NonNull Expression compareOne, @NonNull Expression compareTwo)
Expression neq(@NonNull Expression compareOne, @NonNull Expression compareTwo,
                               @NonNull Expression collator)
Expression neq(Expression compareOne, boolean compareTwo)
Expression neq(@NonNull Expression compareOne, @NonNull String compareTwo)
Expression neq(@NonNull Expression compareOne, @NonNull String compareTwo,
                               @NonNull Expression collator)
Expression neq(@NonNull Expression compareOne, @NonNull Number compareTwo)

eg:

 FillLayer fillLayer = new FillLayer("layer-id", "source-id");
 fillLayer.setFilter(
         neq(get("keyToValue"), get("keyToOtherValue"))
);

3.<

/**
 *<
* 如果第一个输入严格小于第二个输入,则返回true,否则返回false。
* 参数必须是字符串或数字。如果在评估过程中不是,则表达式评估会产生错误。
* 已知此约束在解析时不成立的情况被认为是有效的,并且会产生解析错误。
* 接受可选的排序规则参数来控制与语言环境有关的字符串比较。
 *
* ["<", value, value]: boolean
* ["<", value, value, collator]: boolean
*/

Expression lt(@NonNull Expression compareOne, @NonNull Expression compareTwo)
Expression lt(@NonNull Expression compareOne, @NonNull Expression compareTwo,
                              @NonNull Expression collator)
Expression lt(@NonNull Expression compareOne, @NonNull Number compareTwo)
Expression lt(@NonNull Expression compareOne, @NonNull String compareTwo)
Expression lt(@NonNull Expression compareOne, @NonNull String compareTwo,
                              @NonNull Expression collator)

eg:

   FillLayer fillLayer = new FillLayer("layer-id", "source-id");
   fillLayer.setFilter(
               lt(get("keyToValue"), 2.0f)
             );

4.<=

/**
*<=
* 如果第一个输入小于或等于第二个输入,则返回true,否则返回false。
* 参数必须是字符串或数字。如果在评估过程中不是,则表达式评估会产生错误。
* 已知此约束在解析时不成立的情况被认为是有效的,并且会产生解析错误。
* 接受可选的排序规则参数,以控制与语言环境有关的字符串比较。
*
* ["<=", value, value]: boolean
* ["<=", value, value, collator]: boolean
*/
Expression lte(@NonNull Expression compareOne, @NonNull Expression compareTwo)
Expression lte(@NonNull Expression compareOne, @NonNull Expression compareTwo,
                               @NonNull Expression collator)
Expression lte(@NonNull Expression compareOne, @NonNull Number compareTwo)
Expression lte(@NonNull Expression compareOne, @NonNull String compareTwo)
Expression lte(@NonNull Expression compareOne, @NonNull String compareTwo,
                               @NonNull Expression collator)

eg:

FillLayer fillLayer = new FillLayer("layer-id", "source-id");
fillLayer.setFilter(
           lte(get("keyToValue"), 2.0f)
           );

5.==

/**
*==
* 如果输入值相等,则返回true,否则返回false。
* 比较是严格类型化的:不同运行时类型的值始终被认为是不相等的。
* 在解析时已知类型不同的情况将被视为无效,并会产生解析错误。
* 接受可选的排序规则参数,以控制与语言环境有关的字符串比较。
*
* ["==", value, value]: boolean
* ["==", value, value, collator]: boolean
*/

Expression eq(@NonNull Expression compareOne, @NonNull Expression compareTwo)
Expression eq(@NonNull Expression compareOne, @NonNull Expression compareTwo,
                              @NonNull Expression collator)
Expression eq(@NonNull Expression compareOne, boolean compareTwo)
Expression eq(@NonNull Expression compareOne, @NonNull String compareTwo)
Expression eq(@NonNull Expression compareOne, @NonNull String compareTwo,
                              @NonNull Expression collator)
Expression eq(@NonNull Expression compareOne, @NonNull Number compareTwo)

eg:

FillLayer fillLayer = new FillLayer("layer-id", "source-id");
fillLayer.setFilter(
            eq(get("keyToValue"), get("keyToOtherValue"))
          );

6.>

/**
*>
* 如果第一个输入严格大于第二个输入,则返回true,否则返回false。
* 参数必须是字符串或数字。如果在评估过程中不是,则表达式评估会产生错误。
* 已知此约束在解析时不成立的情况被认为是有效的,并且会产生解析错误。
* 接受可选的排序规则参数,以控制与语言环境有关的字符串比较。
*
* [">", value, value]: boolean
* [">", value, value, collator]: boolean
*/

Expression gt(@NonNull Expression compareOne, @NonNull Expression compareTwo)
Expression gt(@NonNull Expression compareOne, @NonNull Expression compareTwo,
                              @NonNull Expression collator)
Expression gt(@NonNull Expression compareOne, @NonNull Number compareTwo)
Expression gt(@NonNull Expression compareOne, @NonNull String compareTwo)
Expression gt(@NonNull Expression compareOne, @NonNull String compareTwo,
                              @NonNull Expression collator)
eg:

  FillLayer fillLayer = new FillLayer("layer-id", "source-id");
  fillLayer.setFilter(
     gt(get("keyToValue"), 2.0f)
   );

7. >=

/**
*>=
* 如果第一个输入大于或等于第二个输入,则返回true,否则返回false。
* 参数必须是字符串或数字。如果在评估过程中不是,则表达式评估会产生错误。
* 已知此约束在解析时不成立的情况被认为是有效的,并且会产生解析错误。
* 接受可选的排序规则参数,以控制与语言环境有关的字符串比较。
*
* [">=", value, value]: boolean
* [">=", value, value, collator]: boolean
*/

Expression gte(@NonNull Expression compareOne, @NonNull Expression compareTwo)
Expression gte(@NonNull Expression compareOne, @NonNull Expression compareTwo,
                              @NonNull Expression collator)
Expression gte(@NonNull Expression compareOne, @NonNull Number compareTwo)
Expression gte(@NonNull Expression compareOne, @NonNull String compareTwo)
Expression gte(@NonNull Expression compareOne, @NonNull String compareTwo,
                              @NonNull Expression collator)

eg:

  FillLayer fillLayer = new FillLayer("layer-id", "source-id");
  fillLayer.setFilter(
        gte(get("keyToValue"), 2.0f)
    );

 

二.mapbox独有的比较符

 

1.all

/**
*all
* 如果所有输入均为true,则返回true,否则返回false。
* 输入是按顺序求值的,并且求值是短路的:输入表达式的求值为假时,结果为假,并且不再求值任何*输入表达式。
*
* ["all", boolean, boolean]: boolean
* ["all", boolean, boolean, ...]: boolean
*/
Expression all(@NonNull Expression... input)

eg:

FillLayer fillLayer = new FillLayer("layer-id", "source-id");
fillLayer.setFilter(
           all(get("keyToValue"), get("keyToOtherValue"))
);

2.any

/**
*any
* 如果任何输入为true,则返回true,否则返回false。
* 输入是按顺序求值的,并且求值是短路的:一旦输入表达式的求值为真,则结果为真,并且不再对其 * 他输入表达式求值。
*
* ["any", boolean, boolean]: boolean
* ["any", boolean, boolean, ...]: boolean
*/
Expression any(@NonNull Expression... input)

eg:

FillLayer fillLayer = new FillLayer("layer-id", "source-id");
fillLayer.setFilter(
           any(get("keyToValue"), get("keyToOtherValue"))
         );

3.case

/**
*case
* 选择第一个输出,其对应的测试条件评估为true,否则选择回退值。
*
* ["case",
*     condition: boolean, output: OutputType,
*     condition: boolean, output: OutputType,
*     ...,
*     fallback: OutputType
* ]: OutputType
*/
Expression switchCase(@NonNull @Size(min = 1) Expression... input)

eg:

SymbolLayer symbolLayer = new SymbolLayer("layer-id", "source-id");
symbolLayer.setProperties(
             iconSize(
                  switchCase(
                          get("KEY_TO_BOOLEAN"), literal(3.0f),
                          get("KEY_TO_OTHER_BOOLEAN"), literal(5.0f),
                          literal(1.0f) // default value
                          )
                     )
                );

4.coalesce

/**
*coalesce
* 依次评估每个表达式,直到获得第一个非空值,然后返回该值。
*
* ["coalesce", OutputType, OutputType, ...]: OutputType
*/
Expression coalesce(@NonNull Expression... input) 

eg:

SymbolLayer symbolLayer = new SymbolLayer("layer-id", "source-id");
symbolLayer.setProperties(
             textColor(
             coalesce(
                      get("keyToNullValue"),
                      get("keyToNonNullValue")
                    )
                 )
               );

5.match

/**
*match
* 选择标签值与输入值匹配的输出,如果找不到匹配项,则选择回退值。
* 输入可以是任何表达式(例如[“ get”,“ building_type”])。
* 每个标签必须是:
*单个文字值;或一个文字值数组,其值必须是所有字符串或所有数字(例如[100,101]或[“ c”,“ b”])。
* 如果数组中的任何值匹配,则输入匹配,类似于“ in”运算符。
* 每个标签必须唯一。如果输入类型与标签类型不匹配,则结果将是后备值。
* ["match",
*     input: InputType (number or string),
*     label: InputType | [InputType, InputType, ...], output: OutputType,
*     label: InputType | [InputType, InputType, ...], output: OutputType,
*     ...,
*     fallback: OutputType
* ]: OutputType
 */
Expression match(@NonNull @Size(min = 2) Expression... input) 
Expression match(@NonNull Expression input, @NonNull Expression defaultOutput, @NonNull Stop... stops) {

eg:

SymbolLayer symbolLayer = new SymbolLayer("layer-id", "source-id");
symbolLayer.setProperties(
               textColor(
                    match(get("keyToValue"),
                         literal(1), rgba(255, 0, 0, 1.0f),
                         literal(2), rgba(0, 0, 255.0f, 1.0f),
                         rgba(0.0f, 255.0f, 0.0f, 1.0f)
                        )
                    )
                );

6.within

/**
*within
* 如果评估的要素完全包含在输入几何的边界内,则返回true,否则返回false。
* 输入值是类型为Polygon的有效GeoJSON。
* 支持的评估功能:
*
* Point: 如果点在边界上或位于边界之外,则返回false。
* LineString: 如果线的任何部分落在边界之外,线与边界相交或线的端点在边界上,则返回false。
* ["within", object]: boolean
*/

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

总结&示例

总结:

  • 1.比较符的好处就是你会了一个其他的就都简单了。

  • 2.返回booean多用于Filter。

示例:

老规矩,代码放这里,自己可以改动体验一下。

 private static final String SOURCE_ID = "source-id";
    private static final String LAYER_ID = "layer-id";
    private static final String LAYER_TEXT = "layer-text";
    private static final String LAYER_TEXT_BIG = "layer-text-big";
    private static final String LAYER_TEXT_SMALL = "layer-text-small";

    private GeoJsonSource geoJsonSource;
    private final List<Feature> featureList = new ArrayList<>();

    private static final double LATITUDE = 39.9;
    private static final double LONGITUDE = 119.6;

    @Override
    public int getLayoutId() {
        return R.layout.activity_ex_csdn_types_demo;
    }

    @Override
    public void onMapLoaded() {
        super.onMapLoaded();
        //初始化源等
        initTextSource();
        //批量添加文字
        addTextsOnMap();
    }

    private void addTextsOnMap() {
        for (int i = 0; i < 3; i++) {
            //feature
            Point point = Point.fromLngLat(LONGITUDE + i * 0.002, LATITUDE + i * 0.002);
            Feature feature = Feature.fromGeometry(point);
            if (i == 0) {
                //其他10000
                feature.addStringProperty(LAYER_TEXT, "10000");
                feature.addBooleanProperty(LAYER_TEXT_SMALL, true);
                feature.addBooleanProperty(LAYER_TEXT_BIG, false);
            } else if (i == 1) {
                //其他10000
                feature.addStringProperty(LAYER_TEXT, "10001");
                feature.addBooleanProperty(LAYER_TEXT_SMALL, false);
                feature.addBooleanProperty(LAYER_TEXT_BIG, false);
            } else {
                //2是100000,5个0
                feature.addStringProperty(LAYER_TEXT, "1000002");
                feature.addBooleanProperty(LAYER_TEXT_SMALL, false);
                feature.addBooleanProperty(LAYER_TEXT_BIG, true);
            }
            featureList.add(feature);
        }
        //刷新数据源
        geoJsonSource.setGeoJson(FeatureCollection.fromFeatures(featureList));
        //移动地图视角
        mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(LATITUDE, LONGITUDE), 12));
    }

    private void initTextSource() {
        //添加源
        FeatureCollection deviceFeatureCollection = FeatureCollection.fromFeatures(featureList);
        geoJsonSource = new GeoJsonSource(SOURCE_ID, deviceFeatureCollection);
        style.addSource(geoJsonSource);
        //添加Layer
        SymbolLayer symbolLayer = new SymbolLayer(LAYER_ID, SOURCE_ID);
        symbolLayer.withProperties(
                textSize(
                        switchCase(
                                get(LAYER_TEXT_SMALL), literal(12.0f),
                                get(LAYER_TEXT_BIG), literal(28.0f),
                                literal(17.0f) // default value
                        )),
                textColor(Color.YELLOW),
                textField(get(LAYER_TEXT)),
                textAllowOverlap(true)
        ).withFilter(
                //!
//                not(eq(length(get(LAYER_TEXT)), literal(4)))
                //!=
//                neq(length(get(LAYER_TEXT)), literal(4))
                //<
//                lt(length(get(LAYER_TEXT)), literal(4))
                //<=
//                lte(length(get(LAYER_TEXT)), literal(4))
                //==
//                eq(length(get(LAYER_TEXT)), literal(4))
                //>
//                gt(length(get(LAYER_TEXT)), literal(4))
                //>=
//                gte(length(get(LAYER_TEXT)), literal(4))
                //>=2 && <=4
//                all(gte(length(get(LAYER_TEXT)), literal(2)), lte(length(get(LAYER_TEXT)), literal(4)))
                //>=2 | <=4
                any(gte(length(get(LAYER_TEXT)), literal(2)), lte(length(get(LAYER_TEXT)), literal(4)))
        );
        style.addLayer(symbolLayer);
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值