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
|
export const loadImage = (url) => {
return new Promise(imageLoaded => {
const image = new Image();
image.onload = () => imageLoaded(image);
image.src = url;
});
};
export const loadIcon = async (key, background, url) => {
const image = await loadImage(url);
const pixelRatio = window.devicePixelRatio;
const canvas = document.createElement('canvas');
canvas.width = background.width * pixelRatio;
canvas.height = background.height * pixelRatio;
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);
const iconRatio = 0.5;
const imageWidth = canvas.width * iconRatio;
const imageHeight = canvas.height * iconRatio;
context.drawImage(image, (canvas.width - imageWidth) / 2, (canvas.height - imageHeight) / 2, imageWidth, imageHeight);
return context.getImageData(0, 0, canvas.width, canvas.height);
};
export const calculateBounds = features => {
if (features && features.length) {
const first = features[0].geometry.coordinates;
const bounds = [[...first], [...first]];
for (let feature of features) {
const longitude = feature.geometry.coordinates[0]
const latitude = feature.geometry.coordinates[1]
if (longitude < bounds[0][0]) {
bounds[0][0] = longitude;
} else if (longitude > bounds[1][0]) {
bounds[1][0] = longitude;
}
if (latitude < bounds[0][1]) {
bounds[0][1] = latitude;
} else if (latitude > bounds[1][1]) {
bounds[1][1] = latitude;
}
}
return bounds;
} else {
return null;
}
}
export const reverseCoordinates = it => {
if (!it) {
return it;
} else if (Array.isArray(it)) {
if (it.length === 2 && !Number.isNaN(it[0]) && !Number.isNaN(it[1])) {
return [it[1], it[0]];
} else {
return it.map(it => reverseCoordinates(it));
}
} else {
return {
...it,
coordinates: reverseCoordinates(it.coordinates),
}
}
}
|