UNPKG

gis-tools-ts

Version:

A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.

90 lines 2.21 kB
import { S2FileStore } from '../file.js'; /** * # Vector File Store * * ## Description * A filesystem vector store * * ## Usage * ```ts * import { FileVector } from 'gis-tools-ts/file'; * import type { VectorKey } from 'gis-tools-ts'; * * interface Data extends VectorKey { name: string }; * * const vec = new FileVector<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 FileVector { #store; /** @param fileName - the path + file name without the extension */ constructor(fileName) { this.#store = new S2FileStore(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=file.js.map