-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
68 lines (58 loc) · 2.14 KB
/
index.js
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
'use strict'
const proj4 = require('proj4')
const epsg = require('epsg')
const isObject = require('lodash.isobject')
const isNumber = require('lodash.isnumber')
const epsgFromZone = (zone) => 'EPSG:'+(31464+zone) // zone 2 = 31466, zone 5 = 31469
const toWGS = (coordinates) => {
if(!isObject(coordinates) || !isNumber(coordinates.x) || !isNumber(coordinates.y)){
throw new Error('missing or invalid parameter `coordinates`')
}
if(coordinates.x < 0 || coordinates.x >= Math.pow(10, 7)){
throw new Error('`coordinates.x` out of bounds')
}
if(coordinates.y < 0 || coordinates.y >= Math.pow(10, 7)){
throw new Error('`coordinates.y` out of bounds')
}
const zone = +(coordinates.x+'')[0]
const projected = proj4.default(epsg[epsgFromZone(zone)], 'WGS84', Object.assign({}, coordinates))
return ({
longitude: projected.x,
latitude: projected.y
})
}
const toGK = (coordinates, zone) => {
if(!isObject(coordinates) || !isNumber(coordinates.longitude) || !isNumber(coordinates.latitude)){
throw new Error('missing or invalid parameter `coordinates`')
}
if(coordinates.longitude < -180 || coordinates.longitude > 180){
throw new Error('`coordinates.longitude` out of bounds')
}
if(coordinates.latitude < -360 || coordinates.latitude > 360){
throw new Error('`coordinates.latitude` out of bounds')
}
if(zone){
if(!isNumber(zone)){
throw new Error('`zone` parameter must be a number')
}
if(![2,3,4,5].includes(zone)){
throw new Error('if set, `zone` parameter must be one of 2, 3, 4, 5')
}
}
else{
zone = Math.floor((+coordinates.longitude + 1.5) / 3)
if(![2,3,4,5].includes(zone)){
throw new Error('could not detect valid zone (2, 3, 4, 5) from input coordinates')
}
}
const proj4coordinates = {
x: coordinates.longitude,
y: coordinates.latitude
}
const projected = proj4('WGS84', epsg[epsgFromZone(zone)], proj4coordinates)
return ({
x: projected.x,
y: projected.y
})
}
module.exports = {toWGS, toGK}