Skip to content

Commit

Permalink
Add meeting interface (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaudiomv authored Feb 17, 2024
1 parent 5e9735f commit 61d1a5c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 39 deletions.
9 changes: 4 additions & 5 deletions src/lib/DataConverter.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -53,12 +53,11 @@
{/if}

{#if csvDownloadUrl}
<a href={csvDownloadUrl} class="download-links" download="exported_data.csv">Download CSV</a
><br />
<a href={csvDownloadUrl} class="download-links" download="BMLT_data.csv">Download CSV</a><br
/>
{/if}
{#if kmlDownloadUrl}
<a href={kmlDownloadUrl} class="download-links" download="exported_data.kml">Download KML</a
>
<a href={kmlDownloadUrl} class="download-links" download="BMLT_data.kml">Download KML</a>
{/if}
<div id="description">Converts BMLT data from JSON to CSV or KML</div>
</div>
Expand Down
82 changes: 48 additions & 34 deletions src/lib/DataUtils.ts
Original file line number Diff line number Diff line change
@@ -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<any[]> {
try {
if (!query.includes('/client_interface/json')) {
Expand Down Expand Up @@ -49,21 +66,30 @@ export function exportCSV(data: any[]): string {
return URL.createObjectURL(blob);
}

export function exportKML(data: any[]): string {
let kmlContent = `<?xml version="1.0" encoding="UTF-8"?>
export function exportKML(data: Meeting[]): string {
const placemarks = data.map(createPlacemark).filter(Boolean).join('\n');

const kmlContent = `<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>`;

// 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 += `
<Document>
${placemarks}
</Document>
</kml>`;

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 `
<Placemark>
<name>${name}</name>
${address ? `<address>${address}</address>` : ''}
Expand All @@ -72,27 +98,15 @@ export function exportKML(data: any[]): string {
<coordinates>${lng},${lat}</coordinates>
</Point>
</Placemark>
`;
});

kmlContent += `
</Document>
</kml>`;

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');
Expand All @@ -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) {
Expand Down

0 comments on commit 61d1a5c

Please sign in to comment.