Skip to content

Commit fc7b06e

Browse files
committed
refactor: move things to stac utils
1 parent c6035ba commit fc7b06e

File tree

2 files changed

+80
-74
lines changed

2 files changed

+80
-74
lines changed

src/app.tsx

Lines changed: 7 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@ import useStacChildren from "./hooks/stac-children";
88
import useStacValue from "./hooks/stac-value";
99
import type { BBox2D, Color } from "./types/map";
1010
import type { DatetimeBounds, StacValue } from "./types/stac";
11-
import { getItemDatetimes } from "./utils/stac";
11+
import {
12+
isCollectionInBbox,
13+
isCollectionInDatetimeBounds,
14+
isItemInBbox,
15+
isItemInDatetimeBounds,
16+
} from "./utils/stac";
1217

1318
// TODO make this configurable by the user.
1419
const lineColor: Color = [207, 63, 2, 100];
1520
const fillColor: Color = [207, 63, 2, 50];
1621

1722
export default function App() {
18-
// State
23+
// User state
1924
const [href, setHref] = useState<string | undefined>(getInitialHref());
2025
const fileUpload = useFileUpload({ maxFiles: 1 });
2126
const [userCollections, setCollections] = useState<StacCollection[]>();
@@ -195,73 +200,3 @@ function getInitialHref() {
195200
}
196201
return href;
197202
}
198-
199-
function isCollectionInBbox(collection: StacCollection, bbox: BBox2D) {
200-
if (bbox[2] - bbox[0] >= 360) {
201-
// A global bbox always contains every collection
202-
return true;
203-
}
204-
const collectionBbox = collection?.extent?.spatial?.bbox?.[0];
205-
if (collectionBbox) {
206-
return (
207-
!(
208-
collectionBbox[0] < bbox[0] &&
209-
collectionBbox[1] < bbox[1] &&
210-
collectionBbox[2] > bbox[2] &&
211-
collectionBbox[3] > bbox[3]
212-
) &&
213-
!(
214-
collectionBbox[0] > bbox[2] ||
215-
collectionBbox[1] > bbox[3] ||
216-
collectionBbox[2] < bbox[0] ||
217-
collectionBbox[3] < bbox[1]
218-
)
219-
);
220-
} else {
221-
return false;
222-
}
223-
}
224-
225-
function isCollectionInDatetimeBounds(
226-
collection: StacCollection,
227-
bounds: DatetimeBounds
228-
) {
229-
const interval = collection.extent.temporal.interval[0];
230-
const start = interval[0] ? new Date(interval[0]) : null;
231-
const end = interval[1] ? new Date(interval[1]) : null;
232-
return !((end && end < bounds.start) || (start && start > bounds.end));
233-
}
234-
235-
function isItemInBbox(item: StacItem, bbox: BBox2D) {
236-
if (bbox[2] - bbox[0] >= 360) {
237-
// A global bbox always contains every item
238-
return true;
239-
}
240-
const itemBbox = item.bbox;
241-
if (itemBbox) {
242-
return (
243-
!(
244-
itemBbox[0] < bbox[0] &&
245-
itemBbox[1] < bbox[1] &&
246-
itemBbox[2] > bbox[2] &&
247-
itemBbox[3] > bbox[3]
248-
) &&
249-
!(
250-
itemBbox[0] > bbox[2] ||
251-
itemBbox[1] > bbox[3] ||
252-
itemBbox[2] < bbox[0] ||
253-
itemBbox[3] < bbox[1]
254-
)
255-
);
256-
} else {
257-
return false;
258-
}
259-
}
260-
261-
function isItemInDatetimeBounds(item: StacItem, bounds: DatetimeBounds) {
262-
const datetimes = getItemDatetimes(item);
263-
return !(
264-
(datetimes.end && datetimes.end < bounds.start) ||
265-
(datetimes.start && datetimes.start > bounds.end)
266-
);
267-
}

src/utils/stac.ts

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { UseFileUploadReturn } from "@chakra-ui/react";
2-
import type { StacItem } from "stac-ts";
3-
import type { StacValue } from "../types/stac";
2+
import type { StacCollection, StacItem } from "stac-ts";
3+
import type { BBox2D } from "../types/map";
4+
import type { DatetimeBounds, StacValue } from "../types/stac";
45

56
export async function getStacJsonValue(
67
href: string,
@@ -133,3 +134,73 @@ export function getItemDatetimes(item: StacItem) {
133134
: null;
134135
return { start, end };
135136
}
137+
138+
export function isCollectionInBbox(collection: StacCollection, bbox: BBox2D) {
139+
if (bbox[2] - bbox[0] >= 360) {
140+
// A global bbox always contains every collection
141+
return true;
142+
}
143+
const collectionBbox = collection?.extent?.spatial?.bbox?.[0];
144+
if (collectionBbox) {
145+
return (
146+
!(
147+
collectionBbox[0] < bbox[0] &&
148+
collectionBbox[1] < bbox[1] &&
149+
collectionBbox[2] > bbox[2] &&
150+
collectionBbox[3] > bbox[3]
151+
) &&
152+
!(
153+
collectionBbox[0] > bbox[2] ||
154+
collectionBbox[1] > bbox[3] ||
155+
collectionBbox[2] < bbox[0] ||
156+
collectionBbox[3] < bbox[1]
157+
)
158+
);
159+
} else {
160+
return false;
161+
}
162+
}
163+
164+
export function isCollectionInDatetimeBounds(
165+
collection: StacCollection,
166+
bounds: DatetimeBounds
167+
) {
168+
const interval = collection.extent.temporal.interval[0];
169+
const start = interval[0] ? new Date(interval[0]) : null;
170+
const end = interval[1] ? new Date(interval[1]) : null;
171+
return !((end && end < bounds.start) || (start && start > bounds.end));
172+
}
173+
174+
export function isItemInBbox(item: StacItem, bbox: BBox2D) {
175+
if (bbox[2] - bbox[0] >= 360) {
176+
// A global bbox always contains every item
177+
return true;
178+
}
179+
const itemBbox = item.bbox;
180+
if (itemBbox) {
181+
return (
182+
!(
183+
itemBbox[0] < bbox[0] &&
184+
itemBbox[1] < bbox[1] &&
185+
itemBbox[2] > bbox[2] &&
186+
itemBbox[3] > bbox[3]
187+
) &&
188+
!(
189+
itemBbox[0] > bbox[2] ||
190+
itemBbox[1] > bbox[3] ||
191+
itemBbox[2] < bbox[0] ||
192+
itemBbox[3] < bbox[1]
193+
)
194+
);
195+
} else {
196+
return false;
197+
}
198+
}
199+
200+
export function isItemInDatetimeBounds(item: StacItem, bounds: DatetimeBounds) {
201+
const datetimes = getItemDatetimes(item);
202+
return !(
203+
(datetimes.end && datetimes.end < bounds.start) ||
204+
(datetimes.start && datetimes.start > bounds.end)
205+
);
206+
}

0 commit comments

Comments
 (0)