well-known-parser
Version:
A WKT/WKB/EWKT/EWKB/TWKB/GeoJSON parser and serializer
212 lines (211 loc) • 7.1 kB
JavaScript
import { GEOMETRY_TYPES as n } from "./well-known-parser2.js";
import { BinaryReader as l } from "./well-known-parser14.js";
import { WktParser as m } from "./well-known-parser15.js";
import { decode as h } from "./well-known-parser13.js";
import { parsePointTwkb as P, parsePointGeoJSON as S, parsePointWkt as f, parsePointWkb as G } from "./well-known-parser16.js";
import { parseLineStringTwkb as T, parseLineStringGeoJSON as E, parseLineStringWkt as k, parseLineStringWkb as L } from "./well-known-parser17.js";
import { parsePolygonTwkb as Z, parsePolygonGeoJSON as W, parsePolygonWkt as u, parsePolygonWkb as C } from "./well-known-parser18.js";
import { parseMultiPointTwkb as O, parseMultiPointGeoJSON as B, parseMultiPointWkt as w, parseMultiPointWkb as J } from "./well-known-parser19.js";
import { parseMultiLineStringTwkb as N, parseMultiLineStringGeoJSON as I, parseMultiLineStringWkt as y, parseMultiLineStringWkb as x } from "./well-known-parser20.js";
import { parseMultiPolygonTwkb as R, parseMultiPolygonGeoJSON as U, parseMultiPolygonWkt as d, parseMultiPolygonWkb as z } from "./well-known-parser21.js";
import { GeometryCollection as p } from "./well-known-parser10.js";
function oe(t, s) {
if (typeof t == "string" || t instanceof m)
return $(t, s);
if (Buffer.isBuffer(t) || t instanceof l)
return b(t, s);
throw new Error("first argument must be a string or Buffer");
}
function $(t, s) {
let e, r;
t instanceof m ? e = t : e = new m(t);
const o = e.matchRegex([/^SRID=(\d+);/]);
o && (r = parseInt(o[1], 10));
const i = e.matchType(), c = e.matchDimension(), a = {
...s || {},
srid: r,
hasZ: c.hasZ,
hasM: c.hasM
};
switch (i) {
case n.Point.wkt:
return f(e, a);
case n.LineString.wkt:
return k(e, a);
case n.Polygon.wkt:
return u(e, a);
case n.MultiPoint.wkt:
return w(e, a);
case n.MultiLineString.wkt:
return y(e, a);
case n.MultiPolygon.wkt:
return d(e, a);
case n.GeometryCollection.wkt:
return M(e, a);
default:
throw new Error(`GeometryType ${i} not supported`);
}
}
function b(t, s) {
let e, r;
const o = {};
t instanceof l ? e = t : e = new l(t), e.isBigEndian = !e.readInt8();
const i = e.readUInt32();
switch (o.hasSrid = (i & 536870912) === 536870912, o.isEwkb = !!(i & 536870912 || i & 1073741824 || i & 2147483648), o.hasSrid && (o.srid = e.readUInt32()), o.hasZ = !1, o.hasM = !1, !o.isEwkb && (!s || !s.isEwkb) ? i >= 1e3 && i < 2e3 ? (o.hasZ = !0, r = i - 1e3) : i >= 2e3 && i < 3e3 ? (o.hasM = !0, r = i - 2e3) : i >= 3e3 && i < 4e3 ? (o.hasZ = !0, o.hasM = !0, r = i - 3e3) : r = i : (i & 2147483648 && (o.hasZ = !0), i & 1073741824 && (o.hasM = !0), r = i & 15), r) {
case n.Point.wkb:
return G(e, o);
case n.LineString.wkb:
return L(e, o);
case n.Polygon.wkb:
return C(e, o);
case n.MultiPoint.wkb:
return J(e, o);
case n.MultiLineString.wkb:
return x(e, o);
case n.MultiPolygon.wkb:
return z(e, o);
case n.GeometryCollection.wkb:
return F(e, o);
default:
throw new Error(`GeometryType ${r} not supported`);
}
}
function V(t, s) {
let e;
const r = { ...s || {} };
t instanceof l ? e = t : e = new l(t);
const o = e.readUInt8(), i = e.readUInt8(), c = o & 15;
if (r.precision = h(o >> 4), r.precisionFactor = Math.pow(10, r.precision), r.hasBoundingBox = !!(i >> 0 & 1), r.hasSizeAttribute = !!(i >> 1 & 1), r.hasIdList = !!(i >> 2 & 1), r.hasExtendedPrecision = !!(i >> 3 & 1), r.isEmpty = !!(i >> 4 & 1), r.hasExtendedPrecision) {
const a = e.readUInt8();
r.hasZ = (a & 1) === 1, r.hasM = (a & 2) === 2, r.zPrecision = h((a & 28) >> 2), r.zPrecisionFactor = Math.pow(10, r.zPrecision), r.mPrecision = h((a & 224) >> 5), r.mPrecisionFactor = Math.pow(10, r.mPrecision);
} else
r.hasZ = !1, r.hasM = !1;
if (r.hasSizeAttribute && e.readVarInt(), r.hasBoundingBox) {
let a = 2;
r.hasZ && a++, r.hasM && a++;
for (let g = 0; g < a; g++)
e.readVarInt(), e.readVarInt();
}
switch (c) {
case n.Point.wkb:
return P(e, r);
case n.LineString.wkb:
return T(e, r);
case n.Polygon.wkb:
return Z(e, r);
case n.MultiPoint.wkb:
return O(e, r);
case n.MultiLineString.wkb:
return N(e, r);
case n.MultiPolygon.wkb:
return R(e, r);
case n.GeometryCollection.wkb:
return Y(e, r);
default:
throw new Error(`GeometryType ${c} not supported`);
}
}
function D(t, s = !1) {
let e;
switch (t.type) {
case n.Point.geoJSON:
e = S(t.coordinates);
break;
case n.LineString.geoJSON:
e = E(t);
break;
case n.Polygon.geoJSON:
e = W(t);
break;
case n.MultiPoint.geoJSON:
e = B(t);
break;
case n.MultiLineString.geoJSON:
e = I(t);
break;
case n.MultiPolygon.geoJSON:
e = U(t);
break;
case n.GeometryCollection.geoJSON:
e = A(t);
break;
default:
throw new Error(`GeometryType ${t.type} not supported`);
}
return s || (e.srid = 4326), e;
}
function M(t, s) {
const e = new p();
if (e.srid = s.srid, e.hasZ = s.hasZ ?? !1, e.hasM = s.hasM ?? !1, t.isMatch(["EMPTY"]))
return e;
t.expectGroupStart();
do {
const r = t.matchType(), o = t.matchDimension(), i = {
...s,
srid: void 0,
hasZ: o.hasZ,
hasM: o.hasM
};
switch (r) {
case n.Point.wkt:
e.geometries.push(f(t, i));
break;
case n.LineString.wkt:
e.geometries.push(k(t, i));
break;
case n.Polygon.wkt:
e.geometries.push(u(t, i));
break;
case n.MultiPoint.wkt:
e.geometries.push(w(t, i));
break;
case n.MultiLineString.wkt:
e.geometries.push(y(t, i));
break;
case n.MultiPolygon.wkt:
e.geometries.push(d(t, i));
break;
case n.GeometryCollection.wkt:
e.geometries.push(M(t, i));
break;
default:
throw new Error(`GeometryType ${r} not supported`);
}
} while (t.isMatch([","]));
return t.expectGroupEnd(), e;
}
function F(t, s) {
const e = new p();
e.srid = s.srid, e.hasZ = s.hasZ ?? !1, e.hasM = s.hasM ?? !1;
const r = t.readUInt32();
for (let o = 0; o < r; o++)
e.geometries.push(b(t, s));
return e;
}
function Y(t, s) {
const e = new p();
if (e.hasZ = s.hasZ ?? !1, e.hasM = s.hasM ?? !1, s.isEmpty)
return e;
const r = t.readVarInt();
for (let o = 0; o < r; o++)
e.geometries.push(V(t, s));
return e;
}
function A(t) {
const s = new p();
for (let e = 0; e < t.geometries.length; e++)
s.geometries.push(D(t.geometries[e], !0));
return s.geometries.length > 0 && (s.hasZ = s.geometries[0].hasZ), s;
}
export {
oe as parse,
D as parseGeoJSON,
A as parseGeometryCollectionGeoJSON,
Y as parseGeometryCollectionTwkb,
F as parseGeometryCollectionWkb,
M as parseGeometryCollectionWkt,
V as parseTwkb,
b as parseWkb,
$ as parseWkt
};
//# sourceMappingURL=well-known-parser11.js.map