1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
import { parse, stringify } from 'wellknown';
import circle from '@turf/circle';
export const loadImage = (url) => new Promise((imageLoaded) => {
const image = new Image();
image.onload = () => imageLoaded(image);
image.src = url;
});
const canvasTintImage = (image, color) => {
const canvas = document.createElement('canvas');
canvas.width = image.width * devicePixelRatio;
canvas.height = image.height * devicePixelRatio;
canvas.style.width = `${image.width}px`;
canvas.style.height = `${image.height}px`;
const context = canvas.getContext('2d');
context.save();
context.fillStyle = color;
context.globalAlpha = 1;
context.fillRect(0, 0, canvas.width, canvas.height);
context.globalCompositeOperation = 'destination-atop';
context.globalAlpha = 1;
context.drawImage(image, 0, 0, canvas.width, canvas.height);
context.restore();
return canvas;
};
export const prepareIcon = (background, icon, color) => {
const canvas = document.createElement('canvas');
canvas.width = background.width * devicePixelRatio;
canvas.height = background.height * devicePixelRatio;
canvas.style.width = `${background.width}px`;
canvas.style.height = `${background.height}px`;
const context = canvas.getContext('2d');
context.drawImage(background, 0, 0, canvas.width, canvas.height);
if (icon) {
const iconRatio = 0.5;
const imageWidth = canvas.width * iconRatio;
const imageHeight = canvas.height * iconRatio;
if (navigator.userAgent.indexOf('Firefox') > 0) {
context.drawImage(icon, (canvas.width - imageWidth) / 2, (canvas.height - imageHeight) / 2, imageWidth, imageHeight);
} else {
context.drawImage(canvasTintImage(icon, color), (canvas.width - imageWidth) / 2, (canvas.height - imageHeight) / 2, imageWidth, imageHeight);
}
}
return context.getImageData(0, 0, canvas.width, canvas.height);
};
export const reverseCoordinates = (it) => {
if (!it) {
return it;
} if (Array.isArray(it)) {
if (it.length === 2 && !Number.isNaN(it[0]) && !Number.isNaN(it[1])) {
return [it[1], it[0]];
}
return it.map((it) => reverseCoordinates(it));
}
return {
...it,
coordinates: reverseCoordinates(it.coordinates),
};
};
export const geofenceToFeature = (theme, item) => {
let geometry;
if (item.area.indexOf('CIRCLE') > -1) {
const coordinates = item.area.replace(/CIRCLE|\(|\)|,/g, ' ').trim().split(/ +/);
const options = { steps: 32, units: 'meters' };
const polygon = circle([Number(coordinates[1]), Number(coordinates[0])], Number(coordinates[2]), options);
geometry = polygon.geometry;
} else {
geometry = reverseCoordinates(parse(item.area));
}
return {
id: item.id,
type: 'Feature',
geometry,
properties: {
name: item.name,
color: item.attributes.color || theme.palette.colors.geometry,
},
};
};
export const geometryToArea = (geometry) => stringify(reverseCoordinates(geometry));
export const findFonts = (map) => {
const fontSet = new Set();
const { layers } = map.getStyle();
layers?.forEach?.((layer) => {
layer.layout?.['text-font']?.forEach?.(fontSet.add, fontSet);
});
const availableFonts = [...fontSet];
const regularFont = availableFonts.find((it) => it.includes('Regular'));
if (regularFont) {
return [regularFont];
}
const anyFont = availableFonts.find(Boolean);
if (anyFont) {
return [anyFont];
}
return ['Roboto Regular'];
};
|