forked from HSLdevcom/digitransit-ui
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scripts): add script for parsing layers config
- Loading branch information
Andreas Helms
authored and
Andreas Helms
committed
Dec 17, 2024
1 parent
56a74c4
commit b90250a
Showing
4 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters