{"version":3,"file":"well-known-parser4.cjs","sources":["../src/point.ts"],"sourcesContent":["import { Geometry } from './geometry';\nimport type { Coordinates, GeometryOptions } from './types';\nimport { GEOMETRY_TYPES } from './constants';\nimport { BinaryWriter } from './binarywriter';\nimport * as ZigZag from './zigzag';\nimport type { Point as GeoJSONPoint } from 'geojson';\n\nexport class Point extends Geometry {\n  x: number;\n  y: number;\n  z: number | undefined;\n  m: number | undefined;\n\n  constructor(x?: number, y?: number, z?: number, m?: number, srid?: number) {\n    super();\n\n    this.x = x;\n    this.y = y;\n    this.z = z;\n    this.m = m;\n    this.srid = srid;\n\n    this.hasZ = typeof this.z !== 'undefined';\n    this.hasM = typeof this.m !== 'undefined';\n  }\n\n  static Z(x: number, y: number, z: number, srid?: number): Point {\n    const point = new Point(x, y, z, undefined, srid);\n    point.hasZ = true;\n    return point;\n  }\n\n  static M(x: number, y: number, m: number, srid?: number): Point {\n    const point = new Point(x, y, undefined, m, srid);\n    point.hasM = true;\n    return point;\n  }\n\n  static ZM(x: number, y: number, z: number, m: number, srid?: number): Point {\n    const point = new Point(x, y, z, m, srid);\n    point.hasZ = true;\n    point.hasM = true;\n    return point;\n  }\n\n  toWkt(isNested: boolean = false): string {\n    if (isNested) return this.getWktCoordinate();\n\n    if (\n      typeof this.x === 'undefined' &&\n      typeof this.y === 'undefined' &&\n      typeof this.z === 'undefined' &&\n      typeof this.m === 'undefined'\n    ) {\n      return this.getWktType(GEOMETRY_TYPES.Point.wkt, true);\n    }\n\n    return this.getWktType(GEOMETRY_TYPES.Point.wkt, false) + '(' + this.getWktCoordinate() + ')';\n  }\n\n  private getWktCoordinate(): string {\n    let coordinates = `${this.x} ${this.y}`;\n\n    if (this.hasZ) {\n      coordinates += ` ${this.z}`;\n    }\n    if (this.hasM) {\n      coordinates += ` ${this.m}`;\n    }\n\n    return coordinates;\n  }\n\n  toWkb(parentOptions?: GeometryOptions, isNested: boolean = false): Buffer {\n    const wkb = new BinaryWriter(this.getWkbSize(isNested));\n\n    if (!isNested) {\n      wkb.writeInt8(1);\n      this.writeWkbType(wkb, GEOMETRY_TYPES.Point.wkb, parentOptions);\n    }\n\n    if (typeof this.x === 'undefined' && typeof this.y === 'undefined') {\n      wkb.writeDoubleLE(NaN);\n      wkb.writeDoubleLE(NaN);\n\n      if (this.hasZ) {\n        wkb.writeDoubleLE(NaN);\n      }\n      if (this.hasM) {\n        wkb.writeDoubleLE(NaN);\n      }\n    } else {\n      wkb.writeDoubleLE(this.x);\n      wkb.writeDoubleLE(this.y);\n\n      if (this.hasZ && this.z !== undefined) {\n        wkb.writeDoubleLE(this.z);\n      }\n      if (this.hasM && this.m !== undefined) {\n        wkb.writeDoubleLE(this.m);\n      }\n    }\n\n    return wkb.buffer;\n  }\n\n  toTwkb(previousPoint?: Point, isNested: boolean = false): Buffer {\n    const twkb = new BinaryWriter(0, true);\n\n    const precision = Geometry.getTwkbPrecision(5, 0, 0);\n    const isEmpty = typeof this.x === 'undefined' && typeof this.y === 'undefined';\n\n    if (!isNested) {\n      this.writeTwkbHeader(twkb, GEOMETRY_TYPES.Point.wkb, precision, isEmpty);\n    }\n\n    if (isEmpty) {\n      return twkb.buffer;\n    }\n\n    const x = this.x * precision.xyFactor;\n    const y = this.y * precision.xyFactor;\n\n    // Use 0 as default value if z or m is undefined\n    const z = this.hasZ ? (this.z ?? 0) * precision.zFactor : 0;\n    const m = this.hasM ? (this.m ?? 0) * precision.mFactor : 0;\n\n    // Write delta from previous point\n    twkb.writeVarInt(ZigZag.encode(x - (previousPoint?.x ?? 0)));\n    twkb.writeVarInt(ZigZag.encode(y - (previousPoint?.y ?? 0)));\n\n    if (this.hasZ) {\n      // Use 0 as default if z is undefined in previous point\n      const prevZ = previousPoint?.z ?? 0;\n      twkb.writeVarInt(ZigZag.encode(z - prevZ));\n    }\n\n    if (this.hasM) {\n      // Use 0 as default if m is undefined in previous point\n      const prevM = previousPoint?.m ?? 0;\n      twkb.writeVarInt(ZigZag.encode(m - prevM));\n    }\n\n    if (previousPoint) {\n      // Update the previous point with the current values\n      previousPoint.x = x;\n      previousPoint.y = y;\n\n      if (this.hasZ) {\n        previousPoint.z = z;\n      }\n\n      if (this.hasM) {\n        previousPoint.m = m;\n      }\n    }\n\n    return twkb.buffer;\n  }\n\n  getWkbSize(isNested: boolean = false): number {\n    let size = 8 + 8;\n\n    if (!isNested) {\n      size += 1 + 4;\n    }\n\n    if (this.hasZ) {\n      size += 8;\n    }\n    if (this.hasM) {\n      size += 8;\n    }\n\n    return size;\n  }\n\n  toGeoJSON(): GeoJSONPoint;\n  toGeoJSON(isNested: true): Coordinates<GeoJSONPoint>;\n  toGeoJSON(isNested: boolean = false): GeoJSONPoint | Coordinates<GeoJSONPoint> {\n    let coordinates: number[];\n    if (typeof this.x === 'undefined' && typeof this.y === 'undefined') {\n      coordinates = [];\n    } else if (typeof this.z !== 'undefined') {\n      coordinates = [this.x, this.y, this.z];\n    } else {\n      coordinates = [this.x, this.y];\n    }\n\n    if (isNested) return coordinates;\n\n    return {\n      type: GEOMETRY_TYPES.Point.geoJSON,\n      coordinates,\n    };\n  }\n}\n"],"names":["Point","Geometry","x","y","z","m","srid","point","isNested","GEOMETRY_TYPES","coordinates","parentOptions","wkb","BinaryWriter","previousPoint","twkb","precision","isEmpty","ZigZag.encode","prevZ","prevM","size"],"mappings":"gPAOO,MAAMA,UAAcC,EAAAA,QAAS,CAMlC,YAAYC,EAAYC,EAAYC,EAAYC,EAAYC,EAAe,CACzE,MAAA,EAEA,KAAK,EAAIJ,EACT,KAAK,EAAIC,EACT,KAAK,EAAIC,EACT,KAAK,EAAIC,EACT,KAAK,KAAOC,EAEZ,KAAK,KAAO,OAAO,KAAK,EAAM,IAC9B,KAAK,KAAO,OAAO,KAAK,EAAM,GAChC,CAEA,OAAO,EAAEJ,EAAWC,EAAWC,EAAWE,EAAsB,CAC9D,MAAMC,EAAQ,IAAIP,EAAME,EAAGC,EAAGC,EAAG,OAAWE,CAAI,EAChD,OAAAC,EAAM,KAAO,GACNA,CAC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