mapbox / mapbox/mapbox-gl-js

Most efficient way to Change/Add feature property at client-side?

Open
#8,753 7 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature :green_apple:
Dominant language
TypeScript
Stars
12.4k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

A mapbox-vector-tile layer is being served from GeoServer (around 500K features points and multiline strings).

I am using mapbox-gl-js to plot the layer at client side and I use expressions to change certain styles(e.g. line color)

I have an external API which will provide me a list of properties for each asset

[{ id: 123, prop1: 45, prop2: 78 }, ...]

Now, I want to assign these properties to the corresponding features, so that I will be able to write expressions based on 'prop1' to manipulate the style.

Expression:

 [ 'let', 'load_value', ['to-number', ['get', 'prop1']],
      [
        'case',
        [ 'all', ['>=', ['var', 'load_value'], 0], ['<', ['var', 'load_value'], 50] ], 'gray',
        [ 'all', ['>=', ['var', 'load_value'], 50], ['<', ['var', 'load_value'], 70] ], 'yellow',
        ['>=', ['var', 'load_value'], 70], 'red',
        'gray'
      ]
 ];

I have tried using setFeatureState method and it worked. Problem, the map is terribly slow after doing this. I believe this is because I need to set the feature state for 500K features.

Setting feature state:

   data.forEach((datum) => {
      this.map.setFeatureState({
        source: 'geoserver-source',
        sourceLayer: 'mvt-layer',
        id: datum.id
      }, {
        prop1: datum.prop1
      });
    });

The color changes as per the given conditions. But the map itself becomes very slow after this. Is there any better way of mapping these properties from the client side?

P.S. the map is slow even if I don't have the expressions to set 'line-color'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is identified in the report. Start by reviewing the map.setFeatureState calls, the 500K-feature loop, and the Mapbox GL JS expression; determine whether the client-side property mapping can remain responsive at that scale, and validate the chosen approach with and without the line-color expression.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.