@hebcal/geo-sqlite 4.4.2 → 4.4.3

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/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! @hebcal/geo-sqlite v4.4.2 */
1
+ /*! @hebcal/geo-sqlite v4.4.3 */
2
2
  'use strict';
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
@@ -60,18 +60,14 @@ FROM ZIPCodes_Primary
60
60
  WHERE ZipCode LIKE ?
61
61
  ORDER BY Population DESC
62
62
  LIMIT 10`;
63
- const ZIP_FULLTEXT_COMPLETE_SQL = `SELECT ZipCode,CityMixedCase,rank
63
+ const ZIP_FULLTEXT_COMPLETE_SQL = `SELECT ZipCode, rank
64
64
  FROM ZIPCodes_CityFullText5
65
- WHERE CityMixedCase MATCH ?
66
- ORDER BY Population DESC
67
- LIMIT 15`;
68
- const GEONAME_COMPLETE_SQL = `SELECT geonameid, longname,
69
- ((sqrt(population)/40) + (-30 * rank)) as myrank
65
+ WHERE ZIPCodes_CityFullText5 MATCH ?
66
+ LIMIT 20`;
67
+ const GEONAME_COMPLETE_SQL = `SELECT geonameid, rank
70
68
  FROM geoname_fulltext
71
69
  WHERE geoname_fulltext MATCH ?
72
- GROUP BY geonameid
73
- ORDER BY myrank DESC
74
- LIMIT 15`;
70
+ LIMIT 20`;
75
71
  const stateNames = {
76
72
  'AK': 'Alaska',
77
73
  'AL': 'Alabama',
@@ -358,12 +354,24 @@ class GeoDb {
358
354
  }
359
355
 
360
356
  qraw = qraw.replace(/\"/g, '""');
361
- const geoRows = this.geonamesCompStmt.all(`{longname} : "${qraw}" *`);
357
+ const geoRows0 = this.geonamesCompStmt.all(`{longname} : "${qraw}" *`);
358
+ const ids = new Set();
359
+ const geoRows = [];
360
+
361
+ for (const row of geoRows0) {
362
+ const id = row.geonameid;
363
+
364
+ if (!ids.has(id)) {
365
+ ids.add(id);
366
+ geoRows.push(row);
367
+ }
368
+ }
369
+
362
370
  const geoMatches = geoRows.map(res => {
363
371
  const loc = this.lookupGeoname(res.geonameid);
364
372
  const country = this.countryNames.get(loc.getCountryCode()) || '';
365
373
  const admin1 = loc.admin1 || '';
366
- const rank = Math.trunc(res.myrank * 100.0) / 100.0;
374
+ const rankPop = Math.sqrt(loc.population) / 40;
367
375
  const obj = {
368
376
  id: res.geonameid,
369
377
  value: loc.name,
@@ -373,7 +381,7 @@ class GeoDb {
373
381
  longitude: loc.longitude,
374
382
  timezone: loc.getTzid(),
375
383
  geo: 'geoname',
376
- rank: rank
384
+ rank: rankPop + -30 * res.rank
377
385
  };
378
386
 
379
387
  if (loc.population) {
@@ -399,11 +407,11 @@ class GeoDb {
399
407
  this.zipFulltextCompStmt = this.zipsDb.prepare(ZIP_FULLTEXT_COMPLETE_SQL);
400
408
  }
401
409
 
402
- const zipRows = this.zipFulltextCompStmt.all(`"${qraw}" *`);
410
+ const zipRows = this.zipFulltextCompStmt.all(`{longname} : "${qraw}" *`);
403
411
  const zipMatches = zipRows.map(res => {
404
412
  const zipCode = res.ZipCode;
405
413
  const loc = this.lookupZip(zipCode);
406
- const myrank = Math.sqrt(loc.population) / 40 + -30 * res.rank;
414
+ const rankPop = Math.sqrt(loc.population) / 40;
407
415
  const obj = {
408
416
  id: zipCode,
409
417
  value: loc.getName(),
@@ -415,7 +423,7 @@ class GeoDb {
415
423
  timezone: loc.getTzid(),
416
424
  population: loc.population,
417
425
  geo: 'zip',
418
- rank: myrank
426
+ rank: rankPop + -30 * res.rank
419
427
  };
420
428
  return obj;
421
429
  });
@@ -628,12 +636,13 @@ async function buildGeonamesSqlite(opts) {
628
636
  `);
629
637
  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;`);
630
638
  doSql(logger, db, `DROP TABLE IF EXISTS geoname_fulltext`, `CREATE VIRTUAL TABLE geoname_fulltext
631
- USING fts5(geonameid, longname, population);
639
+ USING fts5(geonameid UNINDEXED, longname, population, city, admin1, country);
632
640
  `, `DROP TABLE IF EXISTS geoname_non_ascii`, `CREATE TABLE geoname_non_ascii AS
633
641
  SELECT geonameid FROM geoname WHERE asciiname <> name`, `INSERT INTO geoname_fulltext
634
642
  SELECT g.geonameid,
635
643
  g.asciiname||', '||a.asciiname||', '||c.Country,
636
- g.population
644
+ g.population,
645
+ g.asciiname,a.asciiname,c.Country
637
646
  FROM geoname g, admin1 a, country c
638
647
  WHERE g.country = c.ISO
639
648
  AND g.country <> 'US'
@@ -641,21 +650,24 @@ async function buildGeonamesSqlite(opts) {
641
650
  `, `INSERT INTO geoname_fulltext
642
651
  SELECT g.geonameid,
643
652
  g.asciiname||', '||a.asciiname||', USA',
644
- g.population
653
+ g.population,
654
+ g.asciiname,a.asciiname,'USA'
645
655
  FROM geoname g, admin1 a
646
656
  WHERE g.country = 'US'
647
657
  AND g.country||'.'||g.admin1 = a.key
648
658
  `, `INSERT INTO geoname_fulltext
649
659
  SELECT g.geonameid,
650
660
  g.asciiname||', '||c.Country,
651
- g.population
661
+ g.population,
662
+ g.asciiname,NULL,c.Country
652
663
  FROM geoname g, country c
653
664
  WHERE g.country = c.ISO
654
665
  AND (g.admin1 = '' OR g.admin1 = '00')
655
666
  `, `INSERT INTO geoname_fulltext
656
667
  SELECT g.geonameid,
657
668
  g.name||', '||a.name||', '||c.Country,
658
- g.population
669
+ g.population,
670
+ g.name,a.name,c.Country
659
671
  FROM geoname_non_ascii gna, geoname g, admin1 a, country c
660
672
  WHERE gna.geonameid = g.geonameid
661
673
  AND g.country = c.ISO
@@ -663,7 +675,8 @@ async function buildGeonamesSqlite(opts) {
663
675
  `, `INSERT INTO geoname_fulltext
664
676
  SELECT g.geonameid,
665
677
  alt.name||', ישראל',
666
- g.population
678
+ g.population,
679
+ alt.name,NULL,'ישראל'
667
680
  FROM geoname g, altnames alt
668
681
  WHERE g.country = 'IL'
669
682
  AND alt.isolanguage = 'he'
@@ -671,7 +684,8 @@ async function buildGeonamesSqlite(opts) {
671
684
  `, `INSERT INTO geoname_fulltext
672
685
  SELECT g.geonameid,
673
686
  alt.name||', '||a1.asciiname||', Israel',
674
- g.population
687
+ g.population,
688
+ alt.name,a1.asciiname,'Israel'
675
689
  FROM geoname g, admin1 a1, altnames alt
676
690
  WHERE g.country = 'IL'
677
691
  AND alt.isolanguage = 'en'
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! @hebcal/geo-sqlite v4.4.2 */
1
+ /*! @hebcal/geo-sqlite v4.4.3 */
2
2
  import Database from 'better-sqlite3';
3
3
  import { Location, Locale } from '@hebcal/core';
4
4
  import '@hebcal/cities';
@@ -48,18 +48,14 @@ FROM ZIPCodes_Primary
48
48
  WHERE ZipCode LIKE ?
49
49
  ORDER BY Population DESC
50
50
  LIMIT 10`;
51
- const ZIP_FULLTEXT_COMPLETE_SQL = `SELECT ZipCode,CityMixedCase,rank
51
+ const ZIP_FULLTEXT_COMPLETE_SQL = `SELECT ZipCode, rank
52
52
  FROM ZIPCodes_CityFullText5
53
- WHERE CityMixedCase MATCH ?
54
- ORDER BY Population DESC
55
- LIMIT 15`;
56
- const GEONAME_COMPLETE_SQL = `SELECT geonameid, longname,
57
- ((sqrt(population)/40) + (-30 * rank)) as myrank
53
+ WHERE ZIPCodes_CityFullText5 MATCH ?
54
+ LIMIT 20`;
55
+ const GEONAME_COMPLETE_SQL = `SELECT geonameid, rank
58
56
  FROM geoname_fulltext
59
57
  WHERE geoname_fulltext MATCH ?
60
- GROUP BY geonameid
61
- ORDER BY myrank DESC
62
- LIMIT 15`;
58
+ LIMIT 20`;
63
59
  const stateNames = {
64
60
  'AK': 'Alaska',
65
61
  'AL': 'Alabama',
@@ -346,12 +342,24 @@ class GeoDb {
346
342
  }
347
343
 
348
344
  qraw = qraw.replace(/\"/g, '""');
349
- const geoRows = this.geonamesCompStmt.all(`{longname} : "${qraw}" *`);
345
+ const geoRows0 = this.geonamesCompStmt.all(`{longname} : "${qraw}" *`);
346
+ const ids = new Set();
347
+ const geoRows = [];
348
+
349
+ for (const row of geoRows0) {
350
+ const id = row.geonameid;
351
+
352
+ if (!ids.has(id)) {
353
+ ids.add(id);
354
+ geoRows.push(row);
355
+ }
356
+ }
357
+
350
358
  const geoMatches = geoRows.map(res => {
351
359
  const loc = this.lookupGeoname(res.geonameid);
352
360
  const country = this.countryNames.get(loc.getCountryCode()) || '';
353
361
  const admin1 = loc.admin1 || '';
354
- const rank = Math.trunc(res.myrank * 100.0) / 100.0;
362
+ const rankPop = Math.sqrt(loc.population) / 40;
355
363
  const obj = {
356
364
  id: res.geonameid,
357
365
  value: loc.name,
@@ -361,7 +369,7 @@ class GeoDb {
361
369
  longitude: loc.longitude,
362
370
  timezone: loc.getTzid(),
363
371
  geo: 'geoname',
364
- rank: rank
372
+ rank: rankPop + -30 * res.rank
365
373
  };
366
374
 
367
375
  if (loc.population) {
@@ -387,11 +395,11 @@ class GeoDb {
387
395
  this.zipFulltextCompStmt = this.zipsDb.prepare(ZIP_FULLTEXT_COMPLETE_SQL);
388
396
  }
389
397
 
390
- const zipRows = this.zipFulltextCompStmt.all(`"${qraw}" *`);
398
+ const zipRows = this.zipFulltextCompStmt.all(`{longname} : "${qraw}" *`);
391
399
  const zipMatches = zipRows.map(res => {
392
400
  const zipCode = res.ZipCode;
393
401
  const loc = this.lookupZip(zipCode);
394
- const myrank = Math.sqrt(loc.population) / 40 + -30 * res.rank;
402
+ const rankPop = Math.sqrt(loc.population) / 40;
395
403
  const obj = {
396
404
  id: zipCode,
397
405
  value: loc.getName(),
@@ -403,7 +411,7 @@ class GeoDb {
403
411
  timezone: loc.getTzid(),
404
412
  population: loc.population,
405
413
  geo: 'zip',
406
- rank: myrank
414
+ rank: rankPop + -30 * res.rank
407
415
  };
408
416
  return obj;
409
417
  });
@@ -616,12 +624,13 @@ async function buildGeonamesSqlite(opts) {
616
624
  `);
617
625
  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;`);
618
626
  doSql(logger, db, `DROP TABLE IF EXISTS geoname_fulltext`, `CREATE VIRTUAL TABLE geoname_fulltext
619
- USING fts5(geonameid, longname, population);
627
+ USING fts5(geonameid UNINDEXED, longname, population, city, admin1, country);
620
628
  `, `DROP TABLE IF EXISTS geoname_non_ascii`, `CREATE TABLE geoname_non_ascii AS
621
629
  SELECT geonameid FROM geoname WHERE asciiname <> name`, `INSERT INTO geoname_fulltext
622
630
  SELECT g.geonameid,
623
631
  g.asciiname||', '||a.asciiname||', '||c.Country,
624
- g.population
632
+ g.population,
633
+ g.asciiname,a.asciiname,c.Country
625
634
  FROM geoname g, admin1 a, country c
626
635
  WHERE g.country = c.ISO
627
636
  AND g.country <> 'US'
@@ -629,21 +638,24 @@ async function buildGeonamesSqlite(opts) {
629
638
  `, `INSERT INTO geoname_fulltext
630
639
  SELECT g.geonameid,
631
640
  g.asciiname||', '||a.asciiname||', USA',
632
- g.population
641
+ g.population,
642
+ g.asciiname,a.asciiname,'USA'
633
643
  FROM geoname g, admin1 a
634
644
  WHERE g.country = 'US'
635
645
  AND g.country||'.'||g.admin1 = a.key
636
646
  `, `INSERT INTO geoname_fulltext
637
647
  SELECT g.geonameid,
638
648
  g.asciiname||', '||c.Country,
639
- g.population
649
+ g.population,
650
+ g.asciiname,NULL,c.Country
640
651
  FROM geoname g, country c
641
652
  WHERE g.country = c.ISO
642
653
  AND (g.admin1 = '' OR g.admin1 = '00')
643
654
  `, `INSERT INTO geoname_fulltext
644
655
  SELECT g.geonameid,
645
656
  g.name||', '||a.name||', '||c.Country,
646
- g.population
657
+ g.population,
658
+ g.name,a.name,c.Country
647
659
  FROM geoname_non_ascii gna, geoname g, admin1 a, country c
648
660
  WHERE gna.geonameid = g.geonameid
649
661
  AND g.country = c.ISO
@@ -651,7 +663,8 @@ async function buildGeonamesSqlite(opts) {
651
663
  `, `INSERT INTO geoname_fulltext
652
664
  SELECT g.geonameid,
653
665
  alt.name||', ישראל',
654
- g.population
666
+ g.population,
667
+ alt.name,NULL,'ישראל'
655
668
  FROM geoname g, altnames alt
656
669
  WHERE g.country = 'IL'
657
670
  AND alt.isolanguage = 'he'
@@ -659,7 +672,8 @@ async function buildGeonamesSqlite(opts) {
659
672
  `, `INSERT INTO geoname_fulltext
660
673
  SELECT g.geonameid,
661
674
  alt.name||', '||a1.asciiname||', Israel',
662
- g.population
675
+ g.population,
676
+ alt.name,a1.asciiname,'Israel'
663
677
  FROM geoname g, admin1 a1, altnames alt
664
678
  WHERE g.country = 'IL'
665
679
  AND alt.isolanguage = 'en'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hebcal/geo-sqlite",
3
- "version": "4.4.2",
3
+ "version": "4.4.3",
4
4
  "author": "Michael J. Radwin (https://github.com/mjradwin)",
5
5
  "keywords": [
6
6
  "hebcal"
@@ -29,8 +29,8 @@
29
29
  ],
30
30
  "dependencies": {
31
31
  "@hebcal/cities": "^3.1.2",
32
- "@hebcal/core": "^3.36.2",
33
- "better-sqlite3": "^7.5.1",
32
+ "@hebcal/core": "^3.38.0",
33
+ "better-sqlite3": "^7.5.3",
34
34
  "pino": "^7.11.0",
35
35
  "pino-pretty": "^7.6.1"
36
36
  },
@@ -53,18 +53,18 @@
53
53
  },
54
54
  "license": "BSD-2-Clause",
55
55
  "devDependencies": {
56
- "@babel/core": "^7.17.10",
57
- "@babel/preset-env": "^7.17.10",
56
+ "@babel/core": "^7.18.2",
57
+ "@babel/preset-env": "^7.18.2",
58
58
  "@babel/register": "^7.17.7",
59
59
  "@rollup/plugin-babel": "^5.3.1",
60
60
  "@rollup/plugin-commonjs": "^22.0.0",
61
61
  "@rollup/plugin-json": "^4.1.0",
62
- "@rollup/plugin-node-resolve": "^13.2.1",
62
+ "@rollup/plugin-node-resolve": "^13.3.0",
63
63
  "ava": "^4.2.0",
64
- "eslint": "^8.14.0",
64
+ "eslint": "^8.16.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.71.1"
68
+ "rollup": "^2.75.4"
69
69
  }
70
70
  }
package/zips-dummy.sql CHANGED
@@ -13,4 +13,4 @@ CREATE VIRTUAL TABLE ZIPCodes_CityFullText
13
13
  USING fts4(ZipCode,CityMixedCase,State,Latitude,Longitude,TimeZone,DayLightSaving,Population);
14
14
 
15
15
  CREATE VIRTUAL TABLE ZIPCodes_CityFullText5
16
- USING fts5(ZipCode,CityMixedCase,Population);
16
+ USING fts5(ZipCode UNINDEXED,CityMixedCase,Population UNINDEXED,longname);