|
43 | 43 |
|
44 | 44 | const outputFormats: string[] = ["geojson", "csv", "geojsonseq"];
|
45 | 45 | let selectedOutputFormat: string = "csv";
|
46 |
| - let bboxValue: string = ""; |
| 46 | + let bounds: |
| 47 | + | { _sw: { lat: number; lng: number }; _ne: { lat: number; lng: number } } |
| 48 | + | undefined; |
| 49 | + let bbox: string = ""; |
47 | 50 | let hidden8 = false;
|
48 | 51 | let transitionParams = {
|
49 | 52 | y: 320,
|
50 | 53 | duration: 200,
|
51 | 54 | easing: sineIn,
|
52 | 55 | };
|
53 | 56 |
|
54 |
| - // Event listener to get bounding box on map load and on view change |
55 |
| - // $map.on("load", updateBoundingBox); |
56 |
| - // $map.on("moveend", updateBoundingBox); |
57 |
| - // updateBoundingBox(); |
58 |
| - // let bboxForRequest = bbox.map((el) => Number(el.toFixed(6))); |
59 |
| - // console.log("Bbox", bboxForRequest); |
60 |
| -
|
61 |
| - async function downloadMetrics(dataRequestSpec): Promise<any> { |
| 57 | + async function downloadMetrics(dataRequestSpec: {}): Promise<any> { |
62 | 58 | try {
|
63 | 59 | console.log(dataRequestSpec);
|
64 | 60 | if (!(await $rustBackend!.isLoaded())) {
|
|
79 | 75 | }
|
80 | 76 | }
|
81 | 77 |
|
82 |
| - onMount(async () => { |
83 |
| - try { |
84 |
| - // const metricsDownload = $selectedMetricsList.map((record) => ({ |
85 |
| - // MetricId: { |
86 |
| - // id: record.metric_id, |
87 |
| - // }, |
88 |
| - // })); |
89 |
| - // console.log(metricsDownload); |
90 |
| - // let dataRequestSpec = { |
91 |
| - // region: [], |
92 |
| - // metrics: metricsDownload, |
93 |
| - // years: [], |
94 |
| - // }; |
95 |
| - // const metrics = await downloadMetrics(dataRequestSpec); |
96 |
| - // $previewedMetricsList = metrics; |
97 |
| - // console.log($previewedMetricsList.slice(0, 10)); |
98 |
| - // return; |
99 |
| - } catch (err) { |
100 |
| - console.log("Download"); |
101 |
| - window.alert(`${err}`); |
102 |
| - } |
103 |
| - }); |
104 |
| -
|
105 | 78 | async function setPreviewedMetrics(): Promise<Array<{}>> {
|
| 79 | + // Get metrics |
106 | 80 | const metricsDownload = $selectedMetricsList.map((record) => ({
|
107 | 81 | MetricId: {
|
108 | 82 | id: record.metric_id,
|
|
117 | 91 | const metrics = await downloadMetrics(dataRequestSpec);
|
118 | 92 | $previewedMetricsList = metrics;
|
119 | 93 |
|
120 |
| - // TODO: set geometry from dataRequestSpec |
| 94 | + // Get and set PMTiles URL from data request spec |
121 | 95 | const loaded = await $rustBackend!.isLoaded();
|
122 | 96 | if (!loaded) {
|
123 | 97 | await $rustBackend!.initialise();
|
|
139 | 113 | .map((record: {}) => `--id ${record.metric_id}`)
|
140 | 114 | .join(" ");
|
141 | 115 | let outputFormatStr = "--output-format " + selectedOutputFormat;
|
142 |
| - let bboxStr = bboxValue === "" ? "" : "--bbox " + bboxValue; |
| 116 | + let bboxStr = bbox === "" ? "" : "--bbox " + bbox; |
143 | 117 |
|
144 | 118 | return ["popgetter", "data", ids, outputFormatStr, bboxStr].join(" ");
|
145 | 119 | }
|
|
180 | 154 | }
|
181 | 155 | }
|
182 | 156 |
|
183 |
| - // Assign bounding box from map |
184 |
| - // TODO: fix this, currently returns not initialized |
185 |
| - let mapInstance; |
186 |
| - function updateBoundingBox(): string { |
187 |
| - if (mapInstance) { |
188 |
| - const bounds = mapInstance.getBounds().toArray(); |
189 |
| - console.log("Bounding Box:", bounds); |
190 |
| - let bbox = [ |
191 |
| - bounds.getWest(), |
192 |
| - bounds.getSouth(), |
193 |
| - bounds.getEast(), |
194 |
| - bounds.getNorth(), |
195 |
| - ]; |
196 |
| - return bbox.map((el) => Number(el.toFixed(6)).toString()).join(","); |
197 |
| - } else { |
198 |
| - console.error("Map instance is not yet initialized."); |
199 |
| - return ""; |
200 |
| - } |
| 157 | + // Assign bounding box from bounds |
| 158 | + function updateBoundingBox() { |
| 159 | + console.log("Bounds:", bounds); |
| 160 | + bbox = [bounds._sw.lng, bounds._sw.lat, bounds._ne.lng, bounds._ne.lat] |
| 161 | + .map((el) => Number(el.toFixed(6)).toString()) |
| 162 | + .join(","); |
201 | 163 | }
|
202 | 164 |
|
203 | 165 | async function download(dataRequestSpec: {}): Promise<String> {
|
|
292 | 254 | }
|
293 | 255 | const debouncedHandleInput = debounce(handleInput, 300);
|
294 | 256 |
|
295 |
| - async function handleClick() { |
296 |
| - // let bboxForRequest = bbox.map((el) => Number(el.toFixed(6))); |
297 |
| - let bboxForRequest = bboxValue; |
298 |
| - console.log("Bbox", bboxForRequest); |
299 |
| -
|
300 |
| - // TODO: create data request spec when no bbox is given |
| 257 | + async function downloadAndSave() { |
| 258 | + console.log("Bbox", bbox); |
301 | 259 | let dataRequestSpec = {
|
302 |
| - region: [ |
303 |
| - { |
304 |
| - BoundingBox: bboxForRequest |
305 |
| - .split(",") |
306 |
| - .map((el) => Number(Number(el).toFixed(6))), |
307 |
| - }, |
308 |
| - ], |
309 |
| - // metrics: [{ MetricId: { id: $previewMetricMap.metric_id } }], |
| 260 | + region: |
| 261 | + bbox === "" |
| 262 | + ? [] |
| 263 | + : [ |
| 264 | + { |
| 265 | + BoundingBox: bbox |
| 266 | + .split(",") |
| 267 | + .map((el) => Number(Number(el).toFixed(6))), |
| 268 | + }, |
| 269 | + ], |
310 | 270 | metrics: $selectedMetricsList.map((metric) => ({
|
311 | 271 | MetricId: {
|
312 | 272 | id: metric.metric_id,
|
|
374 | 334 | <!-- Map previews downloaded metrics -->
|
375 | 335 |
|
376 | 336 | <div slot="map">
|
377 |
| - <TilesMap bind:mapInstance></TilesMap> |
| 337 | + <TilesMap bind:bounds></TilesMap> |
378 | 338 |
|
379 | 339 | <div>
|
380 | 340 | <Drawer
|
|
403 | 363 | <p class="text-sm text-gray-500 dark:text-gray-400">
|
404 | 364 | <b
|
405 | 365 | >Preview of selected metrics <button
|
406 |
| - on:click={() => handleClick()}>(view selected on map)</button |
| 366 | + on:click={() => downloadAndSave()} |
| 367 | + >(view selected on map)</button |
407 | 368 | ></b
|
408 | 369 | >
|
409 | 370 | </p>
|
|
456 | 417 | </Table>
|
457 | 418 | </section>
|
458 | 419 | </TabItem>
|
459 |
| - <TabItem title="Download" on:click={() => setPreviewedMetrics()}> |
| 420 | + <TabItem |
| 421 | + title="Download" |
| 422 | + on:click={() => { |
| 423 | + updateBoundingBox(); |
| 424 | + setPreviewedMetrics(); |
| 425 | + }} |
| 426 | + > |
460 | 427 | <div class="pt-8">
|
461 | 428 | <div
|
462 | 429 | style="text-align: left; margin-top: 0.5%; margin-bottom: 0.5%; "
|
|
469 | 436 | id="bbox"
|
470 | 437 | type="text"
|
471 | 438 | placeholder=""
|
472 |
| - bind:value={bboxValue} |
| 439 | + bind:value={bbox} |
473 | 440 | />
|
474 | 441 | <Button
|
475 | 442 | color="light"
|
|
526 | 493 | <Button
|
527 | 494 | color="light"
|
528 | 495 | on:click={() => {
|
529 |
| - handleClick(); |
| 496 | + downloadAndSave(); |
530 | 497 | }}>Download</Button
|
531 | 498 | >
|
532 | 499 | </div>
|
|
0 commit comments