Skip to content

Commit

Permalink
Added parsing for taxi and haltestellen-id
Browse files Browse the repository at this point in the history
  • Loading branch information
BolverBlitz committed Jan 1, 2025
1 parent 969e7cf commit 95ab5bb
Showing 1 changed file with 71 additions and 11 deletions.
82 changes: 71 additions & 11 deletions tools/update_static.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require("fs");
const path = require("path");
const fs = require("node:fs");
const path = require("node:path");
const XLSX = require("xlsx");

const opendata_keys = [
Expand Down Expand Up @@ -84,24 +84,71 @@ const transformSheetByKey = (workbook, columnKey) => {
const sheetName = workbook.SheetNames[0];
const sheet = workbook.Sheets[sheetName];

const rows = XLSX.utils.sheet_to_json(sheet, {
raw: false,
dateNF: "hh:mm"
});

const result = {};
rows.forEach((row) => {
const keyValue = row[columnKey];
const { [columnKey]: _, ...restOfRow } = row;

// Convert boolean values (x => true, nein => false, etc.)
const formattedRow = Object.fromEntries(
Object.entries(restOfRow).map(([field, value]) => [field, parseBoolean(value)])
);

result[keyValue] = formattedRow;
});

return result;
}

const transformStationsSheet = (workbook) => {
const sheetName = workbook.SheetNames[0];
const sheet = workbook.Sheets[sheetName];

const rows = XLSX.utils.sheet_to_json(sheet);

const result = {};
rows.forEach((row) => {
const keyValue = row[columnKey];
const { [columnKey]: _, ...restOfRow } = row;

// Convert boolean values (x => true, nein => false, etc.)
const formattedRow = Object.fromEntries(
Object.entries(restOfRow).map(([field, value]) => [field, parseBoolean(value)])
);
rows.forEach((row) => {
// Convert boolean values (x => true, nein => false, etc.)
const parsedRow = Object.fromEntries(
Object.entries(row).map(([field, value]) => [field, parseBoolean(value)])
);

// Use VGNKennung as the key for the station (Because thats what PULS uses)
const vgnKey = parsedRow.VGNKennung;
if (!vgnKey) return; // Skip broken rows

// If its a unknown VGNKennung, create a new entry
if (!result[vgnKey]) {
result[vgnKey] = {
VAGKennung: parsedRow.VAGKennung,
Platforms: {}
};
}

result[keyValue] = formattedRow;
});
// Insert the platform data
const platformKey = parsedRow.Haltepunkt;
if (platformKey) {
result[vgnKey].Platforms[platformKey] = {
GlobalID: parsedRow.GlobalID,
Haltestellenname: parsedRow.Haltestellenname,
latitude: parsedRow.latitude,
longitude: parsedRow.longitude,
Betriebszweig: parsedRow.Betriebszweig,
Dataprovider: parsedRow.Dataprovider
};
}
});

return result;
}


(async () => {
const avaible_keys = await checkPackageList();

Expand All @@ -113,6 +160,9 @@ const transformSheetByKey = (workbook, columnKey) => {
let fileToWrite = {};

switch (key) {
case "geokoordinaten-taxi-warteplatze":
fileToWrite = transformSheetByKey(workbook, "Name");
break;
case "steighoehen-tram":
fileToWrite = transformSheetByKey(workbook, "Haltestelle");
break;
Expand All @@ -131,6 +181,9 @@ const transformSheetByKey = (workbook, columnKey) => {
case "fuhrpark-bus-ausstattung":
fileToWrite = transformSheetByKey(workbook, "Betriebsnummern");
break;
case "haltestellen-id-geodaten":
fileToWrite = transformStationsSheet(workbook);
break;
default:
console.error(`Key ${key} is not implemented`);
break;
Expand All @@ -144,5 +197,12 @@ const transformSheetByKey = (workbook, columnKey) => {
return true;
});

fs.writeFile(`${path.join(__dirname, "../static_humanreadable")}/${key}.json`, JSON.stringify(fileToWrite, null, 2), err => {
if (err) {
console.error(err);
return false;
}
return true;
});
}
})()

0 comments on commit 95ab5bb

Please sign in to comment.