well-known-parser
Version:
A WKT/WKB/EWKT/EWKB/TWKB/GeoJSON parser and serializer
96 lines (95 loc) • 3.15 kB
JavaScript
import { Geometry as g } from "./well-known-parser3.js";
import { GEOMETRY_TYPES as n } from "./well-known-parser2.js";
import { Point as c } from "./well-known-parser4.js";
import { BinaryWriter as f } from "./well-known-parser12.js";
class s extends g {
constructor(t, i, e) {
super(), this.exteriorRing = t || [], this.interiorRings = i || [], this.srid = e, this.exteriorRing.length > 0 && (this.hasZ = this.exteriorRing[0].hasZ, this.hasM = this.exteriorRing[0].hasM);
}
static Z(t, i, e) {
const r = new s(t, i, e);
return r.hasZ = !0, r;
}
static M(t, i, e) {
const r = new s(t, i, e);
return r.hasM = !0, r;
}
static ZM(t, i, e) {
const r = new s(t, i, e);
return r.hasZ = !0, r.hasM = !0, r;
}
toWkt(t = !1) {
return t ? this.toInnerWkt() : this.exteriorRing.length === 0 ? this.getWktType(n.Polygon.wkt, !0) : this.getWktType(n.Polygon.wkt, !1) + this.toInnerWkt();
}
toInnerWkt() {
let t = "((";
for (const i of this.exteriorRing)
t += i.toWkt(!0) + ",";
t = t.slice(0, -1), t += ")";
for (const i of this.interiorRings) {
t += ",(";
for (const e of i)
t += e.toWkt(!0) + ",";
t = t.slice(0, -1), t += ")";
}
return t += ")", t;
}
toWkb(t) {
const i = new f(this.getWkbSize());
i.writeInt8(1), this.writeWkbType(i, n.Polygon.wkb, t), this.exteriorRing.length > 0 ? (i.writeUInt32LE(1 + this.interiorRings.length), i.writeUInt32LE(this.exteriorRing.length)) : i.writeUInt32LE(0);
for (const e of this.exteriorRing)
i.writeBuffer(e.toWkb(t, !0));
for (const e of this.interiorRings) {
i.writeUInt32LE(e.length);
for (const r of e)
i.writeBuffer(r.toWkb(t, !0));
}
return i.buffer;
}
toTwkb(t = new c(0, 0, 0, 0), i = !1) {
const e = new f(0, !0), r = g.getTwkbPrecision(5, 0, 0), h = this.exteriorRing.length === 0;
if (i || this.writeTwkbHeader(e, n.Polygon.wkb, r, h), this.exteriorRing.length > 0) {
e.writeVarInt(1 + this.interiorRings.length), e.writeVarInt(this.exteriorRing.length);
for (const o of this.exteriorRing)
e.writeBuffer(o.toTwkb(t, !0));
for (const o of this.interiorRings) {
e.writeVarInt(o.length);
for (const u of o)
e.writeBuffer(u.toTwkb(t, !0));
}
}
return e.buffer;
}
getWkbSize() {
let t = 16;
this.hasZ && (t += 8), this.hasM && (t += 8);
let i = 9;
this.exteriorRing.length > 0 && (i += 4 + this.exteriorRing.length * t);
for (let e = 0; e < this.interiorRings.length; e++)
i += 4 + this.interiorRings[e].length * t;
return i;
}
toGeoJSON(t = !1) {
const i = [];
if (this.exteriorRing.length > 0) {
const e = [];
for (const r of this.exteriorRing)
e.push(r.toGeoJSON(!0));
i.push(e);
}
for (const e of this.interiorRings) {
const r = [];
for (const h of e)
r.push(h.toGeoJSON(!0));
i.push(r);
}
return t ? i : {
type: n.Polygon.geoJSON,
coordinates: i
};
}
}
export {
s as Polygon
};
//# sourceMappingURL=well-known-parser6.js.map