diff options
author | Anton Tananaev <anton@traccar.org> | 2022-05-08 11:29:16 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-05-08 11:29:16 -0700 |
commit | 15d7000b16e90db68f52931cc16dfa3ebbd74114 (patch) | |
tree | ac336a1ea285fdbc536f71556e473767d6e3c8e2 /modern/src/map/core/mapUtil.js | |
parent | be0409f7e855cecceb4e38db611afd3beca255ff (diff) | |
download | trackermap-web-15d7000b16e90db68f52931cc16dfa3ebbd74114.tar.gz trackermap-web-15d7000b16e90db68f52931cc16dfa3ebbd74114.tar.bz2 trackermap-web-15d7000b16e90db68f52931cc16dfa3ebbd74114.zip |
Reorganize map folder
Diffstat (limited to 'modern/src/map/core/mapUtil.js')
-rw-r--r-- | modern/src/map/core/mapUtil.js | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/modern/src/map/core/mapUtil.js b/modern/src/map/core/mapUtil.js new file mode 100644 index 00000000..2aa86c68 --- /dev/null +++ b/modern/src/map/core/mapUtil.js @@ -0,0 +1,90 @@ +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 = (item) => { + 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); + return { + id: item.id, + type: 'Feature', + geometry: polygon.geometry, + properties: { name: item.name }, + }; + } + return { + id: item.id, + type: 'Feature', + geometry: reverseCoordinates(parse(item.area)), + properties: { name: item.name }, + }; +}; + +export const geometryToArea = (geometry) => stringify(reverseCoordinates(geometry)); |