@@ -7,9 +7,14 @@ import type {
7
7
} from "maplibre-gl" ;
8
8
9
9
/**
10
- * Generate the map color expression for MapLibre GL
11
- * This creates a "case" expression for use in map style
12
- * @returns A MapLibre GL expression for the fill-color property
10
+ * Generates a color expression for MapLibre GL based on data from pmtiles.
11
+ *
12
+ * Creates a case-based expression that maps different pollution values to specific colors
13
+ * for rendering on the map. Handles both "dernier_prelevement" and "bilan_annuel" data periods.
14
+ *
15
+ * Returns a MapLibre GL expression for the fill-color property
16
+ *
17
+ * MapLibre expressions documentation : https://maplibre.org/maplibre-style-spec/expressions/
13
18
*/
14
19
export function generateColorExpression (
15
20
category : string ,
@@ -24,11 +29,29 @@ export function generateColorExpression(
24
29
return defaultColor ;
25
30
}
26
31
32
+ // Check if we have no data for this zone (when either cdreseau or commune_code_insee exists but is empty)
33
+ // If yes, set the color to transparent to hide these zones on the map
34
+ cases . push ( [
35
+ "any" ,
36
+ [ "all" , [ "has" , "cdreseau" ] , [ "==" , [ "get" , "cdreseau" ] , "" ] ] ,
37
+ [
38
+ "all" ,
39
+ [ "has" , "commune_code_insee" ] ,
40
+ [ "==" , [ "get" , "commune_code_insee" ] , "" ] ,
41
+ ] ,
42
+ ] ) ;
43
+ cases . push ( "transparent" ) ; // Transparent for no data
44
+
27
45
// dernier prélèvement specific logic
28
46
if ( period . startsWith ( "dernier_prel" ) ) {
29
- const propertyId = getPropertyName ( period , category , "resultat" ) ;
47
+ const resultatProp = getPropertyName ( period , category , "resultat" ) ;
30
48
Object . entries ( categoryDetails . resultats ) . forEach ( ( [ value , detail ] ) => {
31
- cases . push ( [ "==" , [ "get" , propertyId ] , value ] ) ;
49
+ // the value "non_recherche" is actually an empty string so we set a special case for it
50
+ if ( value === "non_recherche" ) {
51
+ cases . push ( [ "==" , [ "get" , resultatProp ] , "" ] ) ;
52
+ } else {
53
+ cases . push ( [ "==" , [ "get" , resultatProp ] , value ] ) ;
54
+ }
32
55
33
56
// Check if the color is valid
34
57
const color = detail . couleur || detail . couleurFond ;
@@ -83,7 +106,7 @@ export function generateColorExpression(
83
106
}
84
107
85
108
if ( cases . length > 0 ) {
86
- const expression = [ "case" , ...cases , defaultColor ] ;
109
+ const expression = [ "case" , ...cases , "#333333" ] ; // black for unmatched cases
87
110
console . log ( "Expression:" , expression ) ;
88
111
return expression as DataDrivenPropertyValueSpecification < ColorSpecification > ;
89
112
} else {
0 commit comments