text-to-map 1.1.13 → 1.1.15

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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ import { importCities } from "../open-data-sync/cities";
2
+ import { defaultBinOptions } from "./constants";
3
+ importCities(defaultBinOptions)
4
+ .then(() => {
5
+ console.log("Completed");
6
+ })
7
+ .catch((error) => {
8
+ console.log(error);
9
+ });
@@ -0,0 +1,2 @@
1
+ export declare const insertCityPositions: (data: string[][]) => Promise<number>;
2
+ export declare const insertCities: (buffer: string[][]) => Promise<number>;
@@ -0,0 +1,49 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import chunk from "lodash/chunk";
11
+ import { extractKeyValuesPairs, getKnexDb, insertMultipleRows } from "./db";
12
+ const citiesColumn = {
13
+ cityName: 0,
14
+ cityCode: 1,
15
+ countyName: 2,
16
+ countyCode: 3,
17
+ regionName: 4,
18
+ regionCode: 5,
19
+ postalCode: 6,
20
+ latitude: 7,
21
+ longitude: 8,
22
+ };
23
+ export const insertCityPositions = (data) => __awaiter(void 0, void 0, void 0, function* () {
24
+ let changes = 0;
25
+ // cities are most likely already inserted, but in case they're not,
26
+ // we need to insert them before updating them with region data
27
+ changes += yield insertCities(data);
28
+ const prev = changes;
29
+ const knex = getKnexDb();
30
+ for (const arrayChunk of chunk(data, 1000)) {
31
+ const queries = [];
32
+ for (const row of arrayChunk) {
33
+ queries.push(knex.raw(`UPDATE city SET wgs84_latitude = ?, wgs84_longitude = ? WHERE code = ?`, [
34
+ row[citiesColumn.latitude],
35
+ row[citiesColumn.longitude],
36
+ row[citiesColumn.cityCode],
37
+ ]));
38
+ }
39
+ yield Promise.all(queries);
40
+ changes += queries.length;
41
+ console.log(`Done ${changes - prev} / ${data.length} rows...`);
42
+ }
43
+ return changes;
44
+ });
45
+ export const insertCities = (buffer) => __awaiter(void 0, void 0, void 0, function* () {
46
+ return yield insertMultipleRows(extractKeyValuesPairs(buffer, citiesColumn.cityCode, [
47
+ citiesColumn.cityName,
48
+ ]), "city", ["code", "name"]);
49
+ });
@@ -0,0 +1,25 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ export function up(knex) {
11
+ return __awaiter(this, void 0, void 0, function* () {
12
+ yield knex.schema.alterTable("city", (t) => {
13
+ t.double("wgs84_latitude").nullable();
14
+ t.double("wgs84_longitude").nullable();
15
+ });
16
+ });
17
+ }
18
+ export function down(knex) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ yield knex.schema.alterTable("city", (t) => {
21
+ t.dropColumn("wgs84_latitude");
22
+ t.dropColumn("wgs84_longitude");
23
+ });
24
+ });
25
+ }
@@ -47,5 +47,6 @@ export declare enum SyncPart {
47
47
  AddressPoints = "address-points",
48
48
  Schools = "schools",
49
49
  Regions = "regions",
50
- Streets = "streets"
50
+ Streets = "streets",
51
+ Cities = "cities"
51
52
  }
package/dist/db/types.js CHANGED
@@ -16,4 +16,5 @@ export var SyncPart;
16
16
  SyncPart["Schools"] = "schools";
17
17
  SyncPart["Regions"] = "regions";
18
18
  SyncPart["Streets"] = "streets";
19
+ SyncPart["Cities"] = "cities";
19
20
  })(SyncPart || (SyncPart = {}));
package/dist/index.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import { clearDb, initDb } from "./db/db";
2
2
  import { downloadAndImportAddressPoints } from "./open-data-sync/address-points";
3
+ import { importCities } from "./open-data-sync/cities";
3
4
  import { downloadAndImportRegions } from "./open-data-sync/regions";
4
5
  import { downloadAndImportSchools } from "./open-data-sync/schools";
5
6
  import { downloadAndImportStreets } from "./open-data-sync/streets";
6
7
  import { OpenDataSyncOptionsPartial } from "./utils/helpers";
7
- export * from "./street-markdown/types";
8
8
  export * from "./street-markdown/smd";
9
+ export * from "./street-markdown/types";
9
10
  /**
10
11
  * Download and import all open data to SQLite DB. The DB file will be located
11
12
  * in the `dataDir` folder.
@@ -18,4 +19,4 @@ export * from "./street-markdown/smd";
18
19
  * ```
19
20
  */
20
21
  export declare function downloadAndImportEverything(options?: OpenDataSyncOptionsPartial, syncStreets?: boolean): Promise<void>;
21
- export { initDb, downloadAndImportAddressPoints, clearDb, downloadAndImportSchools, downloadAndImportStreets, downloadAndImportRegions, };
22
+ export { clearDb, downloadAndImportAddressPoints, downloadAndImportRegions, downloadAndImportSchools, downloadAndImportStreets, importCities, initDb, };
package/dist/index.js CHANGED
@@ -8,12 +8,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { clearDb, initDb } from "./db/db";
11
- import { downloadAndImportAddressPoints, } from "./open-data-sync/address-points";
11
+ import { downloadAndImportAddressPoints } from "./open-data-sync/address-points";
12
+ import { importCities } from "./open-data-sync/cities";
12
13
  import { downloadAndImportRegions } from "./open-data-sync/regions";
13
14
  import { downloadAndImportSchools } from "./open-data-sync/schools";
14
15
  import { downloadAndImportStreets } from "./open-data-sync/streets";
15
- export * from "./street-markdown/types";
16
16
  export * from "./street-markdown/smd";
17
+ export * from "./street-markdown/types";
17
18
  /**
18
19
  * Download and import all open data to SQLite DB. The DB file will be located
19
20
  * in the `dataDir` folder.
@@ -31,9 +32,10 @@ export function downloadAndImportEverything(options = {}, syncStreets = true) {
31
32
  yield downloadAndImportAddressPoints(options);
32
33
  yield downloadAndImportSchools(options);
33
34
  yield downloadAndImportRegions(options);
35
+ yield importCities(options);
34
36
  if (syncStreets) {
35
37
  yield downloadAndImportStreets(options);
36
38
  }
37
39
  });
38
40
  }
39
- export { initDb, downloadAndImportAddressPoints, clearDb, downloadAndImportSchools, downloadAndImportStreets, downloadAndImportRegions, };
41
+ export { clearDb, downloadAndImportAddressPoints, downloadAndImportRegions, downloadAndImportSchools, downloadAndImportStreets, importCities, initDb, };
@@ -0,0 +1,2 @@
1
+ import { OpenDataSyncOptionsPartial } from "../utils/helpers";
2
+ export declare const importCities: (options?: OpenDataSyncOptionsPartial) => Promise<void>;
@@ -0,0 +1,40 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { parse } from "csv-parse";
11
+ import iconv from "iconv-lite";
12
+ import { pipeline } from "stream/promises";
13
+ import { SyncPart } from "../db/types";
14
+ import { prepareOptions, } from "../utils/helpers";
15
+ import { runSyncPart } from "./common";
16
+ import { createReadStream } from "fs";
17
+ import { insertCityPositions } from "../db/cities";
18
+ import { fileURLToPath } from "url";
19
+ import { dirname, join } from "path";
20
+ const importDataToDb = (options) => __awaiter(void 0, void 0, void 0, function* () {
21
+ console.log("Starting to parse CSV file...");
22
+ const __filename = fileURLToPath(import.meta.url);
23
+ const __dirname = dirname(__filename);
24
+ // source: https://github.com/33bcdd/souradnice-mest/blob/master/souradnice.csv
25
+ const csvStream = createReadStream(join(__dirname, "..", "souradnice.csv"));
26
+ const rows = [];
27
+ const parseStream = parse({ delimiter: ",", fromLine: 2 }).on("data", (data) => {
28
+ rows.push(data);
29
+ });
30
+ yield pipeline(csvStream, iconv.decodeStream("utf-8"), parseStream);
31
+ console.log("Parsing completed. Starting to import data to DB...");
32
+ yield insertCityPositions(rows);
33
+ console.log(`Import completed. Total imported rows: ${rows.length}`);
34
+ });
35
+ export const importCities = (options = {}) => __awaiter(void 0, void 0, void 0, function* () {
36
+ yield runSyncPart(SyncPart.Regions, [], () => __awaiter(void 0, void 0, void 0, function* () {
37
+ const completeOptions = prepareOptions(options);
38
+ yield importDataToDb(completeOptions);
39
+ }));
40
+ });