Skip to content

Commit 65857f4

Browse files
authored
Merge pull request #30 from developmentseed/add/scale-area
Display scale and area
2 parents f1900a3 + 4c3cac0 commit 65857f4

File tree

6 files changed

+55
-4
lines changed

6 files changed

+55
-4
lines changed

web-vite/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"deploy": "vite build && surge dist https://osm-tardis.surge.sh"
1111
},
1212
"dependencies": {
13+
"@turf/area": "^6.5.0",
14+
"@turf/bbox-polygon": "^6.5.0",
1315
"flatgeobuf": "^3.26.2",
1416
"lodash.get": "^4.4.2",
1517
"maplibre-gl": "^3.3.1",

web-vite/src/app/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import { CartoSlider } from "./cartoslider";
1010
export function App() {
1111
const [appState, dispatchAppState] = useAppReducer();
1212

13-
const { mapStatus, timestamps, currentTimestamp } = appState;
13+
const { mapStatus, timestamps, currentTimestamp, formattedArea } = appState;
1414

1515
const isLoading = mapStatus === MapStatus.LOADING;
1616

1717
return (
1818
<Layout>
1919
<Header />
2020
<Panel>
21-
<PanelInputs />
21+
<PanelInputs formattedArea={formattedArea} />
2222
<Stats
2323
stats={appState.stats}
2424
currentTimestamp={appState.currentTimestamp}

web-vite/src/app/inputs.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
export function PanelInputs() {
1+
interface PanelInputsProps {
2+
formattedArea: string;
3+
}
4+
5+
export function PanelInputs(props: PanelInputsProps) {
6+
const { formattedArea } = props;
27

38
return (
49
<article>
510
<section>
611
<h3>Area of Interest (AOI)</h3>
712
<small>Zoom and pan the map to set the AOI for stats </small>
813
<p>
9-
Currently selected: <strong>{2333}m&sup2;</strong>
14+
Currently selected: <strong>{formattedArea || "-"} km&sup2;</strong>
1015
</p>
1116
</section>
1217
<section>

web-vite/src/app/map/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ export function Map(props: MapProps) {
6868
maxZoom: 18,
6969
minZoom: 0,
7070
});
71+
let scale = new MapLibreGL.ScaleControl({
72+
maxWidth: 160,
73+
unit: 'metric'
74+
});
75+
map.addControl(scale);
7176

7277
map.on("load", () => {
7378
map.addSource("data", {

web-vite/src/app/reducer/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { useReducerAsync } from "use-reducer-async";
22
import logReducer from "./log.ts";
33
import { calculateStats, getFgbData } from "../map/utils.ts";
4+
import tArea from "@turf/area";
5+
import tBboxPolygon from "@turf/bbox-polygon";
46

57
export enum MapStatus {
68
IDLE = "IDLE",
@@ -92,8 +94,18 @@ function appReducer(state: AppState, action: AppAction) {
9294
currentTimestamp
9395
);
9496
const stats = calculateStats(currentTimestampGeojson);
97+
98+
const bounds = state.map.getBounds().toArray();
99+
const [[minX, minY], [maxX, maxY]] = bounds;
100+
const poly = tBboxPolygon([minX, minY, maxX, maxY]);
101+
const area = tArea(poly);
102+
const formattedArea = new Intl.NumberFormat().format(
103+
(area / 1e6).toFixed(2)
104+
);
105+
95106
return {
96107
...state,
108+
formattedArea,
97109
stats,
98110
geojson,
99111
timestamps,

web-vite/yarn.lock

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,33 @@
504504
estree-walker "^2.0.1"
505505
picomatch "^2.2.2"
506506

507+
"@turf/area@^6.5.0":
508+
version "6.5.0"
509+
resolved "https://registry.yarnpkg.com/@turf/area/-/area-6.5.0.tgz#1d0d7aee01d8a4a3d4c91663ed35cc615f36ad56"
510+
integrity sha512-xCZdiuojokLbQ+29qR6qoMD89hv+JAgWjLrwSEWL+3JV8IXKeNFl6XkEJz9HGkVpnXvQKJoRz4/liT+8ZZ5Jyg==
511+
dependencies:
512+
"@turf/helpers" "^6.5.0"
513+
"@turf/meta" "^6.5.0"
514+
515+
"@turf/bbox-polygon@^6.5.0":
516+
version "6.5.0"
517+
resolved "https://registry.yarnpkg.com/@turf/bbox-polygon/-/bbox-polygon-6.5.0.tgz#f18128b012eedfa860a521d8f2b3779cc0801032"
518+
integrity sha512-+/r0NyL1lOG3zKZmmf6L8ommU07HliP4dgYToMoTxqzsWzyLjaj/OzgQ8rBmv703WJX+aS6yCmLuIhYqyufyuw==
519+
dependencies:
520+
"@turf/helpers" "^6.5.0"
521+
522+
"@turf/helpers@^6.5.0":
523+
version "6.5.0"
524+
resolved "https://registry.yarnpkg.com/@turf/helpers/-/helpers-6.5.0.tgz#f79af094bd6b8ce7ed2bd3e089a8493ee6cae82e"
525+
integrity sha512-VbI1dV5bLFzohYYdgqwikdMVpe7pJ9X3E+dlr425wa2/sMJqYDhTO++ec38/pcPvPE6oD9WEEeU3Xu3gza+VPw==
526+
527+
"@turf/meta@^6.5.0":
528+
version "6.5.0"
529+
resolved "https://registry.yarnpkg.com/@turf/meta/-/meta-6.5.0.tgz#b725c3653c9f432133eaa04d3421f7e51e0418ca"
530+
integrity sha512-RrArvtsV0vdsCBegoBtOalgdSOfkBrTJ07VkpiCnq/491W67hnMWmDu7e6Ztw0C3WldRYTXkg3SumfdzZxLBHA==
531+
dependencies:
532+
"@turf/helpers" "^6.5.0"
533+
507534
"@types/geojson@*", "@types/geojson@^7946.0.10":
508535
version "7946.0.10"
509536
resolved "https://registry.yarnpkg.com/@types/geojson/-/geojson-7946.0.10.tgz#6dfbf5ea17142f7f9a043809f1cd4c448cb68249"

0 commit comments

Comments
 (0)