@hebcal/geo-sqlite 4.4.2 → 4.5.1

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.5.1 */
2
2
  'use strict';
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
@@ -60,18 +60,16 @@ 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
64
64
  FROM ZIPCodes_CityFullText5
65
- WHERE CityMixedCase MATCH ?
65
+ WHERE ZIPCodes_CityFullText5 MATCH ?
66
66
  ORDER BY Population DESC
67
- LIMIT 15`;
68
- const GEONAME_COMPLETE_SQL = `SELECT geonameid, longname,
69
- ((sqrt(population)/40) + (-30 * rank)) as myrank
67
+ LIMIT 20`;
68
+ const GEONAME_COMPLETE_SQL = `SELECT geonameid
70
69
  FROM geoname_fulltext
71
70
  WHERE geoname_fulltext MATCH ?
72
- GROUP BY geonameid
73
- ORDER BY myrank DESC
74
- LIMIT 15`;
71
+ ORDER BY population DESC
72
+ LIMIT 20`;
75
73
  const stateNames = {
76
74
  'AK': 'Alaska',
77
75
  'AL': 'Alabama',
@@ -358,12 +356,23 @@ class GeoDb {
358
356
  }
359
357
 
360
358
  qraw = qraw.replace(/\"/g, '""');
361
- const geoRows = this.geonamesCompStmt.all(`{longname} : "${qraw}" *`);
359
+ const geoRows0 = this.geonamesCompStmt.all(`{longname} : "${qraw}" *`);
360
+ const ids = new Set();
361
+ const geoRows = [];
362
+
363
+ for (const row of geoRows0) {
364
+ const id = row.geonameid;
365
+
366
+ if (!ids.has(id)) {
367
+ ids.add(id);
368
+ geoRows.push(row);
369
+ }
370
+ }
371
+
362
372
  const geoMatches = geoRows.map(res => {
363
373
  const loc = this.lookupGeoname(res.geonameid);
364
374
  const country = this.countryNames.get(loc.getCountryCode()) || '';
365
375
  const admin1 = loc.admin1 || '';
366
- const rank = Math.trunc(res.myrank * 100.0) / 100.0;
367
376
  const obj = {
368
377
  id: res.geonameid,
369
378
  value: loc.name,
@@ -372,8 +381,7 @@ class GeoDb {
372
381
  latitude: loc.latitude,
373
382
  longitude: loc.longitude,
374
383
  timezone: loc.getTzid(),
375
- geo: 'geoname',
376
- rank: rank
384
+ geo: 'geoname'
377
385
  };
378
386
 
379
387
  if (loc.population) {
@@ -399,11 +407,10 @@ 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;
407
414
  const obj = {
408
415
  id: zipCode,
409
416
  value: loc.getName(),
@@ -414,8 +421,7 @@ class GeoDb {
414
421
  longitude: loc.longitude,
415
422
  timezone: loc.getTzid(),
416
423
  population: loc.population,
417
- geo: 'zip',
418
- rank: myrank
424
+ geo: 'zip'
419
425
  };
420
426
  return obj;
421
427
  });
@@ -436,8 +442,8 @@ class GeoDb {
436
442
  }
437
443
 
438
444
  const values = Array.from(map.values());
439
- values.sort((a, b) => b.rank - a.rank);
440
- const topN = values.slice(0, 10);
445
+ values.sort((a, b) => b.population - a.population);
446
+ const topN = values.slice(0, 15);
441
447
 
442
448
  if (!latlong) {
443
449
  for (const val of topN) {
@@ -628,12 +634,13 @@ async function buildGeonamesSqlite(opts) {
628
634
  `);
629
635
  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
636
  doSql(logger, db, `DROP TABLE IF EXISTS geoname_fulltext`, `CREATE VIRTUAL TABLE geoname_fulltext
631
- USING fts5(geonameid, longname, population);
637
+ USING fts5(geonameid UNINDEXED, longname, population, city, admin1, country);
632
638
  `, `DROP TABLE IF EXISTS geoname_non_ascii`, `CREATE TABLE geoname_non_ascii AS
633
639
  SELECT geonameid FROM geoname WHERE asciiname <> name`, `INSERT INTO geoname_fulltext
634
640
  SELECT g.geonameid,
635
641
  g.asciiname||', '||a.asciiname||', '||c.Country,
636
- g.population
642
+ g.population,
643
+ g.asciiname,a.asciiname,c.Country
637
644
  FROM geoname g, admin1 a, country c
638
645
  WHERE g.country = c.ISO
639
646
  AND g.country <> 'US'
@@ -641,21 +648,24 @@ async function buildGeonamesSqlite(opts) {
641
648
  `, `INSERT INTO geoname_fulltext
642
649
  SELECT g.geonameid,
643
650
  g.asciiname||', '||a.asciiname||', USA',
644
- g.population
651
+ g.population,
652
+ g.asciiname,a.asciiname,'USA'
645
653
  FROM geoname g, admin1 a
646
654
  WHERE g.country = 'US'
647
655
  AND g.country||'.'||g.admin1 = a.key
648
656
  `, `INSERT INTO geoname_fulltext
649
657
  SELECT g.geonameid,
650
658
  g.asciiname||', '||c.Country,
651
- g.population
659
+ g.population,
660
+ g.asciiname,NULL,c.Country
652
661
  FROM geoname g, country c
653
662
  WHERE g.country = c.ISO
654
663
  AND (g.admin1 = '' OR g.admin1 = '00')
655
664
  `, `INSERT INTO geoname_fulltext
656
665
  SELECT g.geonameid,
657
666
  g.name||', '||a.name||', '||c.Country,
658
- g.population
667
+ g.population,
668
+ g.name,a.name,c.Country
659
669
  FROM geoname_non_ascii gna, geoname g, admin1 a, country c
660
670
  WHERE gna.geonameid = g.geonameid
661
671
  AND g.country = c.ISO
@@ -663,7 +673,8 @@ async function buildGeonamesSqlite(opts) {
663
673
  `, `INSERT INTO geoname_fulltext
664
674
  SELECT g.geonameid,
665
675
  alt.name||', ישראל',
666
- g.population
676
+ g.population,
677
+ alt.name,NULL,'ישראל'
667
678
  FROM geoname g, altnames alt
668
679
  WHERE g.country = 'IL'
669
680
  AND alt.isolanguage = 'he'
@@ -671,7 +682,8 @@ async function buildGeonamesSqlite(opts) {
671
682
  `, `INSERT INTO geoname_fulltext
672
683
  SELECT g.geonameid,
673
684
  alt.name||', '||a1.asciiname||', Israel',
674
- g.population
685
+ g.population,
686
+ alt.name,a1.asciiname,'Israel'
675
687
  FROM geoname g, admin1 a1, altnames alt
676
688
  WHERE g.country = 'IL'
677
689
  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.5.1 */
2
2
  import Database from 'better-sqlite3';
3
3
  import { Location, Locale } from '@hebcal/core';
4
4
  import '@hebcal/cities';
@@ -48,18 +48,16 @@ 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
52
52
  FROM ZIPCodes_CityFullText5
53
- WHERE CityMixedCase MATCH ?
53
+ WHERE ZIPCodes_CityFullText5 MATCH ?
54
54
  ORDER BY Population DESC
55
- LIMIT 15`;
56
- const GEONAME_COMPLETE_SQL = `SELECT geonameid, longname,
57
- ((sqrt(population)/40) + (-30 * rank)) as myrank
55
+ LIMIT 20`;
56
+ const GEONAME_COMPLETE_SQL = `SELECT geonameid
58
57
  FROM geoname_fulltext
59
58
  WHERE geoname_fulltext MATCH ?
60
- GROUP BY geonameid
61
- ORDER BY myrank DESC
62
- LIMIT 15`;
59
+ ORDER BY population DESC
60
+ LIMIT 20`;
63
61
  const stateNames = {
64
62
  'AK': 'Alaska',
65
63
  'AL': 'Alabama',
@@ -346,12 +344,23 @@ class GeoDb {
346
344
  }
347
345
 
348
346
  qraw = qraw.replace(/\"/g, '""');
349
- const geoRows = this.geonamesCompStmt.all(`{longname} : "${qraw}" *`);
347
+ const geoRows0 = this.geonamesCompStmt.all(`{longname} : "${qraw}" *`);
348
+ const ids = new Set();
349
+ const geoRows = [];
350
+
351
+ for (const row of geoRows0) {
352
+ const id = row.geonameid;
353
+
354
+ if (!ids.has(id)) {
355
+ ids.add(id);
356
+ geoRows.push(row);
357
+ }
358
+ }
359
+
350
360
  const geoMatches = geoRows.map(res => {
351
361
  const loc = this.lookupGeoname(res.geonameid);
352
362
  const country = this.countryNames.get(loc.getCountryCode()) || '';
353
363
  const admin1 = loc.admin1 || '';
354
- const rank = Math.trunc(res.myrank * 100.0) / 100.0;
355
364
  const obj = {
356
365
  id: res.geonameid,
357
366
  value: loc.name,
@@ -360,8 +369,7 @@ class GeoDb {
360
369
  latitude: loc.latitude,
361
370
  longitude: loc.longitude,
362
371
  timezone: loc.getTzid(),
363
- geo: 'geoname',
364
- rank: rank
372
+ geo: 'geoname'
365
373
  };
366
374
 
367
375
  if (loc.population) {
@@ -387,11 +395,10 @@ 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;
395
402
  const obj = {
396
403
  id: zipCode,
397
404
  value: loc.getName(),
@@ -402,8 +409,7 @@ class GeoDb {
402
409
  longitude: loc.longitude,
403
410
  timezone: loc.getTzid(),
404
411
  population: loc.population,
405
- geo: 'zip',
406
- rank: myrank
412
+ geo: 'zip'
407
413
  };
408
414
  return obj;
409
415
  });
@@ -424,8 +430,8 @@ class GeoDb {
424
430
  }
425
431
 
426
432
  const values = Array.from(map.values());
427
- values.sort((a, b) => b.rank - a.rank);
428
- const topN = values.slice(0, 10);
433
+ values.sort((a, b) => b.population - a.population);
434
+ const topN = values.slice(0, 15);
429
435
 
430
436
  if (!latlong) {
431
437
  for (const val of topN) {
@@ -616,12 +622,13 @@ async function buildGeonamesSqlite(opts) {
616
622
  `);
617
623
  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
624
  doSql(logger, db, `DROP TABLE IF EXISTS geoname_fulltext`, `CREATE VIRTUAL TABLE geoname_fulltext
619
- USING fts5(geonameid, longname, population);
625
+ USING fts5(geonameid UNINDEXED, longname, population, city, admin1, country);
620
626
  `, `DROP TABLE IF EXISTS geoname_non_ascii`, `CREATE TABLE geoname_non_ascii AS
621
627
  SELECT geonameid FROM geoname WHERE asciiname <> name`, `INSERT INTO geoname_fulltext
622
628
  SELECT g.geonameid,
623
629
  g.asciiname||', '||a.asciiname||', '||c.Country,
624
- g.population
630
+ g.population,
631
+ g.asciiname,a.asciiname,c.Country
625
632
  FROM geoname g, admin1 a, country c
626
633
  WHERE g.country = c.ISO
627
634
  AND g.country <> 'US'
@@ -629,21 +636,24 @@ async function buildGeonamesSqlite(opts) {
629
636
  `, `INSERT INTO geoname_fulltext
630
637
  SELECT g.geonameid,
631
638
  g.asciiname||', '||a.asciiname||', USA',
632
- g.population
639
+ g.population,
640
+ g.asciiname,a.asciiname,'USA'
633
641
  FROM geoname g, admin1 a
634
642
  WHERE g.country = 'US'
635
643
  AND g.country||'.'||g.admin1 = a.key
636
644
  `, `INSERT INTO geoname_fulltext
637
645
  SELECT g.geonameid,
638
646
  g.asciiname||', '||c.Country,
639
- g.population
647
+ g.population,
648
+ g.asciiname,NULL,c.Country
640
649
  FROM geoname g, country c
641
650
  WHERE g.country = c.ISO
642
651
  AND (g.admin1 = '' OR g.admin1 = '00')
643
652
  `, `INSERT INTO geoname_fulltext
644
653
  SELECT g.geonameid,
645
654
  g.name||', '||a.name||', '||c.Country,
646
- g.population
655
+ g.population,
656
+ g.name,a.name,c.Country
647
657
  FROM geoname_non_ascii gna, geoname g, admin1 a, country c
648
658
  WHERE gna.geonameid = g.geonameid
649
659
  AND g.country = c.ISO
@@ -651,7 +661,8 @@ async function buildGeonamesSqlite(opts) {
651
661
  `, `INSERT INTO geoname_fulltext
652
662
  SELECT g.geonameid,
653
663
  alt.name||', ישראל',
654
- g.population
664
+ g.population,
665
+ alt.name,NULL,'ישראל'
655
666
  FROM geoname g, altnames alt
656
667
  WHERE g.country = 'IL'
657
668
  AND alt.isolanguage = 'he'
@@ -659,7 +670,8 @@ async function buildGeonamesSqlite(opts) {
659
670
  `, `INSERT INTO geoname_fulltext
660
671
  SELECT g.geonameid,
661
672
  alt.name||', '||a1.asciiname||', Israel',
662
- g.population
673
+ g.population,
674
+ alt.name,a1.asciiname,'Israel'
663
675
  FROM geoname g, admin1 a1, altnames alt
664
676
  WHERE g.country = 'IL'
665
677
  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.5.1",
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);