Styling and labels · Angular, React and OpenLayers
Rule-based flat styles with expressions
Style 1,027 Polish towns declaratively in OpenLayers with data-driven flat style expressions for rank-based circle radii and population labels.

What this example shows
Against a dark-blue background with Poland’s landmass filled in neutral grey, 1,027 Polish towns render as circles scaled across eight sizes from their rank attribute, while hierarchical population rules label cities above 500,000 inhabitants with City, voivodeship at 16 px and towns above 100,000 at 12 px—decluttered cleanly as you zoom.
When you need it
User-editable GIS style builders, database-stored cartography themes and CMS-configured maps need JSON-serialisable styling rules rather than arbitrary JavaScript closures.
OpenLayers’ expression-based flat styles (get expressions, concat expressions, rule filters and else clauses) express multi-tier thematic cartography purely as plain data.
The code
The example uses cc-openlayers, my Angular component library for OpenLayers: each aol-… element creates the OpenLayers object it names, updates it from its inputs and disposes of it with the component.
style-expressions.tsimport { ChangeDetectionStrategy, Component } from '@angular/core';
import GeoJSON from 'ol/format/GeoJSON';
import type { FlatStyle, Rule } from 'ol/style/flat';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerVectorComponent,
MapComponent,
SourceVectorComponent,
ViewComponent,
} from 'cc-openlayers';
/** Labels in white, outlined in gray. */
const LABEL: FlatStyle = { 'text-fill-color': '#ffffff', 'text-stroke-color': '#808080', 'text-stroke-width': 2 };
/**
* Flat styles with expressions over Poland's towns (OpenStreetMap): a circle sized by each town's `rank` property, and
* labels chosen by rules: a city over 500,000 people named with its voivodeship, one over 100,000 by its name alone.
* The colours are the original's names in hex (darkgray, gray, white).
*/
@Component({
selector: 'app-style-expressions',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerVectorComponent,
MapComponent,
SourceVectorComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<aol-view [zoom]="5.5">
<aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
</aol-view>
<aol-layer-vector background="#1a2b39" [style]="{ 'fill-color': '#a9a9a9' }">
<aol-source-vector url="assets/poland/poland.geojson" [format]="format" attributions="Boundaries: PRG, GUGiK" />
</aol-layer-vector>
<aol-layer-vector [style]="towns">
<aol-source-vector
url="assets/poland/towns.geojson"
[format]="format"
attributions='Towns © <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors'
/>
</aol-layer-vector>
<aol-layer-vector [declutter]="true" [style]="labels">
<aol-source-vector url="assets/poland/towns.geojson" [format]="format" />
</aol-layer-vector>
</aol-map>
`,
})
export class StyleExpressions {
protected readonly format = new GeoJSON();
protected readonly towns: FlatStyle = {
'circle-radius': ['get', 'rank'],
'circle-fill-color': '#808080',
'circle-stroke-color': '#ffffff',
'circle-stroke-width': 0.5,
};
protected readonly labels: Rule[] = [
{
filter: ['>', ['get', 'population'], 500_000],
style: {
'text-value': ['concat', ['get', 'name'], ', ', ['get', 'voivodeship']],
'text-font': '16px sans-serif',
...LABEL,
},
},
{
else: true,
filter: ['>', ['get', 'population'], 100_000],
style: { 'text-value': ['get', 'name'], 'text-font': '12px sans-serif', ...LABEL },
},
];
} The React version uses the library's React adapter, cc-openlayers/react: each component creates the OpenLayers object it names when it mounts, updates it from its props and disposes of it when it unmounts.
style-expressions.react.tsximport GeoJSON from 'ol/format/GeoJSON';
import type { FlatStyle, Rule } from 'ol/style/flat';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
Map,
VectorLayer,
VectorSource,
View,
} from 'cc-openlayers/react';
/** Labels in white, outlined in gray. */
const LABEL: FlatStyle = { 'text-fill-color': '#ffffff', 'text-stroke-color': '#808080', 'text-stroke-width': 2 };
const format = new GeoJSON();
const land: FlatStyle = { 'fill-color': '#a9a9a9' };
const towns: FlatStyle = {
'circle-radius': ['get', 'rank'],
'circle-fill-color': '#808080',
'circle-stroke-color': '#ffffff',
'circle-stroke-width': 0.5,
};
const labels: Rule[] = [
{
filter: ['>', ['get', 'population'], 500_000],
style: {
'text-value': ['concat', ['get', 'name'], ', ', ['get', 'voivodeship']],
'text-font': '16px sans-serif',
...LABEL,
},
},
{
else: true,
filter: ['>', ['get', 'population'], 100_000],
style: { 'text-value': ['get', 'name'], 'text-font': '12px sans-serif', ...LABEL },
},
];
/**
* Flat styles with expressions over Poland's towns (OpenStreetMap): a circle sized by each town's `rank` property, and
* labels chosen by rules: a city over 500,000 people named with its voivodeship, one over 100,000 by its name alone.
* The colours are the original's names in hex (darkgray, gray, white).
*/
export function StyleExpressions() {
return (
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={5.5}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<VectorLayer background="#1a2b39" style={land}>
<VectorSource url="assets/poland/poland.geojson" format={format} attributions="Boundaries: PRG, GUGiK" />
</VectorLayer>
<VectorLayer style={towns}>
<VectorSource
url="assets/poland/towns.geojson"
format={format}
attributions='Towns © <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors'
/>
</VectorLayer>
<VectorLayer declutter={true} style={labels}>
<VectorSource url="assets/poland/towns.geojson" format={format} />
</VectorLayer>
</Map>
);
}How the React version works
Poland's towns from OpenStreetMap as circles sized by their rank property, and labels chosen by rules: a city over 500,000 people with its voivodeship, one over 100,000 by its name; only the labels are decluttered.
What both versions share
OpenLayers renders the map and handles the interactions; the cc-openlayers/core layer gives both adapters the same view binding (MapViewBinding) and the same conversion of coordinates from EPSG:4326 to the map's projection.
From Angular to React
| Angular | React |
|---|---|
three <aol-layer-vector [style]> with flat styles | three <VectorLayer style> with the flat styles as module constants |
<aol-source-vector [url] [format]> | <VectorSource url format> |
Who creates, updates and disposes
Map creates the ol/Map in useLayoutEffect and disposes of it on unmount; each layer, source and interaction component adds its object to the parent it gets from context and removes it in its effect's cleanup, as ngOnDestroy does in Angular.
Architecture and OpenLayers mechanics
Three layers compose the map: Poland’s polygon with a literal grey flat [style], a dedicated non-decluttered circle layer where 'circle-radius': ['get', 'rank'] maps ranks 1–8 directly to pixel radii, and a separate decluttered label layer driven by an array of filtered rules.
On the label layer, cities exceeding 500,000 people build composite strings via ['concat', ['get', 'name'], ', ', ['get', 'voivodeship']], an else rule with a > 100,000 filter labels medium cities, and smaller towns fall through unlabelled.
How it works
- A literal flat
[style]paints Poland grey atop the map’s dark-bluebackground. 'circle-radius': ['get', 'rank']sizes 1,027 town markers dynamically from 1 to 8 pixels using data expressions.- Rule arrays format 500k+ cities at 16 px via
['concat', ['get', 'name'], ', ', ['get', 'voivodeship']]and 100k+ towns at 12 px via anelserule. - Keeping circles and decluttered labels on two separate layers ensures text collision removal never hides town dots.
Pitfalls
- An
elserule carrying its ownfiltermatches only when no earlier rule matched AND its own filter passes—correctly leaving towns of 100,000 or fewer inhabitants unlabelled. - If circles and text share a single decluttered layer, a wide city name will also hide neighbouring town circles—split persistent symbols onto an un-decluttered layer beneath the text layer.
- Because the dataset orders cities largest-first, decluttering naturally prioritises major metropolises at low zoom, where Warszawa comes first in the file and therefore wins collisions.
Related examples
The same feature in the OpenLayers examples .
Background: state and lifecycle of maps in Angular with OpenLayers, which the components of cc-openlayers take care of.