@@ -13,15 +13,15 @@ const db = new Loki('geodata.db', {
1313
1414// Create a collection for geo data with optimized configuration
1515const geoCollection = db . addCollection ( 'geodata' , {
16- indices : [ 'geo2 ' , 'category' ] ,
16+ indices : [ 'geo3 ' , 'category' ] ,
1717 adaptiveBinaryIndices : false , // Disable adaptive indices for bulk operations
1818 transactional : false , // Disable transactions for better performance
1919 clone : false , // Disable object cloning for better performance
2020 disableMeta : true // Disable meta properties for better performance
2121} ) ;
2222
2323const tinyGeoCollection = db . addCollection ( 'tinygeodata' , {
24- indices : [ 'geo2 ' , 'category' ] ,
24+ indices : [ 'geo3 ' , 'category' ] ,
2525 adaptiveBinaryIndices : false ,
2626 transactional : false ,
2727 clone : false ,
@@ -30,7 +30,7 @@ const tinyGeoCollection = db.addCollection('tinygeodata', {
3030
3131/**
3232 * @typedef {Object } GeoDocument
33- * @property {string } geo2 - Geohash
33+ * @property {string } geo3 - Geohash
3434 * @property {string } category - Category
3535 * @property {number } lat - Latitude
3636 * @property {number } lon - Longitude
@@ -51,7 +51,7 @@ function addLatLonToRows(rows) {
5151 const latLon = ngeohash . decode ( row . geohash ) ;
5252 row . lat = latLon . latitude ;
5353 row . lon = latLon . longitude ;
54- row . geo2 = row . geohash . substring ( 0 , 2 ) ;
54+ row . geo3 = row . geohash . substring ( 0 , 3 ) ;
5555 }
5656 }
5757}
@@ -162,14 +162,17 @@ async function downloadMissingData(urls) {
162162
163163
164164function queryGeoTable ( table , minLat , maxLat , minLon , maxLon ) {
165- const geohashes_2 = ngeohash . bboxes ( minLat , minLon , maxLat , maxLon , 2 ) ;
166- return table . find ( { geo2 : { '$in' : geohashes_2 } } ) . filter ( doc =>
167- doc . lat > minLat &&
168- doc . lat < maxLat &&
169- doc . lon > minLon &&
170- doc . lon < maxLon
171- ) ;
172- }
165+ const geohashes_3 = ngeohash . bboxes ( minLat , minLon , maxLat , maxLon , 3 ) ;
166+ console . log ( "geohashes_3" , geohashes_3 ) ;
167+ // Use LokiJS chaining to filter by geo3 and lat first
168+ // const data = table.find()
169+ // console.log("data", data.length);
170+ // console.log("geo3 only", data.filter(doc => geohashes_3.includes(doc.geo3)).length);
171+ return table . chain ( )
172+ . find ( { geo3 : { '$in' : geohashes_3 } } )
173+ . where ( obj => obj . lat >= minLat && obj . lat <= maxLat && obj . lon >= minLon && obj . lon <= maxLon )
174+ . data ( ) ;
175+ }
173176
174177/**
175178 * Get geo entries within bounds - optimized version
0 commit comments