diff options
Diffstat (limited to 'src/dumbyUtils.mjs')
-rw-r--r-- | src/dumbyUtils.mjs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/dumbyUtils.mjs b/src/dumbyUtils.mjs index 8bd4423..7b06840 100644 --- a/src/dumbyUtils.mjs +++ b/src/dumbyUtils.mjs | |||
@@ -1,5 +1,6 @@ | |||
1 | import LeaderLine from 'leader-line' | 1 | import LeaderLine from 'leader-line' |
2 | import { insideWindow, insideParent } from './utils' | 2 | import { insideWindow, insideParent } from './utils' |
3 | import proj4 from 'proj4' | ||
3 | 4 | ||
4 | export const coordPattern = /^geo:([-]?[0-9.]+),([-]?[0-9.]+)/ | 5 | export const coordPattern = /^geo:([-]?[0-9.]+),([-]?[0-9.]+)/ |
5 | 6 | ||
@@ -333,3 +334,52 @@ export const addAnchorByPoint = ({ | |||
333 | 334 | ||
334 | return { ref: anchorName, link, title: desc } | 335 | return { ref: anchorName, link, title: desc } |
335 | } | 336 | } |
337 | |||
338 | /** | ||
339 | * setGeoSchemeByCRS. | ||
340 | * @description Add more information into Anchor Element within Geo Scheme by CRS | ||
341 | * @param {String} crs - EPSG/ESRI Code for CRS | ||
342 | * @return {Function} - Function for link | ||
343 | */ | ||
344 | export const setGeoSchemeByCRS = (crs) => (link) => { | ||
345 | const transform = proj4(crs, 'EPSG:4326') | ||
346 | const params = new URLSearchParams(link.search) | ||
347 | let xy = params.get('xy')?.split(',')?.map(Number) | ||
348 | |||
349 | // Set coords for Geo Scheme | ||
350 | if (link.href.startsWith('geo:0,0')) { | ||
351 | if (!xy) return null | ||
352 | |||
353 | const [lon, lat] = transform.forward(xy) | ||
354 | .map(value => parseFloat(value.toFixed(6))) | ||
355 | link.href = `geo:${lat},${lon}` | ||
356 | } | ||
357 | |||
358 | const [lat, lon] = link.href | ||
359 | .match(coordPattern) | ||
360 | .slice(1) | ||
361 | .map(Number) | ||
362 | |||
363 | if (!xy) { | ||
364 | xy = transform.inverse([lon, lat]) | ||
365 | params.set('xy', xy) | ||
366 | } | ||
367 | |||
368 | // set query strings | ||
369 | params.set('crs', crs) | ||
370 | params.set('q', `${lat},${lon}`) | ||
371 | link.search = params | ||
372 | |||
373 | const unit = proj4(crs).oProj.units | ||
374 | const invalidDegree = unit === 'degrees' && ( | ||
375 | (lon > 180 || lon < -180 || lat > 90 || lat < -90) || | ||
376 | (xy.every(v => v.toString().length < 3)) | ||
377 | ) | ||
378 | const invalidMeter = unit === 'm' && xy.find(v => v < 100) | ||
379 | if (invalidDegree || invalidMeter) { | ||
380 | link.replaceWith(document.createTextNode(link.textContent)) | ||
381 | return null | ||
382 | } | ||
383 | |||
384 | return link | ||
385 | } | ||