Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change “fill-color”、“fill-pattern” with "feature-state" did not get the correct result #4930

Open
beikehanbao23 opened this issue Oct 29, 2024 · 4 comments
Labels
enhancement New feature or request PR is more than welcomed Extra attention is needed

Comments

@beikehanbao23
Copy link

maplibre-gl-js version: 4.7.1
browser: Google Chrome 130.0.6723.70

Hello everyone:

I want to change the fill image of the polygon, using the fill-pattern attribute work with feature-state.
However, I did not get the correct result: "feature-state" is different, but the "fill-pattern" is the same..
image

image

function fillPatternFromFeatureState (){
    map.setPaintProperty('layerid', 'fill-pattern', ["match", ["feature-state", 'for-pattern'],
      "cat1", "airport_11",
      'cat2', "bar_11",
      'cat3', 'bakery_11',
      'archery_11'] )
}

When I use the properties(name) that come with "feature" , it can be achieved .

image

function fillPatternFromFeatureProperties(){
    map.setPaintProperty('layerid', 'fill-pattern', ["match", ["get", 'name'],
      "cat1", "airport_11",
      'cat2', "bar_11",
      'cat3', 'bakery_11',
      'archery_11'])
}

Steps:

  1. Load demo GeoJSON, which contains three different polygons with different name attributes
  2. Assign three different polygon "feature-state" attributes

Link to Demonstration
https://jsbin.com/xebocucufi/3/edit?html,console,output

First question, hope I've made it clear.
I'm not sure if it's a bug. Any help is welcome.

@HarelM
Copy link
Collaborator

HarelM commented Oct 29, 2024

Checkout the docs:
https://maplibre.org/maplibre-style-spec/layers/#fill-color
https://maplibre.org/maplibre-style-spec/layers/#fill-pattern
You can see that fill color support feature state while pattern doesn't.
Here's an old issue about this:
mapbox/mapbox-gl-js#7207

I've looked in the code to better understand why this is not supported and I think the problem is that feature-state is stored in the main thread while the worker is parsing the tiles and preparing the images for pattern it can only do that for a feature without its state, as can be seen here:

let min = patternPropertyValue.evaluate({zoom: zoom - 1}, patternFeature, {}, options.availableImages);

Not sure it's helpful to you, but feel free to explore a way to solve this if this interest you.

@HarelM HarelM added enhancement New feature or request PR is more than welcomed Extra attention is needed labels Oct 29, 2024
@beikehanbao23
Copy link
Author

@HarelM
Thank you for your help.

Will the ' fill-pattern' support the feature-state in the future?

If not, what suggestions are there to implement different fill-patterns, about 10+ different images.

@HarelM
Copy link
Collaborator

HarelM commented Oct 30, 2024

If someone would be interested enough to contribute a PR it might be supported, but otherwise, very low chances.
For different fill patterns you might want to use different geojson features, html markers or other dynamic solutions that are not based on feature-state...

@beikehanbao23
Copy link
Author

beikehanbao23 commented Oct 30, 2024

Thanks

I solved this problem by putting fill values into the attributes object.

function fillPatternFromFeatureProperties(){
    map.setPaintProperty('layerid', 'fill-pattern', ["match", ["get", 'fill-img'],
      "cat1", "airport_11",
      'cat2', "bar_11",
      'cat3', 'bakery_11',
      'archery_11'])
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request PR is more than welcomed Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants