Skip to content

Commit

Permalink
refactor(FeatureContext): use Context on LabelLayer and Feature2Texture
Browse files Browse the repository at this point in the history
  • Loading branch information
ftoromanoff authored and jailln committed Nov 13, 2023
1 parent 219e015 commit 4abab9b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
15 changes: 14 additions & 1 deletion src/Converter/Feature2Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@ import Coordinates from 'Core/Geographic/Coordinates';

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

class FeatureContext {
/**
* @class
* @classdesc FeatureContext is a class to store all informations
* about context to generate the style of each FeatureGeometry.
*
* @property {Coordinates} worldCoord @private Coordinates of the FeatureGeometry in world system.
* @property {Coordinates} localCoordinates @private Are the coordinates systeme origin local or global.
* @property {boolean} isProjected @private Are the coordinates already been projected.
* @property {FeatureGeometry} geometry @private
* @property {Object} globals
* @property {Object} collection
* @property {Coordinates} coordinates
*/
export class FeatureContext {
#worldCoord = new Coordinates('EPSG:4326', 0, 0, 0);
#localCoordinates = new Coordinates('EPSG:4326', 0, 0, 0);
#isProjected = true;
Expand Down
7 changes: 5 additions & 2 deletions src/Converter/Feature2Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { FEATURE_TYPES } from 'Core/Feature';
import Extent from 'Core/Geographic/Extent';
import Coordinates from 'Core/Geographic/Coordinates';
import Style from 'Core/Style';
import { FeatureContext } from 'Converter/Feature2Mesh';

const context = new FeatureContext();

/**
* Draw polygon (contour, line edge and fill) based on feature vertices into canvas
Expand Down Expand Up @@ -74,7 +77,7 @@ const coord = new Coordinates('EPSG:4326', 0, 0, 0);
function drawFeature(ctx, feature, extent, style, invCtxScale) {
const extentDim = extent.planarDimensions();
const scaleRadius = extentDim.x / ctx.canvas.width;
const globals = {
context.globals = {
fill: true,
stroke: true,
point: true,
Expand All @@ -83,7 +86,7 @@ function drawFeature(ctx, feature, extent, style, invCtxScale) {

for (const geometry of feature.geometries) {
if (Extent.intersectsExtent(geometry.extent, extent)) {
const context = { globals, properties: () => geometry.properties };
context.setGeometry(geometry);
const contextStyle = (geometry.properties.style || style).applyContext(context);

if (contextStyle) {
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ class Style {
if (this.text.field.expression) {
return readExpression(this.text.field, ctx);
} else {
return this.text.field.replace(/\{(.+?)\}/g, (a, b) => (ctx.properties()[b] || '')).trim();
return this.text.field.replace(/\{(.+?)\}/g, (a, b) => (ctx.properties[b] || '')).trim();
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/Layer/LabelLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import Label from 'Core/Label';
import { FEATURE_TYPES } from 'Core/Feature';
import { readExpression } from 'Core/Style';
import { ScreenGrid } from 'Renderer/Label2DRenderer';
import { FeatureContext } from 'Converter/Feature2Mesh';

const context = new FeatureContext();

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

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

const geometryField = g.properties.style && g.properties.style.text.field;

context.setGeometry(g);
let content;
const context = { globals, properties: () => g.properties };
if (this.labelDomelement) {
content = readExpression(this.labelDomelement, context);
} else if (!geometryField && !featureField && !layerField) {
Expand Down

0 comments on commit 4abab9b

Please sign in to comment.