UNPKG

shpts

Version:

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

2 lines (1 loc) 26.6 kB
(function(x,E){typeof exports=="object"&&typeof module<"u"?E(exports):typeof define=="function"&&define.amd?define(["exports"],E):(x=typeof globalThis<"u"?globalThis:x||self,E(x.shpts={}))})(this,function(x){"use strict";var $e=Object.defineProperty;var Je=(x,E,Z)=>E in x?$e(x,E,{enumerable:!0,configurable:!0,writable:!0,value:Z}):x[E]=Z;var M=(x,E,Z)=>(Je(x,typeof E!="symbol"?E+"":E,Z),Z);class E{constructor(e){M(this,"dataView");M(this,"offset",0);M(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}peekByte(){return this.dataView.getUint8(this.offset)}readByte(){const e=this.dataView.getUint8(this.offset);return this.offset+=1,e}}const Z={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"]},he=/^.*ANSI\s*(\d+)\s*$/,fe=/^.*ISO\s*8859(\d)\s*$/,de=/^.*UTF[-\s]?8\s*$/;class F{constructor(e){M(this,"encoding");M(this,"decoder");this.encoding=e,this.decoder=new TextDecoder(e)}decode(e){return this.decoder.decode(e)}}function q(r){try{return new TextDecoder(r),!0}catch{return!1}}function ge(r){if(!r)throw new Error("No codepage/CPG string provided");if(r.match(de))return new F("utf8");let e=r.match(fe);if(e!=null)return new F(`ISO-8859-${e[1]}`);if(e=r.match(he),e!=null){const n=`cp${parseInt(e[1])}`;if(!q(n))throw new Error(`Encoding ${n} not supported`);return new F(n)}return new F("cp1252")}function ye(r){if(r===0)return new F("cp1252");if(r in Z){const t=`cp${Z[r][0]}`;if(!q(t))throw new Error(`Encoding ${t} not supported`);return new F(t)}throw new Error(`Could not find converter for codepage ${r}`)}const L=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(L.readCharValue(e,u,n));break;case"N":o.push(L.readNumberValue(e,u,n));break;case"F":o.push(L.readNumberValue(e,u,n));break;case"D":o.push(L.readDateValue(e,u,n));break;case"L":o.push(L.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(L.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 b=L;M(b,"regExDate",/^(\d\d\d\d)(\d\d)(\d\d)$/);const xe={C:"Character",N:"Number",L:"Logical",F:"Float",D:"Date"};class A{constructor(e,t){M(this,"stream");M(this,"header");M(this,"fields",[]);M(this,"recordStartOffset",0);M(this,"recordSize",0);M(this,"decoder");this.stream=new E(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=ge(o)}return new A(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=ye(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 b.fromPresetStream(this.stream,this.fields,this.decoder)}}var w=(r=>(r[r.NULL=0]="NULL",r[r.XY=2]="XY",r[r.XYM=3]="XYM",r[r.XYZM=4]="XYZM",r))(w||{});class N{constructor(e,t){this.coordType=e,this.hasMValuesPresent=t}get hasZ(){return this.coordType===w.XYZM}get hasOptionalM(){return this.coordType===w.XYM||this.coordType===w.XYZM}get hasM(){return this.hasMValuesPresent}get coordLength(){return this.coordType===w.XY?2:this.coordType===w.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 N{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 P=(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))(P||{});class D{static coordType(e){return e===0?w.NULL:e<10?w.XY:e<20?w.XYZM:e<30?w.XYM:e<40?w.XYZM:w.NULL}static hasZ(e){return D.coordType(e)===w.XYZM}static hasOptionalM(e){const t=D.coordType(e);return t===w.XYZM||t===w.XYM}static shapeTypeStr(e){return e in P?P[e]:"Unknown"}}class O extends N{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;O.readBbox(o);const a=o.readInt32(!0),c=o.readDoubleArray(a*2,!0);n&&(i=O.getZValues(o,a));const l=!this.recordReadingFinalized(o,t)&&s;l&&(u=O.getMValues(o,a));const h=O.getCoords(a,c,i,u);return new O(h,D.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 pe extends N{constructor(){super(w.NULL,!1)}get type(){return"Null"}toGeoJson(){throw new Error("Method cannot be implemented.")}}class Y extends N{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 Y(i,D.coordType(t.type),u)}toGeoJson(){const e=this.hasM?this.coordLength-1:this.coordLength;return{type:"Point",coordinates:this.coords.slice(0,e)}}}function Me(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 ve(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 we(r,e){for(let t=0;t<e.length;++t)if(!ve(r,e[t]))return!1;return!0}function _(r){const e=[],t=[];for(const s of r)Me(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--)we(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 S 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;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 p=S.getCoords(l,h,i,u),d=_(p);return new S(d,D.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 C 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;C.readBbox(o);const a=o.readInt32(!0),c=o.readInt32(!0),l=o.readInt32Array(a,!0),h=o.readDoubleArray(c*2,!0);n&&(i=C.getZValues(o,c));const f=!this.recordReadingFinalized(o,t)&&s;f&&(u=C.getMValues(o,c));const p=C.getCoords(l,h,i,u);return new C(p,D.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 m 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;m.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=m.getZValues(o,c));const p=!this.recordReadingFinalized(o,t)&&s;p&&(u=m.getMValues(o,c));const d=m.getCoords(l,f,i,u),I=m.assemblePolygonsWithHoles(d,h);return new m(I,D.coordType(t.type),p)}static assemblePolygonsWithHoles(e,t){const n=[],s=[];let o=!1;for(let i=0;i<t.length;i++)if(t[i]===0)m.closePolygon(s,n),n.push(...m.triangleStripToPolygon(e[i])),o=!1;else if(t[i]===1)m.closePolygon(s,n),n.push(...m.triangleFanToPolygon(e[i])),o=!1;else if(t[i]===2)m.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)m.closePolygon(s,n),s.push(e[i]),o=!0;else if(t[i]===5)o||m.closePolygon(s,n),s.push(e[i]);else throw new Error(`Invalid part type in MultiPatch: ${t[i]}`);return m.closePolygon(s,n),n}static closePolygon(e,t){e.length>0&&(t.push(..._(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 V{constructor(e,t){M(this,"shxStream");M(this,"shxHeader");M(this,"shpHeader");M(this,"shpStream");M(this,"recordCount",0);M(this,"hasZ");M(this,"hasOptionalM");if(this.shpStream=new E(e),this.shpHeader=this.readHeader(this.shpStream),this.shxStream=new E(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=D.hasZ(this.shpHeader.type),this.hasOptionalM=D.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 V(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 P.Null:return new pe;case P.Point:case P.PointZ:case P.PointM:return this.readPoint(n);case P.MultiPoint:case P.MultiPointZ:case P.MultiPointM:return this.readMultiPoint(n);case P.PolyLine:case P.PolyLineZ:case P.PolyLineM:return this.readPolyLine(n);case P.Polygon:case P.PolygonZ:case P.PolygonM:return this.readPolygon(n);case P.MultiPatch:return this.readMultiPatch(n)}throw new Error("Unsupported geometry")}checkForNull(e){if(this.shpHeader.type!==e.type&&e.type!==P.Null)throw new Error(`Unexpected shape type ${D.shapeTypeStr(e.type)}(${e.type}), expected ${D.shapeTypeStr(this.shpHeader.type)}`)}readPoint(e){return Y.fromPresetReader(this,e)}readMultiPoint(e){return O.fromPresetReader(this,e)}readPolyLine(e){return C.fromPresetReader(this,e)}readPolygon(e){return S.fromPresetReader(this,e)}readMultiPatch(e){return m.fromPresetReader(this,e)}}class ee{constructor(){M(this,"features",[])}toGeoJson(){return{type:"FeatureCollection",features:this.features.map(t=>t.toGeoJson())}}}class te{constructor(e,t,n){M(this,"geom");M(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 X{constructor(e,t){M(this,"shpReader");M(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 V.fromArrayBuffer(e,t);let i;return n!=null&&(i=await A.fromArrayBuffer(n,s)),new X(o,i)}static async fromFiles(e,t,n,s){const o=await V.fromFile(e,t);let i;return n!=null&&(i=await A.fromFile(n,s)),new X(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 te(t,n,(s=this.dbfReader)==null?void 0:s.fields)}readFeatureCollection(){const e=new ee;for(let t=0;t<this.featureCount;t++){const n=this.readFeature(t);n!=null&&e.features.push(n)}return e}}function j(r,e,t){t=t||2;var n=e&&e.length,s=n?e[0]*t:r.length,o=re(r,0,s,t,!0),i=[];if(!o||o.next===o.prev)return i;var u,a,c,l,h,f,p;if(n&&(o=Ze(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);p=Math.max(c-u,l-a),p=p!==0?32767/p:0}return H(o,i,t,u,a,p,0),i}function re(r,e,t,n,s){var o,i;if(s===T(r,e,t,n)>0)for(o=e;o<t;o+=n)i=oe(o,r[o],r[o+1],i);else for(o=t-n;o>=e;o-=n)i=oe(o,r[o],r[o+1],i);return i&&U(i,i.next)&&(G(i),i=i.next),i}function B(r,e){if(!r)return r;e||(e=r);var t=r,n;do if(n=!1,!t.steiner&&(U(t,t.next)||v(t.prev,t,t.next)===0)){if(G(t),t=e=t.prev,t===t.next)break;n=!0}else t=t.next;while(n||t!==e);return e}function H(r,e,t,n,s,o,i){if(r){!i&&o&&Be(r,n,s,o);for(var u=r,a,c;r.prev!==r.next;){if(a=r.prev,c=r.next,o?me(r,n,s,o):Pe(r)){e.push(a.i/t|0),e.push(r.i/t|0),e.push(c.i/t|0),G(r),r=c.next,u=c.next;continue}if(r=c,r===u){i?i===1?(r=Ee(B(r),e,t),H(r,e,t,n,s,o,2)):i===2&&De(r,e,t,n,s,o):H(B(r),e,t,n,s,o,1);break}}}}function Pe(r){var e=r.prev,t=r,n=r.next;if(v(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,p=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<=p&&k(s,u,o,a,i,c,d.x,d.y)&&v(d.prev,d,d.next)>=0)return!1;d=d.next}return!0}function me(r,e,t,n){var s=r.prev,o=r,i=r.next;if(v(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,p=u<a?u<c?u:c:a<c?a:c,d=l<h?l<f?l:f:h<f?h:f,I=u>a?u>c?u:c:a>c?a:c,R=l>h?l>f?l:f:h>f?h:f,ce=K(p,d,e,t,n),le=K(I,R,e,t,n),g=r.prevZ,y=r.nextZ;g&&g.z>=ce&&y&&y.z<=le;){if(g.x>=p&&g.x<=I&&g.y>=d&&g.y<=R&&g!==s&&g!==i&&k(u,l,a,h,c,f,g.x,g.y)&&v(g.prev,g,g.next)>=0||(g=g.prevZ,y.x>=p&&y.x<=I&&y.y>=d&&y.y<=R&&y!==s&&y!==i&&k(u,l,a,h,c,f,y.x,y.y)&&v(y.prev,y,y.next)>=0))return!1;y=y.nextZ}for(;g&&g.z>=ce;){if(g.x>=p&&g.x<=I&&g.y>=d&&g.y<=R&&g!==s&&g!==i&&k(u,l,a,h,c,f,g.x,g.y)&&v(g.prev,g,g.next)>=0)return!1;g=g.prevZ}for(;y&&y.z<=le;){if(y.x>=p&&y.x<=I&&y.y>=d&&y.y<=R&&y!==s&&y!==i&&k(u,l,a,h,c,f,y.x,y.y)&&v(y.prev,y,y.next)>=0)return!1;y=y.nextZ}return!0}function Ee(r,e,t){var n=r;do{var s=n.prev,o=n.next.next;!U(s,o)&&ne(s,n,n.next,o)&&z(s,o)&&z(o,s)&&(e.push(s.i/t|0),e.push(n.i/t|0),e.push(o.i/t|0),G(n),G(n.next),n=r=o),n=n.next}while(n!==r);return B(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&&ke(i,u)){var a=se(i,u);i=B(i,i.next),a=B(a,a.next),H(i,e,t,n,s,o,0),H(a,e,t,n,s,o,0);return}u=u.next}i=i.next}while(i!==r)}function Ze(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=re(r,u,a,n,!1),c===c.next&&(c.steiner=!0),s.push(Fe(c));for(s.sort(Oe),o=0;o<s.length;o++)t=Se(s[o],t);return t}function Oe(r,e){return r.x-e.x}function Se(r,e){var t=Ce(r,e);if(!t)return e;var n=se(t,r);return B(n,n.next),B(t,t.next)}function Ce(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&&k(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),z(t,r)&&(f<h||f===h&&(t.x>i.x||t.x===i.x&&Le(i,t)))&&(i=t,h=f)),t=t.next;while(t!==a);return i}function Le(r,e){return v(r.prev,r,e.prev)<0&&v(e.next,r,r.next)<0}function Be(r,e,t,n){var s=r;do s.z===0&&(s.z=K(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,Ie(s)}function Ie(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 K(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 k(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 ke(r,e){return r.next.i!==e.i&&r.prev.i!==e.i&&!be(r,e)&&(z(r,e)&&z(e,r)&&Ae(r,e)&&(v(r.prev,r,e.prev)||v(r,e.prev,e))||U(r,e)&&v(r.prev,r,r.next)>0&&v(e.prev,e,e.next)>0)}function v(r,e,t){return(e.y-r.y)*(t.x-e.x)-(e.x-r.x)*(t.y-e.y)}function U(r,e){return r.x===e.x&&r.y===e.y}function ne(r,e,t,n){var s=J(v(r,e,t)),o=J(v(r,e,n)),i=J(v(t,n,r)),u=J(v(t,n,e));return!!(s!==o&&i!==u||s===0&&$(r,t,e)||o===0&&$(r,n,e)||i===0&&$(t,r,n)||u===0&&$(t,e,n))}function $(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 J(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&&ne(t,t.next,r,e))return!0;t=t.next}while(t!==r);return!1}function z(r,e){return v(r.prev,r,r.next)<0?v(r,e,r.next)>=0&&v(r,r.prev,e)>=0:v(r,e,r.prev)<0||v(r,r.next,e)<0}function Ae(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 se(r,e){var t=new Q(r.i,r.x,r.y),n=new Q(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 oe(r,e,t,n){var s=new Q(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 G(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 Q(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}j.deviation=function(r,e,t,n){var s=e&&e.length,o=s?e[0]*t:r.length,i=Math.abs(T(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(T(r,c,l,t))}var h=0;for(u=0;u<n.length;u+=3){var f=n[u]*t,p=n[u+1]*t,d=n[u+2]*t;h+=Math.abs((r[f]-r[d])*(r[p+1]-r[f+1])-(r[f]-r[p])*(r[d+1]-r[f+1]))}return i===0&&h===0?0:Math.abs((h-i)/i)};function T(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}j.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 ie=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 Ne(){var r=new ie(3);return ie!=Float32Array&&(r[0]=0,r[1]=0,r[2]=0),r}function ue(r){var e=r[0],t=r[1],n=r[2];return Math.hypot(e,t,n)}function Ve(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 ae(r,e){return r[0]*e[0]+r[1]*e[1]+r[2]*e[2]}(function(){var r=Ne();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 He(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 Ve(t,t),t}function ze(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 Ge(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 Re(r,e,t,n,s){const o=s[0],i=s[1],u=[r-n[0],e-n[1],t-n[2]];return[ae(o,u),ae(i,u)]}function Ye(r){return isNaN(ue(r))||ue(r)<1e-10}function Xe(r,e){const t=e===w.XYZM?4:e===w.XY?2:3;if(t===2)return r;const n=He(r,t);if(Ye(n))return[];const s=ze(r,t),o=Ge(n),i=[];for(let u=0;u<r.length;u+=t){const[a,c]=Re(r[u],r[u+1],r[u+2],s,o);i.push(a,c)}return i}function Ue(r,e,t=!1){const n=[],s=[],o=e===w.XYZM?4:e===w.XY?2:3;r.forEach((l,h)=>{h>0&&s.push(n.length/o),l.forEach((f,p)=>{p!==l.length-1&&n.push(...f.slice())})});const i=Xe(n,e);if(i.length===0)return new Float32Array([]);const u=j(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}x.BaseRecord=N,x.CoordType=w,x.DbfReader=A,x.DbfRecord=b,x.Feature=te,x.FeatureCollection=ee,x.FeatureReader=X,x.MultiPatchRecord=m,x.MultiPointRecord=O,x.PointRecord=Y,x.PolyLineRecord=C,x.PolygonRecord=S,x.ShapeReader=V,x.triangulate=Ue,Object.defineProperty(x,Symbol.toStringTag,{value:"Module"})});