UNPKG

well-known-parser

Version:

A WKT/WKB/EWKT/EWKB/TWKB/GeoJSON parser and serializer

3 lines (2 loc) 2.29 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./well-known-parser2.cjs"),r=require("./well-known-parser4.cjs");class a{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.GEOMETRY_TYPES.Point.wkt,s.GEOMETRY_TYPES.LineString.wkt,s.GEOMETRY_TYPES.Polygon.wkt,s.GEOMETRY_TYPES.MultiPoint.wkt,s.GEOMETRY_TYPES.MultiLineString.wkt,s.GEOMETRY_TYPES.MultiPolygon.wkt,s.GEOMETRY_TYPES.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.Point(parseFloat(t[1]),parseFloat(t[2]),parseFloat(t[3]),parseFloat(t[4])):e.hasZ?new r.Point(parseFloat(t[1]),parseFloat(t[2]),parseFloat(t[3]),void 0):e.hasM?new r.Point(parseFloat(t[1]),parseFloat(t[2]),void 0,parseFloat(t[3])):new r.Point(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++}}exports.WktParser=a; //# sourceMappingURL=well-known-parser15.cjs.map