-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
246 lines (225 loc) · 7.47 KB
/
utils.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Copyright ©2014 Esri. All rights reserved.
TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
Unpublished material - all rights reserved under the
Copyright Laws of the United States and applicable international
laws, treaties, and conventions.
For additional information, contact:
Attn: Contracts and Legal Department
Environmental Systems Research Institute, Inc.
380 New York Street
Redlands, California, 92373
USA
email: [email protected]
*/
define([
'dojo/_base/lang',
'dojo/Deferred',
'dojo/_base/array',
'dojo/promise/all',
'jimu/portalUtils',
'jimu/SpatialReference/wkidUtils',
'esri/request'
], function (lang, Deferred, array, all, portalUtils, SRUtils, esriRequest) {
var mo = {};
mo._loadPortalBaseMaps = function(portalUrl, curMapSpatialRefObj){
// map.spatial
//var curMapSpatialRefObj = this.map.spatialReference;
var defRet = new Deferred();
var deferreds = [];
portalUtils.getWebMapsFromBasemapGalleryGroup(portalUrl).then(function(response) {
var basemapItems = response.results;
array.forEach(basemapItems, function(basemapItem){
var def = new Deferred();
deferreds.push(def);
basemapItem.getItemData().then(function(basemapItemData) {
_getBasemapSpatialReference(basemapItem, basemapItemData)
.then(lang.hitch(this, function(basemapSpatialRef) {
var basemapLayers = [];
if (curMapSpatialRefObj &&
basemapSpatialRef &&
SRUtils.isSameSR(curMapSpatialRefObj.wkid, basemapSpatialRef.wkid)) {
basemapLayers = array.map(basemapItemData.baseMap.baseMapLayers, function(layerData){
//return {url: layerData.url};
return layerData;
});
var thumbnailUrl = null;
if (basemapItem.thumbnailUrl) {
var queryIndex = basemapItem.thumbnailUrl.indexOf('?');
if (queryIndex !== -1) {
thumbnailUrl = basemapItem.thumbnailUrl.slice(0, queryIndex);
} else {
thumbnailUrl = basemapItem.thumbnailUrl;
}
}
def.resolve({
layers: basemapLayers,
title: basemapItem.title,
thumbnailUrl: thumbnailUrl,
spatialReference: basemapSpatialRef
});
}else {
def.resolve({});
}
}));
});
});
all(deferreds).then(function(basemaps) {
defRet.resolve(basemaps);
});
}, function(err){
defRet.reject(err);
});
return defRet;
};
mo.compareSameBasemap = function(basemap1, basemap2) {
var basemap1Layers = basemap1.layers, basemap2Layers = basemap2.layers;
var basemap1UrlStr = '', basemap2UrlStr = '';
basemap1UrlStr = _allLayersUrlStr(basemap1Layers);
basemap2UrlStr = _allLayersUrlStr(basemap2Layers);
return (basemap1UrlStr === basemap2UrlStr);
};
mo.compareSameBasemapByOrder = function(basemap1, basemap2) {
var basemap1Layers = basemap1.layers, basemap2Layers = basemap2.layers;
if (basemap1Layers.length !== basemap2Layers.length) {
return false;
}
for(var i = 0; i < basemap1Layers.length; i++) {
// compare type;
if(!basemap1Layers[i].type) {
basemap1Layers[i].type = null;
}
if(!basemap2Layers[i].type) {
basemap2Layers[i].type = null;
}
if(basemap1Layers[i].type !== basemap2Layers[i].type) {
return false;
} else if(!basemap1Layers[i].type && !basemap1Layers[i].type) {
// two layers do not have type property;
// compare url;
if(!basemap1Layers[i].url) {
basemap1Layers[i].url = null;
}
if(!basemap2Layers[i].url) {
basemap2Layers[i].url = null;
}
if(_removeUrlQurey(basemap1Layers[i].url) !== _removeUrlQurey(basemap2Layers[i].url)) {
return false;
}
}
}
return true;
};
mo.isBingMap = function(basemap) {
if(!basemap || !basemap.layers) {
return false;
}
for(var i = 0; i < basemap.layers.length; i++) {
if (basemap.layers[i].type === "BingMapsAerial" ||
basemap.layers[i].type === "BingMapsRoad" ||
basemap.layers[i].type === "BingMapsHybrid") {
return true;
}
}
return false;
};
mo.isNoUrlLayerMap = function(basemap) {
if(!basemap || !basemap.layers) {
return false;
}
for(var i = 0; i < basemap.layers.length; i++) {
if (basemap.layers[i].type === "BingMapsAerial" ||
basemap.layers[i].type === "BingMapsRoad" ||
basemap.layers[i].type === "BingMapsHybrid" ||
basemap.layers[i].type === "OpenStreetMap") {
return true;
}
}
return false;
};
mo.getToken = function(portalUrl) {
var portal = portalUtils.getPortal(portalUrl);
portal.updateCredential();
return portal.credential ? "?token=" + portal.credential.token : "";
};
mo.removeUrlQuery = function(url) {
return _removeUrlQurey(url);
};
function _removeUrlQurey(url) {
if(url === null){
return url;
}
var queryIndex = url.indexOf('?');
var httpIndex = url.indexOf('http');
if (httpIndex === 0 && queryIndex !== -1) {
return url.slice(0, queryIndex);
} else {
return url;
}
}
function _allLayersUrlStr(layers) {
var urlArray = [];
array.forEach(layers, function(layer) {
urlArray.push(layer.url);
});
urlArray.sort();
var allLayersUrlStr = '';
var i = 0;
for(i = 0; i < urlArray.length; i++) {
var queryIndex = urlArray[i].indexOf('?');
var url = '';
if (queryIndex !== -1) {
url = urlArray[i].slice(0, queryIndex);
} else {
url = urlArray[i];
}
allLayersUrlStr += url;
}
return allLayersUrlStr.replace(/\ /, '');
}
function _isBasemapLayersHasTypeProperty(basemapLayers) {
var hasTypeProperty = false;
if(basemapLayers) {
for(var i = 0; i < basemapLayers.length; i++) {
if (basemapLayers[i].type) {
hasTypeProperty = true;
break;
}
}
}
return hasTypeProperty;
}
function _getBasemapSpatialReference(basemapItem, basemapItemData) {
var basemapSpatialRef = null;
var spatialRefDef = new Deferred();
if (basemapItemData.spatialReference || basemapItem.spatialReference) {
basemapSpatialRef = basemapItemData.spatialReference || basemapItem.spatialReference;
spatialRefDef.resolve(basemapSpatialRef);
} else if(_isBasemapLayersHasTypeProperty(basemapItemData.baseMap.baseMapLayers)) {
basemapSpatialRef = {wkid: "102100"};
spatialRefDef.resolve(basemapSpatialRef);
} else if(basemapItemData.baseMap.baseMapLayers && basemapItemData.baseMap.baseMapLayers[0]) {
esriRequest({
url: basemapItemData.baseMap.baseMapLayers[0].url,
content: {
f: 'json'
},
handleAs: 'json',
callbackParamName: 'callback'
}).then(lang.hitch(this, function(res) {
if(res.spatialReference) {
basemapSpatialRef = res.spatialReference;
}
spatialRefDef.resolve(basemapSpatialRef);
}), lang.hitch(this, function() {
// return null if can not get spatialReference;
spatialRefDef.resolve(basemapSpatialRef);
}));
} else {
// return null if can not get spatialReference;
spatialRefDef.resolve(basemapSpatialRef);
}
return spatialRefDef;
}
return mo;
});