gis-tools-ts
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
207 lines • 8.16 kB
JavaScript
export * from './csv';
export * from './gbfs';
export * from './geotiff';
export * from './gpx';
export * from './grib2';
export * from './gtfs';
export * from './image';
export * from './json';
export * from './las';
export * from './netcdf';
export * from './osm';
export * from './pmtiles';
export * from 'pbf-ts';
export * from './shapefile';
export * from './wkt';
export * from './xml';
export * from './fetch';
export * from './nadgrid';
export * from './tile';
/**
* Convenience function that ensures the input is a usable reader
* @param input - the input data
* @returns - a BufferReader
*/
export function toReader(input) {
if (input instanceof BufferReader)
return input;
else if ('buffer' in input)
return new BufferReader(input.buffer);
else if (input instanceof ArrayBuffer || input instanceof SharedArrayBuffer)
return new BufferReader(input);
else
return input;
}
/** A buffer reader is an extension of a DataView with some extra methods */
export class BufferReader extends DataView {
cursor = 0;
textDecoder = new TextDecoder('utf-8');
/**
* @param buffer - the input buffer
* @param byteOffset - offset in the buffer
* @param byteLength - length of the buffer
*/
constructor(buffer, byteOffset, byteLength) {
super(buffer, byteOffset, byteLength);
}
/**
* @returns - the current position of the cursor
*/
tell() {
return this.cursor;
}
/**
* Set the current position of the cursor
* @param pos - where to adjust the current cursor
*/
seek(pos = 0) {
this.cursor = pos;
}
/**
* Reads a 64-bit unsigned integer (biguint64) at the given byteOffset
* @param byteOffset - The position in the file to read from
* @param littleEndian - Optional, specifies if the value is stored in little-endian format. Defaults to false (big-endian).
* @returns The 64-bit unsigned integer as a bigint
*/
getBigInt64(byteOffset = this.cursor, littleEndian = false) {
this.cursor = byteOffset + 8;
return super.getBigInt64(byteOffset, littleEndian);
}
/**
* Reads a 64-bit unsigned integer (biguint64) at the given byteOffset
* @param byteOffset - The position in the file to read from
* @param littleEndian - Optional, specifies if the value is stored in little-endian format. Defaults to false (big-endian).
* @returns The 64-bit unsigned integer as a bigint
*/
getBigUint64(byteOffset = this.cursor, littleEndian = false) {
this.cursor = byteOffset + 8;
return super.getBigUint64(byteOffset, littleEndian);
}
/**
* Reads a 32-bit floating-point number (float32) at the given byteOffset
* @param byteOffset - The position in the file to read from
* @param littleEndian - Optional, specifies if the value is stored in little-endian format. Defaults to false (big-endian).
* @returns The 32-bit floating-point number as a number
*/
getFloat32(byteOffset = this.cursor, littleEndian = false) {
this.cursor = byteOffset + 4;
return super.getFloat32(byteOffset, littleEndian);
}
/**
* Reads a 64-bit floating-point number (float64) at the given byteOffset
* @param byteOffset - The position in the file to read from
* @param littleEndian - Optional, specifies if the value is stored in little-endian format. Defaults to false (big-endian).
* @returns The 64-bit floating-point number as a number
*/
getFloat64(byteOffset = this.cursor, littleEndian = false) {
this.cursor = byteOffset + 8;
return super.getFloat64(byteOffset, littleEndian);
}
/**
* Reads a signed 16-bit integer (int16) at the given byteOffset
* @param byteOffset - The position in the file to read from
* @param littleEndian - Optional, specifies if the value is stored in little-endian format. Defaults to false (big-endian).
* @returns The 16-bit signed integer value as a number
*/
getInt16(byteOffset = this.cursor, littleEndian = false) {
this.cursor = byteOffset + 2;
return super.getInt16(byteOffset, littleEndian);
}
/**
* Reads a signed 32-bit integer (int32) at the given byteOffset
* @param byteOffset - The position in the file to read from
* @param littleEndian - Optional, specifies if the value is stored in little-endian format. Defaults to false (big-endian).
* @returns The 32-bit signed integer value as a number
*/
getInt32(byteOffset = this.cursor, littleEndian = false) {
this.cursor = byteOffset + 4;
return super.getInt32(byteOffset, littleEndian);
}
/**
* Reads a signed byte (int8) at the given byteOffset
* @param byteOffset - The position in the file to read from
* @returns The byte value as a signed number
*/
getInt8(byteOffset = this.cursor) {
this.cursor = byteOffset + 1;
return super.getInt8(byteOffset);
}
/**
* Reads an unsigned 16-bit integer (uint16) at the given byteOffset
* @param byteOffset - The position in the file to read from
* @param littleEndian - Optional, specifies if the value is stored in little-endian format. Defaults to false (big-endian).
* @returns The 16-bit unsigned integer value as a number
*/
getUint16(byteOffset = this.cursor, littleEndian = false) {
this.cursor = byteOffset + 2;
return super.getUint16(byteOffset, littleEndian);
}
/**
* Reads an unsigned 32-bit integer (uint32) at the given byteOffset
* @param byteOffset - The position in the file to read from
* @param littleEndian - Optional, specifies if the value is stored in little-endian format. Defaults to false (big-endian).
* @returns The 32-bit unsigned integer value as a number
*/
getUint32(byteOffset = this.cursor, littleEndian = false) {
this.cursor = byteOffset + 4;
return super.getUint32(byteOffset, littleEndian);
}
/**
* Reads a single byte at the given byteOffset
* @param byteOffset - The position in the file to read from
* @returns The byte value as a number
*/
getUint8(byteOffset = this.cursor) {
this.cursor = byteOffset + 1;
return super.getUint8(byteOffset);
}
/**
* Get a slice of the buffer
* @param begin - beginning of the slice
* @param end - end of the slice. If not provided, the end of the data is used
* @returns - a DataView of the slice
*/
slice(begin = this.cursor, end = this.byteLength) {
this.cursor = end;
return new DataView(this.buffer.slice(this.byteOffset + begin, this.byteOffset + end));
}
/**
* Fetch a slice at the current cursor position. The cursor is updated
* @param size - size of the slice
* @returns - a DataView of the slice
*/
seekSlice(size) {
const pos = this.byteOffset + this.cursor;
return this.slice(pos, pos + size);
}
/**
* Set the text decoder's encoding
* @param encoding - update the text decoder's encoding
*/
setStringEncoding(encoding) {
this.textDecoder = new TextDecoder(encoding);
}
/**
* Reads a string from the buffer
* @param byteOffset - Start of the string
* @param byteLength - Length of the string
* @returns - The string
*/
parseString(byteOffset = this.cursor, byteLength = this.byteLength) {
const { textDecoder } = this;
const data = this.slice(byteOffset, byteOffset + byteLength).buffer;
this.cursor = byteOffset + byteLength;
const out = textDecoder.decode(data, { stream: true }) + textDecoder.decode();
return out.replace(/\0/g, '');
}
/**
* Reads a range from the buffer
* @param offset - the offset of the range
* @param length - the length of the range
* @returns - the ranged buffer
*/
async getRange(offset, length) {
return await new Uint8Array(this.buffer).slice(offset, offset + length);
}
}
//# sourceMappingURL=index.js.map