1- import Style from "ol/style/Style" ;
21import collections from '../sources'
32import { FiboaMap } from "../map" ;
43import { hcat } from "./codes" ;
54import VectorTile from "ol/layer/VectorTile" ;
65import { PMTilesVectorSource } from "ol-pmtiles" ;
76const cropExtension = "https://fiboa.github.io/hcat-extension/v0.1.0/schema.yaml" ;
87
9- const st = hcat . map ( ( { code, color} ) => [ [ '==' , [ 'get' , 'ec:hcat_code' ] , Number ( code ) ] , color ] ) . flat ( ) ;
108const fieldStyle = {
119 "stroke-color" : 'rgb(0, 165, 255)' ,
1210 "stroke-width" : 1 ,
13- "fill-color" :
14- [ 'case' ,
15- ...st ,
16- '#99bbccaa'
17- ]
11+ "fill-color" : [ 'get' , 'color' ]
1812}
13+ const mapping = Object . fromEntries ( hcat . map ( c => [ c . code , c . color ] ) ) ;
14+
1915class CropMap extends FiboaMap {
2016 constructor ( ) {
2117 super ( ) ;
@@ -31,7 +27,6 @@ class CropMap extends FiboaMap {
3127 source : source ,
3228 style : this . fieldStyle ,
3329 } ) ;
34- // TODO, Map color at tileloadend instead of complex style rules
3530 // TODO, Count the features in view and display a legend explaining the top 5 crops
3631 // TODO, Add a filter based on crops. This will not be perfect on high zoom levels (lossy vector tiles)
3732 // source.on('tileloadend', e => {
@@ -41,6 +36,14 @@ class CropMap extends FiboaMap {
4136 // feature.getProperties().color = "##ff0000";
4237 // }
4338 // })
39+ source . on ( 'tileloadend' , e => {
40+ const features = e . tile . getFeatures ( ) ;
41+ for ( const feature of features ) {
42+ const p = feature . getProperties ( ) ;
43+ p . color = mapping [ p [ "ec:hcat_code" ] ] || "#99bbccaa" ;
44+ }
45+ this . updateFeatureCount ( e )
46+ } )
4447 this . map . addLayer ( fields ) ;
4548 }
4649}
0 commit comments