Skip to content

Instantly share code, notes, and snippets.

@makella
Last active February 5, 2024 22:47
Show Gist options
  • Save makella/950a7baee78157bf1c315a7c2ea191e7 to your computer and use it in GitHub Desktop.
Save makella/950a7baee78157bf1c315a7c2ea191e7 to your computer and use it in GitHub Desktop.
protomaps.js styling filters

A place to collect handy tips and tricks for style-based filters

Simple color by type

In this case, there are multiple types filtered from the same layer. I want to assign each type a unique color.

Try 1: fail

If/else conditions with a fallback. (following this example)

  {
    dataLayer: `admin`,
    symbolizer: new LineSymbolizer({
      color: (p:any) => {
        if (p.admin_level === 0){
          return "hsl(0,100%,50%)"
        } else {
          if (p.admin_level === 1) return "hsl(80,80%,80%)"
          return "hsl(242,100%,50%)"
        }
      },
      width: 2,
      opacity: 0.4,
    }),
    filter: (f) => 
      f.admin_level === 0 ||
      f.admin_level === 1,
  },

Try 2: fail

Maybe try a condition with a fallback. Like this

{
  dataLayer: `admin`,
  symbolizer: new LineSymbolizer({
    color: (p:any) => {
      if (p.admin_level === 0) return "hsl(0,100%,50%)"
      return "hsl(80,80%,80%)"
    },
    width: 2,
    opacity: 0.4,
  }),
  filter: (f) => 
    f.admin_level === 0 ||
    f.admin_level === 1,
},

Try 3: fail

Maybe I have to use a color varaible instead of hsl.

{
   dataLayer: `admin`,
   symbolizer: new LineSymbolizer({
     color: (p:any) => {
       if (p.admin_level === 0) return params.boundaries0
       return params.boundaries1
     },
     width: 2,
     opacity: 0.4,
   }),
   filter: (f) => 
     f.admin_level === 0 ||
     f.admin_level === 1,
 },

Try 4: fail

Should it be:

  • color: (p) =>
  • color: (p:any) => { if (p["admin_level"] == 0 { ...
  • does it matter if it is == or ===?

Tried a few other variations of the same idea. Really not sure what I'm doing wrong!

@makella
Copy link
Author

makella commented Sep 1, 2021

ahhh! ok,awesome

@bdon
Copy link

bdon commented Sep 3, 2021

@makella any luck with the dynamic attributes and filters? There's a couple more attributes I haven't made dynamic yet, and also planning to do some cleanup around text label symbolizers so they all have the same capabilities (LineLabelSymbolizer is the exception - need to think about curved text, etc)

@makella
Copy link
Author

makella commented Sep 3, 2021

hiii @bdon! yes! i'm currently just going through all my filters and refactoring with the new pattern to get the map rendering again. you wanna meet next week? i also wanted to talk to you about some thoughts around opacity... i'm composing an email but it is still in draft form might be easier to talk through. let me know!

@bdon
Copy link

bdon commented Sep 3, 2021

@makella you can send me your rough thoughts on the opacity... I noticed it can get a little funky when zoom transitions happen with partial opacity polygons because there can be two tiles simultaneously blended and it looks darker.

@gio0v
Copy link

gio0v commented Jul 21, 2023

@makella you can send me your rough thoughts on the opacity... I noticed it can get a little funky when zoom transitions happen with partial opacity polygons because there can be two tiles simultaneously blended and it looks darker.

hi there, thanks for all your hard work! I wonder if you ever managed to solve that issue with opacity? I can still very much notice it in my maps rendered with your protomaps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment