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 cloneDeep from 'lodash.clonedeep' ;
5
- import { getProjection , registerProjection , toEpsgCode , transformCoodinates } from './utils/epsg-define' ;
5
+ import { getProjection , toEpsgCode , transformCoodinates } from './utils/epsg-define' ;
6
6
import { ColorsPickerUtil } from '../util/ColorsPickerUtil' ;
7
7
import { Util } from '../commontypes/Util' ;
8
8
import { ArrayStatistic } from '../util/ArrayStatistic' ;
@@ -17,7 +17,7 @@ const INTERNET_MAP_BOUNDS = {
17
17
BING : [ - 180 , - 90 , 180 , 90 ]
18
18
}
19
19
20
- export function createWebMapV2Extending ( SuperClass , { MapManager, mapRepo, DataFlowService, GraticuleLayer } ) {
20
+ export function createWebMapV2Extending ( SuperClass , { MapManager, mapRepo, crsManager , DataFlowService, GraticuleLayer } ) {
21
21
return class WebMapV2 extends SuperClass {
22
22
constructor (
23
23
id ,
@@ -49,12 +49,23 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, DataF
49
49
this . _appendLayers = false ;
50
50
}
51
51
52
- initializeMap ( mapInfo , map ) {
53
- if ( map ) {
54
- this . _appendLayers = true ;
55
- this . map = map ;
52
+ async initializeMap ( mapInfo , map ) {
53
+ try {
54
+ this . baseProjection = await this . _registerMapCRS ( mapInfo ) ;
55
+ if ( map ) {
56
+ if ( ! crsManager . isSameProjection ( map , this . baseProjection ) && ! this . ignoreBaseProjection ) {
57
+ this . fire ( 'projectionnotmatch' ) ;
58
+ return ;
59
+ }
60
+ this . _appendLayers = true ;
61
+ this . map = map ;
62
+ }
63
+ this . _mapInfo = mapInfo ;
64
+ this . _loadLayers ( mapInfo , this . _taskID ) ;
65
+ } catch ( error ) {
66
+ console . error ( error ) ;
67
+ this . fire ( 'mapcreatefailed' , { error } ) ;
56
68
}
57
- this . _getMapInfo ( mapInfo , this . _taskID ) ;
58
69
}
59
70
60
71
cleanLayers ( layers ) {
@@ -95,12 +106,10 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, DataF
95
106
96
107
_initWebMap ( ) { }
97
108
109
+ _getMapInfo ( ) { }
110
+
98
111
_loadLayers ( mapInfo , _taskID ) {
99
112
if ( this . map ) {
100
- if ( this . map . getCRS ( ) . epsgCode !== this . baseProjection && ! this . ignoreBaseProjection ) {
101
- this . fire ( 'projectionnotmatch' , { } ) ;
102
- return ;
103
- }
104
113
this . _handleLayerInfo ( mapInfo , _taskID ) ;
105
114
} else {
106
115
setTimeout ( ( ) => {
@@ -112,95 +121,59 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, DataF
112
121
}
113
122
}
114
123
115
- _setCRS ( baseProjection , wkt , bounds ) {
116
- if ( mapRepo . CRS . get ( baseProjection ) ) {
117
- return ;
118
- }
119
- const crs = new mapRepo . CRS ( baseProjection , wkt , bounds , bounds [ 2 ] > 180 ? 'meter' : 'degree' ) ;
120
- mapRepo . CRS . set ( crs ) ;
121
- }
122
-
123
- _getMapInfo ( mapInfo , _taskID ) {
124
- this . _mapInfo = mapInfo ;
125
- const { projection } = mapInfo ;
126
- let bounds , wkt ;
127
- this . baseProjection = toEpsgCode ( projection ) ;
128
- let defaultWktValue = getProjection ( this . baseProjection , this . specifiedProj4 ) ;
129
-
130
- if ( defaultWktValue ) {
131
- wkt = defaultWktValue ;
132
- }
133
- if ( ! mapRepo . CRS . get ( this . baseProjection ) ) {
134
- if ( mapInfo . baseLayer && mapInfo . baseLayer . layerType === 'MAPBOXSTYLE' ) {
135
- let url = mapInfo . baseLayer . dataSource . url ;
136
- if ( url . indexOf ( '/restjsr/' ) > - 1 && ! / \/ s t y l e \. j s o n $ / . test ( url ) ) {
137
- url += '/style.json' ;
138
- }
139
- this . webMapService . getMapBoxStyle ( url ) . then ( ( res ) => {
124
+ async _registerMapCRS ( mapInfo ) {
125
+ const { projection, extent, baseLayer = { } } = mapInfo ;
126
+ const epsgCode = toEpsgCode ( projection ) ;
127
+ let crs = {
128
+ name : epsgCode ,
129
+ extent : [ extent . leftBottom . x , extent . leftBottom . y , extent . rightTop . x , extent . rightTop . y ] ,
130
+ wkt : this . _getProjectionWKT ( projection )
131
+ } ;
132
+ if ( ! crsManager . getCRS ( epsgCode ) ) {
133
+ switch ( baseLayer . layerType ) {
134
+ case 'MAPBOXSTYLE' : {
135
+ let url = baseLayer . dataSource . url ;
136
+ if ( url . indexOf ( '/restjsr/' ) > - 1 && ! / \/ s t y l e \. j s o n $ / . test ( url ) ) {
137
+ url += '/style.json' ;
138
+ }
139
+ const res = await this . webMapService . getMapBoxStyle ( url ) ;
140
140
if ( res && res . metadata && res . metadata . indexbounds ) {
141
- bounds = res . metadata . indexbounds ;
142
- } else {
143
- bounds = [
144
- mapInfo . extent . leftBottom . x ,
145
- mapInfo . extent . leftBottom . y ,
146
- mapInfo . extent . rightTop . x ,
147
- mapInfo . extent . rightTop . y
148
- ] ;
141
+ crs . extent = res . metadata . indexbounds ;
149
142
}
150
- this . _defineProj4 ( projection ) ;
151
- this . _setCRS ( this . baseProjection , wkt , bounds ) ;
152
- this . _loadLayers ( mapInfo , _taskID ) ;
153
- } ) ;
154
- } else if ( mapInfo . baseLayer && mapInfo . baseLayer . layerType === 'TILE' ) {
155
- // 获取地图的wkt
156
- this . getEpsgCodeWKT ( `${ mapInfo . baseLayer . url } /prjCoordSys.wkt` , {
157
- withoutFormatSuffix : true ,
158
- withCredentials : this . webMapService . handleWithCredentials ( '' , mapInfo . baseLayer . url , false )
159
- } ) . then ( ( res ) => {
160
- if ( ! wkt ) {
161
- wkt = res ;
143
+ break ;
144
+ }
145
+ case 'TILE' : {
146
+ // 获取地图的wkt
147
+ if ( ! crs . wkt ) {
148
+ crs . wkt = await this . getEpsgCodeWKT ( `${ baseLayer . url } /prjCoordSys.wkt` , {
149
+ withoutFormatSuffix : true ,
150
+ withCredentials : this . webMapService . handleWithCredentials ( '' , baseLayer . url , false )
151
+ } ) ;
162
152
}
163
- this . getBounds ( `${ mapInfo . baseLayer . url } .json` , {
153
+ const boundsRes = await this . getBounds ( `${ baseLayer . url } .json` , {
164
154
withoutFormatSuffix : true ,
165
- withCredentials : this . webMapService . handleWithCredentials ( '' , mapInfo . baseLayer . url , false )
166
- } ) . then ( ( res ) => {
167
- if ( res && res . bounds ) {
168
- bounds = [
169
- res . bounds . leftBottom . x ,
170
- res . bounds . leftBottom . y ,
171
- res . bounds . rightTop . x ,
172
- res . bounds . rightTop . y
173
- ] ;
174
- } else {
175
- bounds = [
176
- mapInfo . extent . leftBottom . x ,
177
- mapInfo . extent . leftBottom . y ,
178
- mapInfo . extent . rightTop . x ,
179
- mapInfo . extent . rightTop . y
180
- ] ;
181
- }
182
- this . _defineProj4 ( wkt , projection ) ;
183
- this . _setCRS ( this . baseProjection , wkt , bounds ) ;
184
- this . _loadLayers ( mapInfo , _taskID ) ;
155
+ withCredentials : this . webMapService . handleWithCredentials ( '' , baseLayer . url , false )
185
156
} ) ;
186
- } ) ;
187
- } else {
188
- const error = new Error ( 'Unsupported coordinate system!' ) ;
189
- console . log ( error ) ;
190
- this . fire ( 'mapcreatefailed' , { error } ) ;
157
+ if ( boundsRes && boundsRes . bounds ) {
158
+ crs . extent = [
159
+ boundsRes . bounds . leftBottom . x ,
160
+ boundsRes . bounds . leftBottom . y ,
161
+ boundsRes . bounds . rightTop . x ,
162
+ boundsRes . bounds . rightTop . y
163
+ ] ;
164
+ }
165
+ break ;
166
+ }
167
+ default :
168
+ crs = null ;
169
+ break ;
191
170
}
192
- } else {
193
- wkt = mapRepo . CRS . get ( this . baseProjection ) . WKT
194
- this . _defineProj4 ( wkt || projection ) ;
195
- bounds = [
196
- mapInfo . extent . leftBottom . x ,
197
- mapInfo . extent . leftBottom . y ,
198
- mapInfo . extent . rightTop . x ,
199
- mapInfo . extent . rightTop . y
200
- ] ;
201
- this . _setCRS ( this . baseProjection , wkt , bounds ) ;
202
- this . _loadLayers ( mapInfo , _taskID ) ;
203
171
}
172
+ if ( ! crs ) {
173
+ throw new Error ( 'Unsupported coordinate system!' )
174
+ }
175
+ crsManager . registerCRS ( crs ) ;
176
+ return epsgCode ;
204
177
}
205
178
206
179
_handleLayerInfo ( mapInfo , _taskID ) {
@@ -499,7 +472,7 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, DataF
499
472
layerInfo . dataSource &&
500
473
layerInfo . dataSource . type !== 'REST_DATA'
501
474
) {
502
- this . _unprojectProjection = this . _defineProj4 ( projection ) ;
475
+ this . _unprojectProjection = toEpsgCode ( projection ) ;
503
476
features = this . transformFeatures ( features ) ;
504
477
}
505
478
@@ -2772,15 +2745,10 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, DataF
2772
2745
return tiandituUrls ;
2773
2746
}
2774
2747
2775
- _defineProj4 ( projection , defaultEpsgCode ) {
2748
+ _getProjectionWKT ( projection ) {
2776
2749
let epsgCode = toEpsgCode ( projection ) ;
2777
2750
const reg = / ^ E P S G : / ;
2778
- const defValue = epsgCode && projection . match ( reg ) ? getProjection ( epsgCode , this . specifiedProj4 ) : projection ;
2779
- if ( ! epsgCode && defaultEpsgCode && defaultEpsgCode . match ( reg ) ) {
2780
- epsgCode = defaultEpsgCode ;
2781
- }
2782
- registerProjection ( epsgCode , defValue , this . specifiedProj4 ) ;
2783
- return epsgCode ;
2751
+ return epsgCode && projection . match ( reg ) ? getProjection ( epsgCode , this . specifiedProj4 ) : projection ;
2784
2752
}
2785
2753
2786
2754
_fetchRequest ( url , type , options ) {
@@ -2805,7 +2773,7 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, DataF
2805
2773
2806
2774
getBounds ( baseUrl , options ) {
2807
2775
if ( ! baseUrl ) {
2808
- return ;
2776
+ return Promise . resolve ( null ) ;
2809
2777
}
2810
2778
return this . _fetchRequest ( baseUrl , 'json' , options ) ;
2811
2779
}
0 commit comments