2
2
* This program are made available under the terms of the Apache License, Version 2.0
3
3
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
4
4
import ol from 'openlayers' ;
5
+ import proj4 from "proj4" ;
6
+ window . proj4 = proj4 ;
7
+ window . Proj4js = proj4 ;
5
8
import {
6
9
FetchRequest ,
7
10
SecurityManager ,
@@ -153,6 +156,17 @@ export class WebMap extends ol.Observable {
153
156
return ;
154
157
}
155
158
that . baseProjection = mapInfo . projection ;
159
+
160
+ // 多坐标系支持
161
+ if ( proj4 ) ol . proj . setProj4 ( proj4 ) ;
162
+ if ( that . addProjctionFromWKT ( mapInfo . projection ) ) {
163
+ mapInfo . projection = that . baseProjection = that . getEpsgInfoFromWKT ( mapInfo . projection ) ;
164
+ } else {
165
+ // 不支持的坐标系
166
+ that . errorCallback && that . errorCallback ( { type : "Not support CS" , errorMsg : `Not support CS: ${ mapInfo . projection } ` } , 'getMapFaild' , that . map ) ;
167
+ return ;
168
+ }
169
+
156
170
that . mapParams = {
157
171
title : mapInfo . title ,
158
172
description : mapInfo . description
@@ -207,15 +221,15 @@ export class WebMap extends ol.Observable {
207
221
center = [ 0 , 0 ] ;
208
222
}
209
223
//与DV一致用底图的默认范围,不用存储的范围。否则会导致地图拖不动
210
- extent = options . baseLayer . extent ;
224
+ this . baseLayerExtent = extent = options . baseLayer . extent ;
211
225
if ( this . mapParams ) {
212
226
this . mapParams . extent = extent ;
213
227
this . mapParams . projection = projection ;
214
228
}
215
229
216
230
// 计算当前最大分辨率
217
231
let maxResolution ;
218
- if ( extent && extent . length === 4 ) {
232
+ if ( options . baseLayer . layerType === "TILE" && extent && extent . length === 4 ) {
219
233
let width = extent [ 2 ] - extent [ 0 ] ;
220
234
let height = extent [ 3 ] - extent [ 1 ] ;
221
235
let maxResolution1 = width / 256 ;
@@ -508,12 +522,13 @@ export class WebMap extends ol.Observable {
508
522
if ( credential ) {
509
523
SecurityManager [ `register${ keyfix } ` ] ( keyParams , credential ) ;
510
524
}
525
+ // extent: isBaseLayer ? layerInfo.extent : ol.proj.transformExtent(layerInfo.extent, layerInfo.projection, this.baseProjection),
511
526
let source = new ol . source . TileSuperMapRest ( {
512
527
transparent : true ,
513
528
url : layerInfo . url ,
514
529
wrapX : false ,
515
530
serverType : serverType ,
516
- extent : isBaseLayer ? layerInfo . extent : ol . proj . transformExtent ( layerInfo . extent , layerInfo . projection , this . baseProjection ) ,
531
+ extent : this . baseLayerExtent ,
517
532
prjCoordSys :{ epsgCode : isBaseLayer ? layerInfo . projection . split ( ':' ) [ 1 ] : this . baseProjection . split ( ':' ) [ 1 ] } ,
518
533
519
534
tileProxy : this . tileProxy
@@ -630,7 +645,11 @@ export class WebMap extends ol.Observable {
630
645
let that = this ,
631
646
url = layerInfo . url . trim ( ) ;
632
647
if ( layerInfo . layerType === "TILE" ) {
633
- url += ".json" ;
648
+ // 直接使用动态投影方式请求数据
649
+ let projection = {
650
+ epsgCode : that . baseProjection . split ( ":" ) [ 1 ]
651
+ }
652
+ url += `.json?prjCoordSys=${ JSON . stringify ( projection ) } ` ;
634
653
} else {
635
654
url += ( url . indexOf ( '?' ) > - 1 ? '&SERVICE=WMS&REQUEST=GetCapabilities' : '?SERVICE=WMS&REQUEST=GetCapabilities' ) ;
636
655
}
@@ -2510,6 +2529,55 @@ export class WebMap extends ol.Observable {
2510
2529
} )
2511
2530
} ) ;
2512
2531
}
2532
+
2533
+ /**
2534
+ * 通过wkt参数扩展支持多坐标系
2535
+ *
2536
+ * @param {String } wkt 字符串
2537
+ * @returns {Boolean } 坐标系是否添加成功
2538
+ */
2539
+ addProjctionFromWKT ( wkt ) {
2540
+ if ( typeof ( wkt ) !== 'string' ) {
2541
+ //参数类型错误
2542
+ return false ;
2543
+ } else {
2544
+ if ( wkt === "EPSG:4326" || wkt === "EPSG:3857" ) {
2545
+ return true ;
2546
+ } else {
2547
+ let epsgCode = this . getEpsgInfoFromWKT ( wkt ) ;
2548
+ if ( epsgCode ) {
2549
+ proj4 . defs ( epsgCode , wkt ) ;
2550
+ return true ;
2551
+ } else {
2552
+ // 参数类型非wkt标准
2553
+ return false ;
2554
+ }
2555
+ }
2556
+ }
2557
+ }
2558
+
2559
+ /**
2560
+ * 通过wkt参数获取坐标信息
2561
+ *
2562
+ * @param {String } wkt 字符串
2563
+ * @returns {String } epsg 如:"EPSG:4326"
2564
+ */
2565
+ getEpsgInfoFromWKT ( wkt ) {
2566
+ if ( typeof ( wkt ) !== 'string' ) {
2567
+ return false ;
2568
+ } else if ( wkt . indexOf ( "EPSG" ) === 0 ) {
2569
+ return wkt ;
2570
+ } else {
2571
+ let lastAuthority = wkt . lastIndexOf ( "AUTHORITY" ) + 10 ,
2572
+ endString = wkt . indexOf ( "]" , lastAuthority ) - 1 ;
2573
+ if ( lastAuthority > 0 && endString > 0 ) {
2574
+ return `EPSG:${ wkt . substring ( lastAuthority , endString ) . split ( "," ) [ 1 ] . substr ( 1 ) } ` ;
2575
+ } else {
2576
+ return false ;
2577
+ }
2578
+ }
2579
+ }
2580
+
2513
2581
}
2514
2582
2515
2583
ol . supermap . WebMap = WebMap ;
0 commit comments