well-known-parser
Version:
A WKT/WKB/EWKT/EWKB/TWKB/GeoJSON parser and serializer
63 lines (62 loc) • 2.77 kB
JavaScript
import { Geometry as w } from "./well-known-parser3.js";
import { GEOMETRY_TYPES as n } from "./well-known-parser2.js";
import { BinaryWriter as c } from "./well-known-parser12.js";
import { encode as f } from "./well-known-parser13.js";
class o extends w {
constructor(t, i, e, s, h) {
super(), this.x = t, this.y = i, this.z = e, this.m = s, this.srid = h, this.hasZ = typeof this.z < "u", this.hasM = typeof this.m < "u";
}
static Z(t, i, e, s) {
const h = new o(t, i, e, void 0, s);
return h.hasZ = !0, h;
}
static M(t, i, e, s) {
const h = new o(t, i, void 0, e, s);
return h.hasM = !0, h;
}
static ZM(t, i, e, s, h) {
const r = new o(t, i, e, s, h);
return r.hasZ = !0, r.hasM = !0, r;
}
toWkt(t = !1) {
return t ? this.getWktCoordinate() : typeof this.x > "u" && typeof this.y > "u" && typeof this.z > "u" && typeof this.m > "u" ? this.getWktType(n.Point.wkt, !0) : this.getWktType(n.Point.wkt, !1) + "(" + this.getWktCoordinate() + ")";
}
getWktCoordinate() {
let t = `${this.x} ${this.y}`;
return this.hasZ && (t += ` ${this.z}`), this.hasM && (t += ` ${this.m}`), t;
}
toWkb(t, i = !1) {
const e = new c(this.getWkbSize(i));
return i || (e.writeInt8(1), this.writeWkbType(e, n.Point.wkb, t)), typeof this.x > "u" && typeof this.y > "u" ? (e.writeDoubleLE(NaN), e.writeDoubleLE(NaN), this.hasZ && e.writeDoubleLE(NaN), this.hasM && e.writeDoubleLE(NaN)) : (e.writeDoubleLE(this.x), e.writeDoubleLE(this.y), this.hasZ && this.z !== void 0 && e.writeDoubleLE(this.z), this.hasM && this.m !== void 0 && e.writeDoubleLE(this.m)), e.buffer;
}
toTwkb(t, i = !1) {
const e = new c(0, !0), s = w.getTwkbPrecision(5, 0, 0), h = typeof this.x > "u" && typeof this.y > "u";
if (i || this.writeTwkbHeader(e, n.Point.wkb, s, h), h)
return e.buffer;
const r = this.x * s.xyFactor, d = this.y * s.xyFactor, u = this.hasZ ? (this.z ?? 0) * s.zFactor : 0, y = this.hasM ? (this.m ?? 0) * s.mFactor : 0;
if (e.writeVarInt(f(r - (t?.x ?? 0))), e.writeVarInt(f(d - (t?.y ?? 0))), this.hasZ) {
const a = t?.z ?? 0;
e.writeVarInt(f(u - a));
}
if (this.hasM) {
const a = t?.m ?? 0;
e.writeVarInt(f(y - a));
}
return t && (t.x = r, t.y = d, this.hasZ && (t.z = u), this.hasM && (t.m = y)), e.buffer;
}
getWkbSize(t = !1) {
let i = 16;
return t || (i += 5), this.hasZ && (i += 8), this.hasM && (i += 8), i;
}
toGeoJSON(t = !1) {
let i;
return typeof this.x > "u" && typeof this.y > "u" ? i = [] : typeof this.z < "u" ? i = [this.x, this.y, this.z] : i = [this.x, this.y], t ? i : {
type: n.Point.geoJSON,
coordinates: i
};
}
}
export {
o as Point
};
//# sourceMappingURL=well-known-parser4.js.map