|
1 | 1 | /**
|
2 |
| - * Globe coordinate detection global routines |
3 |
| - * Original source: http://simia.net/valueparser/coordinate.js |
4 |
| - * VERSION: 0.1 |
| 2 | + * Globe coordinate module |
5 | 3 | * @class globeCoordinate
|
6 | 4 | * @singleton
|
7 | 5 | * @license GPL-2.0+
|
8 |
| - * @author Denny Vrandečić |
9 |
| - * @author H. Snater < mediawiki@snater.com > |
10 | 6 | */
|
11 |
| -this.globeCoordinate = ( function() { |
12 |
| - 'use strict'; |
13 |
| - |
14 |
| - return { |
15 |
| - /** |
16 |
| - * Return a given decimal value applying a precision. |
17 |
| - * |
18 |
| - * @param {number} value |
19 |
| - * @param {number} precision |
20 |
| - * @return {number} |
21 |
| - */ |
22 |
| - toDecimal: function( value, precision ) { |
23 |
| - var logPrecision = Math.max( -9, Math.floor( Math.log( precision ) / Math.LN10 ) ), |
24 |
| - factor = Math.pow( 10, -1 * logPrecision ); |
25 |
| - |
26 |
| - return Math.round( value * factor ) / factor; |
27 |
| - }, |
28 |
| - |
29 |
| - /** |
30 |
| - * Returns a given decimal value converted to degree taking a precision into account. |
31 |
| - * |
32 |
| - * @param {number} value |
33 |
| - * @param {number} precision |
34 |
| - * @return {Object} Returned object has the following structure: |
35 |
| - * { |
36 |
| - * degree: {number}, |
37 |
| - * minute: {number|undefined}, |
38 |
| - * second: {number|undefined} |
39 |
| - * } |
40 |
| - * "minute" and/or "second" are undefined if not covered by the precision. |
41 |
| - */ |
42 |
| - toDegree: function( value, precision ) { |
43 |
| - var result = {}; |
44 |
| - |
45 |
| - value = Math.abs( value ); |
46 |
| - |
47 |
| - result.degree = Math.floor( value + 0.00000001 ); |
48 |
| - |
49 |
| - if( precision > 0.9999999999 ) { |
50 |
| - result.minute = undefined; |
51 |
| - } else { |
52 |
| - result.minute = Math.abs( Math.floor( ( value - result.degree + 0.000001 ) * 60 ) ); |
53 |
| - } |
54 |
| - |
55 |
| - if( precision > ( 0.9999999999 / 60 ) ) { |
56 |
| - result.second = undefined; |
57 |
| - } else { |
58 |
| - result.second = ( value - result.degree - result.minute / 60 ) * 3600; |
59 |
| - |
60 |
| - if( precision > ( 0.9999999999 / 3600 ) ) { |
61 |
| - result.second = Math.abs( Math.round( result.second ) ); |
62 |
| - } else if( precision > ( 0.9999999999 / 36000 ) ) { |
63 |
| - result.second = Math.abs( Math.round( result.second * 10 ) / 10 ); |
64 |
| - } else if( precision > ( 0.9999999999 / 360000 ) ) { |
65 |
| - result.second = Math.abs( Math.round( result.second * 100 ) / 100 ); |
66 |
| - } else { |
67 |
| - result.second = Math.abs( Math.round( result.second * 1000 ) / 1000 ); |
68 |
| - } |
69 |
| - } |
70 |
| - |
71 |
| - // TODO: precision might be a floating point number and might cause minutes/seconds |
72 |
| - // to be "generated". |
73 |
| - if( precision > 1 ) { |
74 |
| - result.degree = Math.round( result.degree / precision ) * precision; |
75 |
| - |
76 |
| - // JavaScript may cause some disturbance regarding rounding and precision. The |
77 |
| - // result should not have a higher floating point number precision than the |
78 |
| - // applied precision. |
79 |
| - var degreeFloat = String( result.degree ).split( '.' ), |
80 |
| - precisionFloat = String( precision ).split( '.' ); |
81 |
| - |
82 |
| - if( |
83 |
| - degreeFloat[1] && precisionFloat[1] |
84 |
| - && degreeFloat[1].length > precisionFloat[1].length |
85 |
| - ) { |
86 |
| - var trimmedPrecision = degreeFloat[1].substr( 0, precisionFloat[1].length ); |
87 |
| - result.degree = parseFloat( degreeFloat[0] + '.' + trimmedPrecision ); |
88 |
| - } |
89 |
| - } |
90 |
| - |
91 |
| - return result; |
92 |
| - }, |
93 |
| - |
94 |
| - /** |
95 |
| - * Returns a coordinate's ISO 6709 string representation. |
96 |
| - * |
97 |
| - * @param {Object} decimalCoordinateDefinition |
98 |
| - * Object with the following structure: |
99 |
| - * { |
100 |
| - * latitude: {number}, |
101 |
| - * longitude: {number}, |
102 |
| - * precision: {number} |
103 |
| - * } |
104 |
| - * @return {string} |
105 |
| - */ |
106 |
| - iso6709: function( decimalCoordinateDefinition ) { |
107 |
| - var latitude = decimalCoordinateDefinition.latitude, |
108 |
| - longitude = decimalCoordinateDefinition.longitude, |
109 |
| - precision = decimalCoordinateDefinition.precision, |
110 |
| - lat = globeCoordinate.toDegree( latitude, precision ), |
111 |
| - lon = globeCoordinate.toDegree( longitude, precision ), |
112 |
| - latISO, |
113 |
| - lonISO; |
114 |
| - |
115 |
| - /** |
116 |
| - * Strips a number's sign and fills the number's integer part with zeroes according to a |
117 |
| - * given string length. |
118 |
| - * |
119 |
| - * @param {number} number |
120 |
| - * @param {string} length |
121 |
| - */ |
122 |
| - function pad( number, length ) { |
123 |
| - var absolute = Math.abs( number || 0 ), |
124 |
| - string = String( absolute ), |
125 |
| - exploded = string.split( '.' ); |
126 |
| - |
127 |
| - if( exploded[0].length === length ) { |
128 |
| - return string; |
129 |
| - } |
130 |
| - |
131 |
| - return new Array( length - exploded[0].length + 1 ).join( '0' ) |
132 |
| - + exploded[0] |
133 |
| - + ( ( exploded[1] ) ? '.' + exploded[1] : '' ); |
134 |
| - } |
135 |
| - |
136 |
| - latISO = ( latitude < 0 ? '-' : '+' ) |
137 |
| - + pad( lat.degree, 2 ) |
138 |
| - + ( precision < 1 ? pad( lat.minute, 2 ) : '' ) |
139 |
| - + ( precision < 1 / 60 ? pad( lat.second, 2 ) : '' ); |
140 |
| - |
141 |
| - lonISO = ( longitude < 0 ? '-' : '+' ) |
142 |
| - + pad( lon.degree, 3 ) |
143 |
| - + ( precision < 1 ? pad( lon.minute, 2 ) : '' ) |
144 |
| - + ( precision < 1 / 60 ? pad( lon.second, 2 ) : '' ); |
145 |
| - |
146 |
| - // Synchronize precision (longitude degree needs to be 1 digit longer): |
147 |
| - if( lonISO.indexOf( '.' ) !== -1 && latISO.indexOf( '.' ) === -1 ) { |
148 |
| - latISO += '.'; |
149 |
| - } |
150 |
| - while( latISO.length < lonISO.length - 1 ) { |
151 |
| - latISO += '0'; |
152 |
| - } |
153 |
| - if( latISO.indexOf( '.' ) !== -1 && lonISO.indexOf( '.' ) === -1 ) { |
154 |
| - lonISO += '.'; |
155 |
| - } |
156 |
| - while( lonISO.length < latISO.length + 1 ) { |
157 |
| - lonISO += '0'; |
158 |
| - } |
159 |
| - |
160 |
| - return latISO + lonISO + '/'; |
161 |
| - } |
162 |
| - |
163 |
| - }; |
164 |
| - |
165 |
| -}() ); |
| 7 | +this.globeCoordinate = {}; |
0 commit comments