gis-tools-ts
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
284 lines • 11.3 kB
JavaScript
import { CSVReader } from './csv/index.js';
import { GPXReader } from './gpx/index.js';
import { GRIB2Reader } from './grib2/index.js';
import { GeoTIFFReader } from './geotiff/index.js';
import { NetCDFReader } from './netcdf/index.js';
import { OSMReader } from './osm/index.js';
import { WKTGeometryReader } from './wkt/geometry.js';
import { buildGTFSSchedule } from './gtfs/index.js';
import { shapefileFromGzip } from './shapefile/index.js';
import { ALL_DEFINITIONS, EPSG_CODES } from '../proj4/index.js';
import { JSONReader, NewLineDelimitedJSONReader, SequenceJSONReader } from './json/index.js';
import { LASReader, LASZipReader } from './las/index.js';
export * from './csv/index.js';
export * from './gbfs/index.js';
export * from './geotiff/index.js';
export * from './gpx/index.js';
export * from './grib2/index.js';
export * from './gtfs/index.js';
export * from './image/index.js';
export * from './json/index.js';
export * from './las/index.js';
export * from './netcdf/index.js';
export * from './osm/index.js';
export * from './pmtiles/index.js';
export * from './s2tiles/index.js';
export * from 'pbf-ts';
export * from './shapefile/index.js';
export * from './svg/index.js';
export * from './wkt/index.js';
export * from './xml/index.js';
export * from './fetch.js';
export * from './nadgrid.js';
export * from './tile/index.js';
/**
* 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();
// oxlint-disable-next-line no-control-regex
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);
}
}
/**
* Given a file and a file type, return a reader
* @param urlPath - The URL path to the file
* @param type - The file type if specified, otherwise it will be inferred
* @returns - The reader with {@link FeatureIterator} implemented
*/
export async function fileTypeToReader(urlPath, type) {
const data = await fetch(urlPath).then(async (res) => await res.arrayBuffer());
const buffer = new BufferReader(data);
const fileType = (type ?? urlPath.split('.').pop() ?? '').toLowerCase();
switch (fileType) {
case 'csv':
return new CSVReader(buffer);
case 'tif':
case 'tiff':
case 'geotif':
case 'geotiff':
return new GeoTIFFReader(buffer, ALL_DEFINITIONS, EPSG_CODES);
case 'gpx':
return new GPXReader(buffer.parseString());
case 'grib':
case 'grib2':
return new GRIB2Reader(buffer);
case 'gtfs':
return await buildGTFSSchedule(data);
case 'json':
case 'geojson':
case 's2json':
return new JSONReader(buffer);
case 'jsonld':
case 'geojsonld':
case 's2jsonld':
case 'json-ld':
case 'geojson-ld':
case 's2json-ld':
return new NewLineDelimitedJSONReader(buffer);
case 'jsonsq':
case 'geojsonsq':
case 's2jsonsq':
case 'json-sq':
case 'geojson-sq':
case 's2json-sq':
return new SequenceJSONReader(buffer);
case 'las':
return new LASReader(buffer, ALL_DEFINITIONS, EPSG_CODES);
case 'laz':
return new LASZipReader(buffer, ALL_DEFINITIONS, EPSG_CODES);
case 'nc4':
case 'cdf':
case 'nc':
case 'netcdf':
return new NetCDFReader(buffer);
case 'osm':
return new OSMReader(buffer);
case 'shapefile':
return await shapefileFromGzip(data, ALL_DEFINITIONS, EPSG_CODES);
case 'wkt':
return new WKTGeometryReader(buffer.parseString());
default:
throw new Error(`Unsupported file type: ${fileType}`);
}
}
//# sourceMappingURL=index.js.map