UNPKG

gis-vt

Version:

modified version of geojson-vt

3 lines (2 loc) 25.4 kB
var VT=function(v){"use strict";function tt(t,e){const n={};for(let o=0;o<t.length;o++){const s=t[o],c=e(s);Object.hasOwn(n,c)||(n[c]=[]),n[c].push(s)}return n}function nt(t,e,n){return n==null?Math.min(t,e):Math.min(Math.max(t,e),n)}function ut(t){return t==null}function A(t,e){if(!t)throw typeof e=="string"?new Error(e):e}function J({x:t,y:e,z:n}){return[n,e,t].join("/")}function Et(t){const[e,n,o]=t.split("/");return{z:+e,y:+n,x:+o}}function dt({x:t,y:e,z:n,wx:o,wy:s}){return n=n+1,e=e*2,t=t*2,[{z:n,x:t+0,y:e+0,wx:o,wy:s},{z:n,x:t+1,y:e+0,wx:o,wy:s},{z:n,x:t+0,y:e+1,wx:o,wy:s},{z:n,x:t+1,y:e+1,wx:o,wy:s}]}function ot(t,e){return dt(t).map(n=>H(e,n))}function ft({x:t,y:e,z:n,wx:o,wy:s}){return{z:n-1,x:Math.floor(t/2),y:Math.floor(e/2),wx:o,wy:s}}function Ft(t,e){return H(e,ft(t))}function ht({x:t,y:e,z:n,wx:o,wy:s},c,{wrapX:u,wrapY:l}){t=u?it(n,t,o):t,e=l?it(n,e,s):e,t+=c[0],e+=c[1];const a=2**n;return o=u?Math.floor(t/a):0,s=l?Math.floor(e/a):0,t=u?t-o*a:t,e=l?e-s*a:e,{x:t,y:e,z:n,wx:o,wy:s}}function Xt(t,e,n){return H(n,ht(t,e,n))}function Q(t,e){const n=2**t;return e+n&n-1}function it(t,e,n){return e+n*2**t}function $t(t,{x:e,y:n,z:o}){A(o>=0&&o===Math.floor(o),`can not get tile at z:${o}`);const{wrapX:s,wrapY:c}=t,u=2**o,l=s?Math.floor(e/u):0,a=c?Math.floor(n/u):0;return e=s?e-l*u:e,n=c?n-a*u:n,H(t,{x:e,y:n,z:o,wx:l,wy:a})}function H({lods:t,origin:e,tileSize:n,wrapX:o,wrapY:s},{x:c,y:u,z:l,wx:a,wy:r}){A(l>=0&&l===Math.floor(l),`can not get tile at z:${l}`),o?a!==0&&A(c===Q(l,c),`invalid x:${c}`):A(a===0,`invalid wx:${a}`),s?r!==0&&A(u===Q(l,u),`invalid y:${u}`):A(r===0,`invalid wy:${r}`);const{z:d,resolution:h,scale:b}=t[0],x=2**(d-l),i=h*x,m=b*x,f=i*n[0],g=i*n[1],p=e[0]+c*f,w=e[1]-u*g,M=p+f,y=w-g;return{id:[l,u,c,r,a].join("/"),key:J({x:c,y:u,z:l}),x:c,y:u,z:l,wx:a,wy:r,resolution:i,scale:m,bbox:[p,y,M,w]}}function Y(t){let e=1/0,n=-1/0,o=1/0,s=-1/0;for(let c of t)e=Math.min(e,c[0]),o=Math.min(o,c[1]),n=Math.max(n,c[0]),s=Math.max(s,c[1]);return[e,o,n,s]}function mt(t,e){return[t[0]+e[0],t[1]+e[1],t[2]+e[0],t[3]+e[1]]}function Yt(t){return[t[0],t[2]]}function zt(t){return[t[1],t[3]]}function O([t,e,n,o]){return{xmin:t,ymin:e,xmax:n,ymax:o,width:n-t,height:o-e,cx:(n+t)/2,cy:(o+e)/2}}function qt(t){return t&t-1?(t|=t>>1,t|=t>>2,t|=t>>4,t|=t>>8,t|=t>>16,t+1):t===0?1:t}function U(t,e,n){return(e-t)*n+t}function Wt(t,e,n,o){const s=Math.abs(n-t),c=t<n?1:-1,u=-Math.abs(o-e),l=e<o?1:-1;let a=s+u;const r=[];for(;;){r.push([t,e]);const d=2*a;if(d>=u){if(t==n)break;a=a+u,t=t+c}if(d<=s){if(e==o)break;a=a+s,e=e+l}}return r}function k(t){let e=1/0,n=-1/0,o=1/0,s=-1/0;return t.forEach(c=>{switch(c.type){case"point":e=Math.min(e,c.coordinates[0]),o=Math.min(o,c.coordinates[1]),n=Math.max(n,c.coordinates[0]),s=Math.max(s,c.coordinates[1]);break;case"polyline":case"polygon":e=Math.min(e,c.bbox[0]),o=Math.min(o,c.bbox[1]),n=Math.max(n,c.bbox[2]),s=Math.max(s,c.bbox[3])}}),[e,o,n,s]}function st(t,e){const n=t.length;if(e===0||n<=2)return t.map(()=>!0);const o=new Array(n).fill(!1),s=t[0],c=t[n-1],u=s[0]===c[0]&&s[1]===c[1];u&&(o[n-1]=!0);const l=[[0,u?n-2:n-1]];for(;l.length;){let a=0,r;const[d,h]=l.pop(),b=t[d],x=t[h];for(let i=d+1;i<h;i++){const m=t[i],f=Ot(m,b,x);f>=a&&(a=f,r=i)}a>=e?l.push([d,r],[r,h]):(o[d]=!0,o[h]=!0)}return o}function Ot(t,e,n){const[o,s]=t,[c,u]=e,[l,a]=n,r=c-l,d=u-a,h=r**2+d**2;if(h===0)return(o-c)**2+(s-u)**2;const b=o-c,x=s-u;return(r*x-b*d)**2/h}function _t(t,e){const{scale:n,z:o}=e[0];return n/2**(t-o)}function jt(t,e){const{scale:n,z:o}=e[0],s=e[e.length-1].scale;return t=nt(t,s,n),o+Math.log2(n/t)}const gt=.0254;function et(t){const{origin:e,worldSize:n,maxZoom:o,tileSize:s,wrapX:c,wrapY:u,dpi:l}=t,[a,r]=e,d=n/s,h=l/gt*d;return{minZoom:0,maxZoom:o,origin:e,tileSize:[s,s],lods:new Array(o+1).fill(0).map((b,x)=>{const i=1/2**x;return{z:x,resolution:d*i,scale:h*i}}),worldBBox:[a,r-t.worldSize,a+t.worldSize,r],wrapX:c,wrapY:u,dpi:l}}function kt(t=24,e=256,n=96){const o=200375083427892e-7;return et({origin:[-o,o],worldSize:o*2,maxZoom:t,tileSize:e,wrapX:!0,wrapY:!1,dpi:n})}function Vt(t,e,{tileSize:n=256,dpi:o=96,wrapX:s=!1,wrapY:c=!1}={}){const{width:u,height:l,cx:a,cy:r}=O(t),d=Math.max(u,l);return et({origin:[a-d/2,r+d/2],worldSize:d,maxZoom:e,tileSize:n,wrapX:s,wrapY:c,dpi:o})}function Kt(t,e,{tileSize:n=256,dpi:o=96,wrapX:s=!1,wrapY:c=!1}={}){const{width:u,height:l,cx:a,cy:r}=O(t),d=e/(o/gt),h=n*d,b=Math.max(u,l)/h,x=Math.log2(qt(Math.ceil(b))),i=2**x*h;return et({origin:[a-i/2,r+i/2],worldSize:i,maxZoom:x,tileSize:n,wrapX:s,wrapY:c,dpi:o})}function Z(t,e,n,o,s,c){const u=o==="x",l=u?Yt:zt,[a,r]=l(s);if(a>=n||r<=e)return null;if(a>=e&&r<=n)return{data:t,bbox:s};const d=[];for(const i of t)switch(i.type){case"point":h(i);break;case"polyline":b(i);break;case"polygon":x(i);break}if(!d.length)return null;return{data:d,bbox:k(d)};function h(i){const m=i.coordinates[u?0:1];m>=e&&m<=n&&d.push(i)}function b(i){const[m,f]=l(i.bbox);if(m>=e&&f<=n){d.push(i);return}else if(f<e||m>n)return;const g=c.keepLinePoint?Jt(i,e,n,u,c):Gt(i,e,n,u,c);d.push(...g)}function x(i){const{bbox:m,coordinates:f,id:g}=i,[p,w]=l(m);if(p>=e&&w<=n){d.push(i);return}else if(w<e||p>n)return;if(c.keepPolygonPoint){const M=f.map((y,S)=>Ut(y,e,n,u,i.vertexIndex?.[S],c)).filter(y=>y.ring.length>0).filter(Boolean);M.forEach(y=>{const S=y.ring.length;y.ring=yt(y.ring),S!==y.ring.length&&y.index.push(y.index[0])}),d.push({id:g,properties:i.properties,type:"polygon",multiPolygonIndex:i.multiPolygonIndex,coordinates:M.map(y=>y.ring),bbox:Y(M[0].ring),vertexIndex:M.map(y=>y.index),belongwx:i.belongwx,belongwy:i.belongwy})}else{const M=f.map(y=>Ht(y,e,n,u)).filter(y=>y.ring.length>0).map(y=>y.ring);d.push({id:g,properties:i.properties,type:"polygon",multiPolygonIndex:i.multiPolygonIndex,coordinates:M.map(y=>yt(y)),bbox:Y(M[0]),vertexIndex:void 0,belongwx:i.belongwx,belongwy:i.belongwy})}}}function Ht(t,e,n,o){const s=o?xt:bt;let c=[];for(let a=0,r=t.length-1;a<r;a++){const d=t[a],h=t[a+1],b=o?d[0]:d[1],x=o?h[0]:h[1];b<e?x>e&&s(c,d,h,e):b>n?x<n&&s(c,d,h,n):D(c,d),x<e&&b>=e&&s(c,d,h,e),x>n&&b<=n&&s(c,d,h,n)}const u=t[t.length-1],l=o?u[0]:u[1];return l>=e&&l<=n&&D(c,u),{ring:c}}function Ut(t,e,n,o,s,{keepPolygonPointIndex:c}){const u=[],l=c?[]:null;for(let h=0,b=t.length-1;h<b;h++){const x=t[h],i=t[h+1],m=o?x[0]:x[1],f=o?i[0]:i[1];m<e?f>e&&D(u,x)&&c&&l.push(s[h]):m>n?f<n&&D(u,x)&&c&&l.push(s[h]):D(u,x)&&c&&l.push(s[h]),f<e&&m>=e&&D(u,i)&&c&&l.push(s[h+1]),f>n&&m<=n&&D(u,i)&&c&&l.push(s[h+1])}const a=t.length-1,r=t[a],d=o?r[0]:r[1];return d>=e&&d<=n&&D(u,r)&&c&&l.push(s[a]),{ring:u,index:l}}function Gt(t,e,n,o,{calcLineDistance:s}){const c=t.distances,u=t.coordinates,l=[],a=o?xt:bt;let r=b();for(let x=0,i=u.length;x<i-1;x++){const m=u[x],f=u[x+1],g=o?m[0]:m[1],p=o?f[0]:f[1],w=c?.[x],M=c?.[x+1];let y=!1,S=null;g<e?p>e&&(S=a(r.coordinates,m,f,e),s&&r.distances.push(U(w,M,S))):g>n?p<n&&(S=a(r.coordinates,m,f,n),s&&r.distances.push(U(w,M,S))):(D(r.coordinates,m),S=0,s&&r.distances.push(w)),p<e&&g>=e&&(S=a(r.coordinates,m,f,e),s&&r.distances.push(U(w,M,S)),y=!0),p>n&&g<=n&&(S=a(r.coordinates,m,f,n),s&&r.distances.push(U(w,M,S)),y=!0),y&&(r.coordinates.length>=2&&l.push({id:t.id,properties:t.properties,type:"polyline",multiLineStringIndex:t.multiLineStringIndex,coordinates:r.coordinates,bbox:Y(r.coordinates),vertexIndex:void 0,distances:r.distances,totalDistance:t.totalDistance,belongwx:t.belongwx,belongwy:t.belongwy}),r=b(),S=null)}const d=u[u.length-1],h=o?d[0]:d[1];return h>=e&&h<=n&&(D(r.coordinates,d),s&&r.distances.push(c[u.length-1])),r.coordinates.length>=2&&l.push({id:t.id,properties:t.properties,type:"polyline",multiLineStringIndex:t.multiLineStringIndex,coordinates:r.coordinates,bbox:Y(r.coordinates),vertexIndex:void 0,distances:r.distances,totalDistance:t.totalDistance,belongwx:t.belongwx,belongwy:t.belongwy}),l;function b(){return{coordinates:[],distances:s?[]:null,totalDistance:null,vertexIndex:null}}}function Jt(t,e,n,o,{keepLinePointIndex:s,calcLineDistance:c}){const u=t.distances,l=t.coordinates,a=[];let r=x();for(let i=0,m=l.length;i<m-1;i++){const f=l[i],g=l[i+1],p=o?f[0]:f[1],w=o?g[0]:g[1],M=u?.[i],y=u?.[i+1];let S=!1;if(p<e){if(w>e){const P=D(r.coordinates,f);P&&s&&r.vertexIndex.push(t.vertexIndex[i]),P&&c&&r.distances.push(M)}}else if(p>n){if(w<n){const P=D(r.coordinates,f);P&&s&&r.vertexIndex.push(t.vertexIndex[i]),P&&c&&r.distances.push(M)}}else{const P=D(r.coordinates,f);P&&s&&r.vertexIndex.push(t.vertexIndex[i]),P&&c&&r.distances.push(M)}if(w<e&&p>=e){const P=D(r.coordinates,g);P&&s&&r.vertexIndex.push(t.vertexIndex[i+1]),P&&c&&r.distances.push(y),S=!0}if(w>n&&p<=n){const P=D(r.coordinates,g);P&&s&&r.vertexIndex.push(t.vertexIndex[i+1]),P&&c&&r.distances.push(y),S=!0}S&&(r.coordinates.length>=2&&a.push({id:t.id,properties:t.properties,type:"polyline",multiLineStringIndex:t.multiLineStringIndex,coordinates:r.coordinates,bbox:Y(r.coordinates),vertexIndex:r.vertexIndex,distances:r.distances,totalDistance:t.totalDistance,belongwx:t.belongwx,belongwy:t.belongwy}),r=x())}const d=l.length-1,h=l[d],b=o?h[0]:h[1];if(b>=e&&b<=n){const i=D(r.coordinates,h);i&&s&&r.vertexIndex.push(t.vertexIndex[d]),i&&c&&r.distances.push(u[d])}return r.coordinates.length>=2&&a.push({id:t.id,properties:t.properties,type:"polyline",multiLineStringIndex:t.multiLineStringIndex,coordinates:r.coordinates,bbox:Y(r.coordinates),vertexIndex:r.vertexIndex,distances:r.distances,totalDistance:t.totalDistance,belongwx:t.belongwx,belongwy:t.belongwy}),a;function x(){return{coordinates:[],distances:c?[]:null,totalDistance:null,vertexIndex:s?[]:null}}}function D(t,e){const n=t.length;return n===0||!Qt(e,t[n-1],.001)?(t.push(e),!0):!1}function xt(t,e,n,o){const s=(o-e[0])/(n[0]-e[0]);return D(t,[o,U(e[1],n[1],s)]),s}function bt(t,e,n,o){const s=(o-e[1])/(n[1]-e[1]);return D(t,[U(e[0],n[0],s),o]),s}function pt(t,e,n=Number.EPSILON){return Math.abs(t-e)<=n}function Qt(t,e,n=0){return pt(t[0],e[0],n)&&pt(t[1],e[1],n)}function yt(t){const[e,n]=t[0],[o,s]=t[t.length-1];return(e!==o||n!==s)&&t.push([e,n]),t}function rt(t,e){return t.map(wt).filter(Boolean).map(n=>{switch(n.geometry.type){case"Point":case"MultiPoint":return ee(n);case"LineString":case"MultiLineString":return ne(n,e);case"Polygon":case"MultiPolygon":return oe(n,e)}}).flat()}const te={MultiPoint:"Point",MultiLineString:"LineString",MultiPolygon:"Polygon"};function wt(t){const{geometry:e}=t;switch(e?.type){case"Point":return e.coordinates?.length?t:null;case"LineString":return e.coordinates?.length>=2?t:null;case"Polygon":return e.coordinates?.[0]?.length>=4?t:null;case"MultiPoint":case"MultiLineString":case"MultiPolygon":{const n=e.coordinates?.length;return n?n===1?wt({...t,geometry:{type:te[e.type],coordinates:e.coordinates[0]}}):t:null}default:return null}}function ee({geometry:t,properties:e,id:n}){if(t.type==="Point")return{id:n,properties:e,type:"point",multiPointIndex:void 0,coordinates:t.coordinates};if(t.type==="MultiPoint")return t.coordinates.map((o,s)=>({id:n,properties:e,type:"point",multiPointIndex:s,coordinates:o}))}function ne({geometry:t,properties:e,id:n},{calcLineDistance:o,keepLinePointIndex:s,multiLineDistanceStrategy:c,multiLineDistanceLink:u}){if(t.type==="LineString"){const{distances:l,totalDistance:a}=o?ie(t):{};return{id:n,properties:e,type:"polyline",multiLineStringIndex:void 0,coordinates:t.coordinates,bbox:Y(t.coordinates),vertexIndex:s?Array.from(t.coordinates.keys()):null,distances:l,totalDistance:a,belongwx:0,belongwy:0}}else if(t.type==="MultiLineString"){const{distances:l,totalDistances:a}=o?se(t,(e?.multiLineDistanceStrategy??c)==="stand-alone",e?.multiLineDistanceLink??u):{};return t.coordinates.map((r,d)=>({id:n,properties:e,type:"polyline",multiLineStringIndex:d,coordinates:r,bbox:Y(r),vertexIndex:s?Array.from(r.keys()):null,distances:l?.[d],totalDistance:a?.[d],belongwx:0,belongwy:0}))}}function oe({geometry:t,properties:e,id:n},{keepPolygonPointIndex:o}){if(t.type==="Polygon")return{id:n,properties:e,type:"polygon",multiPolygonIndex:void 0,coordinates:t.coordinates,bbox:Y(t.coordinates[0]),vertexIndex:o?St(t.coordinates):void 0,belongwx:0,belongwy:0};if(t.type==="MultiPolygon")return t.coordinates.map((s,c)=>({id:n,properties:e,type:"polygon",multiPolygonIndex:c,coordinates:s,bbox:Y(s[0]),vertexIndex:o?St(s):void 0,belongwx:0,belongwy:0}))}function St(t){return t.map((e,n)=>e.map((o,s)=>[n,s]))}function ie({coordinates:t}){const e=new Array(t.length).fill(0);for(let n=1,o=0;n<t.length;n++,o++)e[n]=e[o]+Math.hypot(t[n][0]-t[o][0],t[n][1]-t[o][1]);return{distances:e,totalDistance:e[e.length-1]}}function se({coordinates:t},e,n){let o=0;const s=[],c=new Array(t.length).fill(0);for(let u=0;u<t.length;u++){const l=t[u];if(e)o=0;else if(n&&u>0){const r=t[u-1],d=r[r.length-1],h=l[0];o+=Math.hypot(h[0]-d[0],h[1]-d[1])}const a=new Array(l.length).fill(o);for(let r=1,d=0;r<l.length;r++,d++)o=a[d]+Math.hypot(l[r][0]-l[d][0],l[r][1]-l[d][1]),a[r]=o;s.push(a),e&&(c[u]=o)}return e||c.fill(o),{distances:s,totalDistances:c}}const Mt=3**.5,Pt=1/Mt;function re(t,e){let n=t[0]/e,o=t[1]/e;n*=Pt,o*=-Pt;const s=Mt*o+1,c=Math.floor(s+n),u=s-n,l=2*n+1,a=(c+l)/3,r=(c+u)/3,d=Math.floor(a),h=Math.floor(r);return[d,-h]}function It(t,e,n,o,s){const{maxZoom:c,Q:u,simplifyAtMaxZoom:l}=o;let a;return n.z===c&&!l?a=u?G(t,O(n.bbox),u):t:a=(o.customSimplify||ce)(t,n,o,s),{version:1,key:n.key,tile:n,source:t,sourceBBox:e,sourcePointNums:Tt(t),simplified:a,simplifiedPointNums:Tt(a),hasDrillDown:!1}}function ce(t,e,n,o){const{point:s,polygon:c,polyline:u}=tt(t,a=>a.type),l=[];return c?.length&&l.push(...de(c,e,n,o)),u?.length&&l.push(...ue(u,e,n,o)),s?.length&&(o?l.push(...s):l.push(...le(s,e,n))),l}function le(t,e,n){n.Q&&(t=G(t,O(e.bbox),n.Q));const o=5*e.resolution,s={};for(let u of t){const[l,a]=re(u.coordinates,o);(s[l+","+a]??=[]).push(u)}const c=[];for(let u in s){const l=s[u],a=l.length,r=Math.ceil(.4**(n.maxZoom-e.z)*a),d=a/r>>0;for(let h=0;h<a;h+=d)c.push(l[h])}return c}const ae={};function ue(t,e,{Q:n,tileScheme:o,tolerance:s},c){const l=e.resolution,a=3*l;let{result:r=[],tiny:d=[]}=c?tt(t,b=>vt(b.bbox,a)?"tiny":"result"):{result:t,tiny:[]};const h=n?O(e.bbox):null;{const b=n?n/o.tileSize[0]:l,x=(s*b)**2;n&&(r=G(r,h,n)),x>0&&(r=r.map(i=>{if(i.coordinates.length<=2)return i;const m=st(i.coordinates,x),f=V(i.coordinates,m);return{id:i.id,properties:i.properties,type:"polyline",multiLineStringIndex:i.multiLineStringIndex,coordinates:f,vertexIndex:V(i.vertexIndex,m),bbox:Y(f),distances:V(i.distances,m),totalDistance:i.totalDistance,belongwx:i.belongwx,belongwy:i.belongwy}}))}if(!d.length)return r;{const b=1/l,{width:x,height:i,xmin:m,ymax:f}=O(k(d)),g=Math.ceil(x*b),p=Math.ceil(i*b),w=g*p,M=new Uint8Array(w);let y=0,S=[];for(let{id:P,coordinates:N,bbox:q,properties:C,totalDistance:T,distances:E,multiLineStringIndex:F,vertexIndex:I,belongwx:_,belongwy:W}of d){const L=N[0],R=N[N.length-1],B=O(q);let X=!1;if(B.width<l&&B.height<l){const j=(B.cx-m)*b>>0,$=(f-B.cy)*b>>0,K=j+$*g;M[K]===0&&(M[K]=1,y+=1,X=!0)}else{const j=(L[0]-m)*b>>0,$=(f-L[1])*b>>0,K=(R[0]-m)*b>>0,Zt=(f-R[1])*b>>0,lt=Math.min(j,K),at=Math.min($,Zt),Dt=j-lt,Bt=$-at,At=K-lt,Nt=Zt-at,xe=[Dt,Bt,At,Nt].join(",");(ae[xe]??=Wt(Dt,Bt,At,Nt)).forEach(Rt=>{const Ct=Rt[0]+lt+(Rt[1]+at)*g;M[Ct]===0&&(y++,M[Ct]=1,X=!0)})}if(X&&(S.push({id:P,properties:C,type:"polyline",multiLineStringIndex:F,coordinates:[L,R],vertexIndex:I?[I[0],I[I.length-1]]:null,bbox:Y([L,R]),distances:E?[E[0],E[E.length-1]]:null,totalDistance:T,belongwx:_,belongwy:W}),y===w))break}n&&(S=G(S,h,n)),r.push(...S)}return r}function de(t,e,{Q:n,tileScheme:o,keepPolygonPointIndex:s,tolerance:c},u){const a=e.resolution,r=2*a;let{result:d=[],tiny:h=[]}=u?tt(t,x=>vt(x.bbox,r)?"tiny":"result"):{result:t,tiny:[]};const b=n?O(e.bbox):null;{const x=n?n/o.tileSize[0]:a,i=(c*x)**2;n&&(d=G(d,b,n)),i>0&&(d=d.map(m=>{const{id:f,coordinates:g,properties:p,vertexIndex:w,multiPolygonIndex:M,belongwx:y,belongwy:S}=m,P=[],N=s?[]:null;for(let q=0;q<g.length;q++){const C=g[q];if(C.length<4)break;const T=st(C,i);if(T.reduce((F,I)=>F+=I?1:0,0)<4)break;P.push(C.filter((F,I)=>T[I])),s&&N.push(w[q].filter((F,I)=>T[I]))}if(!P.length){h.push(m);return}return{id:f,properties:p,type:"polygon",multiPolygonIndex:M,coordinates:P,bbox:Y(P[0]),vertexIndex:N,belongwx:y,belongwy:S}}).filter(Boolean))}if(!h.length)return d;{const x=1/r,{xmin:i,ymax:m,width:f,height:g}=O(k(h)),p=Math.ceil(f*x),w=Math.ceil(g*x),M=p*w;let y=0;const S=new Uint8Array(M);let P=[];for(let{bbox:N,id:q,properties:C,multiPolygonIndex:T,vertexIndex:E,belongwx:F,belongwy:I}of h){const _=(N[0]+N[2])/2,W=(N[1]+N[3])/2,L=(_-i)*x>>0,R=(m-W)*x>>0,B=L+R*p;if(S[B]===1)continue;S[B]=1;const X=[[L,R],[L,R+1],[L+1,R+1],[L+1,R],[L,R]].map(([j,$])=>[i+j/x,m-$/x]);if(P.push({id:q,properties:C,type:"polygon",multiPolygonIndex:T,coordinates:[X],bbox:Y(X),vertexIndex:E?[[...E[0].slice(0,3),E[0][0]]]:null,belongwx:F,belongwy:I}),y===M)break}n&&(P=G(P,b,n)),d.push(...P)}return d}function vt(t,e){return t[2]-t[0]<e&&t[3]-t[1]<e}function Tt(t){let e=0;for(let n of t)switch(n.type){case"polyline":e+=n.coordinates.length;break;case"polygon":n.coordinates.forEach(o=>e+=o.length);break;case"point":e+=1}return e}function G(t,{xmin:e,width:n,height:o,ymax:s},c){const u=c/n,l=c/o;if(Array.isArray(t))return t.map(a);return a(t);function a(i){switch(i.type){case"point":return{...i,coordinates:b(i.coordinates)};case"polyline":return d(i);case"polygon":return r(i)}}function r(i){const m=i.coordinates.map(g=>g.map(b)),f=m.map(g=>h(g));return{...i,coordinates:m,vertexIndex:i.vertexIndex?i.vertexIndex.map((g,p)=>V(g,f[p])):null,bbox:x(i.bbox)}}function d(i){const m=i.coordinates.map(b),f=h(m);return{...i,coordinates:V(m,f),vertexIndex:V(i.vertexIndex,f),distances:V(i.distances,f),bbox:x(i.bbox)}}function h(i){const m=i.length,f=new Array(m).fill(!1);f[0]=!0;for(let g=1;g<m;g++){const[p,w]=i[g-1],[M,y]=i[g];f[g]=p!==M||w!==y}return f}function b(i){return[Math.round((i[0]-e)*u),Math.round((s-i[1])*l)]}function x([i,m,f,g]){const p=b([i,g]),w=b([f,m]);return[p,w].flat()}}function V(t,e){return t?t.filter((n,o)=>e[o]):null}function Lt(t,e){const{tileScheme:n,buffer:o}=e,{worldBBox:s,wrapX:c,wrapY:u}=n,l=k(t);if(!c&&!u)return{data:t,bbox:l};const{xmin:a,xmax:r,ymin:d,ymax:h,width:b,height:x}=O(s),i=o*b;if(c){const f=Z(t,a-i,r+i,"x",l,e),g=Z(t,a-b-i,a+i,"x",l,e),p=Z(t,r-i,r+b+i,"x",l,e);t=[...f.data],g&&t.push(...m(g.data,1,0)),p&&t.push(...m(p.data,-1,0))}if(u){const f=Z(t,d-i,h+i,"y",l,e),g=Z(t,h-i,h+x+i,"y",l,e),p=Z(t,d-x-i,d+i,"y",l,e);t=[...f.data],g&&t.push(...m(g.data,0,1)),p&&t.push(...m(p.data,0,-1))}return{data:t,bbox:k(t)};function m(f,g,p){return fe(f,[g*b,p*-x]),f.forEach(w=>{w.type!=="point"&&(w.belongwx=g,w.belongwy=p)}),f}}function fe(t,e){return t.forEach(n=>{switch(n.type){case"point":n.coordinates=ct(n.coordinates,e);break;case"polyline":{n.coordinates=n.coordinates.map(o=>ct(o,e)),n.bbox=mt(n.bbox,e);break}case"polygon":n.coordinates=n.coordinates.map(o=>o.map(s=>ct(s,e))),n.bbox=mt(n.bbox,e)}}),t}function ct(t,e){return[t[0]+e[0],t[1]+e[1]]}const z={buffer:4/256,indexMaxPoints:1e5,tolerance:1,simplifyAtMaxZoom:!1,keepLinePoint:!1,keepLinePointIndex:!1,calcLineDistance:!1,keepPolygonPoint:!1,keepPolygonPointIndex:!1,multiLineDistanceStrategy:"stand-alone",multiLineDistanceLink:!1,static:!0};function he(t){A(!!t.tileScheme,"tileScheme not exist");const e=t.Q;e&&(A(typeof e=="number"&&e===Math.floor(e),"Q must be positive integer"),A(e>=t.tileScheme.tileSize[0],"Q must >= tileScheme.tileSize"));const n=Math.max(t.minZoom??0,t.tileScheme.minZoom),o=Math.min(t.maxZoom??1/0,t.tileScheme.maxZoom);t.buffer&&A(t.buffer>=0,"vector tile buffer must >= 0");const s=t.keepPolygonPoint??z.keepPolygonPoint,c=t.keepPolygonPointIndex??z.keepPolygonPointIndex;c&&A(s===!0,"when keepPolygonPointIndex is true, keepPolygonPoint must be true");const u=t.keepLinePoint??z.keepLinePoint,l=t.keepLinePointIndex??z.keepLinePointIndex;return l&&A(u===!0,"when keepLinePointIndex is true, keepLinePoint must be true"),{static:t.static??z.static,Q:e,minZoom:n,maxZoom:o,tileScheme:t.tileScheme,indexMaxZoom:nt(t.indexMaxZoom??o/3>>0,n,o),indexMaxPoints:t.indexMaxPoints??z.indexMaxPoints,tolerance:t.tolerance??z.tolerance,buffer:nt(t.buffer??z.buffer,0,.5),debug:t.debug??!1,keepPolygonPoint:s,keepPolygonPointIndex:c,simplifyAtMaxZoom:t.simplifyAtMaxZoom??z.simplifyAtMaxZoom,calcLineDistance:t.calcLineDistance??z.calcLineDistance,multiLineDistanceStrategy:t.multiLineDistanceStrategy??z.multiLineDistanceStrategy,multiLineDistanceLink:t.multiLineDistanceLink??z.multiLineDistanceLink,keepLinePoint:u,keepLinePointIndex:l,customSimplify:t.customSimplify}}function me(t){A(!!t.source?.length,"features is empty");const e=he(t),n=new Map,o=performance.now();e.static||ge(t.source),s(Lt(rt(t.source,e),e),0,0,0,null,null,null),e.debug&&console.log(`生成初始切片${(performance.now()-o).toFixed(2)}ms`);function s(a,r,d,h,b,x,i){const m=[{src:a,tile:H(e.tileScheme,{z:r,x:d,y:h,wx:0,wy:0})}];for(;m.length;){const{src:f,tile:g}=m.pop(),{z:p,x:w,y:M,key:y}=g;if(!f?.data?.length||p<e.minZoom||p>e.maxZoom)continue;let S=n.get(y);if(!S){const $=performance.now();S=It(f.data,f.bbox,g,e,!1),n.set(y,S),e.debug&&console.log([`%c ${y}`,`%c ${(performance.now()-$).toFixed(2)} ms`,`%c points: ${S.sourcePointNums}`,`%c simplify: ${S.simplifiedPointNums}`].join(" ")+" ","background:#2cc9ff;color:white","background:#ff9800","background:darkgreen;color:white","background:skyblue")}if(b==null){if(p===e.indexMaxZoom||S.sourcePointNums<=e.indexMaxPoints)continue}else{if(p===e.maxZoom||p===b){p===e.maxZoom&&(S.source=null);continue}{const $=b-p;if(w!==x>>$||M!==i>>$)continue}}S.source=null,S.sourceBBox=null;const{xmin:P,ymin:N,xmax:q,ymax:C,width:T,cx:E,cy:F}=O(g.bbox),I=e.buffer*T;let _=null,W=null,L=null,R=null,B=Z(f.data,P-I,E+I,"x",f.bbox,e),X=Z(f.data,E-I,q+I,"x",f.bbox,e);B&&(_=Z(B.data,F-I,C+I,"y",B.bbox,e),W=Z(B.data,N-I,F+I,"y",B.bbox,e),B=null),X&&(L=Z(X.data,F-I,C+I,"y",X.bbox,e),R=Z(X.data,N-I,F+I,"y",X.bbox,e),X=null),S.hasDrillDown=!0;const j=ot(g,e.tileScheme);[_,L,W,R].forEach(($,K)=>{$&&m.push({src:{data:$.data,bbox:$.bbox},tile:j[K]})})}}function c(a,r,d){if(a<0||a>e.tileScheme.maxZoom)return null;e.tileScheme.wrapX&&(r=Q(a,r)),e.tileScheme.wrapY&&(d=Q(a,d));const h=J({z:a,x:r,y:d});if(n.has(h))return n.get(h);let b=a,x=r,i=d,m;for(;!m&&b>0;)b--,x=x>>1,i=i>>1,m=n.get(J({z:b,y:i,x}));return m?(s({data:m.source,bbox:m.sourceBBox},b,x,i,a,r,d),n.get(h)):null}function u({adds:a=[],updates:r=[],removes:d=[]}){A(!e.static,"static vector tile can not modify");const h=new Set,b=new Set(d),x=n.keys().filter(m=>l(n.get(m).tile.z));if(r.length){const{news:m=[],propOnly:f=[]}=tt(r.filter(g=>!ut(g.id)&&(g.geometry||g.properties)),g=>g.geometry?"news":"propOnly");if(m.forEach(g=>b.add(g.id)),a.push(...m),f.length){const g=f.reduce((p,w)=>(p[w.id]=w.properties,p),{});for(let p of x){const w=n.get(p);let M=!1;w.source?.forEach(y=>{g[y.id]&&(y.properties=g[y.id],M=!0)}),w.simplified.forEach(y=>{g[y.id]&&(y.properties=g[y.id],M=!0)}),M&&h.add(p)}}}if(b.size)for(let m of x){const f=n.get(m);let g=!1;if(f.source?.length){const w=f.source.length;f.source=f.source.filter(M=>!b.has(M.id)),g=w!==f.source.length}const p=f.simplified.length;f.simplified=f.simplified.filter(w=>!b.has(w.id)),g=g||p!==f.simplified.length,g&&h.add(m)}if(a.length){const m=[{src:Lt(rt(a,e),e),tile:n.get(J({x:0,y:0,z:0})).tile}];for(;m.length;){const{src:f,tile:g}=m.pop();h.add(g.key);const p=n.get(g.key),w=It(f.data,f.bbox,g,e,!0);if(p){if(l(p.tile.z)&&p.simplified.push(...w.simplified),!p.hasDrillDown&&p.tile.z<e.maxZoom){p.source.push(...w.source);continue}}else n.set(g.key,w);if(!f.data.length||g.z===e.tileScheme.maxZoom)continue;const{xmin:M,ymin:y,xmax:S,ymax:P,width:N,cx:q,cy:C}=O(g.bbox),T=e.buffer*N;let E=null,F=null,I=null,_=null,W=Z(f.data,M-T,q+T,"x",f.bbox,e),L=Z(f.data,q-T,S+T,"x",f.bbox,e);W&&(E=Z(W.data,C-T,P+T,"y",W.bbox,e),F=Z(W.data,y-T,C+T,"y",W.bbox,e),W=null),L&&(I=Z(L.data,C-T,P+T,"y",L.bbox,e),_=Z(L.data,y-T,C+T,"y",L.bbox,e),L=null);const R=ot(g,e.tileScheme);[E,I,F,_].forEach((B,X)=>{B&&m.push({src:{data:B.data,bbox:B.bbox},tile:R[X]})})}}const i=Array.from(h.values());return i.forEach(m=>{const f=n.get(m);f.version++,!f.hasDrillDown&&f.tile.z>e.maxZoom&&(f.sourceBBox=k(f.source))}),i}function l(a){return a>=e.minZoom&&a<=e.maxZoom}return{options:e,tiles:n,getTileData({z:a,x:r,y:d}){const h=c(a,r,d);return h?.simplified?.length?{version:h.version,Q:e.Q,features:h.simplified,tile:h.tile}:null},applyEdits:u,destroy(){n.clear()}}}function ge(t){const e=new Set;for(let n of t)A(!ut(n.id),`invalid vector tile source feature id:${n.id}`),A(!e.has(n.id),`editable vector tile source features must have diff id, duplicate id:${n.id}`),e.add(n.id)}return v.DefaultVTOpts=z,v.createTile=H,v.createTileScheme=et,v.createTileSchemeFromBBoxAndScale=Kt,v.createTileSchemeFromBBoxAndZoom=Vt,v.createTileSchemeWebMercator=kt,v.createVectorTile=me,v.douglasSimplify=st,v.getParentXYZW=ft,v.getTileChildren=ot,v.getTileChildrenXYZW=dt,v.getTileKey=J,v.getTileNeighbor=Xt,v.getTileNeighborXYZW=ht,v.getTileParent=Ft,v.resolveTileFromXYZ=$t,v.resolveVFeaturesBBox=k,v.scaleToZoom=jt,v.tileKeyToXYZ=Et,v.toVFeatures=rt,v.unwrapTileIndex=it,v.wrapTileIndex=Q,v.zoomToScale=_t,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"}),v}({}); //# sourceMappingURL=index.iife.js.map