gis-tools-ts
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
90 lines • 2.2 kB
JavaScript
import { S2MMapStore } from '../mmap.js';
/**
* # Vector MMap Store
*
* ## Description
* A mmap vector store
*
* ## Usage
* ```ts
* import { MMapVector } from 'gis-tools-ts/mmap';
* import type { VectorKey } from 'gis-tools-ts';
*
* interface Data extends VectorKey { name: string };
*
* const vec = new MMapVector<Data>();
* // push an entry
* vec.push({ cell: 1n, name: 'test' });
* vec.push({ cell: 1n, name: 'test2' });
* // check if a key exists
* vec.has(1n); // true
* // get length of the store
* console.log(vec.length); // 2
*
* // iterate over the store
* for await (const entry of vec) console.log(entry);
*
* // close the store
* vec.close();
* ```
*/
export class MMapVector {
#store;
/** @param fileName - the path + file name without the extension */
constructor(fileName) {
this.#store = new S2MMapStore(fileName);
}
/** @returns the length of the store */
get length() {
return this.#store.length;
}
/**
* Push a value into the store
* @param value - the value to store
*/
push(value) {
this.#store.set(value.cell, value);
}
/**
* @param index - the position in the store to get the value from
* @returns the value
*/
async get(index) {
const value = await this.#store.get(BigInt(index));
if (value === undefined)
throw new Error('Value not found');
return value[0];
}
/**
* Check if the key exists
* @param key - the key
* @returns true if the key exists
*/
async has(key) {
return await this.#store.has(BigInt(key));
}
/** Sort the store */
async sort() {
await this.#store.sort();
}
/**
* iterate through the values
* @yields {V} - the values iterator
*/
async *values() {
for await (const { value } of this.#store.entries())
yield value;
}
/**
* iterate through the values
* @returns an iterator
*/
[Symbol.asyncIterator]() {
return this.values();
}
/** Closes the store */
close() {
this.#store.close(true);
}
}
//# sourceMappingURL=mmap.js.map