-
Notifications
You must be signed in to change notification settings - Fork 2
/
wmsMapType.js
181 lines (148 loc) · 3.84 KB
/
wmsMapType.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* wmsMapType.js
*
* Author: Beau Grantham
* http://www.nologs.org/
* https://github.com/beaugrantham/
*
* See here for license terms:
* http://opensource.org/licenses/MIT
*/
function WmsMapType(name, url, params, options) {
var TILE_SIZE = 256;
var EARTH_RADIUS_IN_METERS = 6378137;
var CIRCUMFERENCE = 2 * Math.PI * EARTH_RADIUS_IN_METERS;
this.name = name;
this.url = url;
this.tileSize = new google.maps.Size(TILE_SIZE, TILE_SIZE); // required by API
this.tiles = [ ]; // maintain managed tiles
/*
* Params representing key/value pairs included in the GetMap query.
*
* Set default values and then override as needed.
*/
this.params = {
// General
service: 'WMS',
version: '1.1.1',
request: 'GetMap',
// Image props
transparent: true,
format: 'image/png',
width: this.tileSize.width,
height: this.tileSize.height,
// Spatial Reference System
srs: 'EPSG:3857',
// Style
styles: '',
// Layers
layers: ''
};
for (var key in params) {
this.params[key] = params[key];
}
/*
* Extra options.
*
* Set default values and then override as needed.
*/
this.options = {
opacity: 0.5,
cache: false
};
for (var key in options) {
this.options[key] = options[key];
}
/*
* Prototype getTile method.
*/
this.getTile = function(coord, zoom, ownerDocument) {
if (!this.params['layers'].length) {
console.log("[WmsMapType] Required param 'layers' is empty");
return ownerDocument.createElement('div'); // empty div
}
var url = this.url + "?";
for (var key in this.params) {
url += key + "=" + this.params[key] + "&";
}
var bounds = getBounds(coord.x, coord.y, zoom);
url += 'bbox=' + bounds.swX + "," + bounds.swY + "," + bounds.neX + "," + bounds.neY;
if (this.options['cache'] == false) {
var date = new Date();
url += "&cache=" + date.getTime();
}
var div = ownerDocument.createElement('div');
div.innerHTML = '<img src="' + url + '"/>';
div.style.width = this.tileSize.width + 'px';
div.style.height = this.tileSize.height + 'px';
div.style.opacity = this.options['opacity'];
this.tiles.push(div);
return div;
};
/*
* Add this MapType to a map at the given index, or on top of other layers
* if index is omitted.
*/
this.addToMap = function(map, index) {
if (index !== undefined) {
map.overlayMapTypes.insertAt(Math.min(index, map.overlayMapTypes.getLength()), this);
}
else {
map.overlayMapTypes.push(this);
}
};
/*
* Remove this MapType from a map.
*/
this.removeFromMap = function(map) {
var overlayTypes = map.overlayMapTypes;
for (var i = 0; i < overlayTypes.getLength(); i++) {
var element = overlayTypes.getAt(i);
if (element !== undefined && element === this) {
overlayTypes.removeAt(i);
break;
}
}
this.tiles = [ ];
};
/*
* Change opacity on demand.
*/
this.setOpacity = function(opacity) {
this.options['opacity'] = opacity;
for (var i in this.tiles) {
this.tiles[i].style.opacity = opacity;
}
}
/*
* ---------------
* Private methods
* ---------------
*/
/*
* Return the tile bounds for the given x, y, z values.
*/
function getBounds(x, y, z) {
y = Math.pow(2, z) - y - 1; // Translate Y value
var resolution = (CIRCUMFERENCE / TILE_SIZE) / Math.pow(2, z); // meters per pixel
var swPoint = getMercatorCoord(x, y, resolution);
var nePoint = getMercatorCoord(x + 1, y + 1, resolution);
var bounds = {
swX : swPoint.x,
swY : swPoint.y,
neX : nePoint.x,
neY : nePoint.y
};
return bounds;
};
/*
* Translate the xy & resolution to spherical mercator (EPSG:3857, EPSG:900913).
*/
function getMercatorCoord(x, y, resolution) {
var point = {
x: x * TILE_SIZE * resolution - CIRCUMFERENCE / 2.0,
y: y * TILE_SIZE * resolution - CIRCUMFERENCE / 2.0
};
return point;
};
}