UNPKG

@maplat/transform

Version:

A JavaScript library that performs coordinate transformation between two plane coordinate systems using transformation definitions generated by Maplat.

2 lines (1 loc) 20.9 kB
(function(v,V){typeof exports=="object"&&typeof module<"u"?V(exports):typeof define=="function"&&define.amd?define(["exports"],V):(v=typeof globalThis<"u"?globalThis:v||self,V(v.maplat_transform={}))})(this,(function(v){"use strict";function V(e,t,r={}){const n={type:"Feature"};return(r.id===0||r.id)&&(n.id=r.id),r.bbox&&(n.bbox=r.bbox),n.properties=t||{},n.geometry=e,n}function W(e,t,r={}){if(!e)throw new Error("coordinates is required");if(!Array.isArray(e))throw new Error("coordinates must be an Array");if(e.length<2)throw new Error("coordinates must be at least 2 numbers long");if(!et(e[0])||!et(e[1]))throw new Error("coordinates must contain numbers");return V({type:"Point",coordinates:e},t,r)}function $(e,t,r={}){for(const s of e){if(s.length<4)throw new Error("Each LinearRing of a Polygon must have 4 or more Positions.");if(s[s.length-1].length!==s[0].length)throw new Error("First and last Position are not equivalent.");for(let i=0;i<s[s.length-1].length;i++)if(s[s.length-1][i]!==s[0][i])throw new Error("First and last Position are not equivalent.")}return V({type:"Polygon",coordinates:e},t,r)}function L(e,t={}){const r={type:"FeatureCollection"};return t.id&&(r.id=t.id),t.bbox&&(r.bbox=t.bbox),r.features=e,r}function et(e){return!isNaN(e)&&e!==null&&!Array.isArray(e)}function mt(e){if(!e)throw new Error("coord is required");if(!Array.isArray(e)){if(e.type==="Feature"&&e.geometry!==null&&e.geometry.type==="Point")return[...e.geometry.coordinates];if(e.type==="Point")return[...e.coordinates]}if(Array.isArray(e)&&e.length>=2&&!Array.isArray(e[0])&&!Array.isArray(e[1]))return[...e];throw new Error("coord must be GeoJSON Point or an Array of numbers")}function rt(e){if(Array.isArray(e))return e;if(e.type==="Feature"){if(e.geometry!==null)return e.geometry.coordinates}else if(e.coordinates)return e.coordinates;throw new Error("coords must be GeoJSON Feature, Geometry Object or an Array")}function yt(e){return e.type==="Feature"?e.geometry:e}const k=11102230246251565e-32,E=134217729,gt=(3+8*k)*k;function q(e,t,r,n,s){let i,o,u,d,f=t[0],g=n[0],a=0,l=0;g>f==g>-f?(i=f,f=t[++a]):(i=g,g=n[++l]);let y=0;if(a<e&&l<r)for(g>f==g>-f?(o=f+i,u=i-(o-f),f=t[++a]):(o=g+i,u=i-(o-g),g=n[++l]),i=o,u!==0&&(s[y++]=u);a<e&&l<r;)g>f==g>-f?(o=i+f,d=o-i,u=i-(o-d)+(f-d),f=t[++a]):(o=i+g,d=o-i,u=i-(o-d)+(g-d),g=n[++l]),i=o,u!==0&&(s[y++]=u);for(;a<e;)o=i+f,d=o-i,u=i-(o-d)+(f-d),f=t[++a],i=o,u!==0&&(s[y++]=u);for(;l<r;)o=i+g,d=o-i,u=i-(o-d)+(g-d),g=n[++l],i=o,u!==0&&(s[y++]=u);return(i!==0||y===0)&&(s[y++]=i),y}function bt(e,t){let r=t[0];for(let n=1;n<e;n++)r+=t[n];return r}function D(e){return new Float64Array(e)}const pt=(3+16*k)*k,wt=(2+12*k)*k,xt=(9+64*k)*k*k,U=D(4),nt=D(8),st=D(12),ot=D(16),S=D(4);function Mt(e,t,r,n,s,i,o){let u,d,f,g,a,l,y,b,m,h,c,p,x,M,T,w,_,A;const B=e-s,O=r-s,P=t-i,R=n-i;M=B*R,l=E*B,y=l-(l-B),b=B-y,l=E*R,m=l-(l-R),h=R-m,T=b*h-(M-y*m-b*m-y*h),w=P*O,l=E*P,y=l-(l-P),b=P-y,l=E*O,m=l-(l-O),h=O-m,_=b*h-(w-y*m-b*m-y*h),c=T-_,a=T-c,U[0]=T-(c+a)+(a-_),p=M+c,a=p-M,x=M-(p-a)+(c-a),c=x-w,a=x-c,U[1]=x-(c+a)+(a-w),A=p+c,a=A-p,U[2]=p-(A-a)+(c-a),U[3]=A;let N=bt(4,U),Y=wt*o;if(N>=Y||-N>=Y||(a=e-B,u=e-(B+a)+(a-s),a=r-O,f=r-(O+a)+(a-s),a=t-P,d=t-(P+a)+(a-i),a=n-R,g=n-(R+a)+(a-i),u===0&&d===0&&f===0&&g===0)||(Y=xt*o+gt*Math.abs(N),N+=B*g+R*u-(P*f+O*d),N>=Y||-N>=Y))return N;M=u*R,l=E*u,y=l-(l-u),b=u-y,l=E*R,m=l-(l-R),h=R-m,T=b*h-(M-y*m-b*m-y*h),w=d*O,l=E*d,y=l-(l-d),b=d-y,l=E*O,m=l-(l-O),h=O-m,_=b*h-(w-y*m-b*m-y*h),c=T-_,a=T-c,S[0]=T-(c+a)+(a-_),p=M+c,a=p-M,x=M-(p-a)+(c-a),c=x-w,a=x-c,S[1]=x-(c+a)+(a-w),A=p+c,a=A-p,S[2]=p-(A-a)+(c-a),S[3]=A;const Yt=q(4,U,4,S,nt);M=B*g,l=E*B,y=l-(l-B),b=B-y,l=E*g,m=l-(l-g),h=g-m,T=b*h-(M-y*m-b*m-y*h),w=P*f,l=E*P,y=l-(l-P),b=P-y,l=E*f,m=l-(l-f),h=f-m,_=b*h-(w-y*m-b*m-y*h),c=T-_,a=T-c,S[0]=T-(c+a)+(a-_),p=M+c,a=p-M,x=M-(p-a)+(c-a),c=x-w,a=x-c,S[1]=x-(c+a)+(a-w),A=p+c,a=A-p,S[2]=p-(A-a)+(c-a),S[3]=A;const $t=q(Yt,nt,4,S,st);M=u*g,l=E*u,y=l-(l-u),b=u-y,l=E*g,m=l-(l-g),h=g-m,T=b*h-(M-y*m-b*m-y*h),w=d*f,l=E*d,y=l-(l-d),b=d-y,l=E*f,m=l-(l-f),h=f-m,_=b*h-(w-y*m-b*m-y*h),c=T-_,a=T-c,S[0]=T-(c+a)+(a-_),p=M+c,a=p-M,x=M-(p-a)+(c-a),c=x-w,a=x-c,S[1]=x-(c+a)+(a-w),A=p+c,a=A-p,S[2]=p-(A-a)+(c-a),S[3]=A;const zt=q($t,st,4,S,ot);return ot[zt-1]}function _t(e,t,r,n,s,i){const o=(t-i)*(r-s),u=(e-s)*(n-i),d=o-u,f=Math.abs(o+u);return Math.abs(d)>=pt*f?d:-Mt(e,t,r,n,s,i,f)}function Tt(e,t){var r,n,s=0,i,o,u,d,f,g,a,l=e[0],y=e[1],b=t.length;for(r=0;r<b;r++){n=0;var m=t[r],h=m.length-1;if(g=m[0],g[0]!==m[h][0]&&g[1]!==m[h][1])throw new Error("First and last coordinates in a ring must be the same");for(o=g[0]-l,u=g[1]-y,n;n<h;n++){if(a=m[n+1],d=a[0]-l,f=a[1]-y,u===0&&f===0){if(d<=0&&o>=0||o<=0&&d>=0)return 0}else if(f>=0&&u<=0||f<=0&&u>=0){if(i=_t(o,d,u,f,0,0),i===0)return 0;(i>0&&f>0&&u<=0||i<0&&f<=0&&u>0)&&s++}g=a,u=f,o=d}}return s%2!==0}function F(e,t,r={}){if(!e)throw new Error("point is required");if(!t)throw new Error("polygon is required");const n=mt(e),s=yt(t),i=s.type,o=t.bbox;let u=s.coordinates;if(o&&vt(n,o)===!1)return!1;i==="Polygon"&&(u=[u]);for(var d=0;d<u.length;++d){const f=Tt(n,u[d]);if(f===0&&!r.ignoreBoundary)return!0;if(f)return!0}return!1}function vt(e,t){return t[0]<=e[0]&&t[1]<=e[1]&&t[2]>=e[0]&&t[3]>=e[1]}function j(e,t){for(let r=0;r<t.features.length;r++)if(F(e,t.features[r]))return t.features[r]}function it(e,t,r){const n=t.geometry.coordinates[0][0],s=t.geometry.coordinates[0][1],i=t.geometry.coordinates[0][2],o=e.geometry.coordinates,u=t.properties.a.geom,d=t.properties.b.geom,f=t.properties.c.geom,g=[s[0]-n[0],s[1]-n[1]],a=[i[0]-n[0],i[1]-n[1]],l=[o[0]-n[0],o[1]-n[1]],y=[d[0]-u[0],d[1]-u[1]],b=[f[0]-u[0],f[1]-u[1]];let m=(a[1]*l[0]-a[0]*l[1])/(g[0]*a[1]-g[1]*a[0]),h=(g[0]*l[1]-g[1]*l[0])/(g[0]*a[1]-g[1]*a[0]);if(r){const c=r[t.properties.a.index],p=r[t.properties.b.index],x=r[t.properties.c.index];let M;if(m<0||h<0||1-m-h<0){const T=m/(m+h),w=h/(m+h);M=m/p/(T/p+w/x),h=h/x/(T/p+w/x)}else M=m/p/(m/p+h/x+(1-m-h)/c),h=h/x/(m/p+h/x+(1-m-h)/c);m=M}return[m*y[0]+h*b[0]+u[0],m*y[1]+h*b[1]+u[1]]}function Et(e,t,r,n){const s=e.geometry.coordinates,i=r.geometry.coordinates,o=Math.atan2(s[0]-i[0],s[1]-i[1]),u=At(o,t[0]);if(u===void 0)throw new Error("Unable to determine vertex index");const d=t[1][u];return it(e,d.features[0],n)}function at(e,t,r,n,s,i,o,u){let d;if(o&&(d=j(e,L([o]))),!d)if(r){const f=e.geometry.coordinates,g=r.gridNum,a=r.xOrigin,l=r.yOrigin,y=r.xUnit,b=r.yUnit,m=r.gridCache,h=X(f[0],a,y,g),c=X(f[1],l,b,g),p=m[h]?m[h][c]?m[h][c]:[]:[],x=L(p.map(M=>t.features[M]));d=j(e,x)}else d=j(e,t);return u&&u(d),d?it(e,d,i):Et(e,n,s,i)}function X(e,t,r,n){let s=Math.floor((e-t)/r);return s<0&&(s=0),s>=n&&(s=n-1),s}function At(e,t){let r=ct(e-t[0]),n=Math.PI*2,s;for(let i=0;i<t.length;i++){const o=(i+1)%t.length,u=ct(e-t[o]),d=Math.min(Math.abs(r),Math.abs(u));r*u<=0&&d<n&&(n=d,s=i),r=u}return s}function ct(e,t=!1){const r=2*Math.PI,n=e-Math.floor(e/r)*r;return t?n:n>Math.PI?n-r:n}function St(e){const t=e.features;for(let r=0;r<t.length;r++){const n=t[r];`${n.properties.a.index}`.substring(0,1)==="b"&&`${n.properties.b.index}`.substring(0,1)==="b"?t[r]={geometry:{type:"Polygon",coordinates:[[n.geometry.coordinates[0][2],n.geometry.coordinates[0][0],n.geometry.coordinates[0][1],n.geometry.coordinates[0][2]]]},properties:{a:{geom:n.properties.c.geom,index:n.properties.c.index},b:{geom:n.properties.a.geom,index:n.properties.a.index},c:{geom:n.properties.b.geom,index:n.properties.b.index}},type:"Feature"}:`${n.properties.c.index}`.substring(0,1)==="b"&&`${n.properties.a.index}`.substring(0,1)==="b"&&(t[r]={geometry:{type:"Polygon",coordinates:[[n.geometry.coordinates[0][1],n.geometry.coordinates[0][2],n.geometry.coordinates[0][0],n.geometry.coordinates[0][1]]]},properties:{a:{geom:n.properties.b.geom,index:n.properties.b.index},b:{geom:n.properties.c.geom,index:n.properties.c.index},c:{geom:n.properties.a.geom,index:n.properties.a.index}},type:"Feature"})}return e}function Ct(e){const t=["a","b","c","a"].map(i=>e.properties[i].geom),r=e.geometry.coordinates[0],n=e.properties,s={a:{geom:r[0],index:n.a.index},b:{geom:r[1],index:n.b.index},c:{geom:r[2],index:n.c.index}};return $([t],s)}function It(e){const t=[0,1,2,0].map(n=>e[n][0][0]),r={a:{geom:e[0][0][1],index:e[0][1]},b:{geom:e[1][0][1],index:e[1][1]},c:{geom:e[2][0][1],index:e[2][1]}};return $([t],r)}function G(e,t,r,n,s,i=!1,o){const u=e.map(d=>{(!o||o<2.00703)&&(d=ut(d));const f=isFinite(d)?t[d]:d==="c"?n:(function(){const g=d.match(/^b(\d+)$/);if(g)return s[parseInt(g[1])];const a=d.match(/^e(\d+)$/);if(a)return r[parseInt(a[1])];throw new Error("Bad index value for indexesToTri")})();return i?[[f[1],f[0]],d]:[[f[0],f[1]],d]});return It(u)}function ut(e){return typeof e=="number"?e:e.replace(/^(c|e|b)(?:ent|dgeNode|box)(\d+)?$/,"$1$2")}function ft(e,t){return t&&t>=2.00703||Array.isArray(e[0])?e:e.map(r=>[r.illstNodes,r.mercNodes,r.startEnd])}const Z=2.00703;function Bt(e){return!!(e.version!==void 0||!e.tins&&e.points&&e.tins_points)}function Ot(e){return{points:e.points,pointsWeightBuffer:Rt(e),strictStatus:Xt(e),verticesParams:kt(e),centroid:Nt(e),edges:ft(e.edges||[]),edgeNodes:e.edgeNodes||[],tins:Wt(e),kinks:Vt(e.kinks_points),yaxisMode:e.yaxisMode??"invert",strictMode:e.strictMode??"auto",vertexMode:e.vertexMode,bounds:e.bounds,boundsPolygon:e.boundsPolygon,wh:e.wh,xy:e.xy??[0,0]}}function Pt(e){const t=Lt(e),r=t.tins;return{compiled:t,tins:r,points:Ut(r),strictStatus:t.strict_status,pointsWeightBuffer:t.weight_buffer,verticesParams:t.vertices_params,centroid:t.centroid,kinks:t.kinks}}function Rt(e){return!e.version||e.version<Z?["forw","bakw"].reduce((t,r)=>{const n=e.weight_buffer[r];return n&&(t[r]=Object.keys(n).reduce((s,i)=>{const o=ut(i);return s[o]=n[i],s},{})),t},{}):e.weight_buffer}function Xt(e){return e.strict_status?e.strict_status:e.kinks_points?"strict_error":e.tins_points.length===2?"loose":"strict"}function kt(e){const t={forw:[e.vertices_params[0]],bakw:[e.vertices_params[1]]};return t.forw[1]=ht(e,!1),t.bakw[1]=ht(e,!0),t}function ht(e,t){const r=e.vertices_points.length;return Array.from({length:r},(n,s)=>{const i=(s+1)%r,o=G(["c",`b${s}`,`b${i}`],e.points,e.edgeNodes||[],e.centroid_point,e.vertices_points,t,Z);return L([o])})}function Nt(e){return{forw:W(e.centroid_point[0],{target:{geom:e.centroid_point[1],index:"c"}}),bakw:W(e.centroid_point[1],{target:{geom:e.centroid_point[0],index:"c"}})}}function Wt(e){const t=e.tins_points.length===1?0:1;return{forw:L(e.tins_points[0].map(r=>G(r,e.points,e.edgeNodes||[],e.centroid_point,e.vertices_points,!1,e.version))),bakw:L(e.tins_points[t].map(r=>G(r,e.points,e.edgeNodes||[],e.centroid_point,e.vertices_points,!0,e.version)))}}function Vt(e){if(e)return{bakw:L(e.map(t=>W(t)))}}function Lt(e){return JSON.parse(JSON.stringify(e).replace('"cent"','"c"').replace(/"bbox(\d+)"/g,'"b$1"'))}function Ut(e){const t=[],r=e.forw.features;for(let n=0;n<r.length;n++){const s=r[n];["a","b","c"].forEach((i,o)=>{const u=s.geometry.coordinates[0][o],d=s.properties[i].geom,f=s.properties[i].index;typeof f=="number"&&(t[f]=[u,d])})}return t}const Ft=Z;class C{static VERTEX_PLAIN="plain";static VERTEX_BIRDEYE="birdeye";static MODE_STRICT="strict";static MODE_AUTO="auto";static MODE_LOOSE="loose";static STATUS_STRICT="strict";static STATUS_ERROR="strict_error";static STATUS_LOOSE="loose";static YAXIS_FOLLOW="follow";static YAXIS_INVERT="invert";points=[];pointsWeightBuffer;strict_status;vertices_params;centroid;edgeNodes;edges;tins;kinks;yaxisMode=C.YAXIS_INVERT;strictMode=C.MODE_AUTO;vertexMode=C.VERTEX_PLAIN;bounds;boundsPolygon;wh;xy;indexedTins;stateFull=!1;stateTriangle;stateBackward;priority;importance;xyBounds;mercBounds;constructor(){}setCompiled(t){if(Bt(t)){this.applyModernState(Ot(t));return}this.applyLegacyState(Pt(t))}applyModernState(t){this.points=t.points,this.pointsWeightBuffer=t.pointsWeightBuffer,this.strict_status=t.strictStatus,this.vertices_params=t.verticesParams,this.centroid=t.centroid,this.edges=t.edges,this.edgeNodes=t.edgeNodes||[],this.tins=t.tins,this.addIndexedTin(),this.kinks=t.kinks,this.yaxisMode=t.yaxisMode??C.YAXIS_INVERT,this.vertexMode=t.vertexMode??C.VERTEX_PLAIN,this.strictMode=t.strictMode??C.MODE_AUTO,t.bounds?(this.bounds=t.bounds,this.boundsPolygon=t.boundsPolygon,this.xy=t.xy,this.wh=t.wh):(this.bounds=void 0,this.boundsPolygon=void 0,this.xy=t.xy??[0,0],t.wh&&(this.wh=t.wh))}applyLegacyState(t){this.tins=t.tins,this.addIndexedTin(),this.strict_status=t.strictStatus,this.pointsWeightBuffer=t.pointsWeightBuffer,this.vertices_params=t.verticesParams,this.centroid=t.centroid,this.kinks=t.kinks,this.points=t.points}addIndexedTin(){const t=this.tins,r=t.forw,n=t.bakw,s=Math.ceil(Math.sqrt(r.features.length));if(s<3){this.indexedTins=void 0;return}let i=[],o=[];const u=r.features.map(m=>{let h=[];return rt(m)[0].map(c=>{i.length===0?i=[Array.from(c),Array.from(c)]:(c[0]<i[0][0]&&(i[0][0]=c[0]),c[0]>i[1][0]&&(i[1][0]=c[0]),c[1]<i[0][1]&&(i[0][1]=c[1]),c[1]>i[1][1]&&(i[1][1]=c[1])),h.length===0?h=[Array.from(c),Array.from(c)]:(c[0]<h[0][0]&&(h[0][0]=c[0]),c[0]>h[1][0]&&(h[1][0]=c[0]),c[1]<h[0][1]&&(h[0][1]=c[1]),c[1]>h[1][1]&&(h[1][1]=c[1]))}),h}),d=(i[1][0]-i[0][0])/s,f=(i[1][1]-i[0][1])/s,g=u.reduce((m,h,c)=>{const p=X(h[0][0],i[0][0],d,s),x=X(h[1][0],i[0][0],d,s),M=X(h[0][1],i[0][1],f,s),T=X(h[1][1],i[0][1],f,s);for(let w=p;w<=x;w++){m[w]||(m[w]=[]);for(let _=M;_<=T;_++)m[w][_]||(m[w][_]=[]),m[w][_].push(c)}return m},[]),a=n.features.map(m=>{let h=[];return rt(m)[0].map(c=>{o.length===0?o=[Array.from(c),Array.from(c)]:(c[0]<o[0][0]&&(o[0][0]=c[0]),c[0]>o[1][0]&&(o[1][0]=c[0]),c[1]<o[0][1]&&(o[0][1]=c[1]),c[1]>o[1][1]&&(o[1][1]=c[1])),h.length===0?h=[Array.from(c),Array.from(c)]:(c[0]<h[0][0]&&(h[0][0]=c[0]),c[0]>h[1][0]&&(h[1][0]=c[0]),c[1]<h[0][1]&&(h[0][1]=c[1]),c[1]>h[1][1]&&(h[1][1]=c[1]))}),h}),l=(o[1][0]-o[0][0])/s,y=(o[1][1]-o[0][1])/s,b=a.reduce((m,h,c)=>{const p=X(h[0][0],o[0][0],l,s),x=X(h[1][0],o[0][0],l,s),M=X(h[0][1],o[0][1],y,s),T=X(h[1][1],o[0][1],y,s);for(let w=p;w<=x;w++){m[w]||(m[w]=[]);for(let _=M;_<=T;_++)m[w][_]||(m[w][_]=[]),m[w][_].push(c)}return m},[]);this.indexedTins={forw:{gridNum:s,xOrigin:i[0][0],yOrigin:i[0][1],xUnit:d,yUnit:f,gridCache:g},bakw:{gridNum:s,xOrigin:o[0][0],yOrigin:o[0][1],xUnit:l,yUnit:y,gridCache:b}}}transform(t,r,n){if(!this.tins)throw new Error("setCompiled() must be called before transform()");if(r&&this.strict_status==C.STATUS_ERROR)throw new Error('Backward transform is not allowed if strict_status == "strict_error"');this.yaxisMode==C.YAXIS_FOLLOW&&r&&(t=[t[0],-1*t[1]]);const s=W(t);if(this.bounds&&!r&&!n&&!F(s,this.boundsPolygon))return!1;const i=r?this.tins.bakw:this.tins.forw,o=r?this.indexedTins.bakw:this.indexedTins.forw,u=r?this.vertices_params.bakw:this.vertices_params.forw,d=r?this.centroid.bakw:this.centroid.forw,f=r?this.pointsWeightBuffer.bakw:this.pointsWeightBuffer.forw;let g,a;this.stateFull&&(this.stateBackward==r?g=this.stateTriangle:(this.stateBackward=r,this.stateTriangle=void 0),a=y=>{this.stateTriangle=y});let l=at(s,i,o,u,d,f,g,a);if(this.bounds&&r&&!n){const y=W(l);if(!F(y,this.boundsPolygon))return!1}else this.yaxisMode==C.YAXIS_FOLLOW&&!r&&(l=[l[0],-1*l[1]]);return l}}const I=20037508342789244e-9,dt=[[0,0],[0,1],[1,0],[0,-1],[-1,0]];function J(e,t){return Math.floor(Math.min(e[0],e[1])/4)*I/128/Math.pow(2,t)}function lt(e,t){const r=[];for(let n=0;n<e.length;n++){const s=e[n],i=s[0]*Math.cos(t)-s[1]*Math.sin(t),o=s[0]*Math.sin(t)+s[1]*Math.cos(t);r.push([i,o])}return r}function K(e,t,r,n){const s=J(n,t);return lt(dt,r).map(u=>[u[0]*s+e[0],u[1]*s+e[1]])}function Q(e,t){const r=e[0],s=e.slice(1,5).map(y=>[y[0]-r[0],y[1]-r[1]]),i=[[0,1],[1,0],[0,-1],[-1,0]];let o=0,u=0,d=0;for(let y=0;y<4;y++){const b=s[y],m=i[y],h=Math.sqrt(Math.pow(b[0],2)+Math.pow(b[1],2));o+=h;const c=b[0]*m[1]-b[1]*m[0],p=Math.acos((b[0]*m[0]+b[1]*m[1])/h),x=c>0?-1*p:p;u+=Math.cos(x),d+=Math.sin(x)}const f=o/4,g=Math.atan2(d,u),a=Math.floor(Math.min(t[0],t[1])/4),l=Math.log(a*I/128/f)/Math.log(2);return{center:r,zoom:l,rotation:g}}function z(e,t){const r=e[0]*(2*I)/t-I,n=-1*(e[1]*(2*I)/t-I);return[r,n]}function H(e,t){const r=(e[0]+I)*t/(2*I),n=(-e[1]+I)*t/(2*I);return[r,n]}const tt=256;class Dt{mainTin=null;subTins=[];_maxxy=0;setMapData(t){const r=new C;if(r.setCompiled(t.compiled),this.mainTin=r,t.maxZoom!==void 0)this._maxxy=Math.pow(2,t.maxZoom)*tt;else if(t.compiled.wh){const n=Math.max(t.compiled.wh[0],t.compiled.wh[1]),s=Math.ceil(Math.log2(n/tt));this._maxxy=Math.pow(2,s)*tt}if(this.subTins=[],t.sub_maps)for(const n of t.sub_maps){const s=new C;s.setCompiled(n.compiled);const i=n.bounds??n.compiled.bounds;if(!i)throw new Error("SubMapData must have bounds or compiled.bounds to create xyBounds polygon");const o=[...i,i[0]],u=o.map(d=>{const f=s.transform(d,!1);if(!f)throw new Error("Failed to transform sub-map bounds to mercator");return f});this.subTins.push({tin:s,priority:n.priority,importance:n.importance,xyBounds:$([o]),mercBounds:$([u])})}}xy2Merc(t){const r=this.xy2MercWithLayer(t);return r?r[1]:!1}merc2Xy(t){const r=this.merc2XyWithLayer(t),n=r[0]||r[1];return n?n[1]:!1}xy2MercWithLayer(t){this._assertMapData();const r=this._getTinsSortedByPriority();for(let n=0;n<r.length;n++){const{index:s,isMain:i}=r[n];if(i||F(W(t),this.subTins[s-1].xyBounds)){const o=this._transformByIndex(t,s,!1);if(o===!1)continue;return[s,o]}}return!1}merc2XyWithLayer(t){return this._assertMapData(),this._getAllTinsWithIndex().map(({index:o,tin:u,isMain:d})=>{const f=this._transformByIndex(t,o,!0);return f===!1?[u,o]:d||F(W(f),this.subTins[o-1].xyBounds)?[u,o,f]:[u,o]}).sort((o,u)=>{const d=o[0].priority??0,f=u[0].priority??0;return d<f?1:-1}).reduce((o,u,d,f)=>{const g=u[0],a=u[1],l=u[2];if(!l)return o;for(let y=0;y<d;y++){const b=f[y][1],m=b===0;if(f[y][2]&&(m||F(W(l),this.subTins[b-1].xyBounds)))if(o.length){const h=!o[0],c=h?o[1][2]:o[0][2],p=g.importance??0,x=c.importance??0;return h?p<x?o:[void 0,[a,l,g]]:[...o.filter(w=>w!==void 0),[a,l,g]].sort((w,_)=>(w[2].importance??0)<(_[2].importance??0)?1:-1).slice(0,2)}else return[[a,l,g]]}return!o.length||!o[0]?[[a,l,g]]:(o.push([a,l,g]),o.sort((y,b)=>{const m=y[2].importance??0,h=b[2].importance??0;return m<h?1:-1}).filter((y,b)=>b<2))},[]).map(o=>{if(o)return[o[0],o[1]]})}mercs2SysCoords(t){this._assertMapData();const r=this.merc2XyWithLayer(t[0]);let n=!1;return r.map((s,i)=>{if(!s){n=!0;return}const o=s[0],u=s[1];return i!==0&&!n?[this.xy2SysCoordInternal(u)]:t.map((f,g)=>g===0?u:this._transformByIndex(f,o,!0)).map(f=>this.xy2SysCoordInternal(f))})}viewpoint2Mercs(t,r){this._assertMapData(),this._assertMaxxy();const s=K(t.center,t.zoom,t.rotation,r).map(f=>H(f,this._maxxy)),i=this.xy2MercWithLayer(s[0]);if(!i)throw new Error("viewpoint2Mercs: center point is out of bounds");const o=i[0],u=i[1];return s.map((f,g)=>{if(g===0)return u;const a=this._transformByIndex(f,o,!1);if(a===!1)throw new Error(`viewpoint2Mercs: point ${g} is out of bounds`);return a})}mercs2Viewpoint(t,r){this._assertMapData(),this._assertMaxxy();const n=this.merc2XyWithLayer(t[0]),s=n[0]||n[1];if(!s)throw new Error("mercs2Viewpoint: center point is out of bounds");const i=s[0],o=s[1],d=t.map((f,g)=>{if(g===0)return o;const a=this._transformByIndex(f,i,!0);if(a===!1)throw new Error(`mercs2Viewpoint: point ${g} is out of bounds`);return a}).map(f=>z(f,this._maxxy));return Q(d,r)}static zoom2Radius(t,r){return J(t,r)}static mercViewpoint2Mercs(t,r,n,s){return K(t,r,n,s)}static mercs2MercViewpoint(t,r){return Q(t,r)}static xy2SysCoord(t,r){return z(t,r)}static sysCoord2Xy(t,r){return H(t,r)}_assertMapData(){if(!this.mainTin)throw new Error("setMapData() must be called before transformation")}_assertMaxxy(){if(this._maxxy===0)throw new Error("MapData.maxZoom or compiled.wh must be set for viewpoint conversion (xy2SysCoord / sysCoord2Xy)")}getLayerTransform(t){if(t===0)return this.mainTin;const r=this.subTins[t-1];return r?r.tin:null}get layerCount(){return 1+this.subTins.length}get maxxy(){return this._maxxy}_getTinsSortedByPriority(){return this._getAllTinsWithIndex().sort((r,n)=>{const s=r.tin.priority??0,i=n.tin.priority??0;return s<i?1:-1})}_getAllTinsWithIndex(){const t=[{index:0,tin:this.mainTin,isMain:!0}];return this.subTins.forEach((r,n)=>{r.tin.priority=r.priority,r.tin.importance=r.importance,t.push({index:n+1,tin:r.tin,isMain:!1})}),t}_transformByIndex(t,r,n){if(r===0)return this.mainTin.transform(t,n);const s=this.subTins[r-1];return s?s.tin.transform(t,n,!0):!1}xy2SysCoordInternal(t){return z(t,this._maxxy)}}v.MERC_CROSSMATRIX=dt,v.MERC_MAX=I,v.MapTransform=Dt,v.Transform=C,v.counterTri=Ct,v.format_version=Ft,v.mercViewpoint2Mercs=K,v.mercs2MercViewpoint=Q,v.normalizeEdges=ft,v.rotateMatrix=lt,v.rotateVerticesTriangle=St,v.sysCoord2Xy=H,v.transformArr=at,v.xy2SysCoord=z,v.zoom2Radius=J,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"})}));