-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (107 loc) · 2.83 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
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
module.exports = WMSSource;
var debug = require("debug")("wms");
var request = require("retry-request", {
request: require("request")
});
var url = require("url");
var tilelive = require("@mapbox/tilelive");
var SphericalMercator = require("@mapbox/sphericalmercator");
var retry_options = {
retries: 4,
factor: 1.7
};
var merc = new SphericalMercator({
size: 256
});
var SUPPORTED_FORMATS = {
jpg: {
mime: "image/jpeg"
},
png: {
mime: "image/png"
}
};
function WMSSource(uri, callback) {
var parsed_uri = url.parse(uri, true);
this.name = "WMSSource";
this.minzoom = parseInt(parsed_uri.query.minzoom || "0");
this.maxzoom = parseInt(parsed_uri.query.maxzoom || "14");
this.scale = parseInt(parsed_uri.query.scale || "1");
this.tilesize = parseInt(parsed_uri.query.baseTileSize || "256");
this.format = parsed_uri.query.format || "png";
this.server_root = parsed_uri.query["source"];
this.options = parsed_uri.query;
delete this.options.source;
delete this.options.minzoom;
delete this.options.maxzoom;
delete this.options.scale;
delete this.options.baseTileSize;
delete this.options.format;
callback(null, this);
}
WMSSource.prototype.getTile = function(z, x, y, callback) {
var tile_bbox = merc.bbox(x, y, z, false, "900913");
var tilesize = this.tilesize * this.scale;
var format = this.format;
var params = {
width: tilesize,
height: tilesize,
format: SUPPORTED_FORMATS[format].mime,
bbox: tile_bbox.join(","),
srs: "EPSG:3857",
service: "WMS",
request: "GetMap",
version: "1.1.1",
bboxSR:'900913',
layers: "",
styles: "",
transparent: false,
...this.options
};
debug(`getTile ${z}/${x}/${y} params:${JSON.stringify(params)}`);
request(
{
url: this.server_root,
encoding: null,
qs: params
},
retry_options,
function(error, response, body) {
debug(
response
);
if (error) {
return callback(error);
}
switch (response.statusCode) {
case 200:
return callback(null, body, {
"content-type": SUPPORTED_FORMATS[format].mime
});
default:
console.warn(
"WMS URL " +
response.request.href +
" returned HTTP status code " +
response.statusCode +
", tile will be skipped"
);
return callback(new Error("Tile does not exist"));
}
}
);
};
WMSSource.prototype.getInfo = function(callback) {
callback(null, {
name: this.name,
minzoom: this.minzoom,
maxzoom: this.maxzoom,
center: [-119.4835, 37.8042, 12],
bounds: [-180, -85.0511, 180, 85.0511],
format: this.format
});
};
WMSSource.registerProtocols = function(tilelive) {
tilelive.protocols["wms:"] = WMSSource;
};
WMSSource.registerProtocols(tilelive);