@hebcal/geo-sqlite 3.9.0 → 4.0.0

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.
@@ -2,21 +2,22 @@
2
2
 
3
3
  const {buildGeonamesSqlite} = require('@hebcal/geo-sqlite');
4
4
 
5
- if (process.argv.length != 8) {
6
- const infiles = 'countryInfo.txt cities5000.txt cities-patch.txt admin1CodesASCII.txt IL.txt';
5
+ const argv = process.argv.slice(2);
6
+ if (argv.length !== 7) {
7
+ const infiles = 'countryInfo.txt cities5000.txt cities-patch.txt admin1CodesASCII.txt IL.txt IL-alternatenames.txt';
7
8
  console.error(`Usage: build-geonames-sqlite geonames.sqlite3 ${infiles}`);
8
9
  process.exit(1);
9
10
  }
10
11
 
11
- const [dbFilename, countryInfo, cities, citiesPatch, admin1CodesASCII, il] = process.argv.slice(2);
12
-
13
- buildGeonamesSqlite(
14
- dbFilename,
15
- countryInfo,
16
- cities,
17
- citiesPatch,
18
- admin1CodesASCII,
19
- il,
20
- ).then(() => {
12
+ const filenames = {
13
+ dbFilename: argv[0],
14
+ countryInfotxt: argv[1],
15
+ cities5000txt: argv[2],
16
+ citiesPatch: argv[3],
17
+ admin1CodesASCIItxt: argv[4],
18
+ ILtxt: argv[5],
19
+ ILalternate: argv[6],
20
+ };
21
+ buildGeonamesSqlite(filenames).then(() => {
21
22
  console.log('Done!');
22
23
  });
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! @hebcal/geo-sqlite v3.9.0 */
1
+ /*! @hebcal/geo-sqlite v4.0.0 */
2
2
  'use strict';
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
@@ -446,15 +446,17 @@ class GeoDb {
446
446
 
447
447
  /**
448
448
  * Builds `geonames.sqlite3` from files downloaded from geonames.org
449
- * @param {string} dbFilename
450
- * @param {string} countryInfotxt
451
- * @param {string} cities5000txt
452
- * @param {string} citiesPatch
453
- * @param {string} admin1CodesASCIItxt
454
- * @param {string} ILtxt
449
+ * @param {any} opts
455
450
  */
456
451
 
457
- async function buildGeonamesSqlite(dbFilename, countryInfotxt, cities5000txt, citiesPatch, admin1CodesASCIItxt, ILtxt) {
452
+ async function buildGeonamesSqlite(opts) {
453
+ const dbFilename = opts.dbFilename;
454
+ const countryInfotxt = opts.countryInfotxt;
455
+ const cities5000txt = opts.cities5000txt;
456
+ const citiesPatch = opts.citiesPatch;
457
+ const admin1CodesASCIItxt = opts.admin1CodesASCIItxt;
458
+ const ILtxt = opts.ILtxt;
459
+ const ILalternate = opts.ILalternate;
458
460
  const logger = pino__default["default"]({
459
461
  // level: argv.quiet ? 'warn' : 'info',
460
462
  transport: {
@@ -527,8 +529,41 @@ async function buildGeonamesSqlite(dbFilename, countryInfotxt, cities5000txt, ci
527
529
  WHERE geonameid = 4140963;`, `UPDATE admin1
528
530
  SET name = 'Washington, D.C.', asciiname = 'Washington, D.C.'
529
531
  WHERE key = 'US.DC';`);
530
- doSql(logger, db, `DROP TABLE IF EXISTS geoname_he`, `CREATE TABLE geoname_he AS SELECT * FROM geoname LIMIT 0`);
531
- await doFile(logger, db, ILtxt, 'geoname_he', 19, filterPlacesHebrew);
532
+ doSql(logger, db, `DROP TABLE IF EXISTS alternatenames`, `CREATE TABLE alternatenames (
533
+ id int PRIMARY KEY,
534
+ geonameid int NOT NULL,
535
+ isolanguage varchar(7),
536
+ name varchar(400),
537
+ isPreferredName tinyint,
538
+ isShortName tinyint,
539
+ isColloquial tinyint,
540
+ isHistoric tinyint,
541
+ periodFrom NULL,
542
+ periodTo NULL
543
+ );`);
544
+ await doFile(logger, db, ILalternate, 'alternatenames', 10, a => {
545
+ if (a[2] === 'he' || a[2] === 'en') {
546
+ if (a[2] === 'he') {
547
+ a[3] = a[3].replace(/‘/g, '׳');
548
+ a[3] = a[3].replace(/’/g, '׳');
549
+ a[3] = a[3].replace(/\'/g, '׳');
550
+ a[3] = core.Locale.hebrewStripNikkud(a[3]);
551
+ } else {
552
+ a[3] = a[3].replace(/‘/g, '\'');
553
+ a[3] = a[3].replace(/’/g, '\'');
554
+ }
555
+
556
+ return true;
557
+ }
558
+
559
+ return false;
560
+ }); // remove duplicates from alternatenames
561
+
562
+ doSql(logger, db, `DROP TABLE IF EXISTS altnames`, `CREATE TABLE altnames
563
+ AS SELECT geonameid, isolanguage, name
564
+ FROM alternatenames
565
+ GROUP BY 1, 2, 3
566
+ `);
532
567
  doSql(logger, db, `update admin1 set name='',asciiname='' where key like 'PS.%';`, `update country set country = '' where iso = 'PS';`, `delete from geoname where geonameid = 7303419;`);
533
568
  doSql(logger, db, `DROP TABLE IF EXISTS geoname_fulltext`, `CREATE VIRTUAL TABLE geoname_fulltext
534
569
  USING fts3(geonameid int, longname text,
@@ -559,47 +594,23 @@ async function buildGeonamesSqlite(dbFilename, countryInfotxt, cities5000txt, ci
559
594
  AND g.country = c.ISO
560
595
  AND g.country||'.'||g.admin1 = a.key
561
596
  `, `INSERT INTO geoname_fulltext
562
- SELECT g.geonameid, g.name||', ישראל',
563
- g.name, '', 'ישראל',
597
+ SELECT g.geonameid, alt.name||', ישראל',
598
+ alt.name, '', 'ישראל',
564
599
  g.population, g.latitude, g.longitude, g.timezone
565
- FROM geoname_he g, admin1 a, country c
566
- WHERE g.country = c.ISO
567
- AND g.country||'.'||g.admin1 = a.key
600
+ FROM geoname g, country c, altnames alt
601
+ WHERE g.country = 'IL'
602
+ AND alt.isolanguage = 'he'
603
+ AND g.geonameid = alt.geonameid
568
604
  `);
569
605
  db.close();
570
606
  return Promise.resolve(true);
571
607
  }
572
- /**
573
- * @param {string[]} a
574
- * @return {boolean}
575
- */
576
-
577
- function filterPlacesHebrew(a) {
578
- if (a[6] != 'P' || a[7] != 'PPL' && a[7] != 'STLMT') {
579
- return false;
580
- }
581
-
582
- const alternatenames = a[3].split(',');
583
-
584
- for (const name of alternatenames) {
585
- const firstchar = name[0];
586
-
587
- if (firstchar >= '\u05D0' && firstchar <= '\u05EA') {
588
- a[1] = core.Locale.hebrewStripNikkud(name); // replace 'name' field with Hebrew
589
-
590
- return true;
591
- }
592
- }
593
-
594
- return false;
595
- }
596
608
  /**
597
609
  * @param {pino.Logger} logger
598
610
  * @param {Database} db
599
611
  * @param {...string} sqls
600
612
  */
601
613
 
602
-
603
614
  function doSql(logger, db, ...sqls) {
604
615
  for (let i = 0; i < sqls.length; i++) {
605
616
  logger.info(sqls[i]);
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! @hebcal/geo-sqlite v3.9.0 */
1
+ /*! @hebcal/geo-sqlite v4.0.0 */
2
2
  import Database from 'better-sqlite3';
3
3
  import { Location, Locale } from '@hebcal/core';
4
4
  import '@hebcal/cities';
@@ -434,15 +434,17 @@ class GeoDb {
434
434
 
435
435
  /**
436
436
  * Builds `geonames.sqlite3` from files downloaded from geonames.org
437
- * @param {string} dbFilename
438
- * @param {string} countryInfotxt
439
- * @param {string} cities5000txt
440
- * @param {string} citiesPatch
441
- * @param {string} admin1CodesASCIItxt
442
- * @param {string} ILtxt
437
+ * @param {any} opts
443
438
  */
444
439
 
445
- async function buildGeonamesSqlite(dbFilename, countryInfotxt, cities5000txt, citiesPatch, admin1CodesASCIItxt, ILtxt) {
440
+ async function buildGeonamesSqlite(opts) {
441
+ const dbFilename = opts.dbFilename;
442
+ const countryInfotxt = opts.countryInfotxt;
443
+ const cities5000txt = opts.cities5000txt;
444
+ const citiesPatch = opts.citiesPatch;
445
+ const admin1CodesASCIItxt = opts.admin1CodesASCIItxt;
446
+ const ILtxt = opts.ILtxt;
447
+ const ILalternate = opts.ILalternate;
446
448
  const logger = pino({
447
449
  // level: argv.quiet ? 'warn' : 'info',
448
450
  transport: {
@@ -515,8 +517,41 @@ async function buildGeonamesSqlite(dbFilename, countryInfotxt, cities5000txt, ci
515
517
  WHERE geonameid = 4140963;`, `UPDATE admin1
516
518
  SET name = 'Washington, D.C.', asciiname = 'Washington, D.C.'
517
519
  WHERE key = 'US.DC';`);
518
- doSql(logger, db, `DROP TABLE IF EXISTS geoname_he`, `CREATE TABLE geoname_he AS SELECT * FROM geoname LIMIT 0`);
519
- await doFile(logger, db, ILtxt, 'geoname_he', 19, filterPlacesHebrew);
520
+ doSql(logger, db, `DROP TABLE IF EXISTS alternatenames`, `CREATE TABLE alternatenames (
521
+ id int PRIMARY KEY,
522
+ geonameid int NOT NULL,
523
+ isolanguage varchar(7),
524
+ name varchar(400),
525
+ isPreferredName tinyint,
526
+ isShortName tinyint,
527
+ isColloquial tinyint,
528
+ isHistoric tinyint,
529
+ periodFrom NULL,
530
+ periodTo NULL
531
+ );`);
532
+ await doFile(logger, db, ILalternate, 'alternatenames', 10, a => {
533
+ if (a[2] === 'he' || a[2] === 'en') {
534
+ if (a[2] === 'he') {
535
+ a[3] = a[3].replace(/‘/g, '׳');
536
+ a[3] = a[3].replace(/’/g, '׳');
537
+ a[3] = a[3].replace(/\'/g, '׳');
538
+ a[3] = Locale.hebrewStripNikkud(a[3]);
539
+ } else {
540
+ a[3] = a[3].replace(/‘/g, '\'');
541
+ a[3] = a[3].replace(/’/g, '\'');
542
+ }
543
+
544
+ return true;
545
+ }
546
+
547
+ return false;
548
+ }); // remove duplicates from alternatenames
549
+
550
+ doSql(logger, db, `DROP TABLE IF EXISTS altnames`, `CREATE TABLE altnames
551
+ AS SELECT geonameid, isolanguage, name
552
+ FROM alternatenames
553
+ GROUP BY 1, 2, 3
554
+ `);
520
555
  doSql(logger, db, `update admin1 set name='',asciiname='' where key like 'PS.%';`, `update country set country = '' where iso = 'PS';`, `delete from geoname where geonameid = 7303419;`);
521
556
  doSql(logger, db, `DROP TABLE IF EXISTS geoname_fulltext`, `CREATE VIRTUAL TABLE geoname_fulltext
522
557
  USING fts3(geonameid int, longname text,
@@ -547,47 +582,23 @@ async function buildGeonamesSqlite(dbFilename, countryInfotxt, cities5000txt, ci
547
582
  AND g.country = c.ISO
548
583
  AND g.country||'.'||g.admin1 = a.key
549
584
  `, `INSERT INTO geoname_fulltext
550
- SELECT g.geonameid, g.name||', ישראל',
551
- g.name, '', 'ישראל',
585
+ SELECT g.geonameid, alt.name||', ישראל',
586
+ alt.name, '', 'ישראל',
552
587
  g.population, g.latitude, g.longitude, g.timezone
553
- FROM geoname_he g, admin1 a, country c
554
- WHERE g.country = c.ISO
555
- AND g.country||'.'||g.admin1 = a.key
588
+ FROM geoname g, country c, altnames alt
589
+ WHERE g.country = 'IL'
590
+ AND alt.isolanguage = 'he'
591
+ AND g.geonameid = alt.geonameid
556
592
  `);
557
593
  db.close();
558
594
  return Promise.resolve(true);
559
595
  }
560
- /**
561
- * @param {string[]} a
562
- * @return {boolean}
563
- */
564
-
565
- function filterPlacesHebrew(a) {
566
- if (a[6] != 'P' || a[7] != 'PPL' && a[7] != 'STLMT') {
567
- return false;
568
- }
569
-
570
- const alternatenames = a[3].split(',');
571
-
572
- for (const name of alternatenames) {
573
- const firstchar = name[0];
574
-
575
- if (firstchar >= '\u05D0' && firstchar <= '\u05EA') {
576
- a[1] = Locale.hebrewStripNikkud(name); // replace 'name' field with Hebrew
577
-
578
- return true;
579
- }
580
- }
581
-
582
- return false;
583
- }
584
596
  /**
585
597
  * @param {pino.Logger} logger
586
598
  * @param {Database} db
587
599
  * @param {...string} sqls
588
600
  */
589
601
 
590
-
591
602
  function doSql(logger, db, ...sqls) {
592
603
  for (let i = 0; i < sqls.length; i++) {
593
604
  logger.info(sqls[i]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hebcal/geo-sqlite",
3
- "version": "3.9.0",
3
+ "version": "4.0.0",
4
4
  "author": "Michael J. Radwin (https://github.com/mjradwin)",
5
5
  "keywords": [
6
6
  "hebcal"
@@ -32,7 +32,7 @@
32
32
  "@hebcal/core": "^3.33.3",
33
33
  "better-sqlite3": "^7.5.0",
34
34
  "pino": "^7.8.0",
35
- "pino-pretty": "^7.5.1"
35
+ "pino-pretty": "^7.5.3"
36
36
  },
37
37
  "scripts": {
38
38
  "build": "rollup -c",
@@ -61,10 +61,10 @@
61
61
  "@rollup/plugin-json": "^4.1.0",
62
62
  "@rollup/plugin-node-resolve": "^13.1.3",
63
63
  "ava": "^4.0.1",
64
- "eslint": "^8.9.0",
64
+ "eslint": "^8.10.0",
65
65
  "eslint-config-google": "^0.14.0",
66
66
  "jsdoc": "^3.6.10",
67
67
  "jsdoc-to-markdown": "^7.1.1",
68
- "rollup": "^2.68.0"
68
+ "rollup": "^2.69.0"
69
69
  }
70
70
  }