22 * This program are made available under the terms of the Apache License, Version 2.0
33 * which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
44import ol from 'openlayers' ;
5+ import proj4 from "proj4" ;
6+ window . proj4 = proj4 ;
7+ window . Proj4js = proj4 ;
58import {
69 FetchRequest ,
710 SecurityManager ,
@@ -153,6 +156,17 @@ export class WebMap extends ol.Observable {
153156 return ;
154157 }
155158 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+
156170 that . mapParams = {
157171 title : mapInfo . title ,
158172 description : mapInfo . description
@@ -207,15 +221,15 @@ export class WebMap extends ol.Observable {
207221 center = [ 0 , 0 ] ;
208222 }
209223 //与DV一致用底图的默认范围,不用存储的范围。否则会导致地图拖不动
210- extent = options . baseLayer . extent ;
224+ this . baseLayerExtent = extent = options . baseLayer . extent ;
211225 if ( this . mapParams ) {
212226 this . mapParams . extent = extent ;
213227 this . mapParams . projection = projection ;
214228 }
215229
216230 // 计算当前最大分辨率
217231 let maxResolution ;
218- if ( extent && extent . length === 4 ) {
232+ if ( options . baseLayer . layerType === "TILE" && extent && extent . length === 4 ) {
219233 let width = extent [ 2 ] - extent [ 0 ] ;
220234 let height = extent [ 3 ] - extent [ 1 ] ;
221235 let maxResolution1 = width / 256 ;
@@ -508,12 +522,13 @@ export class WebMap extends ol.Observable {
508522 if ( credential ) {
509523 SecurityManager [ `register${ keyfix } ` ] ( keyParams , credential ) ;
510524 }
525+ // extent: isBaseLayer ? layerInfo.extent : ol.proj.transformExtent(layerInfo.extent, layerInfo.projection, this.baseProjection),
511526 let source = new ol . source . TileSuperMapRest ( {
512527 transparent : true ,
513528 url : layerInfo . url ,
514529 wrapX : false ,
515530 serverType : serverType ,
516- extent : isBaseLayer ? layerInfo . extent : ol . proj . transformExtent ( layerInfo . extent , layerInfo . projection , this . baseProjection ) ,
531+ extent : this . baseLayerExtent ,
517532 prjCoordSys :{ epsgCode : isBaseLayer ? layerInfo . projection . split ( ':' ) [ 1 ] : this . baseProjection . split ( ':' ) [ 1 ] } ,
518533
519534 tileProxy : this . tileProxy
@@ -630,7 +645,11 @@ export class WebMap extends ol.Observable {
630645 let that = this ,
631646 url = layerInfo . url . trim ( ) ;
632647 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 ) } ` ;
634653 } else {
635654 url += ( url . indexOf ( '?' ) > - 1 ? '&SERVICE=WMS&REQUEST=GetCapabilities' : '?SERVICE=WMS&REQUEST=GetCapabilities' ) ;
636655 }
@@ -2510,6 +2529,55 @@ export class WebMap extends ol.Observable {
25102529 } )
25112530 } ) ;
25122531 }
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+
25132581}
25142582
25152583ol . supermap . WebMap = WebMap ;
0 commit comments