mapbox-gl-js icon indicating copy to clipboard operation
mapbox-gl-js copied to clipboard

Bitwise operations in expressions for flag checking

Open 5andr0 opened this issue 1 month ago • 0 comments

Motivation

With machine generated geojson it's more space saving to store up to 32 flags in a single integer value.

Design Alternatives

The only alternative is to store boolean properties for each flag, which creates unnecessary overhead and requires us to write a boolean check expression for each flag instead of checking for multiple flags only once with an and operation.

"properties": {
   "type1": true,
   "type2": false,
   "type3": true,
  ...
}

Right now for each flag I want to check inside an integer I have to create an expression, which shifts the bit to the right with a division by powers of 2 and modulo 2 to extract the bit for the flag == 1 check ['==', ['%', ['floor', ['/', ["get", "location-type"], 1<<flagIndex]], 2], 1]

Design

With bitwise operators we could do a check for multiple flags within a simple and operation: ['&', ["get", "location-type"], 0b10101]

Implementation

So for the flag checking use case it would be enough to have the & operator expression or labelled as 'hasFlags' expression. But you could also just add all bitwise operators to the math expressions as

  • '&' and
  • 'xor' because ^ is already in use
  • 'not' because ! is already in use
  • '|' or
  • '<<' left shift
  • '>>' right shift

5andr0 avatar May 20 '24 09:05 5andr0