UNPKG

shpts

Version:

TypeScript package for loading Esri Shapefiles, primary developed for for WebGL applications

1,204 lines (1,203 loc) 36.4 kB
var ae = Object.defineProperty; var ce = (r, e, t) => e in r ? ae(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t; var p = (r, e, t) => (ce(r, typeof e != "symbol" ? e + "" : e, t), t); class X { constructor(e) { p(this, "dataView"); p(this, "offset", 0); p(this, "size", 0); this.size = e.byteLength, this.dataView = new DataView(e); } get tell() { return this.offset; } seek(e) { if (e > this.size + 1) throw new Error("Offset out of bounds"); return this.offset = e, this; } readInt16(e) { const t = this.dataView.getInt16(this.offset, e); return this.offset += 4, t; } readInt32(e) { const t = this.dataView.getInt32(this.offset, e); return this.offset += 4, t; } readInt32Array(e, t) { const n = new Int32Array(e); for (let s = 0; s < e; s++) n[s] = this.readInt32(t); return n; } readDouble(e) { const t = this.dataView.getFloat64(this.offset, e); return this.offset += 8, t; } readDoubleArray(e, t) { const n = new Float64Array(e); for (let s = 0; s < e; s++) n[s] = this.readDouble(t); return n; } /* Returns value at curent pos without advancing */ peekByte() { return this.dataView.getUint8(this.offset); } readByte() { const e = this.dataView.getUint8(this.offset); return this.offset += 1, e; } } const q = { 1: [437, "US MS-DOS"], 2: [850, "International MS-DOS"], 3: [1252, "Windows ANSI Latin I"], 4: [1e4, "Standard Macintosh"], 8: [865, "Danish OEM"], 9: [437, "Dutch OEM"], 10: [850, "Dutch OEM"], 11: [437, "Finnish OEM"], 13: [437, "French OEM"], 14: [850, "French OEM"], 15: [437, "German OEM"], 16: [850, "German OEM"], 17: [437, "Italian OEM"], 18: [850, "Italian OEM"], 19: [932, "Japanese Shift-JIS"], 20: [850, "Spanish OEM"], 21: [437, "Swedish OEM"], 22: [850, "Swedish OEM"], 23: [865, "Norwegian OEM"], 24: [437, "Spanish OEM"], 25: [437, "English OEM (Great Britain)"], 26: [850, "English OEM (Great Britain)"], 27: [437, "English OEM (US)"], 28: [863, "French OEM (Canada)"], 29: [850, "French OEM"], 31: [852, "Czech OEM"], 34: [852, "Hungarian OEM"], 35: [852, "Polish OEM"], 36: [860, "Portuguese OEM"], 37: [850, "Portuguese OEM"], 38: [866, "Russian OEM"], 55: [850, "English OEM (US)"], 64: [852, "Romanian OEM"], 77: [936, "Chinese GBK (PRC)"], 78: [949, "Korean (ANSI/OEM)"], 79: [950, "Chinese Big5 (Taiwan)"], 80: [874, "Thai (ANSI/OEM)"], 87: [1252, "Current ANSI CP", "ANSI"], 88: [1252, "Western European ANSI"], 89: [1252, "Spanish ANSI"], 100: [852, "Eastern European MS-DOS"], 101: [866, "Russian MS-DOS"], 102: [865, "Nordic MS-DOS"], 103: [861, "Icelandic MS-DOS"], 104: [895, "Kamenicky (Czech) MS-DOS"], 105: [620, "Mazovia (Polish) MS-DOS"], 106: [737, "Greek MS-DOS (437G)"], 107: [857, "Turkish MS-DOS"], 108: [863, "French-Canadian MS-DOS"], 120: [950, "Taiwan Big 5"], 121: [949, "Hangul (Wansung)"], 122: [936, "PRC GBK"], 123: [932, "Japanese Shift-JIS"], 124: [874, "Thai Windows/MS–DOS"], 134: [737, "Greek OEM"], 135: [852, "Slovenian OEM"], 136: [857, "Turkish OEM"], 150: [10007, "Russian Macintosh"], 151: [10029, "Eastern European Macintosh"], 152: [10006, "Greek Macintosh"], 200: [1250, "Eastern European Windows"], 201: [1251, "Russian Windows"], 202: [1254, "Turkish Windows"], 203: [1253, "Greek Windows"], 204: [1257, "Baltic Windows"] }, le = /^.*ANSI\s*(\d+)\s*$/, he = /^.*ISO\s*8859(\d)\s*$/, fe = /^.*UTF[-\s]?8\s*$/; class L { constructor(e) { p(this, "encoding"); p(this, "decoder"); this.encoding = e, this.decoder = new TextDecoder(e); } decode(e) { return this.decoder.decode(e); } } function ne(r) { try { return new TextDecoder(r), !0; } catch { return !1; } } function de(r) { if (!r) throw new Error("No codepage/CPG string provided"); if (r.match(fe)) return new L("utf8"); let e = r.match(he); if (e != null) return new L(`ISO-8859-${e[1]}`); if (e = r.match(le), e != null) { const n = `cp${parseInt(e[1])}`; if (!ne(n)) throw new Error(`Encoding ${n} not supported`); return new L(n); } return new L("cp1252"); } function ge(r) { if (r === 0) return new L("cp1252"); if (r in q) { const t = `cp${q[r][0]}`; if (!ne(t)) throw new Error(`Encoding ${t} not supported`); return new L(t); } throw new Error(`Could not find converter for codepage ${r}`); } const E = class { static fromPresetStream(e, t, n) { const s = e.readByte(), o = []; if (s === 42) return t.forEach(() => o.push(null)), o; let i = e.tell; return t.forEach((u) => { switch (e.seek(i), u.type) { case "C": o.push(E.readCharValue(e, u, n)); break; case "N": o.push(E.readNumberValue(e, u, n)); break; case "F": o.push(E.readNumberValue(e, u, n)); break; case "D": o.push(E.readDateValue(e, u, n)); break; case "L": o.push(E.readLogicalValue(e)); break; default: o.push(null); } i += u.fieldLen; }), o; } static readCharValue(e, t, n) { const s = new Uint8Array(t.fieldLen); for (let i = 0; i < t.fieldLen; i++) { const u = e.readByte(); if (u === 0) break; s[i] = u; } return n.decode(s).trim(); } static readNumberValue(e, t, n) { const s = this.readCharValue(e, t, n); return t.decimalCount === 0 ? parseInt(s) : parseFloat(s); } static readDateValue(e, t, n) { const o = this.readCharValue(e, t, n).match(E.regExDate); return o == null ? null : new Date(+o[1], +o[2], +o[3]); } static readLogicalValue(e) { const t = e.readByte(); switch (String.fromCharCode(t)) { case "y": case "Y": return !0; case "n": case "N": return !1; } return null; } }; let V = E; p(V, "regExDate", /^(\d\d\d\d)(\d\d)(\d\d)$/); const xe = { C: "Character", N: "Number", L: "Logical", F: "Float", D: "Date" }; class H { constructor(e, t) { p(this, "stream"); p(this, "header"); p(this, "fields", []); p(this, "recordStartOffset", 0); p(this, "recordSize", 0); p(this, "decoder"); this.stream = new X(e), this.decoder = t, this.header = this.readHeader(); } get recordCount() { return this.header.recordCount; } get encoding() { var e; return ((e = this.decoder) == null ? void 0 : e.encoding) ?? ""; } static async fromFile(e, t) { const n = await e.arrayBuffer(); let s; return t && (s = await t.arrayBuffer()), this.fromArrayBuffer(n, s); } static async fromArrayBuffer(e, t) { let n; if (t) { const o = await new TextDecoder().decode(t); n = de(o); } return new H(e, n); } readHeader() { const e = this.stream; e.seek(0); const t = e.readByte(), n = e.readByte(), s = e.readByte(), o = e.readByte(), i = e.readInt32(!0); e.readInt16(!0), e.readInt16(!0); const u = e.seek(29).readByte(); return this.decoder || (this.decoder = ge(u)), this.readFields(e), this.computeRecordSize(e), { lastUpdated: new Date(n + 1900, s, o), recordCount: i, version: t }; } computeRecordSize(e) { this.recordSize = 1, this.fields.forEach((t) => this.recordSize += t.fieldLen), e.readByte(), this.recordStartOffset = e.tell; } readFields(e) { let t = 32; for (; e.seek(t), e.peekByte() !== 13; ) { let s = ""; for (let l = 0; l < 10; l++) { const h = e.readByte(); if (h === 0) break; s += String.fromCharCode(h); } s = s.trim(), e.seek(t + 11); const o = e.readByte(), i = String.fromCharCode(o); e.seek(t + 16); const u = e.readByte(), a = e.readByte(), c = { name: s, type: i, typeName: xe[i] || "Unknown", fieldLen: u, decimalCount: a }; this.fields.push(c), t += 32; } } readRecord(e) { let t = this.recordStartOffset + e * this.recordSize; if (this.stream.seek(t), !this.decoder) throw new Error("Decoder not initialized"); return V.fromPresetStream(this.stream, this.fields, this.decoder); } } var v = /* @__PURE__ */ ((r) => (r[r.NULL = 0] = "NULL", r[r.XY = 2] = "XY", r[r.XYM = 3] = "XYM", r[r.XYZM = 4] = "XYZM", r))(v || {}); class G { constructor(e, t) { this.coordType = e, this.hasMValuesPresent = t; } get hasZ() { return this.coordType === v.XYZM; } get hasOptionalM() { return this.coordType === v.XYM || this.coordType === v.XYZM; } get hasM() { return this.hasMValuesPresent; } get coordLength() { return this.coordType === v.XY ? 2 : this.coordType === v.XYM ? 3 : 4; } static readBbox(e) { const t = e.readDouble(!0), n = e.readDouble(!0), s = e.readDouble(!0), o = e.readDouble(!0); return { xMin: t, yMin: n, xMax: s, yMax: o }; } static recordReadingFinalized(e, t) { return e.tell === t.offset + (4 + t.length) * 2; } } class W extends G { static getZValues(e, t) { return e.readDouble(!0), e.readDouble(!0), e.readDoubleArray(t, !0); } static getMValues(e, t) { return e.readDouble(!0), e.readDouble(!0), e.readDoubleArray(t, !0); } static getCoords(e, t, n, s) { const o = []; for (let i = 0; i < e.length; ++i) { const u = [], a = e[i], c = e[i + 1] || t.length / 2; for (let l = a; l < c; l++) { const h = []; h.push(t[l * 2]), h.push(t[l * 2 + 1]), n && h.push(n[l]), s && h.push(s[l]), u.push(h); } o.push(u); } return o; } } var w = /* @__PURE__ */ ((r) => (r[r.Null = 0] = "Null", r[r.Point = 1] = "Point", r[r.PolyLine = 3] = "PolyLine", r[r.Polygon = 5] = "Polygon", r[r.MultiPoint = 8] = "MultiPoint", r[r.PointZ = 11] = "PointZ", r[r.PolyLineZ = 13] = "PolyLineZ", r[r.PolygonZ = 15] = "PolygonZ", r[r.MultiPointZ = 18] = "MultiPointZ", r[r.PointM = 21] = "PointM", r[r.PolyLineM = 23] = "PolyLineM", r[r.PolygonM = 25] = "PolygonM", r[r.MultiPointM = 28] = "MultiPointM", r[r.MultiPatch = 31] = "MultiPatch", r))(w || {}); class m { static coordType(e) { return e === 0 ? v.NULL : e < 10 ? v.XY : e < 20 ? v.XYZM : e < 30 ? v.XYM : e < 40 ? v.XYZM : v.NULL; } static hasZ(e) { return m.coordType(e) === v.XYZM; } static hasOptionalM(e) { const t = m.coordType(e); return t === v.XYZM || t === v.XYM; } static shapeTypeStr(e) { return e in w ? w[e] : "Unknown"; } } class D extends G { constructor(e, t, n) { super(t, n), this.coords = e; } get type() { return "MultiPoint"; } static fromPresetReader(e, t) { const n = e.hasZ, s = e.hasOptionalM, o = e.shpStream; let i, u; D.readBbox(o); const a = o.readInt32(!0), c = o.readDoubleArray(a * 2, !0); n && (i = D.getZValues(o, a)); const l = !this.recordReadingFinalized(o, t) && s; l && (u = D.getMValues(o, a)); const h = D.getCoords(a, c, i, u); return new D( h, m.coordType(t.type), l ); } static getZValues(e, t) { return e.readDouble(!0), e.readDouble(!0), e.readDoubleArray(t, !0); } static getMValues(e, t) { return e.readDouble(!0), e.readDouble(!0), e.readDoubleArray(t, !0); } static getCoords(e, t, n, s) { const o = []; for (let i = 0; i < e; i++) { const u = []; u.push(t[i * 2]), u.push(t[i * 2 + 1]), n && u.push(n[i]), s && u.push(s[i]), o.push(u); } return o; } toGeoJson() { const e = [], t = this.hasM ? this.coordLength - 1 : this.coordLength; for (const s of this.coords) e.push(s.slice(0, t)); return { type: "MultiPoint", coordinates: e }; } } class ye extends G { constructor() { super(v.NULL, !1); } get type() { return "Null"; } toGeoJson() { throw new Error("Method cannot be implemented."); } } class j extends G { constructor(e, t, n) { super(t, n), this.coords = e; } get type() { return "Point"; } static fromPresetReader(e, t) { const n = e.hasZ, s = e.hasOptionalM, o = e.shpStream, i = []; i.push(o.readDouble(!0)), i.push(o.readDouble(!0)), n && i.push(o.readDouble(!0)); const u = !this.recordReadingFinalized(o, t) && s; return u && i.push(o.readDouble(!0)), new j(i, m.coordType(t.type), u); } toGeoJson() { const e = this.hasM ? this.coordLength - 1 : this.coordLength; return { type: "Point", coordinates: this.coords.slice(0, e) }; } } function pe(r) { if (r.length < 3) return !1; let e = r[r.length - 1][1] * r[0][0] - r[r.length - 1][0] * r[0][1]; for (let t = 0; t < r.length - 1; ++t) e += r[t][1] * r[t + 1][0] - r[t][0] * r[t + 1][1]; return e > 0; } function Me(r, e) { let t = !1, n, s, o, i, u; for (let a = 0, c = r.length - 1; a < r.length; c = a++) n = r[a][0], s = r[a][1], o = r[c][0], i = r[c][1], u = s > e[1] != i > e[1] && e[0] < (o - n) * (e[1] - s) / (i - s) + n, u && (t = !t); return t; } function ve(r, e) { for (let t = 0; t < e.length; ++t) if (!Me(r, e[t])) return !1; return !0; } function se(r) { const e = [], t = []; for (const s of r) pe(s) ? e.push(s) : t.push(s); const n = []; for (const s of e) { const o = [s]; for (let i = t.length - 1; i >= 0; i--) ve(s, t[i]) && (o.push(t[i]), t.splice(i, 1)); n.push(o); } return t.length && n.push(...t.map((s) => [s])), n; } class O extends W { constructor(e, t, n) { super(t, n), this.coords = e; } get type() { return this.coords.length === 1 ? "Polygon" : "MultiPolygon"; } static fromPresetReader(e, t) { const n = e.hasZ, s = e.hasOptionalM, o = e.shpStream; let i, u; O.readBbox(o); const a = o.readInt32(!0), c = o.readInt32(!0), l = o.readInt32Array(a, !0), h = o.readDoubleArray(c * 2, !0); n && (i = O.getZValues(o, c)); const f = !this.recordReadingFinalized(o, t) && s; f && (u = O.getMValues(o, c)); const y = O.getCoords(l, h, i, u), d = se(y); return new O(d, m.coordType(t.type), f); } toGeoJson() { const e = [], t = this.hasM ? this.coordLength - 1 : this.coordLength; for (const s of this.coords) { const o = []; for (const i of s) { const u = []; for (const a of i) u.push(a.slice(0, t)); o.push(u); } e.push(o); } return e.length === 1 ? { type: "Polygon", coordinates: e[0] } : { type: "MultiPolygon", coordinates: e }; } } class S extends W { constructor(e, t, n) { super(t, n), this.coords = e; } get type() { return this.coords.length === 1 ? "LineString" : "MultiLineString"; } static fromPresetReader(e, t) { const n = e.hasZ, s = e.hasOptionalM, o = e.shpStream; let i, u; S.readBbox(o); const a = o.readInt32(!0), c = o.readInt32(!0), l = o.readInt32Array(a, !0), h = o.readDoubleArray(c * 2, !0); n && (i = S.getZValues(o, c)); const f = !this.recordReadingFinalized(o, t) && s; f && (u = S.getMValues(o, c)); const y = S.getCoords(l, h, i, u); return new S(y, m.coordType(t.type), f); } toGeoJson() { const e = [], t = this.hasM ? this.coordLength - 1 : this.coordLength; for (const s of this.coords) { const o = []; for (const i of s) o.push(i.slice(0, t)); e.push(o); } return e.length === 1 ? { type: "LineString", coordinates: e[0] } : { type: "MultiLineString", coordinates: e }; } } class P extends W { constructor(e, t, n) { super(t, n), this.coords = e; } get type() { return this.coords.length === 1 ? "Polygon" : "MultiPolygon"; } static fromPresetReader(e, t) { const n = e.hasZ, s = e.hasOptionalM, o = e.shpStream; let i, u; P.readBbox(o); const a = o.readInt32(!0), c = o.readInt32(!0), l = o.readInt32Array(a, !0), h = o.readInt32Array(a, !0), f = o.readDoubleArray(c * 2, !0); n && (i = P.getZValues(o, c)); const y = !this.recordReadingFinalized(o, t) && s; y && (u = P.getMValues(o, c)); const d = P.getCoords(l, f, i, u), Z = P.assemblePolygonsWithHoles(d, h); return new P( Z, m.coordType(t.type), y ); } static assemblePolygonsWithHoles(e, t) { const n = [], s = []; let o = !1; for (let i = 0; i < t.length; i++) if (t[i] === 0) P.closePolygon(s, n), n.push(...P.triangleStripToPolygon(e[i])), o = !1; else if (t[i] === 1) P.closePolygon(s, n), n.push(...P.triangleFanToPolygon(e[i])), o = !1; else if (t[i] === 2) P.closePolygon(s, n), s.push(e[i]), o = !1; else if (t[i] === 3) s.push(e[i]), o = !1; else if (t[i] === 4) P.closePolygon(s, n), s.push(e[i]), o = !0; else if (t[i] === 5) o || P.closePolygon(s, n), s.push(e[i]); else throw new Error(`Invalid part type in MultiPatch: ${t[i]}`); return P.closePolygon(s, n), n; } static closePolygon(e, t) { e.length > 0 && (t.push(...se(e)), e.length = 0); } static triangleStripToPolygon(e) { const t = []; let n = 1, s = 2; for (let o = 0; o < e.length - 2; o++) t.push([ [e[o], e[o + n], e[o + s], e[o]] ]), n = n === 1 ? 2 : 1, s = s === 1 ? 2 : 1; return t; } static triangleFanToPolygon(e) { const t = []; for (let n = 1; n < e.length - 1; n++) t.push([[e[0], e[n], e[n + 1], e[0]]]); return t; } toGeoJson() { const e = [], t = this.hasM ? this.coordLength - 1 : this.coordLength; for (const s of this.coords) { const o = []; for (const i of s) { const u = []; for (const a of i) u.push(a.slice(0, t)); o.push(u); } e.push(o); } return e.length === 1 ? { type: "Polygon", coordinates: e[0] } : { type: "MultiPolygon", coordinates: e }; } } class z { constructor(e, t) { p(this, "shxStream"); p(this, "shxHeader"); p(this, "shpHeader"); p(this, "shpStream"); p(this, "recordCount", 0); p(this, "hasZ"); p(this, "hasOptionalM"); if (this.shpStream = new X(e), this.shpHeader = this.readHeader(this.shpStream), this.shxStream = new X(t), this.shxHeader = this.readHeader(this.shxStream), this.shpHeader.type !== this.shxHeader.type) throw new Error("SHP / SHX shapetype mismatch"); this.recordCount = (this.shxHeader.fileLength - 100) / 8, this.hasZ = m.hasZ(this.shpHeader.type), this.hasOptionalM = m.hasOptionalM(this.shpHeader.type); } static async fromFile(e, t) { const n = await e.arrayBuffer(), s = await t.arrayBuffer(); return this.fromArrayBuffer(n, s); } static async fromArrayBuffer(e, t) { return new z(e, t); } readHeader(e) { const t = e.seek(0).readInt32(!1); if (t !== 9994) throw new Error(`Unexpected Shape fileCode: ${t}`); const n = e.seek(24).readInt32(!1), s = e.seek(32).readInt32(!0); e.seek(36); const o = this.readBbox(e); return { type: s, fileLength: n * 2, extent: o }; } readGeomHeader() { const e = this.shpStream.tell, t = this.shpStream.readInt32(!1), n = this.shpStream.readInt32(!1), s = this.shpStream.readInt32(!0); return { offset: e, length: n, recordNum: t, type: s }; } readBbox(e) { const t = e.readDouble(!0), n = e.readDouble(!0), s = e.readDouble(!0), o = e.readDouble(!0); return { xMin: t, yMin: n, xMax: s, yMax: o }; } getShpIndex(e) { const t = e * 8 + 100; return this.shxStream.seek(t).readInt32(!1) * 2; } readGeom(e) { try { return this.readGeometryData(e); } catch { return null; } } readGeometryData(e) { const t = this.getShpIndex(e); this.shpStream.seek(t); const n = this.readGeomHeader(); switch (this.checkForNull(n), n.type) { case w.Null: return new ye(); case w.Point: case w.PointZ: case w.PointM: return this.readPoint(n); case w.MultiPoint: case w.MultiPointZ: case w.MultiPointM: return this.readMultiPoint(n); case w.PolyLine: case w.PolyLineZ: case w.PolyLineM: return this.readPolyLine(n); case w.Polygon: case w.PolygonZ: case w.PolygonM: return this.readPolygon(n); case w.MultiPatch: return this.readMultiPatch(n); } throw new Error("Unsupported geometry"); } checkForNull(e) { if (this.shpHeader.type !== e.type && e.type !== w.Null) throw new Error( `Unexpected shape type ${m.shapeTypeStr(e.type)}(${e.type}), expected ${m.shapeTypeStr(this.shpHeader.type)}` ); } readPoint(e) { return j.fromPresetReader(this, e); } readMultiPoint(e) { return D.fromPresetReader(this, e); } readPolyLine(e) { return S.fromPresetReader(this, e); } readPolygon(e) { return O.fromPresetReader(this, e); } readMultiPatch(e) { return P.fromPresetReader(this, e); } } class we { constructor() { p(this, "features", []); } toGeoJson() { return { type: "FeatureCollection", features: this.features.map((t) => t.toGeoJson()) }; } } class Pe { constructor(e, t, n) { p(this, "geom"); p(this, "properties", {}); if (this.geom = e, !(!t || !n)) for (let s = 0; s < n.length; s++) this.properties[n[s].name] = t[s]; } toGeoJson() { return { geometry: this.geom.toGeoJson(), properties: this.properties, type: "Feature" }; } } class U { constructor(e, t) { p(this, "shpReader"); p(this, "dbfReader"); if (t && e.recordCount !== (t == null ? void 0 : t.recordCount)) throw new Error( `Record count mismatch: SHP-file has ${e.recordCount} records, DBF has ${t == null ? void 0 : t.recordCount}` ); this.shpReader = e, this.dbfReader = t; } get featureCount() { return this.shpReader.recordCount; } get fields() { var e; return (e = this.dbfReader) == null ? void 0 : e.fields; } get shpHeader() { return this.shpReader.shpHeader; } static async fromArrayBuffers(e, t, n, s) { const o = await z.fromArrayBuffer(e, t); let i; return n != null && (i = await H.fromArrayBuffer(n, s)), new U(o, i); } static async fromFiles(e, t, n, s) { const o = await z.fromFile(e, t); let i; return n != null && (i = await H.fromFile(n, s)), new U(o, i); } readFeature(e) { var s; if (e < 0 || e > this.featureCount - 1) throw new Error("Feature index out of range"); const t = this.shpReader.readGeom(e); let n = []; return this.dbfReader != null && (n = this.dbfReader.readRecord(e)), t == null ? null : new Pe(t, n, (s = this.dbfReader) == null ? void 0 : s.fields); } readFeatureCollection() { const e = new we(); for (let t = 0; t < this.featureCount; t++) { const n = this.readFeature(t); n != null && e.features.push(n); } return e; } } function K(r, e, t) { t = t || 2; var n = e && e.length, s = n ? e[0] * t : r.length, o = oe(r, 0, s, t, !0), i = []; if (!o || o.next === o.prev) return i; var u, a, c, l, h, f, y; if (n && (o = Oe(r, e, o, t)), r.length > 80 * t) { u = c = r[0], a = l = r[1]; for (var d = t; d < s; d += t) h = r[d], f = r[d + 1], h < u && (u = h), f < a && (a = f), h > c && (c = h), f > l && (l = f); y = Math.max(c - u, l - a), y = y !== 0 ? 32767 / y : 0; } return k(o, i, t, u, a, y, 0), i; } function oe(r, e, t, n, s) { var o, i; if (s === J(r, e, t, n) > 0) for (o = e; o < t; o += n) i = _(o, r[o], r[o + 1], i); else for (o = t - n; o >= e; o -= n) i = _(o, r[o], r[o + 1], i); return i && Y(i, i.next) && (A(i), i = i.next), i; } function C(r, e) { if (!r) return r; e || (e = r); var t = r, n; do if (n = !1, !t.steiner && (Y(t, t.next) || M(t.prev, t, t.next) === 0)) { if (A(t), t = e = t.prev, t === t.next) break; n = !0; } else t = t.next; while (n || t !== e); return e; } function k(r, e, t, n, s, o, i) { if (r) { !i && o && Ie(r, n, s, o); for (var u = r, a, c; r.prev !== r.next; ) { if (a = r.prev, c = r.next, o ? Ee(r, n, s, o) : me(r)) { e.push(a.i / t | 0), e.push(r.i / t | 0), e.push(c.i / t | 0), A(r), r = c.next, u = c.next; continue; } if (r = c, r === u) { i ? i === 1 ? (r = Ze(C(r), e, t), k(r, e, t, n, s, o, 2)) : i === 2 && De(r, e, t, n, s, o) : k(C(r), e, t, n, s, o, 1); break; } } } } function me(r) { var e = r.prev, t = r, n = r.next; if (M(e, t, n) >= 0) return !1; for (var s = e.x, o = t.x, i = n.x, u = e.y, a = t.y, c = n.y, l = s < o ? s < i ? s : i : o < i ? o : i, h = u < a ? u < c ? u : c : a < c ? a : c, f = s > o ? s > i ? s : i : o > i ? o : i, y = u > a ? u > c ? u : c : a > c ? a : c, d = n.next; d !== e; ) { if (d.x >= l && d.x <= f && d.y >= h && d.y <= y && B(s, u, o, a, i, c, d.x, d.y) && M(d.prev, d, d.next) >= 0) return !1; d = d.next; } return !0; } function Ee(r, e, t, n) { var s = r.prev, o = r, i = r.next; if (M(s, o, i) >= 0) return !1; for (var u = s.x, a = o.x, c = i.x, l = s.y, h = o.y, f = i.y, y = u < a ? u < c ? u : c : a < c ? a : c, d = l < h ? l < f ? l : f : h < f ? h : f, Z = u > a ? u > c ? u : c : a > c ? a : c, I = l > h ? l > f ? l : f : h > f ? h : f, Q = $(y, d, e, t, n), T = $(Z, I, e, t, n), g = r.prevZ, x = r.nextZ; g && g.z >= Q && x && x.z <= T; ) { if (g.x >= y && g.x <= Z && g.y >= d && g.y <= I && g !== s && g !== i && B(u, l, a, h, c, f, g.x, g.y) && M(g.prev, g, g.next) >= 0 || (g = g.prevZ, x.x >= y && x.x <= Z && x.y >= d && x.y <= I && x !== s && x !== i && B(u, l, a, h, c, f, x.x, x.y) && M(x.prev, x, x.next) >= 0)) return !1; x = x.nextZ; } for (; g && g.z >= Q; ) { if (g.x >= y && g.x <= Z && g.y >= d && g.y <= I && g !== s && g !== i && B(u, l, a, h, c, f, g.x, g.y) && M(g.prev, g, g.next) >= 0) return !1; g = g.prevZ; } for (; x && x.z <= T; ) { if (x.x >= y && x.x <= Z && x.y >= d && x.y <= I && x !== s && x !== i && B(u, l, a, h, c, f, x.x, x.y) && M(x.prev, x, x.next) >= 0) return !1; x = x.nextZ; } return !0; } function Ze(r, e, t) { var n = r; do { var s = n.prev, o = n.next.next; !Y(s, o) && ie(s, n, n.next, o) && F(s, o) && F(o, s) && (e.push(s.i / t | 0), e.push(n.i / t | 0), e.push(o.i / t | 0), A(n), A(n.next), n = r = o), n = n.next; } while (n !== r); return C(n); } function De(r, e, t, n, s, o) { var i = r; do { for (var u = i.next.next; u !== i.prev; ) { if (i.i !== u.i && Ae(i, u)) { var a = ue(i, u); i = C(i, i.next), a = C(a, a.next), k(i, e, t, n, s, o, 0), k(a, e, t, n, s, o, 0); return; } u = u.next; } i = i.next; } while (i !== r); } function Oe(r, e, t, n) { var s = [], o, i, u, a, c; for (o = 0, i = e.length; o < i; o++) u = e[o] * n, a = o < i - 1 ? e[o + 1] * n : r.length, c = oe(r, u, a, n, !1), c === c.next && (c.steiner = !0), s.push(Fe(c)); for (s.sort(Se), o = 0; o < s.length; o++) t = Ce(s[o], t); return t; } function Se(r, e) { return r.x - e.x; } function Ce(r, e) { var t = Le(r, e); if (!t) return e; var n = ue(t, r); return C(n, n.next), C(t, t.next); } function Le(r, e) { var t = e, n = r.x, s = r.y, o = -1 / 0, i; do { if (s <= t.y && s >= t.next.y && t.next.y !== t.y) { var u = t.x + (s - t.y) * (t.next.x - t.x) / (t.next.y - t.y); if (u <= n && u > o && (o = u, i = t.x < t.next.x ? t : t.next, u === n)) return i; } t = t.next; } while (t !== e); if (!i) return null; var a = i, c = i.x, l = i.y, h = 1 / 0, f; t = i; do n >= t.x && t.x >= c && n !== t.x && B(s < l ? n : o, s, c, l, s < l ? o : n, s, t.x, t.y) && (f = Math.abs(s - t.y) / (n - t.x), F(t, r) && (f < h || f === h && (t.x > i.x || t.x === i.x && Be(i, t))) && (i = t, h = f)), t = t.next; while (t !== a); return i; } function Be(r, e) { return M(r.prev, r, e.prev) < 0 && M(e.next, r, r.next) < 0; } function Ie(r, e, t, n) { var s = r; do s.z === 0 && (s.z = $(s.x, s.y, e, t, n)), s.prevZ = s.prev, s.nextZ = s.next, s = s.next; while (s !== r); s.prevZ.nextZ = null, s.prevZ = null, ke(s); } function ke(r) { var e, t, n, s, o, i, u, a, c = 1; do { for (t = r, r = null, o = null, i = 0; t; ) { for (i++, n = t, u = 0, e = 0; e < c && (u++, n = n.nextZ, !!n); e++) ; for (a = c; u > 0 || a > 0 && n; ) u !== 0 && (a === 0 || !n || t.z <= n.z) ? (s = t, t = t.nextZ, u--) : (s = n, n = n.nextZ, a--), o ? o.nextZ = s : r = s, s.prevZ = o, o = s; t = n; } o.nextZ = null, c *= 2; } while (i > 1); return r; } function $(r, e, t, n, s) { return r = (r - t) * s | 0, e = (e - n) * s | 0, r = (r | r << 8) & 16711935, r = (r | r << 4) & 252645135, r = (r | r << 2) & 858993459, r = (r | r << 1) & 1431655765, e = (e | e << 8) & 16711935, e = (e | e << 4) & 252645135, e = (e | e << 2) & 858993459, e = (e | e << 1) & 1431655765, r | e << 1; } function Fe(r) { var e = r, t = r; do (e.x < t.x || e.x === t.x && e.y < t.y) && (t = e), e = e.next; while (e !== r); return t; } function B(r, e, t, n, s, o, i, u) { return (s - i) * (e - u) >= (r - i) * (o - u) && (r - i) * (n - u) >= (t - i) * (e - u) && (t - i) * (o - u) >= (s - i) * (n - u); } function Ae(r, e) { return r.next.i !== e.i && r.prev.i !== e.i && !be(r, e) && // dones't intersect other edges (F(r, e) && F(e, r) && Ne(r, e) && // locally visible (M(r.prev, r, e.prev) || M(r, e.prev, e)) || // does not create opposite-facing sectors Y(r, e) && M(r.prev, r, r.next) > 0 && M(e.prev, e, e.next) > 0); } function M(r, e, t) { return (e.y - r.y) * (t.x - e.x) - (e.x - r.x) * (t.y - e.y); } function Y(r, e) { return r.x === e.x && r.y === e.y; } function ie(r, e, t, n) { var s = N(M(r, e, t)), o = N(M(r, e, n)), i = N(M(t, n, r)), u = N(M(t, n, e)); return !!(s !== o && i !== u || s === 0 && b(r, t, e) || o === 0 && b(r, n, e) || i === 0 && b(t, r, n) || u === 0 && b(t, e, n)); } function b(r, e, t) { return e.x <= Math.max(r.x, t.x) && e.x >= Math.min(r.x, t.x) && e.y <= Math.max(r.y, t.y) && e.y >= Math.min(r.y, t.y); } function N(r) { return r > 0 ? 1 : r < 0 ? -1 : 0; } function be(r, e) { var t = r; do { if (t.i !== r.i && t.next.i !== r.i && t.i !== e.i && t.next.i !== e.i && ie(t, t.next, r, e)) return !0; t = t.next; } while (t !== r); return !1; } function F(r, e) { return M(r.prev, r, r.next) < 0 ? M(r, e, r.next) >= 0 && M(r, r.prev, e) >= 0 : M(r, e, r.prev) < 0 || M(r, r.next, e) < 0; } function Ne(r, e) { var t = r, n = !1, s = (r.x + e.x) / 2, o = (r.y + e.y) / 2; do t.y > o != t.next.y > o && t.next.y !== t.y && s < (t.next.x - t.x) * (o - t.y) / (t.next.y - t.y) + t.x && (n = !n), t = t.next; while (t !== r); return n; } function ue(r, e) { var t = new R(r.i, r.x, r.y), n = new R(e.i, e.x, e.y), s = r.next, o = e.prev; return r.next = e, e.prev = r, t.next = s, s.prev = t, n.next = t, t.prev = n, o.next = n, n.prev = o, n; } function _(r, e, t, n) { var s = new R(r, e, t); return n ? (s.next = n.next, s.prev = n, n.next.prev = s, n.next = s) : (s.prev = s, s.next = s), s; } function A(r) { r.next.prev = r.prev, r.prev.next = r.next, r.prevZ && (r.prevZ.nextZ = r.nextZ), r.nextZ && (r.nextZ.prevZ = r.prevZ); } function R(r, e, t) { this.i = r, this.x = e, this.y = t, this.prev = null, this.next = null, this.z = 0, this.prevZ = null, this.nextZ = null, this.steiner = !1; } K.deviation = function(r, e, t, n) { var s = e && e.length, o = s ? e[0] * t : r.length, i = Math.abs(J(r, 0, o, t)); if (s) for (var u = 0, a = e.length; u < a; u++) { var c = e[u] * t, l = u < a - 1 ? e[u + 1] * t : r.length; i -= Math.abs(J(r, c, l, t)); } var h = 0; for (u = 0; u < n.length; u += 3) { var f = n[u] * t, y = n[u + 1] * t, d = n[u + 2] * t; h += Math.abs( (r[f] - r[d]) * (r[y + 1] - r[f + 1]) - (r[f] - r[y]) * (r[d + 1] - r[f + 1]) ); } return i === 0 && h === 0 ? 0 : Math.abs((h - i) / i); }; function J(r, e, t, n) { for (var s = 0, o = e, i = t - n; o < t; o += n) s += (r[i] - r[o]) * (r[o + 1] + r[i + 1]), i = o; return s; } K.flatten = function(r) { for (var e = r[0][0].length, t = { vertices: [], holes: [], dimensions: e }, n = 0, s = 0; s < r.length; s++) { for (var o = 0; o < r[s].length; o++) for (var i = 0; i < e; i++) t.vertices.push(r[s][o][i]); s > 0 && (n += r[s - 1].length, t.holes.push(n)); } return t; }; var ee = typeof Float32Array < "u" ? Float32Array : Array; Math.hypot || (Math.hypot = function() { for (var r = 0, e = arguments.length; e--; ) r += arguments[e] * arguments[e]; return Math.sqrt(r); }); function Ve() { var r = new ee(3); return ee != Float32Array && (r[0] = 0, r[1] = 0, r[2] = 0), r; } function te(r) { var e = r[0], t = r[1], n = r[2]; return Math.hypot(e, t, n); } function He(r, e) { var t = e[0], n = e[1], s = e[2], o = t * t + n * n + s * s; return o > 0 && (o = 1 / Math.sqrt(o)), r[0] = e[0] * o, r[1] = e[1] * o, r[2] = e[2] * o, r; } function re(r, e) { return r[0] * e[0] + r[1] * e[1] + r[2] * e[2]; } (function() { var r = Ve(); return function(e, t, n, s, o, i) { var u, a; for (t || (t = 3), n || (n = 0), s ? a = Math.min(s * t + n, e.length) : a = e.length, u = n; u < a; u += t) r[0] = e[u], r[1] = e[u + 1], r[2] = e[u + 2], o(r, r, i), e[u] = r[0], e[u + 1] = r[1], e[u + 2] = r[2]; return e; }; })(); function ze(r, e) { const t = [0, 0, 0], n = r.length; let s, o, i, u, a, c, l, h; for (let f = 0; f < n; f += e) l = f, h = (f + e) % n, s = r[l], o = r[l + 1], i = r[l + 2], u = r[h], a = r[h + 1], c = r[h + 2], t[0] += (o - a) * (i + c), t[1] += (i - c) * (s + u), t[2] += (s - u) * (o + a); return He(t, t), t; } function Ge(r, e) { const t = r.length; let n = 0, s = 0, o = 0; for (let i = 0; i < t; i += e) n += r[i], s += r[i + 1], o += r[i + 2]; return [n / (t / e), s / (t / e), o / (t / e)]; } function Ye(r) { let e; Math.abs(r[0]) <= Math.abs(r[1]) && Math.abs(r[0]) <= Math.abs(r[2]) ? e = [0, -r[2], r[1]] : Math.abs(r[1]) <= Math.abs(r[0]) && Math.abs(r[1]) <= Math.abs(r[2]) ? e = [-r[2], 0, r[0]] : e = [-r[1], r[0], 0]; const t = [ r[1] * e[2] - r[2] * e[1], r[2] * e[0] - r[0] * e[2], r[0] * e[1] - r[1] * e[0] ]; return [e, t]; } function Xe(r, e, t, n, s) { const o = s[0], i = s[1], u = [r - n[0], e - n[1], t - n[2]]; return [re(o, u), re(i, u)]; } function Ue(r) { return isNaN(te(r)) || te(r) < 1e-10; } function $e(r, e) { const t = e === v.XYZM ? 4 : e === v.XY ? 2 : 3; if (t === 2) return r; const n = ze(r, t); if (Ue(n)) return []; const s = Ge(r, t), o = Ye(n), i = []; for (let u = 0; u < r.length; u += t) { const [a, c] = Xe(r[u], r[u + 1], r[u + 2], s, o); i.push(a, c); } return i; } function Je(r, e, t = !1) { const n = [], s = [], o = e === v.XYZM ? 4 : e === v.XY ? 2 : 3; r.forEach((l, h) => { h > 0 && s.push(n.length / o), l.forEach((f, y) => { y !== l.length - 1 && n.push(...f.slice()); }); }); const i = $e(n, e); if (i.length === 0) return new Float32Array([]); const u = K(i, s, 2); let a = o; t && (a = Math.min(o, 3)); const c = new Float32Array(u.length * a); for (let l = 0; l < u.length; l++) { const h = u[l]; for (let f = 0; f < a; f++) c[l * a + f] = n[h * o + f]; } return c; } export { G as BaseRecord, v as CoordType, H as DbfReader, V as DbfRecord, Pe as Feature, we as FeatureCollection, U as FeatureReader, P as MultiPatchRecord, D as MultiPointRecord, j as PointRecord, S as PolyLineRecord, O as PolygonRecord, z as ShapeReader, Je as triangulate };