@hebcal/geo-sqlite 4.4.1 → 4.4.2

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.1 */
1
+ /*! @hebcal/geo-sqlite v4.4.2 */
2
2
  'use strict';
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
@@ -29,6 +29,7 @@ const GEONAME_SQL = `SELECT
29
29
  a.asciiname as admin1,
30
30
  g.latitude as latitude,
31
31
  g.longitude as longitude,
32
+ g.population as population,
32
33
  g.timezone as timezone
33
34
  FROM geoname g
34
35
  LEFT JOIN country c on g.country = c.iso
@@ -44,27 +45,27 @@ const GEONAME_ALL_SQL = `SELECT
44
45
  a.asciiname as admin1,
45
46
  g.latitude as latitude,
46
47
  g.longitude as longitude,
48
+ g.population as population,
47
49
  g.timezone as timezone
48
50
  FROM geoname g
49
51
  LEFT JOIN country c on g.country = c.iso
50
52
  LEFT JOIN admin1 a on g.country||'.'||g.admin1 = a.key
51
53
  `;
52
- const ZIPCODE_SQL = `SELECT CityMixedCase,State,Latitude,Longitude,TimeZone,DayLightSaving
54
+ const ZIPCODE_SQL = `SELECT ZipCode,CityMixedCase,State,Latitude,Longitude,TimeZone,DayLightSaving,Population
53
55
  FROM ZIPCodes_Primary WHERE ZipCode = ?`;
54
- const ZIPCODE_ALL_SQL = `SELECT ZipCode,CityMixedCase,State,Latitude,Longitude,TimeZone,DayLightSaving
56
+ const ZIPCODE_ALL_SQL = `SELECT ZipCode,CityMixedCase,State,Latitude,Longitude,TimeZone,DayLightSaving,Population
55
57
  FROM ZIPCodes_Primary`;
56
58
  const ZIP_COMPLETE_SQL = `SELECT ZipCode,CityMixedCase,State,Latitude,Longitude,TimeZone,DayLightSaving,Population
57
59
  FROM ZIPCodes_Primary
58
60
  WHERE ZipCode LIKE ?
59
61
  ORDER BY Population DESC
60
62
  LIMIT 10`;
61
- const ZIP_FULLTEXT_COMPLETE_SQL = `SELECT ZipCode,CityMixedCase,State,Latitude,Longitude,TimeZone,DayLightSaving,Population
62
- FROM ZIPCodes_CityFullText
63
+ const ZIP_FULLTEXT_COMPLETE_SQL = `SELECT ZipCode,CityMixedCase,rank
64
+ FROM ZIPCodes_CityFullText5
63
65
  WHERE CityMixedCase MATCH ?
64
66
  ORDER BY Population DESC
65
67
  LIMIT 15`;
66
- const GEONAME_COMPLETE_SQL = `SELECT geonameid, asciiname, admin1, country,
67
- population, latitude, longitude, timezone,
68
+ const GEONAME_COMPLETE_SQL = `SELECT geonameid, longname,
68
69
  ((sqrt(population)/40) + (-30 * rank)) as myrank
69
70
  FROM geoname_fulltext
70
71
  WHERE geoname_fulltext MATCH ?
@@ -226,6 +227,7 @@ class GeoDb {
226
227
  location.stateName = stateNames[location.state];
227
228
  location.geo = 'zip';
228
229
  location.zip = zip;
230
+ location.population = result.Population;
229
231
  return location;
230
232
  }
231
233
  /**
@@ -263,13 +265,9 @@ class GeoDb {
263
265
  const admin1 = result.admin1 || '';
264
266
  const cityDescr = core.Location.geonameCityDescr(result.name, admin1, country);
265
267
  const location = new core.Location(result.latitude, result.longitude, result.cc == 'IL', result.timezone, cityDescr, result.cc, geonameid);
266
-
267
- if (result.asciiname !== result.name) {
268
- location.asciiname = result.asciiname;
269
- }
270
-
271
268
  location.geo = 'geoname';
272
269
  location.geonameid = geonameid;
270
+ location.asciiname = result.asciiname;
273
271
 
274
272
  if (admin1) {
275
273
  location.admin1 = admin1;
@@ -279,6 +277,10 @@ class GeoDb {
279
277
  location.jersualem = true;
280
278
  }
281
279
 
280
+ if (result.population) {
281
+ location.population = result.population;
282
+ }
283
+
282
284
  return location;
283
285
  }
284
286
  /**
@@ -358,23 +360,30 @@ class GeoDb {
358
360
  qraw = qraw.replace(/\"/g, '""');
359
361
  const geoRows = this.geonamesCompStmt.all(`{longname} : "${qraw}" *`);
360
362
  const geoMatches = geoRows.map(res => {
361
- const country = res.country || '';
362
- const admin1 = res.admin1 || '';
363
+ const loc = this.lookupGeoname(res.geonameid);
364
+ const country = this.countryNames.get(loc.getCountryCode()) || '';
365
+ const admin1 = loc.admin1 || '';
363
366
  const rank = Math.trunc(res.myrank * 100.0) / 100.0;
364
367
  const obj = {
365
368
  id: res.geonameid,
366
- value: core.Location.geonameCityDescr(res.asciiname, admin1, country),
367
- asciiname: res.asciiname,
369
+ value: loc.name,
368
370
  admin1,
369
371
  country,
370
- latitude: res.latitude,
371
- longitude: res.longitude,
372
- timezone: res.timezone,
373
- population: res.population,
372
+ latitude: loc.latitude,
373
+ longitude: loc.longitude,
374
+ timezone: loc.getTzid(),
374
375
  geo: 'geoname',
375
376
  rank: rank
376
377
  };
377
378
 
379
+ if (loc.population) {
380
+ obj.population = loc.population;
381
+ }
382
+
383
+ if (loc.asciiname) {
384
+ obj.asciiname = loc.asciiname;
385
+ }
386
+
378
387
  if (country) {
379
388
  obj.country = country;
380
389
  }
@@ -390,8 +399,26 @@ class GeoDb {
390
399
  this.zipFulltextCompStmt = this.zipsDb.prepare(ZIP_FULLTEXT_COMPLETE_SQL);
391
400
  }
392
401
 
393
- const zipRows = this.zipFulltextCompStmt.all(`"${qraw}*"`);
394
- const zipMatches = zipRows.map(GeoDb.zipResultToObj);
402
+ const zipRows = this.zipFulltextCompStmt.all(`"${qraw}" *`);
403
+ const zipMatches = zipRows.map(res => {
404
+ const zipCode = res.ZipCode;
405
+ const loc = this.lookupZip(zipCode);
406
+ const myrank = Math.sqrt(loc.population) / 40 + -30 * res.rank;
407
+ const obj = {
408
+ id: zipCode,
409
+ value: loc.getName(),
410
+ admin1: loc.admin1,
411
+ asciiname: loc.getShortName(),
412
+ country: 'United States',
413
+ latitude: loc.latitude,
414
+ longitude: loc.longitude,
415
+ timezone: loc.getTzid(),
416
+ population: loc.population,
417
+ geo: 'zip',
418
+ rank: myrank
419
+ };
420
+ return obj;
421
+ });
395
422
  const map = new Map();
396
423
 
397
424
  for (const obj of zipMatches) {
@@ -409,7 +436,7 @@ class GeoDb {
409
436
  }
410
437
 
411
438
  const values = Array.from(map.values());
412
- values.sort((a, b) => b.population - a.population);
439
+ values.sort((a, b) => b.rank - a.rank);
413
440
  const topN = values.slice(0, 10);
414
441
 
415
442
  if (!latlong) {
@@ -601,17 +628,12 @@ async function buildGeonamesSqlite(opts) {
601
628
  `);
602
629
  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;`);
603
630
  doSql(logger, db, `DROP TABLE IF EXISTS geoname_fulltext`, `CREATE VIRTUAL TABLE geoname_fulltext
604
- USING fts5(geonameid,
605
- longname,
606
- asciiname, admin1, country,
607
- population, latitude, longitude, timezone
608
- );
631
+ USING fts5(geonameid, longname, population);
609
632
  `, `DROP TABLE IF EXISTS geoname_non_ascii`, `CREATE TABLE geoname_non_ascii AS
610
633
  SELECT geonameid FROM geoname WHERE asciiname <> name`, `INSERT INTO geoname_fulltext
611
634
  SELECT g.geonameid,
612
635
  g.asciiname||', '||a.asciiname||', '||c.Country,
613
- g.asciiname, a.asciiname, c.Country,
614
- g.population, g.latitude, g.longitude, g.timezone
636
+ g.population
615
637
  FROM geoname g, admin1 a, country c
616
638
  WHERE g.country = c.ISO
617
639
  AND g.country <> 'US'
@@ -619,24 +641,21 @@ async function buildGeonamesSqlite(opts) {
619
641
  `, `INSERT INTO geoname_fulltext
620
642
  SELECT g.geonameid,
621
643
  g.asciiname||', '||a.asciiname||', USA',
622
- g.asciiname, a.asciiname, 'United States',
623
- g.population, g.latitude, g.longitude, g.timezone
644
+ g.population
624
645
  FROM geoname g, admin1 a
625
646
  WHERE g.country = 'US'
626
647
  AND g.country||'.'||g.admin1 = a.key
627
648
  `, `INSERT INTO geoname_fulltext
628
649
  SELECT g.geonameid,
629
650
  g.asciiname||', '||c.Country,
630
- g.asciiname, '', c.Country,
631
- g.population, g.latitude, g.longitude, g.timezone
651
+ g.population
632
652
  FROM geoname g, country c
633
653
  WHERE g.country = c.ISO
634
654
  AND (g.admin1 = '' OR g.admin1 = '00')
635
655
  `, `INSERT INTO geoname_fulltext
636
656
  SELECT g.geonameid,
637
657
  g.name||', '||a.name||', '||c.Country,
638
- g.name, a.name, c.Country,
639
- g.population, g.latitude, g.longitude, g.timezone
658
+ g.population
640
659
  FROM geoname_non_ascii gna, geoname g, admin1 a, country c
641
660
  WHERE gna.geonameid = g.geonameid
642
661
  AND g.country = c.ISO
@@ -644,8 +663,7 @@ async function buildGeonamesSqlite(opts) {
644
663
  `, `INSERT INTO geoname_fulltext
645
664
  SELECT g.geonameid,
646
665
  alt.name||', ישראל',
647
- alt.name, '', 'ישראל',
648
- g.population, g.latitude, g.longitude, g.timezone
666
+ g.population
649
667
  FROM geoname g, altnames alt
650
668
  WHERE g.country = 'IL'
651
669
  AND alt.isolanguage = 'he'
@@ -653,8 +671,7 @@ async function buildGeonamesSqlite(opts) {
653
671
  `, `INSERT INTO geoname_fulltext
654
672
  SELECT g.geonameid,
655
673
  alt.name||', '||a1.asciiname||', Israel',
656
- alt.name, a1.asciiname, 'Israel',
657
- g.population, g.latitude, g.longitude, g.timezone
674
+ g.population
658
675
  FROM geoname g, admin1 a1, altnames alt
659
676
  WHERE g.country = 'IL'
660
677
  AND alt.isolanguage = 'en'
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! @hebcal/geo-sqlite v4.4.1 */
1
+ /*! @hebcal/geo-sqlite v4.4.2 */
2
2
  import Database from 'better-sqlite3';
3
3
  import { Location, Locale } from '@hebcal/core';
4
4
  import '@hebcal/cities';
@@ -17,6 +17,7 @@ const GEONAME_SQL = `SELECT
17
17
  a.asciiname as admin1,
18
18
  g.latitude as latitude,
19
19
  g.longitude as longitude,
20
+ g.population as population,
20
21
  g.timezone as timezone
21
22
  FROM geoname g
22
23
  LEFT JOIN country c on g.country = c.iso
@@ -32,27 +33,27 @@ const GEONAME_ALL_SQL = `SELECT
32
33
  a.asciiname as admin1,
33
34
  g.latitude as latitude,
34
35
  g.longitude as longitude,
36
+ g.population as population,
35
37
  g.timezone as timezone
36
38
  FROM geoname g
37
39
  LEFT JOIN country c on g.country = c.iso
38
40
  LEFT JOIN admin1 a on g.country||'.'||g.admin1 = a.key
39
41
  `;
40
- const ZIPCODE_SQL = `SELECT CityMixedCase,State,Latitude,Longitude,TimeZone,DayLightSaving
42
+ const ZIPCODE_SQL = `SELECT ZipCode,CityMixedCase,State,Latitude,Longitude,TimeZone,DayLightSaving,Population
41
43
  FROM ZIPCodes_Primary WHERE ZipCode = ?`;
42
- const ZIPCODE_ALL_SQL = `SELECT ZipCode,CityMixedCase,State,Latitude,Longitude,TimeZone,DayLightSaving
44
+ const ZIPCODE_ALL_SQL = `SELECT ZipCode,CityMixedCase,State,Latitude,Longitude,TimeZone,DayLightSaving,Population
43
45
  FROM ZIPCodes_Primary`;
44
46
  const ZIP_COMPLETE_SQL = `SELECT ZipCode,CityMixedCase,State,Latitude,Longitude,TimeZone,DayLightSaving,Population
45
47
  FROM ZIPCodes_Primary
46
48
  WHERE ZipCode LIKE ?
47
49
  ORDER BY Population DESC
48
50
  LIMIT 10`;
49
- const ZIP_FULLTEXT_COMPLETE_SQL = `SELECT ZipCode,CityMixedCase,State,Latitude,Longitude,TimeZone,DayLightSaving,Population
50
- FROM ZIPCodes_CityFullText
51
+ const ZIP_FULLTEXT_COMPLETE_SQL = `SELECT ZipCode,CityMixedCase,rank
52
+ FROM ZIPCodes_CityFullText5
51
53
  WHERE CityMixedCase MATCH ?
52
54
  ORDER BY Population DESC
53
55
  LIMIT 15`;
54
- const GEONAME_COMPLETE_SQL = `SELECT geonameid, asciiname, admin1, country,
55
- population, latitude, longitude, timezone,
56
+ const GEONAME_COMPLETE_SQL = `SELECT geonameid, longname,
56
57
  ((sqrt(population)/40) + (-30 * rank)) as myrank
57
58
  FROM geoname_fulltext
58
59
  WHERE geoname_fulltext MATCH ?
@@ -214,6 +215,7 @@ class GeoDb {
214
215
  location.stateName = stateNames[location.state];
215
216
  location.geo = 'zip';
216
217
  location.zip = zip;
218
+ location.population = result.Population;
217
219
  return location;
218
220
  }
219
221
  /**
@@ -251,13 +253,9 @@ class GeoDb {
251
253
  const admin1 = result.admin1 || '';
252
254
  const cityDescr = Location.geonameCityDescr(result.name, admin1, country);
253
255
  const location = new Location(result.latitude, result.longitude, result.cc == 'IL', result.timezone, cityDescr, result.cc, geonameid);
254
-
255
- if (result.asciiname !== result.name) {
256
- location.asciiname = result.asciiname;
257
- }
258
-
259
256
  location.geo = 'geoname';
260
257
  location.geonameid = geonameid;
258
+ location.asciiname = result.asciiname;
261
259
 
262
260
  if (admin1) {
263
261
  location.admin1 = admin1;
@@ -267,6 +265,10 @@ class GeoDb {
267
265
  location.jersualem = true;
268
266
  }
269
267
 
268
+ if (result.population) {
269
+ location.population = result.population;
270
+ }
271
+
270
272
  return location;
271
273
  }
272
274
  /**
@@ -346,23 +348,30 @@ class GeoDb {
346
348
  qraw = qraw.replace(/\"/g, '""');
347
349
  const geoRows = this.geonamesCompStmt.all(`{longname} : "${qraw}" *`);
348
350
  const geoMatches = geoRows.map(res => {
349
- const country = res.country || '';
350
- const admin1 = res.admin1 || '';
351
+ const loc = this.lookupGeoname(res.geonameid);
352
+ const country = this.countryNames.get(loc.getCountryCode()) || '';
353
+ const admin1 = loc.admin1 || '';
351
354
  const rank = Math.trunc(res.myrank * 100.0) / 100.0;
352
355
  const obj = {
353
356
  id: res.geonameid,
354
- value: Location.geonameCityDescr(res.asciiname, admin1, country),
355
- asciiname: res.asciiname,
357
+ value: loc.name,
356
358
  admin1,
357
359
  country,
358
- latitude: res.latitude,
359
- longitude: res.longitude,
360
- timezone: res.timezone,
361
- population: res.population,
360
+ latitude: loc.latitude,
361
+ longitude: loc.longitude,
362
+ timezone: loc.getTzid(),
362
363
  geo: 'geoname',
363
364
  rank: rank
364
365
  };
365
366
 
367
+ if (loc.population) {
368
+ obj.population = loc.population;
369
+ }
370
+
371
+ if (loc.asciiname) {
372
+ obj.asciiname = loc.asciiname;
373
+ }
374
+
366
375
  if (country) {
367
376
  obj.country = country;
368
377
  }
@@ -378,8 +387,26 @@ class GeoDb {
378
387
  this.zipFulltextCompStmt = this.zipsDb.prepare(ZIP_FULLTEXT_COMPLETE_SQL);
379
388
  }
380
389
 
381
- const zipRows = this.zipFulltextCompStmt.all(`"${qraw}*"`);
382
- const zipMatches = zipRows.map(GeoDb.zipResultToObj);
390
+ const zipRows = this.zipFulltextCompStmt.all(`"${qraw}" *`);
391
+ const zipMatches = zipRows.map(res => {
392
+ const zipCode = res.ZipCode;
393
+ const loc = this.lookupZip(zipCode);
394
+ const myrank = Math.sqrt(loc.population) / 40 + -30 * res.rank;
395
+ const obj = {
396
+ id: zipCode,
397
+ value: loc.getName(),
398
+ admin1: loc.admin1,
399
+ asciiname: loc.getShortName(),
400
+ country: 'United States',
401
+ latitude: loc.latitude,
402
+ longitude: loc.longitude,
403
+ timezone: loc.getTzid(),
404
+ population: loc.population,
405
+ geo: 'zip',
406
+ rank: myrank
407
+ };
408
+ return obj;
409
+ });
383
410
  const map = new Map();
384
411
 
385
412
  for (const obj of zipMatches) {
@@ -397,7 +424,7 @@ class GeoDb {
397
424
  }
398
425
 
399
426
  const values = Array.from(map.values());
400
- values.sort((a, b) => b.population - a.population);
427
+ values.sort((a, b) => b.rank - a.rank);
401
428
  const topN = values.slice(0, 10);
402
429
 
403
430
  if (!latlong) {
@@ -589,17 +616,12 @@ async function buildGeonamesSqlite(opts) {
589
616
  `);
590
617
  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;`);
591
618
  doSql(logger, db, `DROP TABLE IF EXISTS geoname_fulltext`, `CREATE VIRTUAL TABLE geoname_fulltext
592
- USING fts5(geonameid,
593
- longname,
594
- asciiname, admin1, country,
595
- population, latitude, longitude, timezone
596
- );
619
+ USING fts5(geonameid, longname, population);
597
620
  `, `DROP TABLE IF EXISTS geoname_non_ascii`, `CREATE TABLE geoname_non_ascii AS
598
621
  SELECT geonameid FROM geoname WHERE asciiname <> name`, `INSERT INTO geoname_fulltext
599
622
  SELECT g.geonameid,
600
623
  g.asciiname||', '||a.asciiname||', '||c.Country,
601
- g.asciiname, a.asciiname, c.Country,
602
- g.population, g.latitude, g.longitude, g.timezone
624
+ g.population
603
625
  FROM geoname g, admin1 a, country c
604
626
  WHERE g.country = c.ISO
605
627
  AND g.country <> 'US'
@@ -607,24 +629,21 @@ async function buildGeonamesSqlite(opts) {
607
629
  `, `INSERT INTO geoname_fulltext
608
630
  SELECT g.geonameid,
609
631
  g.asciiname||', '||a.asciiname||', USA',
610
- g.asciiname, a.asciiname, 'United States',
611
- g.population, g.latitude, g.longitude, g.timezone
632
+ g.population
612
633
  FROM geoname g, admin1 a
613
634
  WHERE g.country = 'US'
614
635
  AND g.country||'.'||g.admin1 = a.key
615
636
  `, `INSERT INTO geoname_fulltext
616
637
  SELECT g.geonameid,
617
638
  g.asciiname||', '||c.Country,
618
- g.asciiname, '', c.Country,
619
- g.population, g.latitude, g.longitude, g.timezone
639
+ g.population
620
640
  FROM geoname g, country c
621
641
  WHERE g.country = c.ISO
622
642
  AND (g.admin1 = '' OR g.admin1 = '00')
623
643
  `, `INSERT INTO geoname_fulltext
624
644
  SELECT g.geonameid,
625
645
  g.name||', '||a.name||', '||c.Country,
626
- g.name, a.name, c.Country,
627
- g.population, g.latitude, g.longitude, g.timezone
646
+ g.population
628
647
  FROM geoname_non_ascii gna, geoname g, admin1 a, country c
629
648
  WHERE gna.geonameid = g.geonameid
630
649
  AND g.country = c.ISO
@@ -632,8 +651,7 @@ async function buildGeonamesSqlite(opts) {
632
651
  `, `INSERT INTO geoname_fulltext
633
652
  SELECT g.geonameid,
634
653
  alt.name||', ישראל',
635
- alt.name, '', 'ישראל',
636
- g.population, g.latitude, g.longitude, g.timezone
654
+ g.population
637
655
  FROM geoname g, altnames alt
638
656
  WHERE g.country = 'IL'
639
657
  AND alt.isolanguage = 'he'
@@ -641,8 +659,7 @@ async function buildGeonamesSqlite(opts) {
641
659
  `, `INSERT INTO geoname_fulltext
642
660
  SELECT g.geonameid,
643
661
  alt.name||', '||a1.asciiname||', Israel',
644
- alt.name, a1.asciiname, 'Israel',
645
- g.population, g.latitude, g.longitude, g.timezone
662
+ g.population
646
663
  FROM geoname g, admin1 a1, altnames alt
647
664
  WHERE g.country = 'IL'
648
665
  AND alt.isolanguage = 'en'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hebcal/geo-sqlite",
3
- "version": "4.4.1",
3
+ "version": "4.4.2",
4
4
  "author": "Michael J. Radwin (https://github.com/mjradwin)",
5
5
  "keywords": [
6
6
  "hebcal"
package/zips-dummy.sql CHANGED
@@ -10,4 +10,7 @@ CREATE TABLE ZIPCodes_Primary (
10
10
  );
11
11
 
12
12
  CREATE VIRTUAL TABLE ZIPCodes_CityFullText
13
- USING fts3(ZipCode,CityMixedCase,State,Latitude,Longitude,TimeZone,DayLightSaving,Population);
13
+ USING fts4(ZipCode,CityMixedCase,State,Latitude,Longitude,TimeZone,DayLightSaving,Population);
14
+
15
+ CREATE VIRTUAL TABLE ZIPCodes_CityFullText5
16
+ USING fts5(ZipCode,CityMixedCase,Population);