@@ -2,7 +2,7 @@ import { JSONConfiguration, JSONConverter } from "@deck.gl/json";
2
2
import DeckGL from "@deck.gl/react" ;
3
3
import { PickInfo } from "deck.gl" ;
4
4
import { Feature } from "geojson" ;
5
- import React , { useEffect , useState , useCallback } from "react" ;
5
+ import React , { useEffect , useState , useCallback , useRef } from "react" ;
6
6
import Settings from "./settings/Settings" ;
7
7
import JSON_CONVERTER_CONFIG from "../utils/configuration" ;
8
8
import { MapState } from "../redux/store" ;
@@ -17,6 +17,7 @@ import { DeckGLView } from "./DeckGLView";
17
17
import { Viewport } from "@deck.gl/core" ;
18
18
import { colorTablesArray } from "@emerson-eps/color-tables/" ;
19
19
import { LayerProps , LayerContext } from "@deck.gl/core/lib/layer" ;
20
+ import { ViewStateProps } from "@deck.gl/core/lib/deck" ;
20
21
import { ViewProps } from "@deck.gl/core/views/view" ;
21
22
import { isEmpty } from "lodash" ;
22
23
import ColorLegend from "./ColorLegend" ;
@@ -26,6 +27,7 @@ import {
26
27
getLayersWithDefaultProps ,
27
28
} from "../layers/utils/layerTools" ;
28
29
import ViewFooter from "./ViewFooter" ;
30
+ import fitBounds from "../utils/fit-bounds" ;
29
31
30
32
// eslint-disable-next-line @typescript-eslint/no-var-requires
31
33
const colorTables = require ( "@emerson-eps/color-tables/src/component/color-tables.json" ) ;
@@ -184,14 +186,6 @@ const Map: React.FC<MapProps> = ({
184
186
setEditedData,
185
187
children,
186
188
} : MapProps ) => {
187
- // state for initial views prop (target and zoom) of DeckGL component
188
- const [ initialViewState , setInitialViewState ] =
189
- useState < Record < string , unknown > > ( ) ;
190
- useEffect ( ( ) => {
191
- if ( bounds == undefined || zoom == undefined ) return ;
192
- setInitialViewState ( getInitialViewState ( bounds , zoom ) ) ;
193
- } , [ bounds , zoom ] ) ;
194
-
195
189
// state for views prop of DeckGL component
196
190
const [ viewsProps , setViewsProps ] = useState < ViewProps [ ] > ( [ ] ) ;
197
191
useEffect ( ( ) => {
@@ -230,16 +224,6 @@ const Map: React.FC<MapProps> = ({
230
224
setDeckGLLayers ( jsonToObject ( layers , enumerations ) as Layer < unknown > [ ] ) ;
231
225
} , [ st_layers , resources , editedData ] ) ;
232
226
233
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
234
- const [ viewState , setViewState ] = useState < any > ( ) ;
235
-
236
- const refCb = useCallback ( ( deckRef ) => {
237
- if ( deckRef && deckRef . deck ) {
238
- // Needed to initialize the viewState on first load
239
- setViewState ( deckRef . deck . viewState ) ;
240
- }
241
- } , [ ] ) ;
242
-
243
227
// eslint-disable-next-line @typescript-eslint/no-explicit-any
244
228
const [ hoverInfo , setHoverInfo ] = useState < any > ( [ ] ) ;
245
229
const onHover = useCallback (
@@ -259,8 +243,39 @@ const Map: React.FC<MapProps> = ({
259
243
[ coords ]
260
244
) ;
261
245
262
- const [ isLoaded , setIsLoaded ] = useState < boolean > ( false ) ;
246
+ const deckRef = useRef < DeckGL > ( null ) ;
247
+ const [ viewState , setViewState ] = useState < ViewStateProps > ( ) ;
248
+
249
+ const onLoad = useCallback ( ( ) => {
250
+ if ( deckRef == null ) return ;
251
+
252
+ const deck = deckRef . current ?. deck ;
253
+ if ( deck && bounds ) {
254
+ const width = deck . width ;
255
+ const height = deck . height ;
256
+ const [ west , south ] = [ bounds [ 0 ] , bounds [ 1 ] ] ;
257
+ const [ east , north ] = [ bounds [ 2 ] , bounds [ 3 ] ] ;
258
+ const fitted_bound = fitBounds ( {
259
+ width,
260
+ height,
261
+ bounds : [
262
+ [ west , south ] ,
263
+ [ east , north ] ,
264
+ ] ,
265
+ } ) ;
263
266
267
+ const zoom_defined_by_consumer = zoom != undefined ;
268
+ const view_state = {
269
+ target : [ fitted_bound . x , fitted_bound . y , 0 ] ,
270
+ zoom : zoom_defined_by_consumer ? zoom : fitted_bound . zoom ,
271
+ rotationX : 90 , // look down z -axis.
272
+ rotationOrbit : 0 ,
273
+ } ;
274
+ setViewState ( view_state ) ;
275
+ }
276
+ } , [ deckRef ] ) ;
277
+
278
+ const [ isLoaded , setIsLoaded ] = useState < boolean > ( false ) ;
264
279
const onAfterRender = useCallback ( ( ) => {
265
280
if ( deckGLLayers ) {
266
281
const state = deckGLLayers . every ( ( layer ) => layer . isLoaded ) ;
@@ -301,7 +316,6 @@ const Map: React.FC<MapProps> = ({
301
316
< DeckGL
302
317
id = { id }
303
318
viewState = { viewState }
304
- initialViewState = { initialViewState }
305
319
views = { deckGLViews }
306
320
layerFilter = { layerFilter }
307
321
layers = { deckGLLayers }
@@ -325,11 +339,13 @@ const Map: React.FC<MapProps> = ({
325
339
return feat ?. properties ?. [ "name" ] ;
326
340
}
327
341
} }
328
- ref = { refCb }
329
342
onHover = { onHover }
330
343
onViewStateChange = { ( viewport ) =>
331
344
setViewState ( viewport . viewState )
332
345
}
346
+ ref = { deckRef }
347
+ onLoad = { onLoad }
348
+ onResize = { onLoad }
333
349
onAfterRender = { onAfterRender }
334
350
>
335
351
{ children }
@@ -372,11 +388,7 @@ const Map: React.FC<MapProps> = ({
372
388
373
389
{ scale ?. visible ? (
374
390
< DistanceScale
375
- zoom = {
376
- viewState ?. zoom
377
- ? viewState . zoom
378
- : initialViewState ?. [ "zoom" ]
379
- }
391
+ zoom = { viewState ?. zoom }
380
392
incrementValue = { scale . incrementValue }
381
393
widthPerUnit = { scale . widthPerUnit }
382
394
position = { scale . position }
@@ -412,7 +424,6 @@ Map.defaultProps = {
412
424
horizontal : false ,
413
425
} ,
414
426
coordinateUnit : "m" ,
415
- zoom : - 3 ,
416
427
views : {
417
428
layout : [ 1 , 1 ] ,
418
429
showLabel : false ,
@@ -450,25 +461,6 @@ function jsonToObject(
450
461
return jsonConverter . convert ( filtered_data ) ;
451
462
}
452
463
453
- // returns initial view state for DeckGL
454
- function getInitialViewState (
455
- bounds : [ number , number , number , number ] ,
456
- zoom : number
457
- ) : Record < string , unknown > {
458
- const width = bounds [ 2 ] - bounds [ 0 ] ; // right - left
459
- const height = bounds [ 3 ] - bounds [ 1 ] ; // top - bottom
460
-
461
- const initial_view_state = {
462
- // target to center of the bound
463
- target : [ bounds [ 0 ] + width / 2 , bounds [ 1 ] + height / 2 , 0 ] ,
464
- zoom : zoom ,
465
- rotationX : 90 , // look down z -axis.
466
- rotationOrbit : 0 ,
467
- } ;
468
-
469
- return initial_view_state ;
470
- }
471
-
472
464
// construct views object for DeckGL component
473
465
function getViews ( views : ViewsType | undefined ) : Record < string , unknown > [ ] {
474
466
const deckgl_views = [ ] ;
0 commit comments