forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageMapLayer.js
More file actions
176 lines (154 loc) · 7.24 KB
/
ImageMapLayer.js
File metadata and controls
176 lines (154 loc) · 7.24 KB
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
import L from "leaflet";
import "../core/Base";
import {ServerGeometry, ServerType, CommonUtil, SecurityManager, Credential} from "@supermap/iclient-common" ;
/**
* @class L.supermap.imageMapLayer
* @classdesc SuperMap iServer 的 REST 地图服务的图层(SuperMap iServer Java 6R 及以上分块动态 REST 图层)。使用Image资源出图
* @extends L.TileLayer{@linkdoc-leaflet/#tilelayer}
* @example
* L.superMap.imageMapLayer(url).addTo(map);
* @param url -{string} 影像图层地址
* @param options -{Object} 影像图层可选参数。如:<br>
* layersID - {number}图层ID,如果有layersID,则是在使用专题图。<br>
* redirect - {boolean} 是否从定向,如果为 true,则将请求重定向到图片的真实地址;如果为 false,则响应体中是图片的字节流。<br>
* transparent - {number}设置透明度。<br>
* cacheEnabled - {boolean} 是否启用缓存。<br>
* clipRegionEnabled - {boolean} 是否启用地图裁剪。<br>
* prjCoordSys - {Object} 请求的地图的坐标参考系统。 如:prjCoordSys={"epsgCode":3857}。<br>
* overlapDisplayed - {boolean} 地图对象在同一范围内时,是否重叠显示。<br>
* overlapDisplayedOptions - {string} 避免地图对象压盖显示的过滤选项。<br>
* tileversion - {string} 切片版本名称,cacheEnabled 为 true 时有效。<br>
* crs - {{@link L.Proj.CRS}} 坐标系统类。<br>
* serverType - {{@link SuperMap.ServerType}} 服务来源 iServer|iPortal|online。<br>
* attribution - {string} 版权信息。
*/
export var ImageMapLayer = L.TileLayer.extend({
options: {
//如果有layersID,则是在使用专题图
layersID: null,
//如果为 true,则将请求重定向到图片的真实地址;如果为 false,则响应体中是图片的字节流
redirect: false,
transparent: null,
cacheEnabled: null,
clipRegionEnabled: false,
//请求的地图的坐标参考系统。 如:prjCoordSys= {"epsgCode":3857}。
prjCoordSys: null,
//地图对象在同一范围内时,是否重叠显示
overlapDisplayed: true,
//避免地图对象压盖显示的过滤选项
overlapDisplayedOptions: null,
//切片版本名称,cacheEnabled 为 true 时有效。
tileversion: null,
crs: null,
serverType: ServerType.ISERVER,
attribution: "Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> with <span>© <a href='http://iclient.supermap.io' target='_blank'>SuperMap iClient</a></span>"
},
initialize: function (url, options) {
this.url = this._url = url;
L.TileLayer.prototype.initialize.apply(this, arguments);
L.setOptions(this, options);
L.stamp(this);
},
/**
* @private
* @function L.supermap.imageMapLayer.prototype.onAdd
* @description 添加影像地图。
* @param map - {L.map} 待添加的影像地图参数
*/
onAdd: function (map) {
this._crs = this.options.crs || map.options.crs;
this._initLayerUrl();
L.TileLayer.prototype.onAdd.call(this, map);
},
/**
* @function L.supermap.imageMapLayer.prototype.getTileUrl
* @description 根据行列号获取切片地址
* @param coords - {Object} 行列号
* @return {String} 切片地址
*/
getTileUrl: function (coords) {
//使用ViewBounds出图
var tileBounds = this._tileCoordsToBounds(coords),
nw = this._crs.project(tileBounds.getNorthWest()),
se = this._crs.project(tileBounds.getSouthEast());
var params = "&viewBounds=" + "{\"leftBottom\" : {\"x\":" + nw.x + ",\"y\":" + se.y + "},\"rightTop\" : {\"x\":" + se.x + ",\"y\":" + nw.y + "}}";
return this._layerUrl + encodeURI(params);
},
_initLayerUrl: function () {
var me = this;
var layerUrl = me.url + "/image.png?";
layerUrl += encodeURI(me._initAllRequestParams().join('&'));
layerUrl = this._appendCredential(layerUrl);
this._layerUrl = layerUrl;
},
_initAllRequestParams: function () {
var me = this, options = me.options || {}, params = [];
var tileSize = this.options.tileSize;
if (!(tileSize instanceof L.Point)) {
tileSize = L.point(tileSize, tileSize);
}
params.push("width=" + tileSize.x);
params.push("height=" + tileSize.y);
var redirect = (options.redirect === true) ? options.redirect : false;
params.push("redirect=" + redirect);
var transparent = (options.transparent === true) ? options.transparent : false;
params.push("transparent=" + transparent);
var cacheEnabled = (options.cacheEnabled === false) ? options.cacheEnabled : true;
params.push("cacheEnabled=" + cacheEnabled);
if (options.prjCoordSys) {
params.push("prjCoordSys=" + JSON.stringify(options.prjCoordSys));
}
if (options.layersID) {
params.push("layersID=" + options.layersID);
}
if (options.clipRegionEnabled && options.clipRegion instanceof L.Path) {
options.clipRegion = L.Util.toSuperMapGeometry(options.clipRegion.toGeoJSON());
options.clipRegion = CommonUtil.toJSON(ServerGeometry.fromGeometry(options.clipRegion));
params.push("clipRegionEnabled=" + options.clipRegionEnabled);
params.push("clipRegion=" + JSON.stringify(options.clipRegion));
}
if (options.overlapDisplayed === false) {
params.push("overlapDisplayed=false");
if (options.overlapDisplayedOptions) {
params.push("overlapDisplayedOptions=" + me.overlapDisplayedOptions.toString());
}
} else {
params.push("overlapDisplayed=true");
}
if (options.cacheEnabled === true && options.tileversion) {
params.push("tileversion=" + options.tileversion)
}
return params;
},
//追加token或key
_appendCredential: function (url) {
var newUrl = url, credential, value;
switch (this.options.serverType) {
case ServerType.IPORTAL:
value = SecurityManager.getToken(url);
credential = value ? new Credential(value, "token") : null;
if (!credential) {
value = SecurityManager.getKey(url);
credential = value ? new Credential(value, "key") : null;
}
break;
case ServerType.ONLINE:
value = SecurityManager.getKey(url);
credential = value ? new Credential(value, "key") : null;
break;
default:
//iserver or others
value = SecurityManager.getToken(url);
credential = value ? new Credential(value, "token") : null;
break;
}
if (credential) {
newUrl += "&" + credential.getUrlParameters();
}
return newUrl;
}
});
export var imageMapLayer = function (url, options) {
return new ImageMapLayer(url, options);
};
L.supermap.imageMapLayer = imageMapLayer;