Skip to content

Commit 95b5d19

Browse files
committed
Optimize color rendering rule
1 parent 53368ea commit 95b5d19

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

map/crop/crop.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
import Style from "ol/style/Style";
21
import collections from '../sources'
32
import {FiboaMap} from "../map";
43
import {hcat} from "./codes";
54
import VectorTile from "ol/layer/VectorTile";
65
import {PMTilesVectorSource} from "ol-pmtiles";
76
const 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();
108
const 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+
1915
class 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

Comments
 (0)