!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(t="undefined"!=typeof globalThis?globalThis:t||self).geojsonvt=n()}(this,(function(){"use strict";function t(e,o,i,s){let l=s;const r=o+(i-o>>1);let u,f=i-o;const c=e[o],a=e[o+1],m=e[i],g=e[i+1];for(let t=o+3;tl)u=t,l=o;else if(o===l){const n=Math.abs(t-r);ns&&(u-o>3&&t(e,o,u,s),e[u+2]=l,i-u>3&&t(e,u,i,s))}function n(t,n,e,o,i,s){let l=i-e,r=s-o;if(0!==l||0!==r){const u=((t-e)*l+(n-o)*r)/(l*l+r*r);u>1?(e=i,o=s):u>0&&(e+=l*u,o+=r*u)}return l=t-e,r=n-o,l*l+r*r}function e(t,n,e,i){const s={id:null==t?null:t,type:n,geometry:e,tags:i,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0};if("Point"===n||"MultiPoint"===n||"LineString"===n)o(s,e);else if("Polygon"===n)o(s,e[0]);else if("MultiLineString"===n)for(const t of e)o(s,t);else if("MultiPolygon"===n)for(const t of e)o(s,t[0]);return s}function o(t,n){for(let e=0;e0&&(r+=i?(s*c-o*l)/2:Math.sqrt(Math.pow(o-s,2)+Math.pow(c-l,2))),s=o,l=c}const c=e.length-3;e[2]=1,t(e,0,c,o),e[c+2]=1,e.size=Math.abs(r),e.start=0,e.end=e.size}function r(t,n,e,o){for(let i=0;i1?1:e}function c(t,n,o,i,s,l,r,u){if(i/=n,l>=(o/=n)&&r=i)return null;const f=[];for(const n of t){const t=n.geometry;let l=n.type;const r=0===s?n.minX:n.minY,c=0===s?n.maxX:n.maxY;if(r>=o&&c=i)continue;let g=[];if("Point"===l||"MultiPoint"===l)a(t,g,o,i,s);else if("LineString"===l)m(t,g,o,i,s,!1,u.lineMetrics);else if("MultiLineString"===l)h(t,g,o,i,s,!1);else if("Polygon"===l)h(t,g,o,i,s,!0);else if("MultiPolygon"===l)for(const n of t){const t=[];h(n,t,o,i,s,!0),t.length&&g.push(t)}if(g.length){if(u.lineMetrics&&"LineString"===l){for(const t of g)f.push(e(n.id,l,t,n.tags));continue}"LineString"!==l&&"MultiLineString"!==l||(1===g.length?(l="LineString",g=g[0]):l="MultiLineString"),"Point"!==l&&"MultiPoint"!==l||(l=3===g.length?"Point":"MultiPoint"),f.push(e(n.id,l,g,n.tags))}}return f.length?f:null}function a(t,n,e,o,i){for(let s=0;s=e&&l<=o&&p(n,t[s],t[s+1],t[s+2])}}function m(t,n,e,o,i,s,l){let r=g(t);const u=0===i?d:x;let f,c,a=t.start;for(let m=0;me&&(c=u(r,h,d,M,y,e),l&&(r.start=a+f*c)):P>o?S=e&&(c=u(r,h,d,M,y,e),Y=!0),S>o&&P<=o&&(c=u(r,h,d,M,y,o),Y=!0),!s&&Y&&(l&&(r.end=a+f*c),n.push(r),r=g(t)),l&&(a+=f)}let m=t.length-3;const h=t[m],M=t[m+1],y=t[m+2],P=0===i?h:M;P>=e&&P<=o&&p(r,h,M,y),m=r.length-3,s&&m>=3&&(r[m]!==r[0]||r[m+1]!==r[1])&&p(r,r[0],r[1],r[2]),r.length&&n.push(r)}function g(t){const n=[];return n.size=t.size,n.start=t.start,n.end=t.end,n}function h(t,n,e,o,i,s){for(const l of t)m(l,n,e,o,i,s,!1)}function p(t,n,e,o){t.push(n,e,o)}function d(t,n,e,o,i,s){const l=(s-n)/(o-n);return p(t,s,e+(i-e)*l,1),l}function x(t,n,e,o,i,s){const l=(s-e)/(i-e);return p(t,n+(o-n)*l,s,1),l}function M(t,n){const o=[];for(let i=0;i0&&n.size<(i?l:o))return void(e.numPoints+=n.length/3);const r=[];for(let t=0;tl)&&(e.numSimplified++,r.push(n[t],n[t+1])),e.numPoints++;i&&function(t,n){let e=0;for(let n=0,o=t.length,i=o-2;n0===n)for(let n=0,e=t.length;n24)throw new Error("maxZoom should be in the 0-24 range");if(n.promoteId&&n.generateId)throw new Error("promoteId and generateId cannot be used together.");let o=function(t,n){const e=[];if("FeatureCollection"===t.type)for(let o=0;o1&&console.time("creation"),g=this.tiles[m]=Y(t,n,e,o,u),this.tileCoords.push({z:n,x:e,y:o}),f)){f>1&&(console.log("tile z%d-%d-%d (features: %d, points: %d, simplified: %d)",n,e,o,g.numFeatures,g.numPoints,g.numSimplified),console.timeEnd("creation"));const t=`z${n}`;this.stats[t]=(this.stats[t]||0)+1,this.total++}if(g.source=t,null==i){if(n===u.indexMaxZoom||g.numPoints<=u.indexMaxPoints)continue}else{if(n===u.maxZoom||n===i)continue;if(null!=i){const t=i-n;if(e!==s>>t||o!==l>>t)continue}}if(g.source=null,0===t.length)continue;f>1&&console.time("clipping");const h=.5*u.buffer/u.extent,p=.5-h,d=.5+h,x=1+h;let M=null,y=null,P=null,S=null,X=c(t,a,e-h,e+d,0,g.minX,g.maxX,u),L=c(t,a,e+p,e+x,0,g.minX,g.maxX,u);t=null,X&&(M=c(X,a,o-h,o+d,1,g.minY,g.maxY,u),y=c(X,a,o+p,o+x,1,g.minY,g.maxY,u),X=null),L&&(P=c(L,a,o-h,o+d,1,g.minY,g.maxY,u),S=c(L,a,o+p,o+x,1,g.minY,g.maxY,u),L=null),f>1&&console.timeEnd("clipping"),r.push(M||[],n+1,2*e,2*o),r.push(y||[],n+1,2*e,2*o+1),r.push(P||[],n+1,2*e+1,2*o),r.push(S||[],n+1,2*e+1,2*o+1)}}getTile(t,n,e){t=+t,n=+n,e=+e;const o=this.options,{extent:i,debug:s}=o;if(t<0||t>24)return null;const l=1<1&&console.log("drilling down to z%d-%d-%d",t,n,e);let u,f=t,c=n,a=e;for(;!u&&f>0;)f--,c>>=1,a>>=1,u=this.tiles[w(f,c,a)];return u&&u.source?(s>1&&(console.log("found parent tile z%d-%d-%d",f,c,a),console.time("drilling down")),this.splitTile(u.source,f,c,a,t,n,e),s>1&&console.timeEnd("drilling down"),this.tiles[r]?P(this.tiles[r],i):null):null}}function w(t,n,e){return 32*((1<