Skip to content

Commit 4abab9b

Browse files
ftoromanoffjailln
authored andcommitted
refactor(FeatureContext): use Context on LabelLayer and Feature2Texture
1 parent 219e015 commit 4abab9b

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

src/Converter/Feature2Mesh.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,20 @@ import Coordinates from 'Core/Geographic/Coordinates';
1010

1111
const coord = new Coordinates('EPSG:4326', 0, 0, 0);
1212

13-
class FeatureContext {
13+
/**
14+
* @class
15+
* @classdesc FeatureContext is a class to store all informations
16+
* about context to generate the style of each FeatureGeometry.
17+
*
18+
* @property {Coordinates} worldCoord @private Coordinates of the FeatureGeometry in world system.
19+
* @property {Coordinates} localCoordinates @private Are the coordinates systeme origin local or global.
20+
* @property {boolean} isProjected @private Are the coordinates already been projected.
21+
* @property {FeatureGeometry} geometry @private
22+
* @property {Object} globals
23+
* @property {Object} collection
24+
* @property {Coordinates} coordinates
25+
*/
26+
export class FeatureContext {
1427
#worldCoord = new Coordinates('EPSG:4326', 0, 0, 0);
1528
#localCoordinates = new Coordinates('EPSG:4326', 0, 0, 0);
1629
#isProjected = true;

src/Converter/Feature2Texture.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { FEATURE_TYPES } from 'Core/Feature';
33
import Extent from 'Core/Geographic/Extent';
44
import Coordinates from 'Core/Geographic/Coordinates';
55
import Style from 'Core/Style';
6+
import { FeatureContext } from 'Converter/Feature2Mesh';
7+
8+
const context = new FeatureContext();
69

710
/**
811
* Draw polygon (contour, line edge and fill) based on feature vertices into canvas
@@ -74,7 +77,7 @@ const coord = new Coordinates('EPSG:4326', 0, 0, 0);
7477
function drawFeature(ctx, feature, extent, style, invCtxScale) {
7578
const extentDim = extent.planarDimensions();
7679
const scaleRadius = extentDim.x / ctx.canvas.width;
77-
const globals = {
80+
context.globals = {
7881
fill: true,
7982
stroke: true,
8083
point: true,
@@ -83,7 +86,7 @@ function drawFeature(ctx, feature, extent, style, invCtxScale) {
8386

8487
for (const geometry of feature.geometries) {
8588
if (Extent.intersectsExtent(geometry.extent, extent)) {
86-
const context = { globals, properties: () => geometry.properties };
89+
context.setGeometry(geometry);
8790
const contextStyle = (geometry.properties.style || style).applyContext(context);
8891

8992
if (contextStyle) {

src/Core/Style.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ class Style {
987987
if (this.text.field.expression) {
988988
return readExpression(this.text.field, ctx);
989989
} else {
990-
return this.text.field.replace(/\{(.+?)\}/g, (a, b) => (ctx.properties()[b] || '')).trim();
990+
return this.text.field.replace(/\{(.+?)\}/g, (a, b) => (ctx.properties[b] || '')).trim();
991991
}
992992
}
993993
}

src/Layer/LabelLayer.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import Label from 'Core/Label';
88
import { FEATURE_TYPES } from 'Core/Feature';
99
import { readExpression } from 'Core/Style';
1010
import { ScreenGrid } from 'Renderer/Label2DRenderer';
11+
import { FeatureContext } from 'Converter/Feature2Mesh';
12+
13+
const context = new FeatureContext();
1114

1215
const coord = new Coordinates('EPSG:4326', 0, 0, 0);
1316

@@ -241,7 +244,7 @@ class LabelLayer extends GeometryLayer {
241244
// Converting the extent now is faster for further operation
242245
extent.as(data.crs, _extent);
243246
coord.crs = data.crs;
244-
const globals = {
247+
context.globals = {
245248
icon: true,
246249
text: true,
247250
zoom: extent.zoom,
@@ -272,8 +275,9 @@ class LabelLayer extends GeometryLayer {
272275
if (!_extent.isPointInside(coord)) { return; }
273276

274277
const geometryField = g.properties.style && g.properties.style.text.field;
278+
279+
context.setGeometry(g);
275280
let content;
276-
const context = { globals, properties: () => g.properties };
277281
if (this.labelDomelement) {
278282
content = readExpression(this.labelDomelement, context);
279283
} else if (!geometryField && !featureField && !layerField) {

0 commit comments

Comments
 (0)