Skip to content

Commit 4f1f34f

Browse files
committed
Add labels on geometries & show them in the various examples
1 parent a01fd8c commit 4f1f34f

File tree

7 files changed

+47
-2
lines changed

7 files changed

+47
-2
lines changed

cases/common.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
trackPerformance,
77
// @ts-ignore
88
} from '@camptocamp/rendering-analyzer';
9+
import randomName from '@scaleway/random-name';
910
import lilGui from 'lil-gui';
1011
import Map from 'ol/Map.js';
1112
import View from 'ol/View.js';
@@ -164,11 +165,13 @@ export function generatePolygons(count, numVertices) {
164165
// Close the polygon by adding the first vertex at the end
165166
polygonCoordinates.push(polygonCoordinates[0]);
166167

168+
const label = `This area covers ${randomName()}`;
167169
features.push({
168170
type: 'Feature',
169171
properties: {
170172
color: getRandomPaletteColor(),
171173
ratio: Math.round(Math.random() * 100),
174+
label,
172175
},
173176
geometry: {
174177
type: 'Polygon',
@@ -197,11 +200,13 @@ export function generatePoints(count, radius) {
197200
for (let lon = -180; lon < 180 - size / 4; lon += size) {
198201
for (let lat = -90; lat < 90 - size / 4; lat += size) {
199202
const buffer = (0.3 + Math.random() * 0.2) * size * (radius / 5); // Increase the buffer for larger points
203+
const label = randomName();
200204
features.push({
201205
type: 'Feature',
202206
properties: {
203207
color: getRandomPaletteColor(),
204208
radius,
209+
label,
205210
},
206211
geometry: {
207212
type: 'Point',
@@ -254,11 +259,13 @@ export function generateLines(lineCount, curveComplexity, width) {
254259
}
255260
coordinates.push(...singleCurve);
256261
}
262+
const label = `This leads to ${randomName()}`;
257263
features.push({
258264
type: 'Feature',
259265
properties: {
260266
color: getRandomPaletteColor(), // Use deterministic color selection
261267
width,
268+
label,
262269
},
263270
geometry: {
264271
type: 'LineString',

cases/line-rendering/main.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ const style = {
2323
'stroke-width': ['get', 'width'],
2424
'stroke-color': ['get', 'color'],
2525
'stroke-line-dash': [15, 15],
26+
'text-value': ['get', 'label'],
27+
'text-font': 'bold 12px "Open Sans", "Arial Unicode MS", sans-serif',
28+
'text-fill-color': '#333',
29+
'text-stroke-color': 'rgba(255,255,255,0.8)',
30+
'text-stroke-width': 2,
31+
'text-placement': 'line',
32+
'text-repeat': 2000,
2633
};
2734

2835
/**

cases/point-rendering/main.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ const source = new VectorSource({
1919
*/
2020
const style = {
2121
// This has to be fixed upstream
22-
// @ts-ignore
2322
'circle-radius': ['get', 'radius'],
2423
'circle-fill-color': ['get', 'color'],
2524
'circle-stroke-color': 'gray',
2625
'circle-stroke-width': 0.5,
26+
'text-value': ['get', 'label'],
27+
'text-font': 'bold 12px "Open Sans", "Arial Unicode MS", sans-serif',
28+
'text-fill-color': '#333',
29+
'text-stroke-color': 'rgba(255,255,255,0.8)',
30+
'text-stroke-width': 2,
31+
'text-offset-y': -12,
2732
};
2833

2934
/**

cases/polygon-rendering/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ const source = new VectorSource({
2020
*/
2121
const style = {
2222
'fill-color': ['get', 'color'],
23+
'text-value': ['get', 'label'],
24+
'text-font': 'bold 12px "Open Sans", "Arial Unicode MS", sans-serif',
25+
'text-fill-color': '#333',
26+
'text-stroke-color': 'rgba(255,255,255,0.8)',
27+
'text-stroke-width': 2,
2328
};
2429

2530
/**

cases/vector-tiles-rendering/main.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ function makeData(
115115
(width + height) / 4 / (Math.ceil(Math.sqrt(countPoints)) + 1);
116116

117117
// Generate polygons on the left bottom corner
118+
let number = 1;
118119
for (let lon = bbox[0] + gridSpacing; lon < centerLon; lon += gridSpacing) {
119120
for (let lat = bbox[1] + gridSpacing; lat < centerLat; lat += gridSpacing) {
120121
const buffer = (0.3 + Math.random() * 0.2) * gridSpacing;
@@ -129,11 +130,13 @@ function makeData(
129130
polygonCoordinates.push([x, y]);
130131
}
131132
polygonCoordinates.push(polygonCoordinates[0]);
133+
const label = `polygon n°${number++}`;
132134

133135
features.push({
134136
type: 'Feature',
135137
properties: {
136138
propValue: propValues[Math.floor(Math.random() * propValues.length)],
139+
label,
137140
},
138141
geometry: {
139142
type: 'Polygon',
@@ -148,6 +151,7 @@ function makeData(
148151
type: 'Feature',
149152
properties: {
150153
propValue: propValues[Math.floor(Math.random() * propValues.length)],
154+
label: '',
151155
},
152156
geometry: {
153157
type: 'LineString',
@@ -162,14 +166,17 @@ function makeData(
162166
});
163167

164168
// Generate points on the right top corner
169+
number = 1;
165170
for (let lon = centerLon + gridSpacing; lon < bbox[2]; lon += gridSpacing) {
166171
for (let lat = bbox[1] + gridSpacing; lat < centerLat; lat += gridSpacing) {
167172
const point = [lon, lat];
173+
const label = `point n°${number++}`;
168174

169175
features.push({
170176
type: 'Feature',
171177
properties: {
172178
propValue: propValues[Math.floor(Math.random() * propValues.length)],
179+
label,
173180
},
174181
geometry: {
175182
type: 'Point',
@@ -190,6 +197,7 @@ function makeData(
190197
*/
191198
let singleCurve = []; // Create a singleCurve array outside the loop
192199

200+
number = 1;
193201
for (let j = 0; j < countLines; j++) {
194202
const coordinates = [];
195203
for (let i = 0; i < periodCount; i++) {
@@ -207,11 +215,13 @@ function makeData(
207215
}
208216
coordinates.push(...singleCurve);
209217
}
218+
const label = `line n°${number++}`;
210219

211220
features.push({
212221
type: 'Feature',
213222
properties: {
214223
propValue: propValues[Math.floor(Math.random() * propValues.length)],
224+
label,
215225
},
216226
geometry: {
217227
type: 'LineString',

package-lock.json

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"dependencies": {
2929
"@camptocamp/rendering-analyzer": "^0.2.0",
30+
"@scaleway/random-name": "^5.1.2",
3031
"ol": "10.6.1"
3132
}
3233
}

0 commit comments

Comments
 (0)