Skip to content

Commit

Permalink
feat(scripts): add script for parsing layers config
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Helms authored and Andreas Helms committed Dec 17, 2024
1 parent 56a74c4 commit b90250a
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 1 deletion.
19 changes: 18 additions & 1 deletion docs/Common-stadtnavi-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ CONFIG=herrenberg \

## Changing the submodules

Digitransit is split up into several submodules. They live in
Digitransit is split up into several submodules. They live in

- `digitransit-component`
- `digitransit-search-util`
Expand All @@ -50,3 +50,20 @@ yarn run digitransit-watch-components
```

This watches if any of the submodules has been changed and compiles it on-the-fly.

## Import map layers config

Map layers config from layers spreadsheet can be converted from CSV to JSON running

```
node ./scripts/parse-layers-sheet.js
```

Before running the script make sure to have CSV export of layers spreadsheet
in place and to have set the correct input and output file names in the script.

Created JSON file can be used to replace files in

```
./app/configurations/layers/
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
"classnames": "2.2.6",
"connect-redis": "5.0.0",
"cookie-parser": "1.4.5",
"csv-parser": "^3.0.0",
"debug": "4.1.1",
"esm": "3.2.25",
"express": "4.17.1",
Expand Down
119 changes: 119 additions & 0 deletions scripts/parse-layers-sheet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* eslint-disable dot-notation */
const fs = require('fs');
const csv = require('csv-parser');

const results = [];

fs.createReadStream('layers.csv')
.pipe(csv())
.on('data', data => results.push(data))
.on('end', () => {
const mergedLayers = {};

results.forEach(row => {
if (!mergedLayers[row['Oberkategorie Code']]) {
mergedLayers[row['Oberkategorie Code']] = {
code: row['Oberkategorie Code'],
translations: {
de: row['Oberkategorie Deutsch'],
en: row['Oberkategorie Englisch'],
},
categories: [],
};
}

let category = mergedLayers[row['Oberkategorie Code']].categories.find(
cat => cat.code === row['Kategorie Code'],
);

if (!category) {
category = {
code: row['Kategorie Code'],
translations: {
de: row['Kategorie Deutsch'],
en: row['Kategorie Englisch'],
},
categories: [],
properties: null,
};
mergedLayers[row['Oberkategorie Code']].categories.push(category);
}

if (row['Unterkategorie Code']) {
category.categories.push({
code: row['Unterkategorie Code'],
translations: {
de: row['Unterkategorie Deutsch'],
en: row['Unterkategorie Englisch'],
},
properties: {
layer: {
type: row['Layer-Typ'],
url: row['Url'],
priority: parseInt(row['Priorisierung bei Überlappung'], 10),
min_zoom: parseInt(row['Minimales Zoomlevel'], 10),
},
icon: {
svg: row['Icon .svg'],
origin: row['Herkunft Symbol'],
background_color: row['Icon: Farbe Hintergrund_Code'],
color: row['Icon: Farbe Symbol'],
},
attributes: row['Eigenschaften']
.split(',')
.map(attr => attr.trim()),
osm: {
filter: row['OSM-Filter'],
example: row['Beispielobjekt aus OSM'],
wiki: row['OSM-Wiki-Seite'],
overpass_query: row['Overpass Query'],
},
misc: {
comment: row['Kommentar'],
origin: row['Herkunft'],
},
},
});
} else {
category.properties = {
layer: {
type: row['Layer-Typ'],
url: row['Url'],
priority: parseInt(row['Priorisierung bei Überlappung'], 10),
min_zoom: parseInt(row['Minimales Zoomlevel'], 10),
},
icon: {
svg: row['Icon .svg'],
origin: row['Herkunft Symbol'],
background_color: row['Icon: Farbe Hintergrund_Code'],
color: row['Icon: Farbe Symbol'],
},
attributes: row['Eigenschaften'].split(',').map(attr => attr.trim()),
osm: {
filter: row['OSM-Filter'],
example: row['Beispielobjekt aus OSM'],
wiki: row['OSM-Wiki-Seite'],
overpass_query: row['Overpass Query'],
},
misc: {
comment: row['Kommentar'],
origin: row['Herkunft'],
},
};
}
});

// Remove categories property if it's an empty array
Object.values(mergedLayers).forEach(layer => {
layer.categories.forEach(category => {
if (category.categories.length === 0) {
// eslint-disable-next-line no-param-reassign
delete category.categories;
}
});
});

const jsonOutput = Object.values(mergedLayers);

fs.writeFileSync('layers.json', JSON.stringify(jsonOutput, null, 2));
});
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10809,6 +10809,17 @@ __metadata:
languageName: node
linkType: hard

"csv-parser@npm:^3.0.0":
version: 3.0.0
resolution: "csv-parser@npm:3.0.0"
dependencies:
minimist: ^1.2.0
bin:
csv-parser: bin/csv-parser
checksum: adc9d67d9f185249825570778c24d13004625301655330f6b735a052b9fdfbe1a239a014afb1f89939e0626ee573718f71f9f14164db7c17e4bcb2f38d6a162b
languageName: node
linkType: hard

"current-script-polyfill@npm:^1.0.0":
version: 1.0.0
resolution: "current-script-polyfill@npm:1.0.0"
Expand Down Expand Up @@ -11407,6 +11418,7 @@ __metadata:
cookie-parser: 1.4.5
copy-webpack-plugin: 6.1.0
css-loader: ^5.2.7
csv-parser: ^3.0.0
debug: 4.1.1
documentation: 13.2.5
dotenv: ^10.0.0
Expand Down

0 comments on commit b90250a

Please sign in to comment.