@janekolszak/poland 1.0.7 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  #!/usr/bin/env node
2
- import { parseCsv, parseLocalityCsv, parseLocalityTypeCsv } from "./src/csvparse"
2
+ import { parseCsv, parseLocalityCsv, parseLocalityTypeCsv, parseStreetsCsv } from "./src/csvparse"
3
3
  import { compute } from "./src/compute"
4
4
  import { generate } from "./src/generate"
5
5
 
6
6
 
7
7
  (() => {
8
- Promise.all([parseCsv(), parseLocalityCsv(), parseLocalityTypeCsv()])
9
- .then(r => compute(r[0], r[1], r[2]))
8
+ Promise.all([parseCsv(), parseLocalityCsv(), parseLocalityTypeCsv(), parseStreetsCsv()])
9
+ .then(r => compute(r[0], r[1], r[2], r[3]))
10
10
  .then(generate)
11
11
  })();
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@janekolszak/poland",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Administrative division of Poland",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
7
- "generate": "tsc && node ./dist/index.js -o ./generated -i ./TERC_Urzedowy_2023-02-12.csv -l ./SIMC_Urzedowy_2023-02-12.csv -t ./WMRODZ_2023-02-12.csv"
7
+ "generate": "tsc && node ./dist/index.js -o ./generated -i ./TERC_Urzedowy_2023-02-12.csv -l ./SIMC_Urzedowy_2023-02-12.csv -t ./WMRODZ_2023-02-12.csv -s ./ULIC_Adresowy_2023-02-19.csv"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
package/src/args.ts CHANGED
@@ -3,6 +3,7 @@ import { parse } from 'ts-command-line-args';
3
3
  export interface Args {
4
4
  input: string;
5
5
  localities: string;
6
+ streets: string;
6
7
  types: string;
7
8
  output: string;
8
9
  help?: boolean;
@@ -12,6 +13,7 @@ export const args = parse<Args>(
12
13
  {
13
14
  input: { type: String, alias: 'i', description: 'Input TERC CSV as downloaded from https://eteryt.stat.gov.pl/eTeryt/rejestr_teryt/udostepnianie_danych/baza_teryt/uzytkownicy_indywidualni/pobieranie/pliki_pelne.aspx' },
14
15
  localities: { type: String, alias: 'l', description: 'Input SIMC CSV as downloaded from https://eteryt.stat.gov.pl/eTeryt/rejestr_teryt/udostepnianie_danych/baza_teryt/uzytkownicy_indywidualni/pobieranie/pliki_pelne.aspx' },
16
+ streets: { type: String, alias: 's', description: 'Input ULIC CSV as downloaded from https://eteryt.stat.gov.pl/eTeryt/rejestr_teryt/udostepnianie_danych/baza_teryt/uzytkownicy_indywidualni/pobieranie/pliki_pelne.aspx' },
15
17
  types: { type: String, alias: 't', description: 'Input WMRODZ CSV as downloaded from https://eteryt.stat.gov.pl/eTeryt/rejestr_teryt/udostepnianie_danych/baza_teryt/uzytkownicy_indywidualni/pobieranie/pliki_pelne.aspx' },
16
18
  output: { type: String, alias: 'o', description: 'Output file path' },
17
19
  help: { type: Boolean, optional: true, alias: 'h', description: 'Prints this usage guide' },
package/src/compute.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { LocalityType, Region, Locality } from "./csvparse"
1
+ import { LocalityType, Region, Locality, Street } from "./csvparse"
2
2
  import { MultiKeyMap } from "./map"
3
3
 
4
4
  export interface NamesMap {
@@ -12,25 +12,50 @@ export interface NamesMapTwoKeys {
12
12
  export interface NamesMapThreeKeys {
13
13
  [postcode: string]: NamesMapTwoKeys
14
14
  };
15
+
15
16
  export interface NamesMapFourKeys {
16
17
  [postcode: string]: NamesMapThreeKeys
17
18
  };
18
19
 
20
+ export interface NamesMapFiveKeys {
21
+ [postcode: string]: NamesMapFourKeys
22
+ };
23
+
19
24
  export interface Computed {
20
25
  voivodeships: string[]
21
26
  counties: NamesMap
22
27
  municipalities: MultiKeyMap
23
28
  localities: MultiKeyMap
24
29
  districts: MultiKeyMap
30
+ streetsLocalities: MultiKeyMap
31
+ streetsDistricts: MultiKeyMap
25
32
  }
26
33
 
27
- export function compute(regions: Array<Region>, localities: Array<Locality>, localityTypes: Array<LocalityType>): Computed {
34
+
35
+ const getFullName = (f: string, s: string): string => {
36
+ if (!s || s === "") {
37
+ return f
38
+ }
39
+ return f + " " + s
40
+ }
41
+ // RODZ_GMI
42
+ // 1 - gmina miejska,
43
+ // 2 - gmina wiejska,
44
+ // 3 - gmina miejsko-wiejska,
45
+ // 4 - miasto w gminie miejsko-wiejskiej,
46
+ // 5 - obszar wiejski w gminie miejsko-wiejskiej,
47
+ // 8 - dzielnica w m.st. Warszawa,
48
+ // 9 - delegatury miast: Kraków, Łódź, Poznań i Wrocław
49
+
50
+ export function compute(regions: Array<Region>, localities: Array<Locality>, localityTypes: Array<LocalityType>, streets: Array<Street>): Computed {
28
51
  let out: Computed = {
29
52
  voivodeships: [],
30
53
  counties: {},
31
54
  municipalities: new MultiKeyMap(),
32
55
  localities: new MultiKeyMap(),
33
- districts: new MultiKeyMap()
56
+ districts: new MultiKeyMap(),
57
+ streetsLocalities: new MultiKeyMap(),
58
+ streetsDistricts: new MultiKeyMap()
34
59
  }
35
60
 
36
61
  const locTypeIdMap: Map<string, string> = new Map(localityTypes.map(r => [r.name, r.id]))
@@ -72,8 +97,6 @@ export function compute(regions: Array<Region>, localities: Array<Locality>, loc
72
97
  municipalityIdMap.set([r.voivodeship, r.county, r.municipality], r.name)
73
98
  })
74
99
 
75
- console.log(out.municipalities.Get2Deep())
76
-
77
100
  // Miejscowosci
78
101
  let localityTypeIds = [
79
102
  locTypeIdMap.get("wieś"),
@@ -104,7 +127,7 @@ export function compute(regions: Array<Region>, localities: Array<Locality>, loc
104
127
  })
105
128
 
106
129
  // Dzielnice
107
- const allLocalitiesIdMap = new Map(localities.map(r => [r.localityId, r.name]))
130
+ const allLocalitiesIdMap = new Map(localities.map(r => [r.localityId, r]))
108
131
 
109
132
  let districtIds = [
110
133
  locTypeIdMap.get("dzielnica"),
@@ -121,7 +144,7 @@ export function compute(regions: Array<Region>, localities: Array<Locality>, loc
121
144
  const v = vivIdMap.get(r.voivodeship)
122
145
  const c = countyIdMap.get([r.voivodeship, r.county])
123
146
  const m = municipalityIdMap.get([r.voivodeship, r.county, r.municipality])
124
- const l = allLocalitiesIdMap.get(r.baseLocalityId)
147
+ const l = allLocalitiesIdMap.get(r.baseLocalityId)?.name
125
148
 
126
149
  if (!v || !c || !m || !l) {
127
150
  // console.error("Missing voivodeship, county, municipality or base locality for district: " + r.name + " (" + v + ", " + c + ", " + m + ", " + l + ")")
@@ -179,12 +202,105 @@ export function compute(regions: Array<Region>, localities: Array<Locality>, loc
179
202
  d = d ? d : []
180
203
  out.districts.set([v, c, m, l], [...d, name])
181
204
  })
182
- // console.log(out.districts)
183
205
 
206
+ // Ulice w miejscowosciach
207
+ let districtMunicipalityIds = ["8", "9"]
208
+ streets
209
+ .filter(r => !districtMunicipalityIds.includes(r.municipalityTypeId))
210
+ .forEach(r => {
211
+ const v = vivIdMap.get(r.voivodeship)
212
+ const c = countyIdMap.get([r.voivodeship, r.county])
213
+ const m = municipalityIdMap.get([r.voivodeship, r.county, r.municipality])
214
+ const l = allLocalitiesIdMap.get(r.localityId)
215
+
216
+ if (!v || !c || !m || !l) {
217
+ console.error("4 Missing voivodeship, county, municipality or base locality for district: " + r.name + " (" + v + ", " + c + ", " + m + ", " + l + ")")
218
+ console.error(r.voivodeship, r.county, r.municipality)
219
+ return
220
+ }
221
+
222
+ const fullName = getFullName(r.name, r.restName)
223
+
224
+ if (districtIds.includes(l.localityTypeId)) {
225
+ const d = out.districts.get([v, c, m, l.name])
226
+ if (!d) {
227
+ console.error("Missing district: " + r.name)
228
+ return
229
+ }
230
+
231
+ let s = out.streetsDistricts.get([v, c, m, l.name, d])
232
+ s = s ? s : []
233
+ if (!s.includes(fullName)) {
234
+ out.streetsDistricts.set([v, c, m, l.name, d], [...s, fullName])
235
+ }
236
+ } else {
237
+ let s = out.streetsLocalities.get([v, c, m, l.name])
238
+ s = s ? s : []
239
+ if (!s.includes(fullName)) {
240
+ out.streetsLocalities.set([v, c, m, l.name], [...s, fullName])
241
+ }
242
+ }
243
+ })
244
+
245
+ // Ulice w dzielnicach Warszawy
246
+ streets
247
+ .filter(r => r.municipalityTypeId === "8")
248
+ .forEach(r => {
249
+ const v = vivIdMap.get(r.voivodeship)
250
+ const c = countyIdMap.get([r.voivodeship, r.county])
251
+ const m = "Warszawa"
252
+ const l = "Warszawa"
253
+ const d = allLocalitiesIdMap.get(r.localityId)?.name
254
+
255
+
256
+ if (!v || !c || !m || !l || !d) {
257
+ console.error("4 Missing voivodeship, county, municipality or base locality for district: " + r.name + " (" + v + ", " + c + ", " + m + ", " + l + ")")
258
+ console.error(r.voivodeship, r.county, r.municipality)
259
+ return
260
+ }
261
+
262
+ let s = out.streetsDistricts.get([v, c, m, l, d])
263
+ s = s ? s : []
264
+ const fullName = getFullName(r.name, r.restName)
265
+
266
+ if (!s.includes(fullName)) {
267
+ out.streetsDistricts.set([v, c, m, l, d], [...s, fullName])
268
+ }
269
+ })
270
+
271
+ // Ulice w delegaturach miast
272
+ streets
273
+ .filter(r => r.municipalityTypeId === "9")
274
+ .forEach(r => {
275
+ const v = vivIdMap.get(r.voivodeship)
276
+ const c = countyIdMap.get([r.voivodeship, r.county])
277
+
278
+ const name = allLocalitiesIdMap.get(r.localityId)?.name
279
+ const d = name?.substring(name.indexOf('-') + 1)
280
+ const m = name?.substring(0, name?.indexOf('-'))
281
+ const l = name?.substring(0, name?.indexOf('-'))
282
+
283
+ if (!v || !c || !m || !l || !d) {
284
+ console.error("4 Missing voivodeship, county, municipality or base locality for district: " + r.name + " (" + v + ", " + c + ", " + m + ", " + l + ")")
285
+ console.error(r.voivodeship, r.county, r.municipality)
286
+ return
287
+ }
288
+
289
+ let s = out.streetsDistricts.get([v, c, m, l, d])
290
+ s = s ? s : []
291
+ const fullName = getFullName(r.name, r.restName)
292
+ if (!s.includes(fullName)) {
293
+ out.streetsDistricts.set([v, c, m, l, d], [...s, fullName])
294
+ }
295
+ })
184
296
 
297
+ // Sort all lists
185
298
  out.municipalities.sortValues()
186
299
  out.localities.sortValues()
187
300
  out.districts.sortValues()
301
+ out.streetsLocalities.sortValues()
302
+ out.streetsDistricts.sortValues()
303
+
188
304
 
189
305
  return out
190
306
  }
package/src/csvparse.ts CHANGED
@@ -28,6 +28,20 @@ export type Locality = {
28
28
  date: string;
29
29
  };
30
30
 
31
+ // WOJ;POW;GMI;RODZ_GMI;SYM;SYM_UL;CECHA;NAZWA_1;NAZWA_2;STAN_NA
32
+ export type Street = {
33
+ voivodeship: string
34
+ county: string;
35
+ municipality: string;
36
+ municipalityTypeId: string;
37
+ localityId: string;
38
+ streenId: string;
39
+ characteristic: string;
40
+ name: string;
41
+ restName: string
42
+ date: string;
43
+ };
44
+
31
45
  export type LocalityType = {
32
46
  id: string;
33
47
  name: string;
@@ -111,4 +125,28 @@ export async function parseLocalityTypeCsv() {
111
125
  });
112
126
  await finished(parser);
113
127
  return records;
128
+ };
129
+
130
+ export async function parseStreetsCsv() {
131
+ const records: Street[] = [];
132
+ const parser = fs
133
+ .createReadStream(args.streets)
134
+ .pipe(parse({
135
+ relax_quotes: true,
136
+ delimiter: ';',
137
+ skip_empty_lines: true,
138
+ record_delimiter: '\r\n',
139
+ columns: () => {
140
+ return ["voivodeship", "county", "municipality", "municipalityTypeId", "localityId", "streenId", "characteristic", "name", "restName", "date"]
141
+ },
142
+ }))
143
+ .on('readable', function () {
144
+ let record; while ((record = parser.read()) !== null) {
145
+ // Work with each record
146
+ // record.name = toTitleCase(record.name)
147
+ records.push(record);
148
+ }
149
+ });
150
+ await finished(parser);
151
+ return records;
114
152
  };
package/src/generate.ts CHANGED
@@ -8,6 +8,8 @@ function genJson(c: Computed) {
8
8
  fs.writeFileSync(args.output + "/json/municipalities.json", JSON.stringify(c.municipalities.Get2Deep()))
9
9
  fs.writeFileSync(args.output + "/json/localities.json", JSON.stringify(c.localities.Get3Deep()))
10
10
  fs.writeFileSync(args.output + "/json/districts.json", JSON.stringify(c.districts.Get4Deep()))
11
+ fs.writeFileSync(args.output + "/json/streetsInLocalities.json", JSON.stringify(c.streetsLocalities.Get4Deep()))
12
+ fs.writeFileSync(args.output + "/json/streetsInDistricts.json", JSON.stringify(c.streetsDistricts.Get5Deep()))
11
13
 
12
14
  }
13
15
 
@@ -17,6 +19,8 @@ function genGo(c: Computed) {
17
19
  fs.copyFileSync(args.output + "/json/municipalities.json", args.output + "/go/municipalities.json")
18
20
  fs.copyFileSync(args.output + "/json/localities.json", args.output + "/go/localities.json")
19
21
  fs.copyFileSync(args.output + "/json/districts.json", args.output + "/go/districts.json")
22
+ fs.copyFileSync(args.output + "/json/streetsInLocalities.json", args.output + "/go/streetsInLocalities.json")
23
+ fs.copyFileSync(args.output + "/json/streetsInDistricts.json", args.output + "/go/streetsInDistricts.json")
20
24
  const data = `package poland
21
25
  import (
22
26
  "embed"
package/src/map.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { NamesMapThreeKeys, NamesMapFourKeys, NamesMapTwoKeys } from "./compute";
1
+ import { NamesMapThreeKeys, NamesMapFourKeys, NamesMapTwoKeys, NamesMapFiveKeys } from "./compute";
2
2
 
3
3
  export class MultiKeyMap {
4
4
  private map: Record<string, any> = {};
@@ -100,4 +100,25 @@ export class MultiKeyMap {
100
100
  })
101
101
  return out
102
102
  }
103
+
104
+ Get5Deep(): NamesMapFiveKeys {
105
+ var out: NamesMapFiveKeys = {}
106
+ this.forEach((keys, value) => {
107
+ const [v, c, m, l, d] = keys
108
+ if (!out[v]) {
109
+ out[v] = {}
110
+ }
111
+ if (!out[v][c]) {
112
+ out[v][c] = {}
113
+ }
114
+ if (!out[v][c][m]) {
115
+ out[v][c][m] = {}
116
+ }
117
+ if (!out[v][c][m][l]) {
118
+ out[v][c][m][l] = {}
119
+ }
120
+ out[v][c][m][l][d] = value
121
+ })
122
+ return out
123
+ }
103
124
  }