UNPKG

5.77 kBJavaScriptView Raw
1goog.require('ol.Map');
2goog.require('ol.View');
3goog.require('ol.control');
4goog.require('ol.control.ScaleLine');
5goog.require('ol.layer.Tile');
6goog.require('ol.proj');
7goog.require('ol.proj.Projection');
8goog.require('ol.source.TileWMS');
9
10
11// By default OpenLayers does not know about the EPSG:21781 (Swiss) projection.
12// So we create a projection instance for EPSG:21781 and pass it to
13// ol.proj.addProjection to make it available to the library for lookup by its
14// code.
15
16var projection = new ol.proj.Projection({
17 code: 'EPSG:21781',
18 // The extent is used to determine zoom level 0. Recommended values for a
19 // projection's validity extent can be found at https://epsg.io/.
20 extent: [485869.5728, 76443.1884, 837076.5648, 299941.7864],
21 units: 'm'
22});
23ol.proj.addProjection(projection);
24
25// We also declare EPSG:21781/EPSG:4326 transform functions. These functions
26// are necessary for the ScaleLine control and when calling ol.proj.transform
27// for setting the view's initial center (see below).
28
29ol.proj.addCoordinateTransforms('EPSG:4326', projection,
30 function(coordinate) {
31 return [
32 WGStoCHy(coordinate[1], coordinate[0]),
33 WGStoCHx(coordinate[1], coordinate[0])
34 ];
35 },
36 function(coordinate) {
37 return [
38 CHtoWGSlng(coordinate[0], coordinate[1]),
39 CHtoWGSlat(coordinate[0], coordinate[1])
40 ];
41 });
42
43var extent = [420000, 30000, 900000, 350000];
44var layers = [
45 new ol.layer.Tile({
46 extent: extent,
47 source: new ol.source.TileWMS({
48 url: 'https://wms.geo.admin.ch/',
49 crossOrigin: 'anonymous',
50 attributions: '© <a href="http://www.geo.admin.ch/internet/geoportal/' +
51 'en/home.html">Pixelmap 1:1000000 / geo.admin.ch</a>',
52 params: {
53 'LAYERS': 'ch.swisstopo.pixelkarte-farbe-pk1000.noscale',
54 'FORMAT': 'image/jpeg'
55 },
56 serverType: 'mapserver'
57 })
58 }),
59 new ol.layer.Tile({
60 extent: extent,
61 source: new ol.source.TileWMS({
62 url: 'https://wms.geo.admin.ch/',
63 crossOrigin: 'anonymous',
64 attributions: '© <a href="http://www.geo.admin.ch/internet/geoportal/' +
65 'en/home.html">National parks / geo.admin.ch</a>',
66 params: {'LAYERS': 'ch.bafu.schutzgebiete-paerke_nationaler_bedeutung'},
67 serverType: 'mapserver'
68 })
69 })
70];
71
72var map = new ol.Map({
73 controls: ol.control.defaults().extend([
74 new ol.control.ScaleLine({
75 units: 'metric'
76 })
77 ]),
78 layers: layers,
79 target: 'map',
80 view: new ol.View({
81 projection: projection,
82 center: ol.proj.transform([8.23, 46.86], 'EPSG:4326', 'EPSG:21781'),
83 extent: extent,
84 zoom: 2
85 })
86});
87
88
89/*
90 * Swiss projection transform functions downloaded from
91 * http://www.swisstopo.admin.ch/internet/swisstopo/en/home/products/software/products/skripts.html
92 */
93
94// Convert WGS lat/long (° dec) to CH y
95function WGStoCHy(lat, lng) {
96
97 // Converts degrees dec to sex
98 lat = DECtoSEX(lat);
99 lng = DECtoSEX(lng);
100
101 // Converts degrees to seconds (sex)
102 lat = DEGtoSEC(lat);
103 lng = DEGtoSEC(lng);
104
105 // Axiliary values (% Bern)
106 var lat_aux = (lat - 169028.66) / 10000;
107 var lng_aux = (lng - 26782.5) / 10000;
108
109 // Process Y
110 var y = 600072.37 +
111 211455.93 * lng_aux -
112 10938.51 * lng_aux * lat_aux -
113 0.36 * lng_aux * Math.pow(lat_aux, 2) -
114 44.54 * Math.pow(lng_aux, 3);
115
116 return y;
117}
118
119// Convert WGS lat/long (° dec) to CH x
120function WGStoCHx(lat, lng) {
121
122 // Converts degrees dec to sex
123 lat = DECtoSEX(lat);
124 lng = DECtoSEX(lng);
125
126 // Converts degrees to seconds (sex)
127 lat = DEGtoSEC(lat);
128 lng = DEGtoSEC(lng);
129
130 // Axiliary values (% Bern)
131 var lat_aux = (lat - 169028.66) / 10000;
132 var lng_aux = (lng - 26782.5) / 10000;
133
134 // Process X
135 var x = 200147.07 +
136 308807.95 * lat_aux +
137 3745.25 * Math.pow(lng_aux, 2) +
138 76.63 * Math.pow(lat_aux, 2) -
139 194.56 * Math.pow(lng_aux, 2) * lat_aux +
140 119.79 * Math.pow(lat_aux, 3);
141
142 return x;
143
144}
145
146
147// Convert CH y/x to WGS lat
148function CHtoWGSlat(y, x) {
149
150 // Converts militar to civil and to unit = 1000km
151 // Axiliary values (% Bern)
152 var y_aux = (y - 600000) / 1000000;
153 var x_aux = (x - 200000) / 1000000;
154
155 // Process lat
156 var lat = 16.9023892 +
157 3.238272 * x_aux -
158 0.270978 * Math.pow(y_aux, 2) -
159 0.002528 * Math.pow(x_aux, 2) -
160 0.0447 * Math.pow(y_aux, 2) * x_aux -
161 0.0140 * Math.pow(x_aux, 3);
162
163 // Unit 10000" to 1 " and converts seconds to degrees (dec)
164 lat = lat * 100 / 36;
165
166 return lat;
167
168}
169
170// Convert CH y/x to WGS long
171function CHtoWGSlng(y, x) {
172
173 // Converts militar to civil and to unit = 1000km
174 // Axiliary values (% Bern)
175 var y_aux = (y - 600000) / 1000000;
176 var x_aux = (x - 200000) / 1000000;
177
178 // Process long
179 var lng = 2.6779094 +
180 4.728982 * y_aux +
181 0.791484 * y_aux * x_aux +
182 0.1306 * y_aux * Math.pow(x_aux, 2) -
183 0.0436 * Math.pow(y_aux, 3);
184
185 // Unit 10000" to 1 " and converts seconds to degrees (dec)
186 lng = lng * 100 / 36;
187
188 return lng;
189
190}
191
192
193// Convert DEC angle to SEX DMS
194function DECtoSEX(angle) {
195
196 // Extract DMS
197 var deg = parseInt(angle, 10);
198 var min = parseInt((angle - deg) * 60, 10);
199 var sec = (((angle - deg) * 60) - min) * 60;
200
201 // Result in degrees sex (dd.mmss)
202 return deg + min / 100 + sec / 10000;
203
204}
205
206// Convert Degrees angle to seconds
207function DEGtoSEC(angle) {
208
209 // Extract DMS
210 var deg = parseInt(angle, 10);
211 var min = parseInt((angle - deg) * 100, 10);
212 var sec = (((angle - deg) * 100) - min) * 100;
213
214 // Avoid rounding problems with seconds=0
215 var parts = String(angle).split('.');
216 if (parts.length == 2 && parts[1].length == 2) {
217 min = Number(parts[1]);
218 sec = 0;
219 }
220
221 // Result in degrees sex (dd.mmss)
222 return sec + min * 60 + deg * 3600;
223
224}