MunicipalitiesLoader.kt 1.24 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package org.memobase

import org.apache.logging.log4j.LogManager
import kotlin.system.exitProcess

object MunicipalitiesLoader {

    private val log = LogManager.getLogger("MunicipalitiesLoader")

    fun getMunicipalities(): Map<String, Municipality> {
        val stream = ClassLoader.getSystemResourceAsStream("municipalities.tsv")
        if (stream != null) {
            return stream.bufferedReader().lineSequence().filterNot {
                it.startsWith("item")
            }.map {
                val values = it.split("\t")
                Municipality(
                    values[1].trim('"').split(", "),
                    values[2].trim('<', '>'),
                    values[0].trim('<', '>'),
                    values[3].replace("@de", "").trim('"'),
                    values[4].replace("@fr", "").trim('"'),
                    values[5].replace("@it", "").trim('"'),
                    values[6].split(",")
                )
            }.map { municipality ->
                municipality.postalCodes.map { code ->
                    Pair(code, municipality)
                }
            }.flatten().toMap()
        } else {
            log.error("Could not load municipalities.tsv from classpath!")
            exitProcess(1)
        }
    }
}