Skip to content

Commit

Permalink
feat: parse placement of TextSymbolizer (#292)
Browse files Browse the repository at this point in the history
* feat: parse placement of TextSymbolizer

* Apply suggestions from code review

Co-authored-by: Jan Suleiman <[email protected]>

---------

Co-authored-by: Faouzi Homsani <[email protected]>
Co-authored-by: Jan Suleiman <[email protected]>
  • Loading branch information
3 people authored Nov 20, 2023
1 parent 075b77a commit 9acb43f
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 5 deletions.
24 changes: 24 additions & 0 deletions data/mapbox/text_placement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { MbStyle } from '../../src/MapboxStyleParser';

const textPlacement: MbStyle = {
version: 8,
name: 'symbol placement',
sources: {
testsource: {
type: 'vector'
}
},
layers: [
{
id: 'Simple Text',
type: 'symbol',
source: 'testsource',
'source-layer': 'foo',
layout: {
'symbol-placement': 'line'
}
}
]
};

export default textPlacement;
24 changes: 24 additions & 0 deletions data/mapbox/text_placement_line_center.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { MbStyle } from '../../src/MapboxStyleParser';

const textPlacementLineCenter: MbStyle = {
version: 8,
name: 'symbol placement',
sources: {
testsource: {
type: 'vector'
}
},
layers: [
{
id: 'Simple Text',
type: 'symbol',
source: 'testsource',
'source-layer': 'foo',
layout: {
'symbol-placement': 'line-center'
}
}
]
};

export default textPlacementLineCenter;
24 changes: 24 additions & 0 deletions data/mapbox/text_placement_point.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { MbStyle } from '../../src/MapboxStyleParser';

const textPlacementPoint: MbStyle = {
version: 8,
name: 'symbol placement',
sources: {
testsource: {
type: 'vector'
}
},
layers: [
{
id: 'Simple Text',
source: 'testsource',
type: 'symbol',
'source-layer': 'foo',
layout: {
'symbol-placement': 'point'
}
}
]
};

export default textPlacementPoint;
36 changes: 36 additions & 0 deletions data/mapbox_metadata/text_placement_line.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { MbStyle } from '../../src/MapboxStyleParser';

const textPlacementLine: MbStyle = {
version: 8,
name: 'symbol placement',
sources: {
testsource: {
type: 'vector'
}
},
layers: [
{
id: 'r0_sy0_st0',
source: 'testsource',
'source-layer': 'foo',
type: 'symbol',
layout: {
'symbol-placement': 'line'
}
}
],
metadata: {
'geostyler:ref': {
rules: [{
name: 'Simple Text',
symbolizers: [
[
'r0_sy0_st0'
]
]
}]
}
}
};

export default textPlacementLine;
36 changes: 36 additions & 0 deletions data/mapbox_metadata/text_placement_line_center.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { MbStyle } from '../../src/MapboxStyleParser';

const textPlacementLineCenter: MbStyle = {
version: 8,
name: 'symbol placement',
sources: {
testsource: {
type: 'vector'
}
},
layers: [
{
id: 'r0_sy0_st0',
source: 'testsource',
'source-layer': 'foo',
type: 'symbol',
layout: {
'symbol-placement': 'line-center'
}
}
],
metadata: {
'geostyler:ref': {
rules: [{
name: 'Simple Text',
symbolizers: [
[
'r0_sy0_st0'
]
]
}]
}
}
};

export default textPlacementLineCenter;
36 changes: 36 additions & 0 deletions data/mapbox_metadata/text_placement_point.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { MbStyle } from '../../src/MapboxStyleParser';

const textPlacementPoint: MbStyle = {
version: 8,
name: 'symbol placement',
sources: {
testsource: {
type: 'vector'
}
},
layers: [
{
id: 'r0_sy0_st0',
source: 'testsource',
'source-layer': 'foo',
type: 'symbol',
layout: {
'symbol-placement': 'point'
}
}
],
metadata: {
'geostyler:ref': {
rules: [{
name: 'Simple Text',
symbolizers: [
[
'r0_sy0_st0'
]
]
}]
}
}
};

export default textPlacementPoint;
29 changes: 29 additions & 0 deletions data/styles/text_placement_line.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Style } from 'geostyler-style';

const textPlacement: Style = {
name: 'symbol placement',
rules: [{
name: 'Simple Text',
symbolizers: [{
kind: 'Text',
placement: 'line'
}]
}],
metadata: {
'mapbox:ref': {
sources: {
testsource: {
type: 'vector'
}
},
sourceMapping: {
testsource: [0]
},
sourceLayerMapping: {
foo: [0]
}
}
}
};

export default textPlacement;
29 changes: 29 additions & 0 deletions data/styles/text_placement_line_center.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Style } from 'geostyler-style';

const textPlacementLineCenter: Style = {
name: 'symbol placement',
rules: [{
name: 'Simple Text',
symbolizers: [{
kind: 'Text',
placement: 'line-center'
}]
}],
metadata: {
'mapbox:ref': {
sources: {
testsource: {
type: 'vector'
}
},
sourceMapping: {
testsource: [0]
},
sourceLayerMapping: {
foo: [0]
}
}
}
};

export default textPlacementLineCenter;
29 changes: 29 additions & 0 deletions data/styles/text_placement_point.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Style } from 'geostyler-style';

const textPlacementPoint: Style = {
name: 'symbol placement',
rules: [{
name: 'Simple Text',
symbolizers: [{
kind: 'Text',
placement: 'point'
}]
}],
metadata: {
'mapbox:ref': {
sources: {
testsource: {
type: 'vector'
}
},
sourceMapping: {
testsource: [0]
},
sourceLayerMapping: {
foo: [0]
}
}
}
};

export default textPlacementPoint;
50 changes: 47 additions & 3 deletions src/MapboxStyleParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,24 @@ import mb_source_layer_mapping from '../data/mapbox/source_layer_mapping';
import mb_source_layer_mapping_metadata from '../data/mapbox_metadata/source_layer_mapping';
import { CustomLayerInterface } from 'mapbox-gl';
import { AnyLayer } from 'mapbox-gl';

import text_placement_line_center from '../data/styles/text_placement_line_center';
import mb_placement_line_center from '../data/mapbox/text_placement_line_center';
import mb_text_placement_line_center_metadata from '../data/mapbox_metadata/text_placement_line_center';
import mb_text_placement_line from '../data/mapbox/text_placement';
import text_placement_line from '../data/styles/text_placement_line';
import mb_text_placement_line_metadata from '../data/mapbox_metadata/text_placement_line';
import mb_text_placement_point from '../data/mapbox/text_placement_point';
import text_placement_point from '../data/styles/text_placement_point';
import mb_text_placement_point_metadata from '../data/mapbox_metadata/text_placement_point';
it('MapboxStyleParser is defined', () => {
expect(MapboxStyleParser).toBeDefined();
});

describe('MapboxStyleParser implements StyleParser', () => {
let styleParser: MapboxStyleParser;

beforeEach(() => {
styleParser = new MapboxStyleParser();
});

describe('#readStyle', () => {
it('can read a mapbox Line style', async () => {
expect.assertions(2);
Expand All @@ -85,6 +91,26 @@ describe('MapboxStyleParser implements StyleParser', () => {
expect(geoStylerStyle).toBeDefined();
expect(geoStylerStyle).toEqual(line_patternline);
});
it('can read a mapbox text placement line value', async () => {
expect.assertions(2);
const { output: geoStylerStyle } = await styleParser.readStyle(mb_text_placement_line);
expect(geoStylerStyle).toBeDefined();
expect(geoStylerStyle).toEqual(text_placement_line);
});

it('can read a mapbox text placement point value', async () => {
expect.assertions(2);
const { output: geoStylerStyle } = await styleParser.readStyle(mb_text_placement_point);
expect(geoStylerStyle).toBeDefined();
expect(geoStylerStyle).toEqual(text_placement_point);
});

it('can read a mapbox text placement line center value', async () => {
expect.assertions(2);
const { output: geoStylerStyle } = await styleParser.readStyle(mb_placement_line_center);
expect(geoStylerStyle).toBeDefined();
expect(geoStylerStyle).toEqual(text_placement_line_center);
});

it('can read a mapbox Fill style with outline', async () => {
expect.assertions(2);
Expand Down Expand Up @@ -286,6 +312,24 @@ describe('MapboxStyleParser implements StyleParser', () => {
expect(mbStyle).toEqual(mb_multi_rule_line_fill_metadata);
});

it('can write a mapbox text line placement', async() => {
expect.assertions(2);
const {output: mbStyle} = await styleParser.writeStyle(text_placement_line);
expect(mbStyle).toBeDefined();
expect(mbStyle).toEqual(mb_text_placement_line_metadata);
});
it('can write a mapbox text line center placement', async() => {
expect.assertions(2);
const {output: mbStyle} = await styleParser.writeStyle(text_placement_line_center);
expect(mbStyle).toBeDefined();
expect(mbStyle).toEqual(mb_text_placement_line_center_metadata);
});
it('can write a mapbox text point placement', async() => {
expect.assertions(2);
const {output: mbStyle} = await styleParser.writeStyle(text_placement_point);
expect(mbStyle).toBeDefined();
expect(mbStyle).toEqual(mb_text_placement_point_metadata);
});
it('can write a mapbox style with a complex filter', async () => {
expect.assertions(2);
const { output: mbStyle } = await styleParser.writeStyle(line_simpleline_basefilter);
Expand Down
6 changes: 4 additions & 2 deletions src/MapboxStyleParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ export class MapboxStyleParser implements StyleParser<Omit<MbStyle, 'sources'>>
size: mb2gsExpression<number>(layout?.['text-size']),
// TODO: handle enum values
transform: layout?.['text-transform'] as TextSymbolizer['transform'],
visibility: layout?.visibility && layout?.visibility !== 'none'
visibility: layout?.visibility && layout?.visibility !== 'none',
placement: mb2gsExpression<TextSymbolizer['placement']>(layout?.['symbol-placement'])
};

if (MapboxStyleUtil.symbolizerAllUndefined(symbolizer)) {
Expand Down Expand Up @@ -867,7 +868,6 @@ export class MapboxStyleParser implements StyleParser<Omit<MbStyle, 'sources'>>

/**
* Write a Mapbox Style Object based on a GeoStylerStyle.
*
* @param geoStylerStyle A GeoStylerStyle-Style
* @return A Mapbox Style object
*/
Expand Down Expand Up @@ -1515,6 +1515,7 @@ export class MapboxStyleParser implements StyleParser<Omit<MbStyle, 'sources'>>
size,
transform,
avoidEdges,
placement,
visibility
} = symbolizer;

Expand Down Expand Up @@ -1542,6 +1543,7 @@ export class MapboxStyleParser implements StyleParser<Omit<MbStyle, 'sources'>>
'text-offset': offset as SymbolLayout['text-offset'],
'text-allow-overlap': allowOverlap as SymbolLayout['text-allow-overlap'],
'text-optional': optional as SymbolLayout['text-optional'],
'symbol-placement': placement as SymbolLayout['symbol-placement'],
visibility: this.getVisibility(visibility)
};

Expand Down

0 comments on commit 9acb43f

Please sign in to comment.