Skip to content

Commit

Permalink
fit plots to bounds when data is fetched or changed
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsingal committed Mar 20, 2024
1 parent 427041c commit edabac5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@tanstack/react-query": "^4.2.1",
"@tanstack/react-table": "8.13.2",
"@tanstack/react-virtual": "3.0.1",
"@turf/bbox": "^6.5.0",
"autoprefixer": "10.2.5",
"axios": "1.3.4",
"chroma-js": "2.1.2",
Expand Down
24 changes: 22 additions & 2 deletions client/src/containers/analysis-eudr/map/component.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useState, useCallback } from 'react';
import { useState, useCallback, useEffect } from 'react';
import DeckGL from '@deck.gl/react/typed';
import { BitmapLayer, GeoJsonLayer } from '@deck.gl/layers/typed';
import Map from 'react-map-gl/maplibre';
import { MapView } from '@deck.gl/core/typed';
import { MapView, WebMercatorViewport } from '@deck.gl/core/typed';
import { TileLayer } from '@deck.gl/geo-layers/typed';
import { CartoLayer, setDefaultCredentials, MAP_TYPES, API_VERSIONS } from '@deck.gl/carto/typed';
import { useParams } from 'next/navigation';
import { format } from 'date-fns';
import bbox from '@turf/bbox';

import ZoomControl from './zoom';
import LegendControl from './legend';
Expand Down Expand Up @@ -200,6 +201,25 @@ const EUDRMap = () => {
setViewState({ ...viewState, zoom });
}, [viewState]);

useEffect(() => {
if (!plotGeometries.data || !plotGeometries.isLoading) return;
const dataBounds = bbox(plotGeometries.data);
const newViewport = new WebMercatorViewport(viewState);
if (newViewport) {
const { latitude, longitude, zoom } = newViewport.fitBounds(
[
[dataBounds[0], dataBounds[1]],
[dataBounds[2], dataBounds[3]],
],
{
padding: 10,
},
);
setViewState({ ...viewState, latitude, longitude, zoom });
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [plotGeometries.data, plotGeometries.isLoading]);

return (
<>
<div className="absolute left-0 top-0 h-full w-full overflow-hidden">
Expand Down
27 changes: 27 additions & 0 deletions client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2651,6 +2651,32 @@ __metadata:
languageName: node
linkType: hard

"@turf/bbox@npm:^6.5.0":
version: 6.5.0
resolution: "@turf/bbox@npm:6.5.0"
dependencies:
"@turf/helpers": ^6.5.0
"@turf/meta": ^6.5.0
checksum: 537be56ae0c5ad44e71a691717b35745e947e19a6bd9f20fdac2ab4318caf98cd88472d7dbf576e8b32ead5da034d273ffb3f4559d6d386820ddcb88a1f7fedd
languageName: node
linkType: hard

"@turf/helpers@npm:^6.5.0":
version: 6.5.0
resolution: "@turf/helpers@npm:6.5.0"
checksum: d57f746351357838c654e0a9b98be3285a14b447504fd6d59753d90c6d437410bb24805d61c65b612827f07f6c2ade823bb7e56e41a1a946217abccfbd64c117
languageName: node
linkType: hard

"@turf/meta@npm:^6.5.0":
version: 6.5.0
resolution: "@turf/meta@npm:6.5.0"
dependencies:
"@turf/helpers": ^6.5.0
checksum: c6bb936aa92bf3365e87a50dc65f248e070c5767a36fac390754c00c89bf2d1583418686ab19a10332bfa9340b8cac6aaf2c55dad7f5fcf77f1a2dda75ccf363
languageName: node
linkType: hard

"@types/chroma-js@npm:2.1.3":
version: 2.1.3
resolution: "@types/chroma-js@npm:2.1.3"
Expand Down Expand Up @@ -7889,6 +7915,7 @@ __metadata:
"@tanstack/react-query": ^4.2.1
"@tanstack/react-table": 8.13.2
"@tanstack/react-virtual": 3.0.1
"@turf/bbox": ^6.5.0
"@types/chroma-js": 2.1.3
"@types/d3-format": 3.0.1
"@types/d3-scale": 4.0.2
Expand Down

0 comments on commit edabac5

Please sign in to comment.