From f1ede7a84e56695bb67b43e2c89215898064bbc4 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Wed, 30 Aug 2023 13:45:35 +0100 Subject: [PATCH] Split out parsing logic for criticals --- src/lib/browse/data.ts | 63 +++++++++++++++++++ .../layers/CriticalIssuesLayerControl.svelte | 62 +----------------- 2 files changed, 65 insertions(+), 60 deletions(-) diff --git a/src/lib/browse/data.ts b/src/lib/browse/data.ts index aa853205d..cbf7b6f2d 100644 --- a/src/lib/browse/data.ts +++ b/src/lib/browse/data.ts @@ -1,4 +1,6 @@ import type { FeatureCollection } from "geojson"; +import readXlsxFile from "read-excel-file"; +import { setPrecision } from "../maplibre"; // This must be filled out in the input file interface SchemeData { @@ -47,3 +49,64 @@ export function processInput( return schemes; } + +interface CriticalRow { + id: string; + inspector: string; + submission_time: Date; + scheme_reference: string; + current_design_stage: string; + critical_type: string; + lat_lon: string; + location_description: string; + notes: string; +} + +// Returns a FeatureCollection of points. The properties are described by +// CriticalRow (with the exception that submission_time is already formatted as +// a string). +export async function parseCriticalIssuesExcel( + input: File +): Promise { + let mapping = { + ID: "id", + Inspector: "inspector", + "Submission Time": "submission_time", + "Please enter the Scheme Reference number, (e.g. ATF2_WYO_01)": + "scheme_reference", + "Please enter current Design Stage": "current_design_stage", + "Select Critical Issue type below:": "critical_type", + "Please Enter Latitude and Longitude \n(Right click on location in Google, left click on information to copy to clipboard, paste below)": + "lat_lon", + Column1: "location_description", + "Enter any additional information (e.g. any comments or notes about this critical issue)": + "notes", + }; + + let { rows } = await readXlsxFile(input, { + map: mapping, + sheet: "Form Input", + }); + + let gj: FeatureCollection = { + type: "FeatureCollection", + features: [], + }; + for (let row of rows as CriticalRow[]) { + let coordinates = setPrecision( + row.lat_lon.split(",").map(parseFloat).reverse() + ); + let properties: { [name: string]: any } = row; + properties.submission_time = row.submission_time.toLocaleString(); + gj.features.push({ + type: "Feature", + properties, + geometry: { + type: "Point", + coordinates, + }, + id: gj.features.length + 1, + }); + } + return gj; +} diff --git a/src/lib/browse/layers/CriticalIssuesLayerControl.svelte b/src/lib/browse/layers/CriticalIssuesLayerControl.svelte index e83e587ca..2aca7759c 100644 --- a/src/lib/browse/layers/CriticalIssuesLayerControl.svelte +++ b/src/lib/browse/layers/CriticalIssuesLayerControl.svelte @@ -1,7 +1,5 @@