@maplat/transform 0.5.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +162 -0
- package/README.md +162 -0
- package/dist/index.d.ts +217 -0
- package/dist/maplat_transform.js +774 -468
- package/dist/maplat_transform.umd.js +1 -1
- package/package.json +7 -5
- package/src/constants.ts +8 -0
- package/src/coord-utils.ts +27 -0
- package/src/index.ts +17 -379
- package/src/map-transform.ts +473 -0
- package/src/transform.ts +338 -0
- package/src/types.ts +40 -0
- package/src/viewpoint.ts +104 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(O,k){typeof exports=="object"&&typeof module<"u"?k(exports):typeof define=="function"&&define.amd?define(["exports"],k):(O=typeof globalThis<"u"?globalThis:O||self,k(O.maplat_transform={}))})(this,(function(O){"use strict";var Wt=Object.defineProperty;var Xt=(O,k,R)=>k in O?Wt(O,k,{enumerable:!0,configurable:!0,writable:!0,value:R}):O[k]=R;var x=(O,k,R)=>Xt(O,typeof k!="symbol"?k+"":k,R);function k(t,e,r={}){const n={type:"Feature"};return(r.id===0||r.id)&&(n.id=r.id),r.bbox&&(n.bbox=r.bbox),n.properties=e||{},n.geometry=t,n}function R(t,e,r={}){if(!t)throw new Error("coordinates is required");if(!Array.isArray(t))throw new Error("coordinates must be an Array");if(t.length<2)throw new Error("coordinates must be at least 2 numbers long");if(!K(t[0])||!K(t[1]))throw new Error("coordinates must contain numbers");return k({type:"Point",coordinates:t},e,r)}function J(t,e,r={}){for(const c of t){if(c.length<4)throw new Error("Each LinearRing of a Polygon must have 4 or more Positions.");if(c[c.length-1].length!==c[0].length)throw new Error("First and last Position are not equivalent.");for(let i=0;i<c[c.length-1].length;i++)if(c[c.length-1][i]!==c[0][i])throw new Error("First and last Position are not equivalent.")}return k({type:"Polygon",coordinates:t},e,r)}function X(t,e={}){const r={type:"FeatureCollection"};return e.id&&(r.id=e.id),e.bbox&&(r.bbox=e.bbox),r.features=t,r}function K(t){return!isNaN(t)&&t!==null&&!Array.isArray(t)}function at(t){if(!t)throw new Error("coord is required");if(!Array.isArray(t)){if(t.type==="Feature"&&t.geometry!==null&&t.geometry.type==="Point")return[...t.geometry.coordinates];if(t.type==="Point")return[...t.coordinates]}if(Array.isArray(t)&&t.length>=2&&!Array.isArray(t[0])&&!Array.isArray(t[1]))return[...t];throw new Error("coord must be GeoJSON Point or an Array of numbers")}function Q(t){if(Array.isArray(t))return t;if(t.type==="Feature"){if(t.geometry!==null)return t.geometry.coordinates}else if(t.coordinates)return t.coordinates;throw new Error("coords must be GeoJSON Feature, Geometry Object or an Array")}function ct(t){return t.type==="Feature"?t.geometry:t}const F=11102230246251565e-32,P=134217729,ft=(3+8*F)*F;function $(t,e,r,n,c){let i,f,g,h,l=e[0],y=n[0],o=0,u=0;y>l==y>-l?(i=l,l=e[++o]):(i=y,y=n[++u]);let b=0;if(o<t&&u<r)for(y>l==y>-l?(f=l+i,g=i-(f-l),l=e[++o]):(f=y+i,g=i-(f-y),y=n[++u]),i=f,g!==0&&(c[b++]=g);o<t&&u<r;)y>l==y>-l?(f=i+l,h=f-i,g=i-(f-h)+(l-h),l=e[++o]):(f=i+y,h=f-i,g=i-(f-h)+(y-h),y=n[++u]),i=f,g!==0&&(c[b++]=g);for(;o<t;)f=i+l,h=f-i,g=i-(f-h)+(l-h),l=e[++o],i=f,g!==0&&(c[b++]=g);for(;u<r;)f=i+y,h=f-i,g=i-(f-h)+(y-h),y=n[++u],i=f,g!==0&&(c[b++]=g);return(i!==0||b===0)&&(c[b++]=i),b}function ut(t,e){let r=e[0];for(let n=1;n<t;n++)r+=e[n];return r}function Y(t){return new Float64Array(t)}const dt=(3+16*F)*F,ht=(2+12*F)*F,gt=(9+64*F)*F*F,V=Y(4),H=Y(8),Z=Y(12),tt=Y(16),T=Y(4);function lt(t,e,r,n,c,i,f){let g,h,l,y,o,u,b,p,d,a,s,m,M,v,_,w,A,S;const I=t-c,N=r-c,B=e-i,C=n-i;v=I*C,u=P*I,b=u-(u-I),p=I-b,u=P*C,d=u-(u-C),a=C-d,_=p*a-(v-b*d-p*d-b*a),w=B*N,u=P*B,b=u-(u-B),p=B-b,u=P*N,d=u-(u-N),a=N-d,A=p*a-(w-b*d-p*d-b*a),s=_-A,o=_-s,V[0]=_-(s+o)+(o-A),m=v+s,o=m-v,M=v-(m-o)+(s-o),s=M-w,o=M-s,V[1]=M-(s+o)+(o-w),S=m+s,o=S-m,V[2]=m-(S-o)+(s-o),V[3]=S;let W=ut(4,V),L=ht*f;if(W>=L||-W>=L||(o=t-I,g=t-(I+o)+(o-c),o=r-N,l=r-(N+o)+(o-c),o=e-B,h=e-(B+o)+(o-i),o=n-C,y=n-(C+o)+(o-i),g===0&&h===0&&l===0&&y===0)||(L=gt*f+ft*Math.abs(W),W+=I*y+C*g-(B*l+N*h),W>=L||-W>=L))return W;v=g*C,u=P*g,b=u-(u-g),p=g-b,u=P*C,d=u-(u-C),a=C-d,_=p*a-(v-b*d-p*d-b*a),w=h*N,u=P*h,b=u-(u-h),p=h-b,u=P*N,d=u-(u-N),a=N-d,A=p*a-(w-b*d-p*d-b*a),s=_-A,o=_-s,T[0]=_-(s+o)+(o-A),m=v+s,o=m-v,M=v-(m-o)+(s-o),s=M-w,o=M-s,T[1]=M-(s+o)+(o-w),S=m+s,o=S-m,T[2]=m-(S-o)+(s-o),T[3]=S;const Rt=$(4,V,4,T,H);v=I*y,u=P*I,b=u-(u-I),p=I-b,u=P*y,d=u-(u-y),a=y-d,_=p*a-(v-b*d-p*d-b*a),w=B*l,u=P*B,b=u-(u-B),p=B-b,u=P*l,d=u-(u-l),a=l-d,A=p*a-(w-b*d-p*d-b*a),s=_-A,o=_-s,T[0]=_-(s+o)+(o-A),m=v+s,o=m-v,M=v-(m-o)+(s-o),s=M-w,o=M-s,T[1]=M-(s+o)+(o-w),S=m+s,o=S-m,T[2]=m-(S-o)+(s-o),T[3]=S;const Ut=$(Rt,H,4,T,Z);v=g*y,u=P*g,b=u-(u-g),p=g-b,u=P*y,d=u-(u-y),a=y-d,_=p*a-(v-b*d-p*d-b*a),w=h*l,u=P*h,b=u-(u-h),p=h-b,u=P*l,d=u-(u-l),a=l-d,A=p*a-(w-b*d-p*d-b*a),s=_-A,o=_-s,T[0]=_-(s+o)+(o-A),m=v+s,o=m-v,M=v-(m-o)+(s-o),s=M-w,o=M-s,T[1]=M-(s+o)+(o-w),S=m+s,o=S-m,T[2]=m-(S-o)+(s-o),T[3]=S;const Ft=$(Ut,Z,4,T,tt);return tt[Ft-1]}function yt(t,e,r,n,c,i){const f=(e-i)*(r-c),g=(t-c)*(n-i),h=f-g,l=Math.abs(f+g);return Math.abs(h)>=dt*l?h:-lt(t,e,r,n,c,i,l)}function bt(t,e){var r,n,c=0,i,f,g,h,l,y,o,u=t[0],b=t[1],p=e.length;for(r=0;r<p;r++){n=0;var d=e[r],a=d.length-1;if(y=d[0],y[0]!==d[a][0]&&y[1]!==d[a][1])throw new Error("First and last coordinates in a ring must be the same");for(f=y[0]-u,g=y[1]-b,n;n<a;n++){if(o=d[n+1],h=o[0]-u,l=o[1]-b,g===0&&l===0){if(h<=0&&f>=0||f<=0&&h>=0)return 0}else if(l>=0&&g<=0||l<=0&&g>=0){if(i=yt(f,h,g,l,0,0),i===0)return 0;(i>0&&l>0&&g<=0||i<0&&l<=0&&g>0)&&c++}y=o,g=l,f=h}}return c%2!==0}function D(t,e,r={}){if(!t)throw new Error("point is required");if(!e)throw new Error("polygon is required");const n=at(t),c=ct(e),i=c.type,f=e.bbox;let g=c.coordinates;if(f&&mt(n,f)===!1)return!1;i==="Polygon"&&(g=[g]);let h=!1;for(var l=0;l<g.length;++l){const y=bt(n,g[l]);if(y===0)return!r.ignoreBoundary;y&&(h=!0)}return h}function mt(t,e){return e[0]<=t[0]&&e[1]<=t[1]&&e[2]>=t[0]&&e[3]>=t[1]}function q(t,e){for(let r=0;r<e.features.length;r++)if(D(t,e.features[r]))return e.features[r]}function et(t,e,r){const n=e.geometry.coordinates[0][0],c=e.geometry.coordinates[0][1],i=e.geometry.coordinates[0][2],f=t.geometry.coordinates,g=e.properties.a.geom,h=e.properties.b.geom,l=e.properties.c.geom,y=[c[0]-n[0],c[1]-n[1]],o=[i[0]-n[0],i[1]-n[1]],u=[f[0]-n[0],f[1]-n[1]],b=[h[0]-g[0],h[1]-g[1]],p=[l[0]-g[0],l[1]-g[1]];let d=(o[1]*u[0]-o[0]*u[1])/(y[0]*o[1]-y[1]*o[0]),a=(y[0]*u[1]-y[1]*u[0])/(y[0]*o[1]-y[1]*o[0]);if(r){const s=r[e.properties.a.index],m=r[e.properties.b.index],M=r[e.properties.c.index];let v;if(d<0||a<0||1-d-a<0){const _=d/(d+a),w=a/(d+a);v=d/m/(_/m+w/M),a=a/M/(_/m+w/M)}else v=d/m/(d/m+a/M+(1-d-a)/s),a=a/M/(d/m+a/M+(1-d-a)/s);d=v}return[d*b[0]+a*p[0]+g[0],d*b[1]+a*p[1]+g[1]]}function wt(t,e,r,n){const c=t.geometry.coordinates,i=r.geometry.coordinates,f=Math.atan2(c[0]-i[0],c[1]-i[1]),g=xt(f,e[0]);if(g===void 0)throw new Error("Unable to determine vertex index");const h=e[1][g];return et(t,h.features[0],n)}function rt(t,e,r,n,c,i,f,g){let h;if(f&&(h=q(t,X([f]))),!h)if(r){const l=t.geometry.coordinates,y=r.gridNum,o=r.xOrigin,u=r.yOrigin,b=r.xUnit,p=r.yUnit,d=r.gridCache,a=U(l[0],o,b,y),s=U(l[1],u,p,y),m=d[a]?d[a][s]?d[a][s]:[]:[],M=X(m.map(v=>e.features[v]));h=q(t,M)}else h=q(t,e);return g&&g(h),h?et(t,h,i):wt(t,n,c,i)}function U(t,e,r,n){let c=Math.floor((t-e)/r);return c<0&&(c=0),c>=n&&(c=n-1),c}function xt(t,e){let r=nt(t-e[0]),n=Math.PI*2,c;for(let i=0;i<e.length;i++){const f=(i+1)%e.length,g=nt(t-e[f]),h=Math.min(Math.abs(r),Math.abs(g));r*g<=0&&h<n&&(n=h,c=i),r=g}return c}function nt(t,e=!1){const r=2*Math.PI,n=t-Math.floor(t/r)*r;return e?n:n>Math.PI?n-r:n}function it(t,e){return e&&e>=2.00703||Array.isArray(t[0])?t:t.map(r=>[r.illstNodes,r.mercNodes,r.startEnd])}function pt(t){const e=t.features;for(let r=0;r<e.length;r++){const n=e[r];`${n.properties.a.index}`.substring(0,1)==="b"&&`${n.properties.b.index}`.substring(0,1)==="b"?e[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"&&(e[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 t}function vt(t){const e=["a","b","c","a"].map(i=>t.properties[i].geom),r=t.geometry.coordinates[0],n=t.properties,c={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 J([e],c)}function Mt(t){const e=[0,1,2,0].map(n=>t[n][0][0]),r={a:{geom:t[0][0][1],index:t[0][1]},b:{geom:t[1][0][1],index:t[1][1]},c:{geom:t[2][0][1],index:t[2][1]}};return J([e],r)}function z(t,e,r,n,c,i=!1,f){const g=t.map(h=>{(!f||f<2.00703)&&(h=st(h));const l=isFinite(h)?e[h]:h==="c"?n:(function(){const y=h.match(/^b(\d+)$/);if(y)return c[parseInt(y[1])];const o=h.match(/^e(\d+)$/);if(o)return r[parseInt(o[1])];throw new Error("Bad index value for indexesToTri")})();return i?[[l[1],l[0]],h]:[[l[0],l[1]],h]});return Mt(g)}function st(t){return typeof t=="number"?t:t.replace(/^(c|e|b)(?:ent|dgeNode|box)(\d+)?$/,"$1$2")}const j=2.00703;function At(t){return!!(t.version!==void 0||!t.tins&&t.points&&t.tins_points)}function _t(t){return{points:t.points,pointsWeightBuffer:Ot(t),strictStatus:Pt(t),verticesParams:St(t),centroid:Tt(t),edges:it(t.edges||[]),edgeNodes:t.edgeNodes||[],tins:kt(t),kinks:It(t.kinks_points),yaxisMode:t.yaxisMode??"invert",strictMode:t.strictMode??"auto",vertexMode:t.vertexMode,bounds:t.bounds,boundsPolygon:t.boundsPolygon,wh:t.wh,xy:t.xy??[0,0]}}function Et(t){const e=Nt(t),r=e.tins;return{compiled:e,tins:r,points:Bt(r),strictStatus:e.strict_status,pointsWeightBuffer:e.weight_buffer,verticesParams:e.vertices_params,centroid:e.centroid,kinks:e.kinks}}function Ot(t){return!t.version||t.version<j?["forw","bakw"].reduce((e,r)=>{const n=t.weight_buffer[r];return n&&(e[r]=Object.keys(n).reduce((c,i)=>{const f=st(i);return c[f]=n[i],c},{})),e},{}):t.weight_buffer}function Pt(t){return t.strict_status?t.strict_status:t.kinks_points?"strict_error":t.tins_points.length===2?"loose":"strict"}function St(t){const e={forw:[t.vertices_params[0]],bakw:[t.vertices_params[1]]};return e.forw[1]=ot(t,!1),e.bakw[1]=ot(t,!0),e}function ot(t,e){const r=t.vertices_points.length;return Array.from({length:r},(n,c)=>{const i=(c+1)%r,f=z(["c",`b${c}`,`b${i}`],t.points,t.edgeNodes||[],t.centroid_point,t.vertices_points,e,j);return X([f])})}function Tt(t){return{forw:R(t.centroid_point[0],{target:{geom:t.centroid_point[1],index:"c"}}),bakw:R(t.centroid_point[1],{target:{geom:t.centroid_point[0],index:"c"}})}}function kt(t){const e=t.tins_points.length===1?0:1;return{forw:X(t.tins_points[0].map(r=>z(r,t.points,t.edgeNodes||[],t.centroid_point,t.vertices_points,!1,t.version))),bakw:X(t.tins_points[e].map(r=>z(r,t.points,t.edgeNodes||[],t.centroid_point,t.vertices_points,!0,t.version)))}}function It(t){if(t)return{bakw:X(t.map(e=>R(e)))}}function Nt(t){return JSON.parse(JSON.stringify(t).replace('"cent"','"c"').replace(/"bbox(\d+)"/g,'"b$1"'))}function Bt(t){const e=[],r=t.forw.features;for(let n=0;n<r.length;n++){const c=r[n];["a","b","c"].forEach((i,f)=>{const g=c.geometry.coordinates[0][f],h=c.properties[i].geom,l=c.properties[i].index;typeof l=="number"&&(e[l]=[g,h])})}return e}const Ct=j,E=class E{constructor(){x(this,"points",[]);x(this,"pointsWeightBuffer");x(this,"strict_status");x(this,"vertices_params");x(this,"centroid");x(this,"edgeNodes");x(this,"edges");x(this,"tins");x(this,"kinks");x(this,"yaxisMode",E.YAXIS_INVERT);x(this,"strictMode",E.MODE_AUTO);x(this,"vertexMode",E.VERTEX_PLAIN);x(this,"bounds");x(this,"boundsPolygon");x(this,"wh");x(this,"xy");x(this,"indexedTins");x(this,"stateFull",!1);x(this,"stateTriangle");x(this,"stateBackward");x(this,"priority");x(this,"importance");x(this,"xyBounds");x(this,"mercBounds")}setCompiled(e){if(At(e)){this.applyModernState(_t(e));return}this.applyLegacyState(Et(e))}applyModernState(e){this.points=e.points,this.pointsWeightBuffer=e.pointsWeightBuffer,this.strict_status=e.strictStatus,this.vertices_params=e.verticesParams,this.centroid=e.centroid,this.edges=e.edges,this.edgeNodes=e.edgeNodes||[],this.tins=e.tins,this.addIndexedTin(),this.kinks=e.kinks,this.yaxisMode=e.yaxisMode??E.YAXIS_INVERT,this.vertexMode=e.vertexMode??E.VERTEX_PLAIN,this.strictMode=e.strictMode??E.MODE_AUTO,e.bounds?(this.bounds=e.bounds,this.boundsPolygon=e.boundsPolygon,this.xy=e.xy,this.wh=e.wh):(this.bounds=void 0,this.boundsPolygon=void 0,this.xy=e.xy??[0,0],e.wh&&(this.wh=e.wh))}applyLegacyState(e){this.tins=e.tins,this.addIndexedTin(),this.strict_status=e.strictStatus,this.pointsWeightBuffer=e.pointsWeightBuffer,this.vertices_params=e.verticesParams,this.centroid=e.centroid,this.kinks=e.kinks,this.points=e.points}addIndexedTin(){const e=this.tins,r=e.forw,n=e.bakw,c=Math.ceil(Math.sqrt(r.features.length));if(c<3){this.indexedTins=void 0;return}let i=[],f=[];const g=r.features.map(d=>{let a=[];return Q(d)[0].map(s=>{i.length===0?i=[Array.from(s),Array.from(s)]:(s[0]<i[0][0]&&(i[0][0]=s[0]),s[0]>i[1][0]&&(i[1][0]=s[0]),s[1]<i[0][1]&&(i[0][1]=s[1]),s[1]>i[1][1]&&(i[1][1]=s[1])),a.length===0?a=[Array.from(s),Array.from(s)]:(s[0]<a[0][0]&&(a[0][0]=s[0]),s[0]>a[1][0]&&(a[1][0]=s[0]),s[1]<a[0][1]&&(a[0][1]=s[1]),s[1]>a[1][1]&&(a[1][1]=s[1]))}),a}),h=(i[1][0]-i[0][0])/c,l=(i[1][1]-i[0][1])/c,y=g.reduce((d,a,s)=>{const m=U(a[0][0],i[0][0],h,c),M=U(a[1][0],i[0][0],h,c),v=U(a[0][1],i[0][1],l,c),_=U(a[1][1],i[0][1],l,c);for(let w=m;w<=M;w++){d[w]||(d[w]=[]);for(let A=v;A<=_;A++)d[w][A]||(d[w][A]=[]),d[w][A].push(s)}return d},[]),o=n.features.map(d=>{let a=[];return Q(d)[0].map(s=>{f.length===0?f=[Array.from(s),Array.from(s)]:(s[0]<f[0][0]&&(f[0][0]=s[0]),s[0]>f[1][0]&&(f[1][0]=s[0]),s[1]<f[0][1]&&(f[0][1]=s[1]),s[1]>f[1][1]&&(f[1][1]=s[1])),a.length===0?a=[Array.from(s),Array.from(s)]:(s[0]<a[0][0]&&(a[0][0]=s[0]),s[0]>a[1][0]&&(a[1][0]=s[0]),s[1]<a[0][1]&&(a[0][1]=s[1]),s[1]>a[1][1]&&(a[1][1]=s[1]))}),a}),u=(f[1][0]-f[0][0])/c,b=(f[1][1]-f[0][1])/c,p=o.reduce((d,a,s)=>{const m=U(a[0][0],f[0][0],u,c),M=U(a[1][0],f[0][0],u,c),v=U(a[0][1],f[0][1],b,c),_=U(a[1][1],f[0][1],b,c);for(let w=m;w<=M;w++){d[w]||(d[w]=[]);for(let A=v;A<=_;A++)d[w][A]||(d[w][A]=[]),d[w][A].push(s)}return d},[]);this.indexedTins={forw:{gridNum:c,xOrigin:i[0][0],yOrigin:i[0][1],xUnit:h,yUnit:l,gridCache:y},bakw:{gridNum:c,xOrigin:f[0][0],yOrigin:f[0][1],xUnit:u,yUnit:b,gridCache:p}}}transform(e,r,n){if(!this.tins)throw new Error("setCompiled() must be called before transform()");if(r&&this.strict_status==E.STATUS_ERROR)throw new Error('Backward transform is not allowed if strict_status == "strict_error"');this.yaxisMode==E.YAXIS_FOLLOW&&r&&(e=[e[0],-1*e[1]]);const c=R(e);if(this.bounds&&!r&&!n&&!D(c,this.boundsPolygon))return!1;const i=r?this.tins.bakw:this.tins.forw,f=r?this.indexedTins.bakw:this.indexedTins.forw,g=r?this.vertices_params.bakw:this.vertices_params.forw,h=r?this.centroid.bakw:this.centroid.forw,l=r?this.pointsWeightBuffer.bakw:this.pointsWeightBuffer.forw;let y,o;this.stateFull&&(this.stateBackward==r?y=this.stateTriangle:(this.stateBackward=r,this.stateTriangle=void 0),o=b=>{this.stateTriangle=b});let u=rt(c,i,f,g,h,l,y,o);if(this.bounds&&r&&!n){const b=R(u);if(!D(b,this.boundsPolygon))return!1}else this.yaxisMode==E.YAXIS_FOLLOW&&!r&&(u=[u[0],-1*u[1]]);return u}};x(E,"VERTEX_PLAIN","plain"),x(E,"VERTEX_BIRDEYE","birdeye"),x(E,"MODE_STRICT","strict"),x(E,"MODE_AUTO","auto"),x(E,"MODE_LOOSE","loose"),x(E,"STATUS_STRICT","strict"),x(E,"STATUS_ERROR","strict_error"),x(E,"STATUS_LOOSE","loose"),x(E,"YAXIS_FOLLOW","follow"),x(E,"YAXIS_INVERT","invert");let G=E;O.Transform=G,O.counterTri=vt,O.format_version=Ct,O.normalizeEdges=it,O.rotateVerticesTriangle=pt,O.transformArr=rt,Object.defineProperty(O,Symbol.toStringTag,{value:"Module"})}));
|
|
1
|
+
(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,l,f=t[0],y=n[0],a=0,d=0;y>f==y>-f?(i=f,f=t[++a]):(i=y,y=n[++d]);let g=0;if(a<e&&d<r)for(y>f==y>-f?(o=f+i,u=i-(o-f),f=t[++a]):(o=y+i,u=i-(o-y),y=n[++d]),i=o,u!==0&&(s[g++]=u);a<e&&d<r;)y>f==y>-f?(o=i+f,l=o-i,u=i-(o-l)+(f-l),f=t[++a]):(o=i+y,l=o-i,u=i-(o-l)+(y-l),y=n[++d]),i=o,u!==0&&(s[g++]=u);for(;a<e;)o=i+f,l=o-i,u=i-(o-l)+(f-l),f=t[++a],i=o,u!==0&&(s[g++]=u);for(;d<r;)o=i+y,l=o-i,u=i-(o-l)+(y-l),y=n[++d],i=o,u!==0&&(s[g++]=u);return(i!==0||g===0)&&(s[g++]=i),g}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,l,f,y,a,d,g,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,d=E*B,g=d-(d-B),b=B-g,d=E*R,m=d-(d-R),h=R-m,T=b*h-(M-g*m-b*m-g*h),w=P*O,d=E*P,g=d-(d-P),b=P-g,d=E*O,m=d-(d-O),h=O-m,_=b*h-(w-g*m-b*m-g*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,l=t-(P+a)+(a-i),a=n-R,y=n-(R+a)+(a-i),u===0&&l===0&&f===0&&y===0)||(Y=xt*o+gt*Math.abs(N),N+=B*y+R*u-(P*f+O*l),N>=Y||-N>=Y))return N;M=u*R,d=E*u,g=d-(d-u),b=u-g,d=E*R,m=d-(d-R),h=R-m,T=b*h-(M-g*m-b*m-g*h),w=l*O,d=E*l,g=d-(d-l),b=l-g,d=E*O,m=d-(d-O),h=O-m,_=b*h-(w-g*m-b*m-g*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*y,d=E*B,g=d-(d-B),b=B-g,d=E*y,m=d-(d-y),h=y-m,T=b*h-(M-g*m-b*m-g*h),w=P*f,d=E*P,g=d-(d-P),b=P-g,d=E*f,m=d-(d-f),h=f-m,_=b*h-(w-g*m-b*m-g*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*y,d=E*u,g=d-(d-u),b=u-g,d=E*y,m=d-(d-y),h=y-m,T=b*h-(M-g*m-b*m-g*h),w=l*f,d=E*l,g=d-(d-l),b=l-g,d=E*f,m=d-(d-f),h=f-m,_=b*h-(w-g*m-b*m-g*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),l=o-u,f=Math.abs(o+u);return Math.abs(l)>=pt*f?l:-Mt(e,t,r,n,s,i,f)}function Tt(e,t){var r,n,s=0,i,o,u,l,f,y,a,d=e[0],g=e[1],b=t.length;for(r=0;r<b;r++){n=0;var m=t[r],h=m.length-1;if(y=m[0],y[0]!==m[h][0]&&y[1]!==m[h][1])throw new Error("First and last coordinates in a ring must be the same");for(o=y[0]-d,u=y[1]-g,n;n<h;n++){if(a=m[n+1],l=a[0]-d,f=a[1]-g,u===0&&f===0){if(l<=0&&o>=0||o<=0&&l>=0)return 0}else if(f>=0&&u<=0||f<=0&&u>=0){if(i=_t(o,l,u,f,0,0),i===0)return 0;(i>0&&f>0&&u<=0||i<0&&f<=0&&u>0)&&s++}y=a,u=f,o=l}}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]);let l=!1;for(var f=0;f<u.length;++f){const y=Tt(n,u[f]);if(y===0)return!r.ignoreBoundary;y&&(l=!0)}return l}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,l=t.properties.b.geom,f=t.properties.c.geom,y=[s[0]-n[0],s[1]-n[1]],a=[i[0]-n[0],i[1]-n[1]],d=[o[0]-n[0],o[1]-n[1]],g=[l[0]-u[0],l[1]-u[1]],b=[f[0]-u[0],f[1]-u[1]];let m=(a[1]*d[0]-a[0]*d[1])/(y[0]*a[1]-y[1]*a[0]),h=(y[0]*d[1]-y[1]*d[0])/(y[0]*a[1]-y[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*g[0]+h*b[0]+u[0],m*g[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 l=t[1][u];return it(e,l.features[0],n)}function at(e,t,r,n,s,i,o,u){let l;if(o&&(l=j(e,L([o]))),!l)if(r){const f=e.geometry.coordinates,y=r.gridNum,a=r.xOrigin,d=r.yOrigin,g=r.xUnit,b=r.yUnit,m=r.gridCache,h=X(f[0],a,g,y),c=X(f[1],d,b,y),p=m[h]?m[h][c]?m[h][c]:[]:[],x=L(p.map(M=>t.features[M]));l=j(e,x)}else l=j(e,t);return u&&u(l),l?it(e,l,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]),l=Math.min(Math.abs(r),Math.abs(u));r*u<=0&&l<n&&(n=l,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(l=>{(!o||o<2.00703)&&(l=ut(l));const f=isFinite(l)?t[l]:l==="c"?n:(function(){const y=l.match(/^b(\d+)$/);if(y)return s[parseInt(y[1])];const a=l.match(/^e(\d+)$/);if(a)return r[parseInt(a[1])];throw new Error("Bad index value for indexesToTri")})();return i?[[f[1],f[0]],l]:[[f[0],f[1]],l]});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],l=s.properties[i].geom,f=s.properties[i].index;typeof f=="number"&&(t[f]=[u,l])})}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}),l=(i[1][0]-i[0][0])/s,f=(i[1][1]-i[0][1])/s,y=u.reduce((m,h,c)=>{const p=X(h[0][0],i[0][0],l,s),x=X(h[1][0],i[0][0],l,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}),d=(o[1][0]-o[0][0])/s,g=(o[1][1]-o[0][1])/s,b=a.reduce((m,h,c)=>{const p=X(h[0][0],o[0][0],d,s),x=X(h[1][0],o[0][0],d,s),M=X(h[0][1],o[0][1],g,s),T=X(h[1][1],o[0][1],g,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:l,yUnit:f,gridCache:y},bakw:{gridNum:s,xOrigin:o[0][0],yOrigin:o[0][1],xUnit:d,yUnit:g,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,l=r?this.centroid.bakw:this.centroid.forw,f=r?this.pointsWeightBuffer.bakw:this.pointsWeightBuffer.forw;let y,a;this.stateFull&&(this.stateBackward==r?y=this.stateTriangle:(this.stateBackward=r,this.stateTriangle=void 0),a=g=>{this.stateTriangle=g});let d=at(s,i,o,u,l,f,y,a);if(this.bounds&&r&&!n){const g=W(d);if(!F(g,this.boundsPolygon))return!1}else this.yaxisMode==C.YAXIS_FOLLOW&&!r&&(d=[d[0],-1*d[1]]);return d}}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(g=>[g[0]-r[0],g[1]-r[1]]),i=[[0,1],[1,0],[0,-1],[-1,0]];let o=0,u=0,l=0;for(let g=0;g<4;g++){const b=s[g],m=i[g],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),l+=Math.sin(x)}const f=o/4,y=Math.atan2(l,u),a=Math.floor(Math.min(t[0],t[1])/4),d=Math.log(a*I/128/f)/Math.log(2);return{center:r,zoom:d,rotation:y}}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(l=>{const f=s.transform(l,!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:l})=>{const f=this._transformByIndex(t,o,!0);return f===!1?[u,o]:l||F(W(f),this.subTins[o-1].xyBounds)?[u,o,f]:[u,o]}).sort((o,u)=>{const l=o[0].priority??0,f=u[0].priority??0;return l<f?1:-1}).reduce((o,u,l,f)=>{const y=u[0],a=u[1],d=u[2];if(!d)return o;for(let g=0;g<l;g++){const b=f[g][1],m=b===0;if(f[g][2]&&(m||F(W(d),this.subTins[b-1].xyBounds)))if(o.length){const h=!o[0],c=h?o[1][2]:o[0][2],p=y.importance??0,x=c.importance??0;return h?p<x?o:[void 0,[a,d,y]]:[...o.filter(w=>w!==void 0),[a,d,y]].sort((w,_)=>(w[2].importance??0)<(_[2].importance??0)?1:-1).slice(0,2)}else return[[a,d,y]]}return!o.length||!o[0]?[[a,d,y]]:(o.push([a,d,y]),o.sort((g,b)=>{const m=g[2].importance??0,h=b[2].importance??0;return m<h?1:-1}).filter((g,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,y)=>y===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,y)=>{if(y===0)return u;const a=this._transformByIndex(f,o,!1);if(a===!1)throw new Error(`viewpoint2Mercs: point ${y} 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],l=t.map((f,y)=>{if(y===0)return o;const a=this._transformByIndex(f,i,!0);if(a===!1)throw new Error(`mercs2Viewpoint: point ${y} is out of bounds`);return a}).map(f=>z(f,this._maxxy));return Q(l,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"})}));
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.5.
|
|
7
|
+
"version": "0.5.2",
|
|
8
8
|
"description": "A JavaScript library that performs coordinate transformation between two plane coordinate systems using transformation definitions generated by Maplat.",
|
|
9
9
|
"type": "module",
|
|
10
10
|
"engines": {
|
|
@@ -46,10 +46,11 @@
|
|
|
46
46
|
],
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@eslint/js": "^9.39.2",
|
|
49
|
+
"@maplat/tin": "0.14.0",
|
|
49
50
|
"@types/geojson": "^7946.0.16",
|
|
50
|
-
"@types/node": "^
|
|
51
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
52
|
-
"@typescript-eslint/parser": "^8.
|
|
51
|
+
"@types/node": "^25.5.1",
|
|
52
|
+
"@typescript-eslint/eslint-plugin": "^8.58.0",
|
|
53
|
+
"@typescript-eslint/parser": "^8.58.0",
|
|
53
54
|
"@vitest/coverage-v8": "^3.2.4",
|
|
54
55
|
"cross-env": "^10.1.0",
|
|
55
56
|
"eslint": "^9.39.2",
|
|
@@ -58,7 +59,7 @@
|
|
|
58
59
|
"jsdom": "^27.4.0",
|
|
59
60
|
"prettier": "^3.7.4",
|
|
60
61
|
"typescript": "^5.9.3",
|
|
61
|
-
"vite": "^
|
|
62
|
+
"vite": "^7.3.0",
|
|
62
63
|
"vite-plugin-dts": "^4.5.4",
|
|
63
64
|
"vitest": "^3.2.4"
|
|
64
65
|
},
|
|
@@ -66,6 +67,7 @@
|
|
|
66
67
|
"@turf/turf": "^7.3.1"
|
|
67
68
|
},
|
|
68
69
|
"scripts": {
|
|
70
|
+
"generate:submaps": "node scripts/generate-submaps-compiled.mjs",
|
|
69
71
|
"dev": "vite --host",
|
|
70
72
|
"build": "pnpm run typecheck && cross-env BUILD_MODE=package vite build",
|
|
71
73
|
"typecheck": "tsc --noEmit --allowImportingTsExtensions",
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { MERC_MAX } from "./constants.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ピクセル座標をシステム座標(EPSG:3857相当)に変換する
|
|
5
|
+
*
|
|
6
|
+
* @param xy - ピクセル座標 [x, y]
|
|
7
|
+
* @param maxxy - 最大座標値(2^maxZoom * 256)
|
|
8
|
+
* @returns システム座標 [x, y]
|
|
9
|
+
*/
|
|
10
|
+
export function xy2SysCoord(xy: number[], maxxy: number): number[] {
|
|
11
|
+
const sysCoordX = (xy[0] * (2 * MERC_MAX)) / maxxy - MERC_MAX;
|
|
12
|
+
const sysCoordY = -1 * ((xy[1] * (2 * MERC_MAX)) / maxxy - MERC_MAX);
|
|
13
|
+
return [sysCoordX, sysCoordY];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* システム座標(EPSG:3857相当)をピクセル座標に変換する
|
|
18
|
+
*
|
|
19
|
+
* @param sysCoord - システム座標 [x, y]
|
|
20
|
+
* @param maxxy - 最大座標値(2^maxZoom * 256)
|
|
21
|
+
* @returns ピクセル座標 [x, y]
|
|
22
|
+
*/
|
|
23
|
+
export function sysCoord2Xy(sysCoord: number[], maxxy: number): number[] {
|
|
24
|
+
const x = ((sysCoord[0] + MERC_MAX) * maxxy) / (2 * MERC_MAX);
|
|
25
|
+
const y = ((-sysCoord[1] + MERC_MAX) * maxxy) / (2 * MERC_MAX);
|
|
26
|
+
return [x, y];
|
|
27
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,31 +1,3 @@
|
|
|
1
|
-
import type { Feature, Polygon, Position } from "geojson";
|
|
2
|
-
import { booleanPointInPolygon, point, getCoords } from "@turf/turf";
|
|
3
|
-
import { unitCalc, transformArr } from "./geometry.ts";
|
|
4
|
-
import type { Tri } from "./geometry.ts";
|
|
5
|
-
import {
|
|
6
|
-
FORMAT_VERSION,
|
|
7
|
-
isModernCompiled,
|
|
8
|
-
restoreLegacyState,
|
|
9
|
-
restoreModernState
|
|
10
|
-
} from "./compiled-state.ts";
|
|
11
|
-
import type { EdgeSet } from "./edgeutils.ts";
|
|
12
|
-
import type {
|
|
13
|
-
Compiled,
|
|
14
|
-
CompiledLegacy,
|
|
15
|
-
IndexedTinsBD,
|
|
16
|
-
KinksBD,
|
|
17
|
-
LegacyStatePayload,
|
|
18
|
-
ModernStatePayload,
|
|
19
|
-
PointSet,
|
|
20
|
-
StrictMode,
|
|
21
|
-
StrictStatus,
|
|
22
|
-
TinsBD,
|
|
23
|
-
VertexMode,
|
|
24
|
-
VerticesParamsBD,
|
|
25
|
-
WeightBufferBD,
|
|
26
|
-
YaxisMode,
|
|
27
|
-
CentroidBD
|
|
28
|
-
} from "./types.ts";
|
|
29
1
|
export type {
|
|
30
2
|
PointSet,
|
|
31
3
|
BiDirectionKey,
|
|
@@ -47,354 +19,20 @@ export { transformArr } from "./geometry.ts";
|
|
|
47
19
|
export { rotateVerticesTriangle, counterTri } from "./triangulation.ts";
|
|
48
20
|
export type { Edge, EdgeSet, EdgeSetLegacy } from "./edgeutils.ts";
|
|
49
21
|
export { normalizeEdges } from "./edgeutils.ts";
|
|
50
|
-
export
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
static MODE_AUTO = "auto" as const;
|
|
68
|
-
static MODE_LOOSE = "loose" as const;
|
|
69
|
-
static STATUS_STRICT = "strict" as const;
|
|
70
|
-
static STATUS_ERROR = "strict_error" as const;
|
|
71
|
-
static STATUS_LOOSE = "loose" as const;
|
|
72
|
-
static YAXIS_FOLLOW = "follow" as const;
|
|
73
|
-
static YAXIS_INVERT = "invert" as const;
|
|
74
|
-
|
|
75
|
-
points: PointSet[] = [];
|
|
76
|
-
pointsWeightBuffer?: WeightBufferBD;
|
|
77
|
-
strict_status?: StrictStatus;
|
|
78
|
-
vertices_params?: VerticesParamsBD;
|
|
79
|
-
centroid?: CentroidBD;
|
|
80
|
-
edgeNodes?: PointSet[];
|
|
81
|
-
edges?: EdgeSet[];
|
|
82
|
-
tins?: TinsBD;
|
|
83
|
-
kinks?: KinksBD;
|
|
84
|
-
yaxisMode: YaxisMode = Transform.YAXIS_INVERT;
|
|
85
|
-
strictMode: StrictMode = Transform.MODE_AUTO;
|
|
86
|
-
vertexMode?: VertexMode = Transform.VERTEX_PLAIN;
|
|
87
|
-
bounds?: number[][];
|
|
88
|
-
boundsPolygon?: Feature<Polygon>;
|
|
89
|
-
wh?: number[];
|
|
90
|
-
xy?: number[];
|
|
91
|
-
indexedTins?: IndexedTinsBD;
|
|
92
|
-
stateFull = false;
|
|
93
|
-
stateTriangle?: Tri;
|
|
94
|
-
stateBackward?: boolean;
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Optional properties for MaplatCore extension
|
|
98
|
-
* These properties allow consuming applications to extend Transform instances
|
|
99
|
-
* with additional metadata without requiring Module Augmentation
|
|
100
|
-
*/
|
|
101
|
-
|
|
102
|
-
/** Layer priority for rendering order */
|
|
103
|
-
priority?: number;
|
|
104
|
-
|
|
105
|
-
/** Layer importance for display decisions */
|
|
106
|
-
importance?: number;
|
|
107
|
-
|
|
108
|
-
/** Bounds in XY (source) coordinate system */
|
|
109
|
-
xyBounds?: Feature<Polygon>;
|
|
110
|
-
|
|
111
|
-
/** Bounds in Mercator (Web Mercator) coordinate system */
|
|
112
|
-
mercBounds?: Feature<Polygon>;
|
|
113
|
-
|
|
114
|
-
constructor() { }
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* コンパイルされた設定を適用します
|
|
118
|
-
*
|
|
119
|
-
* @param compiled - コンパイルされた設定オブジェクト
|
|
120
|
-
* @returns 変換に必要な主要なオブジェクトのセット
|
|
121
|
-
*
|
|
122
|
-
* 以下の処理を行います:
|
|
123
|
-
* 1. バージョンに応じた設定の解釈
|
|
124
|
-
* 2. 各種パラメータの復元
|
|
125
|
-
* 3. TINネットワークの再構築
|
|
126
|
-
* 4. インデックスの作成
|
|
127
|
-
*/
|
|
128
|
-
setCompiled(compiled: Compiled | CompiledLegacy): void {
|
|
129
|
-
if (isModernCompiled(compiled)) {
|
|
130
|
-
this.applyModernState(restoreModernState(compiled));
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
this.applyLegacyState(restoreLegacyState(compiled as CompiledLegacy));
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
private applyModernState(state: ModernStatePayload): void {
|
|
137
|
-
this.points = state.points;
|
|
138
|
-
this.pointsWeightBuffer = state.pointsWeightBuffer;
|
|
139
|
-
this.strict_status = state.strictStatus;
|
|
140
|
-
this.vertices_params = state.verticesParams;
|
|
141
|
-
this.centroid = state.centroid;
|
|
142
|
-
this.edges = state.edges;
|
|
143
|
-
this.edgeNodes = state.edgeNodes || [];
|
|
144
|
-
this.tins = state.tins;
|
|
145
|
-
this.addIndexedTin();
|
|
146
|
-
this.kinks = state.kinks;
|
|
147
|
-
this.yaxisMode = state.yaxisMode ?? Transform.YAXIS_INVERT;
|
|
148
|
-
this.vertexMode = state.vertexMode ?? Transform.VERTEX_PLAIN;
|
|
149
|
-
this.strictMode = state.strictMode ?? Transform.MODE_AUTO;
|
|
150
|
-
if (state.bounds) {
|
|
151
|
-
this.bounds = state.bounds;
|
|
152
|
-
this.boundsPolygon = state.boundsPolygon;
|
|
153
|
-
this.xy = state.xy;
|
|
154
|
-
this.wh = state.wh;
|
|
155
|
-
} else {
|
|
156
|
-
this.bounds = undefined;
|
|
157
|
-
this.boundsPolygon = undefined;
|
|
158
|
-
this.xy = state.xy ?? [0, 0];
|
|
159
|
-
if (state.wh) this.wh = state.wh;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
private applyLegacyState(state: LegacyStatePayload): void {
|
|
164
|
-
this.tins = state.tins;
|
|
165
|
-
this.addIndexedTin();
|
|
166
|
-
this.strict_status = state.strictStatus;
|
|
167
|
-
this.pointsWeightBuffer = state.pointsWeightBuffer;
|
|
168
|
-
this.vertices_params = state.verticesParams;
|
|
169
|
-
this.centroid = state.centroid;
|
|
170
|
-
this.kinks = state.kinks;
|
|
171
|
-
this.points = state.points;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* TINネットワークのインデックスを作成します
|
|
176
|
-
*
|
|
177
|
-
* インデックスは変換処理を高速化するために使用されます。
|
|
178
|
-
* グリッド形式のインデックスを作成し、各グリッドに
|
|
179
|
-
* 含まれる三角形を記録します。
|
|
180
|
-
*/
|
|
181
|
-
addIndexedTin() {
|
|
182
|
-
const tins = this.tins!;
|
|
183
|
-
const forw = tins.forw;
|
|
184
|
-
const bakw = tins.bakw;
|
|
185
|
-
const gridNum = Math.ceil(Math.sqrt(forw!.features.length));
|
|
186
|
-
if (gridNum < 3) {
|
|
187
|
-
this.indexedTins = undefined;
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
let forwBound: Position[] = [];
|
|
191
|
-
let bakwBound: Position[] = [];
|
|
192
|
-
const forwEachBound = forw!.features.map((tri: Tri) => {
|
|
193
|
-
let eachBound: Position[] = [];
|
|
194
|
-
getCoords(tri)[0].map((point: Position) => {
|
|
195
|
-
if (forwBound.length === 0)
|
|
196
|
-
forwBound = [Array.from(point), Array.from(point)];
|
|
197
|
-
else {
|
|
198
|
-
if (point[0] < forwBound[0][0]) forwBound[0][0] = point[0];
|
|
199
|
-
if (point[0] > forwBound[1][0]) forwBound[1][0] = point[0];
|
|
200
|
-
if (point[1] < forwBound[0][1]) forwBound[0][1] = point[1];
|
|
201
|
-
if (point[1] > forwBound[1][1]) forwBound[1][1] = point[1];
|
|
202
|
-
}
|
|
203
|
-
if (eachBound.length === 0)
|
|
204
|
-
eachBound = [Array.from(point), Array.from(point)];
|
|
205
|
-
else {
|
|
206
|
-
if (point[0] < eachBound[0][0]) eachBound[0][0] = point[0];
|
|
207
|
-
if (point[0] > eachBound[1][0]) eachBound[1][0] = point[0];
|
|
208
|
-
if (point[1] < eachBound[0][1]) eachBound[0][1] = point[1];
|
|
209
|
-
if (point[1] > eachBound[1][1]) eachBound[1][1] = point[1];
|
|
210
|
-
}
|
|
211
|
-
});
|
|
212
|
-
return eachBound;
|
|
213
|
-
});
|
|
214
|
-
const forwXUnit = (forwBound[1][0] - forwBound[0][0]) / gridNum;
|
|
215
|
-
const forwYUnit = (forwBound[1][1] - forwBound[0][1]) / gridNum;
|
|
216
|
-
const forwGridCache = forwEachBound.reduce(
|
|
217
|
-
(prev: number[][][], bound: Position[], index: number) => {
|
|
218
|
-
const normXMin = unitCalc(
|
|
219
|
-
bound[0][0],
|
|
220
|
-
forwBound[0][0],
|
|
221
|
-
forwXUnit,
|
|
222
|
-
gridNum
|
|
223
|
-
);
|
|
224
|
-
const normXMax = unitCalc(
|
|
225
|
-
bound[1][0],
|
|
226
|
-
forwBound[0][0],
|
|
227
|
-
forwXUnit,
|
|
228
|
-
gridNum
|
|
229
|
-
);
|
|
230
|
-
const normYMin = unitCalc(
|
|
231
|
-
bound[0][1],
|
|
232
|
-
forwBound[0][1],
|
|
233
|
-
forwYUnit,
|
|
234
|
-
gridNum
|
|
235
|
-
);
|
|
236
|
-
const normYMax = unitCalc(
|
|
237
|
-
bound[1][1],
|
|
238
|
-
forwBound[0][1],
|
|
239
|
-
forwYUnit,
|
|
240
|
-
gridNum
|
|
241
|
-
);
|
|
242
|
-
for (let cx = normXMin; cx <= normXMax; cx++) {
|
|
243
|
-
if (!prev[cx]) prev[cx] = [];
|
|
244
|
-
for (let cy = normYMin; cy <= normYMax; cy++) {
|
|
245
|
-
if (!prev[cx][cy]) prev[cx][cy] = [];
|
|
246
|
-
prev[cx][cy].push(index);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
return prev;
|
|
250
|
-
},
|
|
251
|
-
[]
|
|
252
|
-
);
|
|
253
|
-
const bakwEachBound = bakw!.features.map((tri: Tri) => {
|
|
254
|
-
let eachBound: Position[] = [];
|
|
255
|
-
getCoords(tri)[0].map((point: Position) => {
|
|
256
|
-
if (bakwBound.length === 0)
|
|
257
|
-
bakwBound = [Array.from(point), Array.from(point)];
|
|
258
|
-
else {
|
|
259
|
-
if (point[0] < bakwBound[0][0]) bakwBound[0][0] = point[0];
|
|
260
|
-
if (point[0] > bakwBound[1][0]) bakwBound[1][0] = point[0];
|
|
261
|
-
if (point[1] < bakwBound[0][1]) bakwBound[0][1] = point[1];
|
|
262
|
-
if (point[1] > bakwBound[1][1]) bakwBound[1][1] = point[1];
|
|
263
|
-
}
|
|
264
|
-
if (eachBound.length === 0)
|
|
265
|
-
eachBound = [Array.from(point), Array.from(point)];
|
|
266
|
-
else {
|
|
267
|
-
if (point[0] < eachBound[0][0]) eachBound[0][0] = point[0];
|
|
268
|
-
if (point[0] > eachBound[1][0]) eachBound[1][0] = point[0];
|
|
269
|
-
if (point[1] < eachBound[0][1]) eachBound[0][1] = point[1];
|
|
270
|
-
if (point[1] > eachBound[1][1]) eachBound[1][1] = point[1];
|
|
271
|
-
}
|
|
272
|
-
});
|
|
273
|
-
return eachBound;
|
|
274
|
-
});
|
|
275
|
-
const bakwXUnit = (bakwBound[1][0] - bakwBound[0][0]) / gridNum;
|
|
276
|
-
const bakwYUnit = (bakwBound[1][1] - bakwBound[0][1]) / gridNum;
|
|
277
|
-
const bakwGridCache = bakwEachBound.reduce(
|
|
278
|
-
(prev: number[][][], bound: Position[], index: number) => {
|
|
279
|
-
const normXMin = unitCalc(
|
|
280
|
-
bound[0][0],
|
|
281
|
-
bakwBound[0][0],
|
|
282
|
-
bakwXUnit,
|
|
283
|
-
gridNum
|
|
284
|
-
);
|
|
285
|
-
const normXMax = unitCalc(
|
|
286
|
-
bound[1][0],
|
|
287
|
-
bakwBound[0][0],
|
|
288
|
-
bakwXUnit,
|
|
289
|
-
gridNum
|
|
290
|
-
);
|
|
291
|
-
const normYMin = unitCalc(
|
|
292
|
-
bound[0][1],
|
|
293
|
-
bakwBound[0][1],
|
|
294
|
-
bakwYUnit,
|
|
295
|
-
gridNum
|
|
296
|
-
);
|
|
297
|
-
const normYMax = unitCalc(
|
|
298
|
-
bound[1][1],
|
|
299
|
-
bakwBound[0][1],
|
|
300
|
-
bakwYUnit,
|
|
301
|
-
gridNum
|
|
302
|
-
);
|
|
303
|
-
for (let cx = normXMin; cx <= normXMax; cx++) {
|
|
304
|
-
if (!prev[cx]) prev[cx] = [];
|
|
305
|
-
for (let cy = normYMin; cy <= normYMax; cy++) {
|
|
306
|
-
if (!prev[cx][cy]) prev[cx][cy] = [];
|
|
307
|
-
prev[cx][cy].push(index);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
return prev;
|
|
311
|
-
},
|
|
312
|
-
[]
|
|
313
|
-
);
|
|
314
|
-
this.indexedTins = {
|
|
315
|
-
forw: {
|
|
316
|
-
gridNum,
|
|
317
|
-
xOrigin: forwBound[0][0],
|
|
318
|
-
yOrigin: forwBound[0][1],
|
|
319
|
-
xUnit: forwXUnit,
|
|
320
|
-
yUnit: forwYUnit,
|
|
321
|
-
gridCache: forwGridCache
|
|
322
|
-
},
|
|
323
|
-
bakw: {
|
|
324
|
-
gridNum,
|
|
325
|
-
xOrigin: bakwBound[0][0],
|
|
326
|
-
yOrigin: bakwBound[0][1],
|
|
327
|
-
xUnit: bakwXUnit,
|
|
328
|
-
yUnit: bakwYUnit,
|
|
329
|
-
gridCache: bakwGridCache
|
|
330
|
-
}
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* 座標変換を実行します
|
|
336
|
-
*
|
|
337
|
-
* @param apoint - 変換する座標
|
|
338
|
-
* @param backward - 逆方向の変換かどうか
|
|
339
|
-
* @param ignoreBounds - 境界チェックを無視するかどうか
|
|
340
|
-
* @returns 変換後の座標、または境界外の場合はfalse
|
|
341
|
-
*
|
|
342
|
-
* @throws {Error} 逆方向変換が許可されていない状態での逆変換時
|
|
343
|
-
*/
|
|
344
|
-
transform(apoint: number[], backward?: boolean, ignoreBounds?: boolean): number[] | false {
|
|
345
|
-
if (!this.tins)
|
|
346
|
-
throw new Error("setCompiled() must be called before transform()");
|
|
347
|
-
if (backward && this.strict_status == Transform.STATUS_ERROR)
|
|
348
|
-
throw new Error('Backward transform is not allowed if strict_status == "strict_error"');
|
|
349
|
-
// if (!this.tins) this.updateTin();
|
|
350
|
-
if (this.yaxisMode == Transform.YAXIS_FOLLOW && backward) {
|
|
351
|
-
apoint = [apoint[0], -1 * apoint[1]];
|
|
352
|
-
}
|
|
353
|
-
const tpoint = point(apoint);
|
|
354
|
-
if (this.bounds && !backward && !ignoreBounds) {
|
|
355
|
-
if (!booleanPointInPolygon(tpoint, this.boundsPolygon!)) return false;
|
|
356
|
-
}
|
|
357
|
-
const tins = backward ? this.tins!.bakw : this.tins!.forw;
|
|
358
|
-
const indexedTins = backward
|
|
359
|
-
? this.indexedTins!.bakw
|
|
360
|
-
: this.indexedTins!.forw;
|
|
361
|
-
const verticesParams = backward
|
|
362
|
-
? this.vertices_params!.bakw
|
|
363
|
-
: this.vertices_params!.forw;
|
|
364
|
-
const centroid = backward ? this.centroid!.bakw : this.centroid!.forw;
|
|
365
|
-
const weightBuffer = backward
|
|
366
|
-
? this.pointsWeightBuffer!.bakw
|
|
367
|
-
: this.pointsWeightBuffer!.forw;
|
|
368
|
-
let stateTriangle = undefined,
|
|
369
|
-
stateSetFunc = undefined;
|
|
370
|
-
if (this.stateFull) {
|
|
371
|
-
if (this.stateBackward == backward) {
|
|
372
|
-
stateTriangle = this.stateTriangle;
|
|
373
|
-
} else {
|
|
374
|
-
this.stateBackward = backward;
|
|
375
|
-
this.stateTriangle = undefined;
|
|
376
|
-
}
|
|
377
|
-
stateSetFunc = (tri?: Tri) => {
|
|
378
|
-
this.stateTriangle = tri;
|
|
379
|
-
};
|
|
380
|
-
}
|
|
381
|
-
let ret = transformArr(
|
|
382
|
-
tpoint,
|
|
383
|
-
tins!,
|
|
384
|
-
indexedTins,
|
|
385
|
-
verticesParams,
|
|
386
|
-
centroid,
|
|
387
|
-
weightBuffer,
|
|
388
|
-
stateTriangle,
|
|
389
|
-
stateSetFunc
|
|
390
|
-
);
|
|
391
|
-
if (this.bounds && backward && !ignoreBounds) {
|
|
392
|
-
const rpoint = point(ret);
|
|
393
|
-
if (!booleanPointInPolygon(rpoint, this.boundsPolygon!)) return false;
|
|
394
|
-
} else if (this.yaxisMode == Transform.YAXIS_FOLLOW && !backward) {
|
|
395
|
-
ret = [ret[0], -1 * ret[1]];
|
|
396
|
-
}
|
|
397
|
-
return ret;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
}
|
|
22
|
+
export { Transform, format_version } from "./transform.ts";
|
|
23
|
+
|
|
24
|
+
// New exports: constants, pure functions, MapTransform
|
|
25
|
+
export { MERC_MAX, MERC_CROSSMATRIX } from "./constants.ts";
|
|
26
|
+
export {
|
|
27
|
+
zoom2Radius,
|
|
28
|
+
rotateMatrix,
|
|
29
|
+
mercViewpoint2Mercs,
|
|
30
|
+
mercs2MercViewpoint
|
|
31
|
+
} from "./viewpoint.ts";
|
|
32
|
+
export { xy2SysCoord, sysCoord2Xy } from "./coord-utils.ts";
|
|
33
|
+
export { MapTransform } from "./map-transform.ts";
|
|
34
|
+
export type {
|
|
35
|
+
Viewpoint,
|
|
36
|
+
SubMapData,
|
|
37
|
+
MapData
|
|
38
|
+
} from "./types.ts";
|