twkb-parser
Version:
TWKB parser and serializer
127 lines (104 loc) • 3.6 kB
TypeScript
type Position = number[]
type BBox = number[]
interface GeoJSONObject {
type: "Point" | "LineString" | "Polygon" | "MultiPoint" | "MultiLineString" | "MultiPolygon" | "GeometryCollection"
bbox?: BBox | undefined
}
interface Point extends GeoJsonObject {
type: "Point"
coordinates: Position
}
interface LineString extends GeoJsonObject {
type: "LineString"
coordinates: Position[]
}
interface Polygon extends GeoJsonObject {
type: "Polygon"
coordinates: Position[][]
}
interface MultiPoint extends GeoJsonObject {
type: "MultiPoint"
coordinates: Position[]
}
interface MultiLineString extends GeoJsonObject {
type: "MultiLineString"
coordinates: Position[][]
}
interface MultiPolygon extends GeoJsonObject {
type: "MultiPolygon"
coordinates: Position[][][]
}
interface GeometryCollection extends GeoJsonObject {
type: "GeometryCollection"
geometries: Geometry[]
}
type Geometry = Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon | GeometryCollection
// declare module "TWKB"
export namespace TWKB {
export const DEFAULT_PRECISION = 6
export interface EncodingParameters {
precisionZ?: number,
precisionM?: number,
includeBbox?: boolean,
includeSize?: boolean,
ids?: number[]
}
export const DEFAULT_ENCODING_PARAMETERS: EncodingParameters = {
precisionZ: 0,
precisionM: 0,
includeBbox: false,
includeSize: false
}
// export function geojson2bytes(geojson: Geometry, precision: number, parameters = DEFAULT_ENCODING_PARAMETERS): number[]
/**
* Convert a GeoJSON geometry to a TWKB geometry
* @param geojson The GeoJSON geometry to encode as TWKB
* @param precision The precision used for encoding the X / Y coordinates
* @param parameters The encoding parameters
* @returns {string} The TWKB geometry as a Base64 encoded string
*/
// export function geojson2string(geojson: Geometry, precision: number, parameters = DEFAULT_ENCODING_PARAMETERS): string
/**
* Convert a TWKB geometry to a GeoJSON geometry
* @param bytes The TWKB geometry as an array of byte (0-255) values
* @returns {Geometry} The GeoJSON geometry
*/
// export function bytes2geojson(bytes: ByteArray): Geometry
/**
* Convert a TWKB geometry to a GeoJSON geometry
* @param string The TWKB geometry as a Base64 encoded string
* @returns {Geometry} The GeoJSON geometry
*/
// export function string2geojson(string: string): Geometry
/**
* Encode a GeoJSON geometry
* @param geojson The GeoJSON geometry to encode as TWKB
* @param precision The precision used for encoding the X / Y coordinates (default: 6)
* @param parameters The encoding parameters
* @returns An interface to output the geometry in different format
*/
export function fromGeoJSON(geojson: Geometry, precision = DEFAULT_PRECISION, parameters = DEFAULT_ENCODING_PARAMETERS): {
toArray (): number[]
toByteArray(): Uint8Array
toBase64(): string
}
/**
* Decode a TWKB geometry
* @param twkb The TWKB geometry as an array of byte (0-255) values
* @returns An interface to output the geometry in different format
*/
export function fromArray(twkb: number[] | Uint8Array): {
toGeoJSON (): Geometry
toBase64(): string
}
/**
* Decode a TWKB geometry
* @param twkb The TWKB geometry as a Base64 encoded string
* @returns An interface to output the geometry in different format
*/
export function fromBase64(twkb: string): {
toGeoJSON (): Geometry
toArray (): number[]
toByteArray(): Uint8Array
}
}