well-known-parser
Version:
A WKT/WKB/EWKT/EWKB/TWKB/GeoJSON parser and serializer
87 lines (86 loc) • 2.71 kB
JavaScript
import { GEOMETRY_TYPES as s } from "./well-known-parser2.js";
import { Point as r } from "./well-known-parser4.js";
class n {
constructor(e) {
this.value = e, this.position = 0;
}
match(e) {
this.skipWhitespaces();
for (let t = 0; t < e.length; t++)
if (this.value.substring(this.position).indexOf(e[t]) === 0)
return this.position += e[t].length, e[t];
return null;
}
matchRegex(e) {
this.skipWhitespaces();
for (let t = 0; t < e.length; t++) {
const i = this.value.substring(this.position).match(e[t]);
if (i)
return this.position += i[0].length, i;
}
return null;
}
isMatch(e) {
this.skipWhitespaces();
for (let t = 0; t < e.length; t++)
if (this.value.substring(this.position).indexOf(e[t]) === 0)
return this.position += e[t].length, !0;
return !1;
}
matchType() {
const e = this.match([
s.Point.wkt,
s.LineString.wkt,
s.Polygon.wkt,
s.MultiPoint.wkt,
s.MultiLineString.wkt,
s.MultiPolygon.wkt,
s.GeometryCollection.wkt
]);
if (!e)
throw new Error("Expected geometry type");
return e;
}
matchDimension() {
switch (this.match(["ZM", "Z", "M"])) {
case "ZM":
return { hasZ: !0, hasM: !0 };
case "Z":
return { hasZ: !0, hasM: !1 };
case "M":
return { hasZ: !1, hasM: !0 };
default:
return { hasZ: !1, hasM: !1 };
}
}
expectGroupStart() {
if (!this.isMatch(["("]))
throw new Error("Expected group start");
}
expectGroupEnd() {
if (!this.isMatch([")"]))
throw new Error("Expected group end");
}
matchCoordinate(e) {
let t;
if (e.hasZ && e.hasM ? t = this.matchRegex([/^(\S*)\s+(\S*)\s+(\S*)\s+([^\s,)]*)/]) : e.hasZ || e.hasM ? t = this.matchRegex([/^(\S*)\s+(\S*)\s+([^\s,)]*)/]) : t = this.matchRegex([/^(\S*)\s+([^\s,)]*)/]), !t)
throw new Error("Expected coordinates");
return e.hasZ && e.hasM ? new r(parseFloat(t[1]), parseFloat(t[2]), parseFloat(t[3]), parseFloat(t[4])) : e.hasZ ? new r(parseFloat(t[1]), parseFloat(t[2]), parseFloat(t[3]), void 0) : e.hasM ? new r(parseFloat(t[1]), parseFloat(t[2]), void 0, parseFloat(t[3])) : new r(parseFloat(t[1]), parseFloat(t[2]), void 0, void 0);
}
matchCoordinates(e) {
const t = [];
do {
const i = this.isMatch(["("]);
t.push(this.matchCoordinate(e)), i && this.expectGroupEnd();
} while (this.isMatch([","]));
return t;
}
skipWhitespaces() {
for (; this.position < this.value.length && this.value[this.position] === " "; )
this.position++;
}
}
export {
n as WktParser
};
//# sourceMappingURL=well-known-parser15.js.map