Skip to content

Commit

Permalink
Split out parsing logic for criticals
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Aug 30, 2023
1 parent 74c90a8 commit f1ede7a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 60 deletions.
63 changes: 63 additions & 0 deletions src/lib/browse/data.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<FeatureCollection> {
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;
}
62 changes: 2 additions & 60 deletions src/lib/browse/layers/CriticalIssuesLayerControl.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<script lang="ts">
import type { FeatureCollection } from "geojson";
import type { GeoJSONSource, MapGeoJSONFeature } from "maplibre-gl";
import readXlsxFile from "read-excel-file";
import { map } from "../../../stores";
import { CollapsibleCard, ColorLegend, InteractiveLayer } from "../../common";
import {
Expand All @@ -14,8 +12,8 @@
cleanupSource,
emptyGeojson,
overwriteCircleLayer,
setPrecision,
} from "../../maplibre";
import { parseCriticalIssuesExcel } from "../data";
let fileInput: HTMLInputElement;
Expand Down Expand Up @@ -77,65 +75,9 @@
}
}
interface Row {
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;
}
async function parseExcel(): Promise<FeatureCollection> {
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(fileInput.files![0], {
map: mapping,
sheet: "Form Input",
});
let gj: FeatureCollection = {
type: "FeatureCollection",
features: [],
};
for (let row of rows as Row[]) {
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;
}
async function onChange(e: Event) {
try {
let gj = await parseExcel();
let gj = await parseCriticalIssuesExcel(fileInput.files![0]);
($map.getSource(source) as GeoJSONSource).setData(gj);
numberIssues = gj.features.length;
errorMessage = "";
Expand Down

0 comments on commit f1ede7a

Please sign in to comment.