diff --git a/src/lib/DataConverter.svelte b/src/lib/DataConverter.svelte index fe8add4..859804e 100644 --- a/src/lib/DataConverter.svelte +++ b/src/lib/DataConverter.svelte @@ -2,7 +2,7 @@ import { writable } from 'svelte/store'; import { fetchData, exportCSV, exportKML } from './DataUtils'; - let query = ''; // User input for data query + let query = ''; const processing = writable(false); const errorMessage = writable(''); let csvDownloadUrl = ''; @@ -53,12 +53,11 @@ {/if} {#if csvDownloadUrl} - Download CSV
+ Download CSV
{/if} {#if kmlDownloadUrl} - Download KML + Download KML {/if}
Converts BMLT data from JSON to CSV or KML
diff --git a/src/lib/DataUtils.ts b/src/lib/DataUtils.ts index dc9152a..1145124 100644 --- a/src/lib/DataUtils.ts +++ b/src/lib/DataUtils.ts @@ -1,5 +1,22 @@ import fetchJsonp from 'fetch-jsonp'; +interface Meeting { + meeting_name: string; + longitude: string; + latitude: string; + weekday_tinyint: string; + start_time: string; + location_text: string; + location_street: string; + location_city_subsection: string; + location_municipality: string; + location_neighborhood: string; + location_province: string; + location_postal_code_1: string; + location_nation: string; + location_info: string; +} + export async function fetchData(query: string): Promise { try { if (!query.includes('/client_interface/json')) { @@ -49,21 +66,30 @@ export function exportCSV(data: any[]): string { return URL.createObjectURL(blob); } -export function exportKML(data: any[]): string { - let kmlContent = ` +export function exportKML(data: Meeting[]): string { + const placemarks = data.map(createPlacemark).filter(Boolean).join('\n'); + + const kmlContent = ` - `; - - // Add place marks - data.forEach((meeting) => { - const name = meeting['meeting_name'].trim() || 'NA Meeting'; - const lng = parseFloat(meeting['longitude']); - const lat = parseFloat(meeting['latitude']); - if (!lng || !lat) return ''; - const description = prepareSimpleLine(meeting); - const address = prepareSimpleLine(meeting, false); - - kmlContent += ` + + ${placemarks} + +`; + + const blob = new Blob([kmlContent], { type: 'application/vnd.google-earth.kml+xml' }); + return URL.createObjectURL(blob); +} + +function createPlacemark(meeting: Meeting): string { + const name = meeting.meeting_name.trim() || 'NA Meeting'; + const lng = parseFloat(meeting.longitude); + const lat = parseFloat(meeting.latitude); + if (!lng || !lat) return ''; + + const description = prepareSimpleLine(meeting); + const address = prepareSimpleLine(meeting, false); + + return ` ${name} ${address ? `
${address}
` : ''} @@ -72,27 +98,15 @@ export function exportKML(data: any[]): string { ${lng},${lat}
-`; - }); - - kmlContent += ` - -`; - - const blob = new Blob([kmlContent], { type: 'application/vnd.google-earth.kml+xml' }); - return URL.createObjectURL(blob); + `.trim(); } -function prepareSimpleLine(meeting: { [x: string]: any }, withDate = true): string { +function prepareSimpleLine(meeting: Meeting, withDate = true): string { const getLocationInfo = () => { - const locationInfo: any[] = []; - const addInfo = (property: string) => { - if (property in meeting) { - const value = meeting[property].trim(); - if (value) { - locationInfo.push(value); - } - } + const locationInfo: string[] = []; + const addInfo = (property: keyof Meeting) => { + const value = meeting[property]?.trim() ?? ''; + if (value) locationInfo.push(value); }; addInfo('location_text'); @@ -119,10 +133,10 @@ function prepareSimpleLine(meeting: { [x: string]: any }, withDate = true): stri 'Friday', 'Saturday' ]; - const weekday = parseInt(meeting['weekday_tinyint'].trim()); + const weekday = parseInt(meeting.weekday_tinyint?.trim() ?? '0'); const weekdayString = weekday_strings[weekday]; - const startTime = `2000-01-01 ${meeting['start_time']}`; + const startTime = `2000-01-01 ${meeting.start_time}`; const time = new Date(startTime); if (weekdayString && withDate) {