@maplat/transform 0.2.2 → 0.3.0
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/LICENSE +224 -224
- package/README.ja.md +68 -68
- package/README.md +143 -143
- package/dist/index.d.ts +15 -23
- package/dist/index.html +42 -39
- package/dist/maplat_transform.cjs +1 -1
- package/dist/maplat_transform.js +505 -440
- package/dist/maplat_transform.umd.js +1 -1
- package/package.json +34 -38
- package/src/compiled-state.ts +211 -0
- package/src/edgeutils.ts +47 -47
- package/src/geometry.ts +248 -248
- package/src/index.ts +382 -567
- package/src/triangulation.ts +179 -179
- package/src/types.ts +122 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(p,O){typeof exports=="object"&&typeof module<"u"?O(exports):typeof define=="function"&&define.amd?define(["exports"],O):(p=typeof globalThis<"u"?globalThis:p||self,O(p.maplat_transform={}))})(this,function(p){"use strict";var Ot=Object.defineProperty;var St=(p,O,S)=>O in p?Ot(p,O,{enumerable:!0,configurable:!0,writable:!0,value:S}):p[O]=S;var _=(p,O,S)=>St(p,typeof O!="symbol"?O+"":O,S);const O=11102230246251565e-32,S=134217729,ot=(3+8*O)*O;function D(e,t,n,r,a){let s,c,d,g,l=t[0],y=r[0],o=0,u=0;y>l==y>-l?(s=l,l=t[++o]):(s=y,y=r[++u]);let b=0;if(o<e&&u<n)for(y>l==y>-l?(c=l+s,d=s-(c-l),l=t[++o]):(c=y+s,d=s-(c-y),y=r[++u]),s=c,d!==0&&(a[b++]=d);o<e&&u<n;)y>l==y>-l?(c=s+l,g=c-s,d=s-(c-g)+(l-g),l=t[++o]):(c=s+y,g=c-s,d=s-(c-g)+(y-g),y=r[++u]),s=c,d!==0&&(a[b++]=d);for(;o<e;)c=s+l,g=c-s,d=s-(c-g)+(l-g),l=t[++o],s=c,d!==0&&(a[b++]=d);for(;u<n;)c=s+y,g=c-s,d=s-(c-g)+(y-g),y=r[++u],s=c,d!==0&&(a[b++]=d);return(s!==0||b===0)&&(a[b++]=s),b}function at(e,t){let n=t[0];for(let r=1;r<e;r++)n+=t[r];return n}function Y(e){return new Float64Array(e)}const ft=(3+16*O)*O,ct=(2+12*O)*O,ut=(9+64*O)*O*O,X=Y(4),G=Y(8),z=Y(12),J=Y(16),I=Y(4);function ht(e,t,n,r,a,s,c){let d,g,l,y,o,u,b,x,h,f,i,w,v,A,E,m,M,P;const k=e-a,N=n-a,C=t-s,U=r-s;A=k*U,u=S*k,b=u-(u-k),x=k-b,u=S*U,h=u-(u-U),f=U-h,E=x*f-(A-b*h-x*h-b*f),m=C*N,u=S*C,b=u-(u-C),x=C-b,u=S*N,h=u-(u-N),f=N-h,M=x*f-(m-b*h-x*h-b*f),i=E-M,o=E-i,X[0]=E-(i+o)+(o-M),w=A+i,o=w-A,v=A-(w-o)+(i-o),i=v-m,o=v-i,X[1]=v-(i+o)+(o-m),P=w+i,o=P-w,X[2]=w-(P-o)+(i-o),X[3]=P;let B=at(4,X),W=ct*c;if(B>=W||-B>=W||(o=e-k,d=e-(k+o)+(o-a),o=n-N,l=n-(N+o)+(o-a),o=t-C,g=t-(C+o)+(o-s),o=r-U,y=r-(U+o)+(o-s),d===0&&g===0&&l===0&&y===0)||(W=ut*c+ot*Math.abs(B),B+=k*y+U*d-(C*l+N*g),B>=W||-B>=W))return B;A=d*U,u=S*d,b=u-(u-d),x=d-b,u=S*U,h=u-(u-U),f=U-h,E=x*f-(A-b*h-x*h-b*f),m=g*N,u=S*g,b=u-(u-g),x=g-b,u=S*N,h=u-(u-N),f=N-h,M=x*f-(m-b*h-x*h-b*f),i=E-M,o=E-i,I[0]=E-(i+o)+(o-M),w=A+i,o=w-A,v=A-(w-o)+(i-o),i=v-m,o=v-i,I[1]=v-(i+o)+(o-m),P=w+i,o=P-w,I[2]=w-(P-o)+(i-o),I[3]=P;const Mt=D(4,X,4,I,G);A=k*y,u=S*k,b=u-(u-k),x=k-b,u=S*y,h=u-(u-y),f=y-h,E=x*f-(A-b*h-x*h-b*f),m=C*l,u=S*C,b=u-(u-C),x=C-b,u=S*l,h=u-(u-l),f=l-h,M=x*f-(m-b*h-x*h-b*f),i=E-M,o=E-i,I[0]=E-(i+o)+(o-M),w=A+i,o=w-A,v=A-(w-o)+(i-o),i=v-m,o=v-i,I[1]=v-(i+o)+(o-m),P=w+i,o=P-w,I[2]=w-(P-o)+(i-o),I[3]=P;const Et=D(Mt,G,4,I,z);A=d*y,u=S*d,b=u-(u-d),x=d-b,u=S*y,h=u-(u-y),f=y-h,E=x*f-(A-b*h-x*h-b*f),m=g*l,u=S*g,b=u-(u-g),x=g-b,u=S*l,h=u-(u-l),f=l-h,M=x*f-(m-b*h-x*h-b*f),i=E-M,o=E-i,I[0]=E-(i+o)+(o-M),w=A+i,o=w-A,v=A-(w-o)+(i-o),i=v-m,o=v-i,I[1]=v-(i+o)+(o-m),P=w+i,o=P-w,I[2]=w-(P-o)+(i-o),I[3]=P;const Tt=D(Et,z,4,I,J);return J[Tt-1]}function gt(e,t,n,r,a,s){const c=(t-s)*(n-a),d=(e-a)*(r-s),g=c-d,l=Math.abs(c+d);return Math.abs(g)>=ft*l?g:-ht(e,t,n,r,a,s,l)}function dt(e,t){var n,r,a=0,s,c,d,g,l,y,o,u=e[0],b=e[1],x=t.length;for(n=0;n<x;n++){r=0;var h=t[n],f=h.length-1;if(y=h[0],y[0]!==h[f][0]&&y[1]!==h[f][1])throw new Error("First and last coordinates in a ring must be the same");for(c=y[0]-u,d=y[1]-b,r;r<f;r++){if(o=h[r+1],g=o[0]-u,l=o[1]-b,d===0&&l===0){if(g<=0&&c>=0||c<=0&&g>=0)return 0}else if(l>=0&&d<=0||l<=0&&d>=0){if(s=gt(c,g,d,l,0,0),s===0)return 0;(s>0&&l>0&&d<=0||s<0&&l<=0&&d>0)&&a++}y=o,d=l,c=g}}return a%2!==0}function K(e,t,n={}){const r={type:"Feature"};return(n.id===0||n.id)&&(r.id=n.id),n.bbox&&(r.bbox=n.bbox),r.properties=t||{},r.geometry=e,r}function $(e,t,n={}){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(!H(e[0])||!H(e[1]))throw new Error("coordinates must contain numbers");return K({type:"Point",coordinates:e},t,n)}function Q(e,t,n={}){for(const a of e){if(a.length<4)throw new Error("Each LinearRing of a Polygon must have 4 or more Positions.");if(a[a.length-1].length!==a[0].length)throw new Error("First and last Position are not equivalent.");for(let s=0;s<a[a.length-1].length;s++)if(a[a.length-1][s]!==a[0][s])throw new Error("First and last Position are not equivalent.")}return K({type:"Polygon",coordinates:e},t,n)}function F(e,t={}){const n={type:"FeatureCollection"};return t.id&&(n.id=t.id),t.bbox&&(n.bbox=t.bbox),n.features=e,n}function H(e){return!isNaN(e)&&e!==null&&!Array.isArray(e)}function lt(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 Z(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}function bt(e,t,n={}){if(!e)throw new Error("point is required");if(!t)throw new Error("polygon is required");const r=lt(e),a=yt(t),s=a.type,c=t.bbox;let d=a.coordinates;if(c&&wt(r,c)===!1)return!1;s==="Polygon"&&(d=[d]);let g=!1;for(var l=0;l<d.length;++l){const y=dt(r,d[l]);if(y===0)return!n.ignoreBoundary;y&&(g=!0)}return g}function wt(e,t){return t[0]<=e[0]&&t[1]<=e[1]&&t[2]>=e[0]&&t[3]>=e[1]}var q=bt;function mt(e){const t=e.features;for(let n=0;n<t.length;n++){const r=t[n];`${r.properties.a.index}`.substring(0,1)==="b"&&`${r.properties.b.index}`.substring(0,1)==="b"?t[n]={geometry:{type:"Polygon",coordinates:[[r.geometry.coordinates[0][2],r.geometry.coordinates[0][0],r.geometry.coordinates[0][1],r.geometry.coordinates[0][2]]]},properties:{a:{geom:r.properties.c.geom,index:r.properties.c.index},b:{geom:r.properties.a.geom,index:r.properties.a.index},c:{geom:r.properties.b.geom,index:r.properties.b.index}},type:"Feature"}:`${r.properties.c.index}`.substring(0,1)==="b"&&`${r.properties.a.index}`.substring(0,1)==="b"&&(t[n]={geometry:{type:"Polygon",coordinates:[[r.geometry.coordinates[0][1],r.geometry.coordinates[0][2],r.geometry.coordinates[0][0],r.geometry.coordinates[0][1]]]},properties:{a:{geom:r.properties.b.geom,index:r.properties.b.index},b:{geom:r.properties.c.geom,index:r.properties.c.index},c:{geom:r.properties.a.geom,index:r.properties.a.index}},type:"Feature"})}return e}function xt(e){const t=["a","b","c","a"].map(s=>e.properties[s].geom),n=e.geometry.coordinates[0],r=e.properties,a={a:{geom:n[0],index:r.a.index},b:{geom:n[1],index:r.b.index},c:{geom:n[2],index:r.c.index}};return Q([t],a)}function _t(e){const t=[0,1,2,0].map(r=>e[r][0][0]),n={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 Q([t],n)}function V(e,t,n,r,a,s=!1,c){const d=e.map(g=>{(!c||c<2.00703)&&(g=tt(g));const l=isFinite(g)?t[g]:g==="c"?r:g==="b0"?a[0]:g==="b1"?a[1]:g==="b2"?a[2]:g==="b3"?a[3]:function(){const y=g.match(/e(\d+)/);if(y){const o=parseInt(y[1]);return n[o]}throw"Bad index value for indexesToTri"}();return s?[[l[1],l[0]],g]:[[l[0],l[1]],g]});return _t(d)}function tt(e){return typeof e=="number"?e:e.replace(/^(c|e|b)(?:ent|dgeNode|box)(\d+)?$/,"$1$2")}function et(e,t){return t&&t>=2.00703||Array.isArray(e[0])?e:e.map(n=>[n.illstNodes,n.mercNodes,n.startEnd])}function rt(e,t){for(let n=0;n<t.features.length;n++)if(q(e,t.features[n]))return t.features[n]}function nt(e,t,n){const r=t.geometry.coordinates[0][0],a=t.geometry.coordinates[0][1],s=t.geometry.coordinates[0][2],c=e.geometry.coordinates,d=t.properties.a.geom,g=t.properties.b.geom,l=t.properties.c.geom,y=[a[0]-r[0],a[1]-r[1]],o=[s[0]-r[0],s[1]-r[1]],u=[c[0]-r[0],c[1]-r[1]],b=[g[0]-d[0],g[1]-d[1]],x=[l[0]-d[0],l[1]-d[1]];let h=(o[1]*u[0]-o[0]*u[1])/(y[0]*o[1]-y[1]*o[0]),f=(y[0]*u[1]-y[1]*u[0])/(y[0]*o[1]-y[1]*o[0]);if(n){const i=n[t.properties.a.index],w=n[t.properties.b.index],v=n[t.properties.c.index];let A;if(h<0||f<0||1-h-f<0){const E=h/(h+f),m=f/(h+f);A=h/w/(E/w+m/v),f=f/v/(E/w+m/v)}else A=h/w/(h/w+f/v+(1-h-f)/i),f=f/v/(h/w+f/v+(1-h-f)/i);h=A}return[h*b[0]+f*x[0]+d[0],h*b[1]+f*x[1]+d[1]]}function vt(e,t,n,r){const a=e.geometry.coordinates,s=n.geometry.coordinates,c=Math.atan2(a[0]-s[0],a[1]-s[1]),d=At(c,t[0]);if(d===void 0)throw new Error("Unable to determine vertex index");const g=t[1][d];return nt(e,g.features[0],r)}function st(e,t,n,r,a,s,c,d){let g;if(c&&(g=rt(e,F([c]))),!g){if(n){const l=e.geometry.coordinates,y=n.gridNum,o=n.xOrigin,u=n.yOrigin,b=n.xUnit,x=n.yUnit,h=n.gridCache,f=R(l[0],o,b,y),i=R(l[1],u,x,y),w=h[f]?h[f][i]?h[f][i]:[]:[];t=F(w.map(v=>t.features[v]))}g=rt(e,t)}return d&&d(g),g?nt(e,g,s):vt(e,r,a,s)}function R(e,t,n,r){let a=Math.floor((e-t)/n);return a>=r&&(a=r-1),a}function At(e,t){let n=it(e-t[0]),r=Math.PI*2,a;for(let s=0;s<t.length;s++){const c=(s+1)%t.length,d=it(e-t[c]),g=Math.min(Math.abs(n),Math.abs(d));n*d<=0&&g<r&&(r=g,a=s),n=d}return a}function it(e,t=!1){const n=t?function(r){return!(r>=0&&r<Math.PI*2)}:function(r){return!(r>-1*Math.PI&&r<=Math.PI)};for(;n(e);)e=e+2*Math.PI*(e>0?-1:1);return e}const L=2.00703,T=class T{constructor(){_(this,"points",[]);_(this,"pointsWeightBuffer");_(this,"strict_status");_(this,"vertices_params");_(this,"centroid");_(this,"edgeNodes");_(this,"edges");_(this,"tins");_(this,"kinks");_(this,"yaxisMode",T.YAXIS_INVERT);_(this,"strictMode",T.MODE_AUTO);_(this,"vertexMode",T.VERTEX_PLAIN);_(this,"bounds");_(this,"boundsPolygon");_(this,"wh");_(this,"xy");_(this,"indexedTins");_(this,"stateFull",!1);_(this,"stateTriangle");_(this,"stateBackward")}setCompiled(t){if(t.version||!t.tins&&t.points&&t.tins_points){this.points=t.points,this.pointsWeightBuffer=!t.version||t.version<2.00703?["forw","bakw"].reduce((r,a)=>{const s=t.weight_buffer[a];return s&&(r[a]=Object.keys(s).reduce((c,d)=>{const g=tt(d);return c[g]=s[d],c},{})),r},{}):t.weight_buffer,t.strict_status?this.strict_status=t.strict_status:t.kinks_points?this.strict_status=T.STATUS_ERROR:t.tins_points.length==2?this.strict_status=T.STATUS_LOOSE:this.strict_status=T.STATUS_STRICT,this.vertices_params={forw:[t.vertices_params[0]],bakw:[t.vertices_params[1]]},this.vertices_params.forw[1]=[0,1,2,3].map(r=>{const a=(r+1)%4,s=V(["c",`b${r}`,`b${a}`],t.points,t.edgeNodes||[],t.centroid_point,t.vertices_points,!1,L);return F([s])}),this.vertices_params.bakw[1]=[0,1,2,3].map(r=>{const a=(r+1)%4,s=V(["c",`b${r}`,`b${a}`],t.points,t.edgeNodes||[],t.centroid_point,t.vertices_points,!0,L);return F([s])}),this.centroid={forw:$(t.centroid_point[0],{target:{geom:t.centroid_point[1],index:"c"}}),bakw:$(t.centroid_point[1],{target:{geom:t.centroid_point[0],index:"c"}})},this.edges=et(t.edges||[]),this.edgeNodes=t.edgeNodes||[];const n=t.tins_points.length==1?0:1;this.tins={forw:F(t.tins_points[0].map(r=>V(r,t.points,t.edgeNodes||[],t.centroid_point,t.vertices_points,!1,t.version))),bakw:F(t.tins_points[n].map(r=>V(r,t.points,t.edgeNodes||[],t.centroid_point,t.vertices_points,!0,t.version)))},this.addIndexedTin(),t.kinks_points&&(this.kinks={bakw:F(t.kinks_points.map(r=>$(r)))}),t.yaxisMode?this.yaxisMode=t.yaxisMode:this.yaxisMode=T.YAXIS_INVERT,t.vertexMode&&(this.vertexMode=t.vertexMode),t.strictMode&&(this.strictMode=t.strictMode),t.bounds?(this.bounds=t.bounds,this.boundsPolygon=t.boundsPolygon,this.xy=t.xy,this.wh=t.wh):(this.xy=[0,0],t.wh&&(this.wh=t.wh),this.bounds=void 0,this.boundsPolygon=void 0)}else{t=JSON.parse(JSON.stringify(t).replace('"cent"','"c"').replace(/"bbox(\d+)"/g,'"b$1"')),this.tins=t.tins,this.addIndexedTin(),this.strict_status=t.strict_status,this.pointsWeightBuffer=t.weight_buffer,this.vertices_params=t.vertices_params,this.centroid=t.centroid,this.kinks=t.kinks;const n=[];for(let r=0;r<this.tins.forw.features.length;r++){const a=this.tins.forw.features[r];["a","b","c"].map((s,c)=>{const d=a.geometry.coordinates[0][c],g=a.properties[s].geom,l=a.properties[s].index;typeof l=="number"&&(n[l]=[d,g])})}this.points=n}}addIndexedTin(){const t=this.tins,n=t.forw,r=t.bakw,a=Math.ceil(Math.sqrt(n.features.length));if(a<3){this.indexedTins=void 0;return}let s=[],c=[];const d=n.features.map(h=>{let f=[];return Z(h)[0].map(i=>{s.length===0?s=[Array.from(i),Array.from(i)]:(i[0]<s[0][0]&&(s[0][0]=i[0]),i[0]>s[1][0]&&(s[1][0]=i[0]),i[1]<s[0][1]&&(s[0][1]=i[1]),i[1]>s[1][1]&&(s[1][1]=i[1])),f.length===0?f=[Array.from(i),Array.from(i)]:(i[0]<f[0][0]&&(f[0][0]=i[0]),i[0]>f[1][0]&&(f[1][0]=i[0]),i[1]<f[0][1]&&(f[0][1]=i[1]),i[1]>f[1][1]&&(f[1][1]=i[1]))}),f}),g=(s[1][0]-s[0][0])/a,l=(s[1][1]-s[0][1])/a,y=d.reduce((h,f,i)=>{const w=R(f[0][0],s[0][0],g,a),v=R(f[1][0],s[0][0],g,a),A=R(f[0][1],s[0][1],l,a),E=R(f[1][1],s[0][1],l,a);for(let m=w;m<=v;m++){h[m]||(h[m]=[]);for(let M=A;M<=E;M++)h[m][M]||(h[m][M]=[]),h[m][M].push(i)}return h},[]),o=r.features.map(h=>{let f=[];return Z(h)[0].map(i=>{c.length===0?c=[Array.from(i),Array.from(i)]:(i[0]<c[0][0]&&(c[0][0]=i[0]),i[0]>c[1][0]&&(c[1][0]=i[0]),i[1]<c[0][1]&&(c[0][1]=i[1]),i[1]>c[1][1]&&(c[1][1]=i[1])),f.length===0?f=[Array.from(i),Array.from(i)]:(i[0]<f[0][0]&&(f[0][0]=i[0]),i[0]>f[1][0]&&(f[1][0]=i[0]),i[1]<f[0][1]&&(f[0][1]=i[1]),i[1]>f[1][1]&&(f[1][1]=i[1]))}),f}),u=(c[1][0]-c[0][0])/a,b=(c[1][1]-c[0][1])/a,x=o.reduce((h,f,i)=>{const w=R(f[0][0],c[0][0],u,a),v=R(f[1][0],c[0][0],u,a),A=R(f[0][1],c[0][1],b,a),E=R(f[1][1],c[0][1],b,a);for(let m=w;m<=v;m++){h[m]||(h[m]=[]);for(let M=A;M<=E;M++)h[m][M]||(h[m][M]=[]),h[m][M].push(i)}return h},[]);this.indexedTins={forw:{gridNum:a,xOrigin:s[0][0],yOrigin:s[0][1],xUnit:g,yUnit:l,gridCache:y},bakw:{gridNum:a,xOrigin:c[0][0],yOrigin:c[0][1],xUnit:u,yUnit:b,gridCache:x}}}transform(t,n,r){if(n&&this.strict_status==T.STATUS_ERROR)throw'Backward transform is not allowed if strict_status == "strict_error"';this.yaxisMode==T.YAXIS_FOLLOW&&n&&(t=[t[0],-1*t[1]]);const a=$(t);if(this.bounds&&!n&&!r&&!q(a,this.boundsPolygon))return!1;const s=n?this.tins.bakw:this.tins.forw,c=n?this.indexedTins.bakw:this.indexedTins.forw,d=n?this.vertices_params.bakw:this.vertices_params.forw,g=n?this.centroid.bakw:this.centroid.forw,l=n?this.pointsWeightBuffer.bakw:this.pointsWeightBuffer.forw;let y,o;this.stateFull&&(this.stateBackward==n?y=this.stateTriangle:(this.stateBackward=n,this.stateTriangle=void 0),o=b=>{this.stateTriangle=b});let u=st(a,s,c,d,g,l,y,o);if(this.bounds&&n&&!r){const b=$(u);if(!q(b,this.boundsPolygon))return!1}else this.yaxisMode==T.YAXIS_FOLLOW&&!n&&(u=[u[0],-1*u[1]]);return u}};_(T,"VERTEX_PLAIN","plain"),_(T,"VERTEX_BIRDEYE","birdeye"),_(T,"MODE_STRICT","strict"),_(T,"MODE_AUTO","auto"),_(T,"MODE_LOOSE","loose"),_(T,"STATUS_STRICT","strict"),_(T,"STATUS_ERROR","strict_error"),_(T,"STATUS_LOOSE","loose"),_(T,"YAXIS_FOLLOW","follow"),_(T,"YAXIS_INVERT","invert");let j=T;p.Transform=j,p.counterTri=xt,p.format_version=L,p.normalizeEdges=et,p.rotateVerticesTriangle=mt,p.transformArr=st,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(S,T){typeof exports=="object"&&typeof module<"u"?T(exports):typeof define=="function"&&define.amd?define(["exports"],T):(S=typeof globalThis<"u"?globalThis:S||self,T(S.maplat_transform={}))})(this,function(S){"use strict";const T=11102230246251565e-32,A=134217729,st=(3+8*T)*T;function Y(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 ot(t,e){let r=e[0];for(let n=1;n<t;n++)r+=e[n];return r}function X(t){return new Float64Array(t)}const at=(3+16*T)*T,ct=(2+12*T)*T,ut=(9+64*T)*T*T,U=X(4),q=X(8),z=X(12),j=X(16),P=X(4);function ft(t,e,r,n,c,i,f){let g,h,l,y,o,u,b,x,d,a,s,m,_,p,M,w,v,E;const k=t-c,I=r-c,N=e-i,B=n-i;p=k*B,u=A*k,b=u-(u-k),x=k-b,u=A*B,d=u-(u-B),a=B-d,M=x*a-(p-b*d-x*d-b*a),w=N*I,u=A*N,b=u-(u-N),x=N-b,u=A*I,d=u-(u-I),a=I-d,v=x*a-(w-b*d-x*d-b*a),s=M-v,o=M-s,U[0]=M-(s+o)+(o-v),m=p+s,o=m-p,_=p-(m-o)+(s-o),s=_-w,o=_-s,U[1]=_-(s+o)+(o-w),E=m+s,o=E-m,U[2]=m-(E-o)+(s-o),U[3]=E;let R=ot(4,U),V=ct*f;if(R>=V||-R>=V||(o=t-k,g=t-(k+o)+(o-c),o=r-I,l=r-(I+o)+(o-c),o=e-N,h=e-(N+o)+(o-i),o=n-B,y=n-(B+o)+(o-i),g===0&&h===0&&l===0&&y===0)||(V=ut*f+st*Math.abs(R),R+=k*y+B*g-(N*l+I*h),R>=V||-R>=V))return R;p=g*B,u=A*g,b=u-(u-g),x=g-b,u=A*B,d=u-(u-B),a=B-d,M=x*a-(p-b*d-x*d-b*a),w=h*I,u=A*h,b=u-(u-h),x=h-b,u=A*I,d=u-(u-I),a=I-d,v=x*a-(w-b*d-x*d-b*a),s=M-v,o=M-s,P[0]=M-(s+o)+(o-v),m=p+s,o=m-p,_=p-(m-o)+(s-o),s=_-w,o=_-s,P[1]=_-(s+o)+(o-w),E=m+s,o=E-m,P[2]=m-(E-o)+(s-o),P[3]=E;const Ct=Y(4,U,4,P,q);p=k*y,u=A*k,b=u-(u-k),x=k-b,u=A*y,d=u-(u-y),a=y-d,M=x*a-(p-b*d-x*d-b*a),w=N*l,u=A*N,b=u-(u-N),x=N-b,u=A*l,d=u-(u-l),a=l-d,v=x*a-(w-b*d-x*d-b*a),s=M-v,o=M-s,P[0]=M-(s+o)+(o-v),m=p+s,o=m-p,_=p-(m-o)+(s-o),s=_-w,o=_-s,P[1]=_-(s+o)+(o-w),E=m+s,o=E-m,P[2]=m-(E-o)+(s-o),P[3]=E;const Rt=Y(Ct,q,4,P,z);p=g*y,u=A*g,b=u-(u-g),x=g-b,u=A*y,d=u-(u-y),a=y-d,M=x*a-(p-b*d-x*d-b*a),w=h*l,u=A*h,b=u-(u-h),x=h-b,u=A*l,d=u-(u-l),a=l-d,v=x*a-(w-b*d-x*d-b*a),s=M-v,o=M-s,P[0]=M-(s+o)+(o-v),m=p+s,o=m-p,_=p-(m-o)+(s-o),s=_-w,o=_-s,P[1]=_-(s+o)+(o-w),E=m+s,o=E-m,P[2]=m-(E-o)+(s-o),P[3]=E;const Ut=Y(Rt,z,4,P,j);return j[Ut-1]}function dt(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)>=at*l?h:-ft(t,e,r,n,c,i,l)}function ht(t,e){var r,n,c=0,i,f,g,h,l,y,o,u=t[0],b=t[1],x=e.length;for(r=0;r<x;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=dt(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 G(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 W(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 G({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 G({type:"Polygon",coordinates:t},e,r)}function F(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 gt(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 lt(t){return t.type==="Feature"?t.geometry:t}function yt(t,e,r={}){if(!t)throw new Error("point is required");if(!e)throw new Error("polygon is required");const n=gt(t),c=lt(e),i=c.type,f=e.bbox;let g=c.coordinates;if(f&&bt(n,f)===!1)return!1;i==="Polygon"&&(g=[g]);let h=!1;for(var l=0;l<g.length;++l){const y=ht(n,g[l]);if(y===0)return!r.ignoreBoundary;y&&(h=!0)}return h}function bt(t,e){return e[0]<=t[0]&&e[1]<=t[1]&&e[2]>=t[0]&&e[3]>=t[1]}var L=yt;function H(t,e){for(let r=0;r<e.features.length;r++)if(L(t,e.features[r]))return e.features[r]}function Z(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]],x=[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],_=r[e.properties.c.index];let p;if(d<0||a<0||1-d-a<0){const M=d/(d+a),w=a/(d+a);p=d/m/(M/m+w/_),a=a/_/(M/m+w/_)}else p=d/m/(d/m+a/_+(1-d-a)/s),a=a/_/(d/m+a/_+(1-d-a)/s);d=p}return[d*b[0]+a*x[0]+g[0],d*b[1]+a*x[1]+g[1]]}function mt(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=wt(f,e[0]);if(g===void 0)throw new Error("Unable to determine vertex index");const h=e[1][g];return Z(t,h.features[0],n)}function tt(t,e,r,n,c,i,f,g){let h;if(f&&(h=H(t,F([f]))),!h){if(r){const l=t.geometry.coordinates,y=r.gridNum,o=r.xOrigin,u=r.yOrigin,b=r.xUnit,x=r.yUnit,d=r.gridCache,a=C(l[0],o,b,y),s=C(l[1],u,x,y),m=d[a]?d[a][s]?d[a][s]:[]:[];e=F(m.map(_=>e.features[_]))}h=H(t,e)}return g&&g(h),h?Z(t,h,i):mt(t,n,c,i)}function C(t,e,r,n){let c=Math.floor((t-e)/r);return c>=n&&(c=n-1),c}function wt(t,e){let r=et(t-e[0]),n=Math.PI*2,c;for(let i=0;i<e.length;i++){const f=(i+1)%e.length,g=et(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 et(t,e=!1){const r=e?function(n){return!(n>=0&&n<Math.PI*2)}:function(n){return!(n>-1*Math.PI&&n<=Math.PI)};for(;r(t);)t=t+2*Math.PI*(t>0?-1:1);return t}function rt(t,e){return e&&e>=2.00703||Array.isArray(t[0])?t:t.map(r=>[r.illstNodes,r.mercNodes,r.startEnd])}function xt(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 _t(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 pt(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 $(t,e,r,n,c,i=!1,f){const g=t.map(h=>{(!f||f<2.00703)&&(h=nt(h));const l=isFinite(h)?e[h]:h==="c"?n:h==="b0"?c[0]:h==="b1"?c[1]:h==="b2"?c[2]:h==="b3"?c[3]:function(){const y=h.match(/e(\d+)/);if(y){const o=parseInt(y[1]);return r[o]}throw"Bad index value for indexesToTri"}();return i?[[l[1],l[0]],h]:[[l[0],l[1]],h]});return pt(g)}function nt(t){return typeof t=="number"?t:t.replace(/^(c|e|b)(?:ent|dgeNode|box)(\d+)?$/,"$1$2")}const D=2.00703;function vt(t){return!!(t.version||!t.tins&&t.points&&t.tins_points)}function Mt(t){return{points:t.points,pointsWeightBuffer:Et(t),strictStatus:Pt(t),verticesParams:Tt(t),centroid:St(t),edges:rt(t.edges||[]),edgeNodes:t.edgeNodes||[],tins:Ot(t),kinks:kt(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.bounds?t.xy:[0,0]}}function At(t){const e=It(t),r=e.tins;return{compiled:e,tins:r,points:Nt(r),strictStatus:e.strict_status,pointsWeightBuffer:e.weight_buffer,verticesParams:e.vertices_params,centroid:e.centroid,kinks:e.kinks}}function Et(t){return!t.version||t.version<D?["forw","bakw"].reduce((e,r)=>{const n=t.weight_buffer[r];return n&&(e[r]=Object.keys(n).reduce((c,i)=>{const f=nt(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 Tt(t){const e={forw:[t.vertices_params[0]],bakw:[t.vertices_params[1]]};return e.forw[1]=it(t,!1),e.bakw[1]=it(t,!0),e}function it(t,e){return[0,1,2,3].map(r=>{const n=(r+1)%4,c=$(["c",`b${r}`,`b${n}`],t.points,t.edgeNodes||[],t.centroid_point,t.vertices_points,e,D);return F([c])})}function St(t){return{forw:W(t.centroid_point[0],{target:{geom:t.centroid_point[1],index:"c"}}),bakw:W(t.centroid_point[1],{target:{geom:t.centroid_point[0],index:"c"}})}}function Ot(t){const e=t.tins_points.length===1?0:1;return{forw:F(t.tins_points[0].map(r=>$(r,t.points,t.edgeNodes||[],t.centroid_point,t.vertices_points,!1,t.version))),bakw:F(t.tins_points[e].map(r=>$(r,t.points,t.edgeNodes||[],t.centroid_point,t.vertices_points,!0,t.version)))}}function kt(t){if(t)return{bakw:F(t.map(e=>W(e)))}}function It(t){return JSON.parse(JSON.stringify(t).replace('"cent"','"c"').replace(/"bbox(\d+)"/g,'"b$1"'))}function Nt(t){const e=[],r=t.forw.features;for(let n=0;n<r.length;n++){const c=r[n];["a","b","c"].map((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 Bt=D;class O{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=O.YAXIS_INVERT;strictMode=O.MODE_AUTO;vertexMode=O.VERTEX_PLAIN;bounds;boundsPolygon;wh;xy;indexedTins;stateFull=!1;stateTriangle;stateBackward;constructor(){}setCompiled(e){if(vt(e)){this.applyModernState(Mt(e));return}this.applyLegacyState(At(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??O.YAXIS_INVERT,this.vertexMode=e.vertexMode??O.VERTEX_PLAIN,this.strictMode=e.strictMode??O.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=C(a[0][0],i[0][0],h,c),_=C(a[1][0],i[0][0],h,c),p=C(a[0][1],i[0][1],l,c),M=C(a[1][1],i[0][1],l,c);for(let w=m;w<=_;w++){d[w]||(d[w]=[]);for(let v=p;v<=M;v++)d[w][v]||(d[w][v]=[]),d[w][v].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,x=o.reduce((d,a,s)=>{const m=C(a[0][0],f[0][0],u,c),_=C(a[1][0],f[0][0],u,c),p=C(a[0][1],f[0][1],b,c),M=C(a[1][1],f[0][1],b,c);for(let w=m;w<=_;w++){d[w]||(d[w]=[]);for(let v=p;v<=M;v++)d[w][v]||(d[w][v]=[]),d[w][v].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:x}}}transform(e,r,n){if(r&&this.strict_status==O.STATUS_ERROR)throw'Backward transform is not allowed if strict_status == "strict_error"';this.yaxisMode==O.YAXIS_FOLLOW&&r&&(e=[e[0],-1*e[1]]);const c=W(e);if(this.bounds&&!r&&!n&&!L(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=tt(c,i,f,g,h,l,y,o);if(this.bounds&&r&&!n){const b=W(u);if(!L(b,this.boundsPolygon))return!1}else this.yaxisMode==O.YAXIS_FOLLOW&&!r&&(u=[u[0],-1*u[1]]);return u}}S.Transform=O,S.counterTri=_t,S.format_version=Bt,S.normalizeEdges=rt,S.rotateVerticesTriangle=xt,S.transformArr=tt,Object.defineProperty(S,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.
|
|
7
|
+
"version": "0.3.0",
|
|
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
|
"main": "./dist/maplat_transform.cjs",
|
|
@@ -13,36 +13,19 @@
|
|
|
13
13
|
"types": "./dist/index.d.ts",
|
|
14
14
|
"exports": {
|
|
15
15
|
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"browser": "./dist/maplat_transform.umd.js",
|
|
16
18
|
"import": "./dist/maplat_transform.js",
|
|
17
19
|
"require": "./dist/maplat_transform.cjs",
|
|
18
|
-
"
|
|
19
|
-
"types": "./dist/index.d.ts"
|
|
20
|
+
"default": "./dist/maplat_transform.js"
|
|
20
21
|
}
|
|
21
22
|
},
|
|
22
23
|
"files": [
|
|
23
24
|
"dist",
|
|
24
25
|
"src",
|
|
25
|
-
"README.md"
|
|
26
|
+
"README.md",
|
|
27
|
+
"README.ja.md"
|
|
26
28
|
],
|
|
27
|
-
"scripts": {
|
|
28
|
-
"dev": "vite",
|
|
29
|
-
"build": "npm run typecheck && cross-env BUILD_MODE=package vite build",
|
|
30
|
-
"typecheck": "tsc --noEmit --allowImportingTsExtensions",
|
|
31
|
-
"deploy": "cp public/*.html ./ && tsc && vite build",
|
|
32
|
-
"test": "vitest run",
|
|
33
|
-
"test:watch": "vitest",
|
|
34
|
-
"coverage": "vitest run --coverage",
|
|
35
|
-
"lint": "eslint src tests",
|
|
36
|
-
"prepublishOnly": "npm run lint && npm run typecheck && npm run test && npm run build",
|
|
37
|
-
"version:bump": "node scripts/bump-version.js",
|
|
38
|
-
"version:sync": "node scripts/sync-version.js",
|
|
39
|
-
"publish:npm": "node scripts/publish-npm.js",
|
|
40
|
-
"publish:npm:dry": "node scripts/publish-npm.js --dry-run",
|
|
41
|
-
"publish:deno": "node scripts/publish-deno.js",
|
|
42
|
-
"publish:deno:dry": "node scripts/publish-deno.js --dry-run",
|
|
43
|
-
"publish:all": "node scripts/publish-all.js",
|
|
44
|
-
"publish:all:dry": "node scripts/publish-all.js --dry-run"
|
|
45
|
-
},
|
|
46
29
|
"repository": {
|
|
47
30
|
"type": "git",
|
|
48
31
|
"url": "git+https://github.com/code4history/MaplatTransform.git"
|
|
@@ -59,29 +42,42 @@
|
|
|
59
42
|
"Maplat",
|
|
60
43
|
"MaplatTransform"
|
|
61
44
|
],
|
|
62
|
-
"peerDependencies": {},
|
|
63
45
|
"devDependencies": {
|
|
64
|
-
"@eslint/js": "^9.
|
|
65
|
-
"@testing-library/jest-dom": "^6.
|
|
46
|
+
"@eslint/js": "^9.39.1",
|
|
47
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
66
48
|
"@types/geojson": "^7946.0.15",
|
|
67
|
-
"@types/node": "^
|
|
68
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
69
|
-
"@typescript-eslint/parser": "^8.
|
|
70
|
-
"@vitest/coverage-v8": "^
|
|
71
|
-
"cross-env": "^
|
|
72
|
-
"eslint": "^9.
|
|
73
|
-
"eslint-config-prettier": "^
|
|
49
|
+
"@types/node": "^24.10.0",
|
|
50
|
+
"@typescript-eslint/eslint-plugin": "^8.46.3",
|
|
51
|
+
"@typescript-eslint/parser": "^8.46.3",
|
|
52
|
+
"@vitest/coverage-v8": "^4.0.8",
|
|
53
|
+
"cross-env": "^10.1.0",
|
|
54
|
+
"eslint": "^9.39.1",
|
|
55
|
+
"eslint-config-prettier": "^10.1.8",
|
|
74
56
|
"jest-matcher-deep-close-to": "^3.0.2",
|
|
75
|
-
"jsdom": "^
|
|
57
|
+
"jsdom": "^27.1.0",
|
|
76
58
|
"prettier": "^3.4.2",
|
|
77
|
-
"typescript": "^5.
|
|
78
|
-
"vite": "^
|
|
59
|
+
"typescript": "^5.9.3",
|
|
60
|
+
"vite": "^7.2.2",
|
|
79
61
|
"vite-plugin-dts": "^4.4.0",
|
|
80
|
-
"vitest": "^
|
|
62
|
+
"vitest": "^4.0.8"
|
|
81
63
|
},
|
|
82
64
|
"dependencies": {
|
|
83
65
|
"@turf/boolean-point-in-polygon": "^7.2.0",
|
|
84
|
-
"@turf/helpers": "7.2.0",
|
|
66
|
+
"@turf/helpers": "^7.2.0",
|
|
85
67
|
"@turf/invariant": "^7.2.0"
|
|
68
|
+
},
|
|
69
|
+
"scripts": {
|
|
70
|
+
"dev": "vite",
|
|
71
|
+
"build": "npm run typecheck && cross-env BUILD_MODE=package vite build",
|
|
72
|
+
"typecheck": "tsc --noEmit --allowImportingTsExtensions",
|
|
73
|
+
"deploy": "cp public/*.html ./ && tsc && vite build",
|
|
74
|
+
"test": "vitest run",
|
|
75
|
+
"test:watch": "vitest",
|
|
76
|
+
"coverage": "vitest run --coverage",
|
|
77
|
+
"lint": "eslint src tests",
|
|
78
|
+
"version:bump": "node scripts/bump-version.js",
|
|
79
|
+
"version:sync": "node scripts/sync-version.js",
|
|
80
|
+
"publish:npm": "node scripts/publish-npm.js",
|
|
81
|
+
"publish:npm:dry": "node scripts/publish-npm.js --dry-run"
|
|
86
82
|
}
|
|
87
83
|
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { featureCollection, point } from "@turf/helpers";
|
|
2
|
+
import type { Position } from "geojson";
|
|
3
|
+
import { normalizeEdges } from "./edgeutils.ts";
|
|
4
|
+
import { indexesToTri, normalizeNodeKey } from "./triangulation.ts";
|
|
5
|
+
import type { PropertyTriKey } from "./geometry.ts";
|
|
6
|
+
import type {
|
|
7
|
+
BiDirectionKey,
|
|
8
|
+
Compiled,
|
|
9
|
+
CompiledLegacy,
|
|
10
|
+
LegacyStatePayload,
|
|
11
|
+
ModernStatePayload,
|
|
12
|
+
PointSet,
|
|
13
|
+
StrictStatus,
|
|
14
|
+
TinsBD,
|
|
15
|
+
VerticesParamsBD,
|
|
16
|
+
WeightBufferBD
|
|
17
|
+
} from "./types.ts";
|
|
18
|
+
import type { WeightBuffer } from "./geometry.ts";
|
|
19
|
+
|
|
20
|
+
export const FORMAT_VERSION = 2.00703;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Type guard for discriminating modern compiled payloads.
|
|
24
|
+
*/
|
|
25
|
+
export function isModernCompiled(
|
|
26
|
+
compiled: Compiled | CompiledLegacy
|
|
27
|
+
): compiled is Compiled {
|
|
28
|
+
return Boolean(
|
|
29
|
+
compiled.version ||
|
|
30
|
+
(!(compiled as CompiledLegacy).tins && compiled.points && compiled.tins_points)
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Restore the in-memory state produced by modern compiled payloads.
|
|
36
|
+
*/
|
|
37
|
+
export function restoreModernState(compiled: Compiled): ModernStatePayload {
|
|
38
|
+
return {
|
|
39
|
+
points: compiled.points,
|
|
40
|
+
pointsWeightBuffer: normalizeWeightBuffer(compiled),
|
|
41
|
+
strictStatus: deriveStrictStatus(compiled),
|
|
42
|
+
verticesParams: buildVerticesParams(compiled),
|
|
43
|
+
centroid: buildCentroid(compiled),
|
|
44
|
+
edges: normalizeEdges(compiled.edges || []),
|
|
45
|
+
edgeNodes: compiled.edgeNodes || [],
|
|
46
|
+
tins: buildTins(compiled),
|
|
47
|
+
kinks: buildKinks(compiled.kinks_points),
|
|
48
|
+
yaxisMode: compiled.yaxisMode ?? "invert",
|
|
49
|
+
strictMode: compiled.strictMode ?? "auto",
|
|
50
|
+
vertexMode: compiled.vertexMode,
|
|
51
|
+
bounds: compiled.bounds,
|
|
52
|
+
boundsPolygon: compiled.boundsPolygon,
|
|
53
|
+
wh: compiled.wh,
|
|
54
|
+
xy: compiled.bounds ? compiled.xy : [0, 0]
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Restore the in-memory state produced by the legacy payloads.
|
|
60
|
+
*/
|
|
61
|
+
export function restoreLegacyState(
|
|
62
|
+
rawCompiled: CompiledLegacy
|
|
63
|
+
): LegacyStatePayload {
|
|
64
|
+
const normalized = normalizeLegacyStructure(rawCompiled);
|
|
65
|
+
const tins = normalized.tins!;
|
|
66
|
+
return {
|
|
67
|
+
compiled: normalized,
|
|
68
|
+
tins,
|
|
69
|
+
points: rebuildLegacyPoints(tins),
|
|
70
|
+
strictStatus: normalized.strict_status,
|
|
71
|
+
pointsWeightBuffer: normalized.weight_buffer,
|
|
72
|
+
verticesParams: normalized.vertices_params as VerticesParamsBD,
|
|
73
|
+
centroid: normalized.centroid,
|
|
74
|
+
kinks: normalized.kinks
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function normalizeWeightBuffer(compiled: Compiled): WeightBufferBD {
|
|
79
|
+
if (!compiled.version || compiled.version < FORMAT_VERSION) {
|
|
80
|
+
return (["forw", "bakw"] as BiDirectionKey[]).reduce((bd, forb) => {
|
|
81
|
+
const base = compiled.weight_buffer[forb];
|
|
82
|
+
if (base) {
|
|
83
|
+
bd[forb] = Object.keys(base).reduce((buffer, key) => {
|
|
84
|
+
const normKey = normalizeNodeKey(key);
|
|
85
|
+
buffer[normKey] = base[key];
|
|
86
|
+
return buffer;
|
|
87
|
+
}, {} as WeightBuffer);
|
|
88
|
+
}
|
|
89
|
+
return bd;
|
|
90
|
+
}, {} as WeightBufferBD);
|
|
91
|
+
}
|
|
92
|
+
return compiled.weight_buffer;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function deriveStrictStatus(compiled: Compiled): StrictStatus {
|
|
96
|
+
if (compiled.strict_status) return compiled.strict_status;
|
|
97
|
+
if (compiled.kinks_points) return "strict_error";
|
|
98
|
+
if (compiled.tins_points.length === 2) return "loose";
|
|
99
|
+
return "strict";
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function buildVerticesParams(compiled: Compiled): VerticesParamsBD {
|
|
103
|
+
const params: VerticesParamsBD = {
|
|
104
|
+
forw: [compiled.vertices_params[0]],
|
|
105
|
+
bakw: [compiled.vertices_params[1]]
|
|
106
|
+
};
|
|
107
|
+
params.forw![1] = buildVertexTins(compiled, false);
|
|
108
|
+
params.bakw![1] = buildVertexTins(compiled, true);
|
|
109
|
+
return params;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function buildVertexTins(compiled: Compiled, bakw: boolean) {
|
|
113
|
+
return [0, 1, 2, 3].map(idx => {
|
|
114
|
+
const idxNxt = (idx + 1) % 4;
|
|
115
|
+
const tri = indexesToTri(
|
|
116
|
+
["c", `b${idx}`, `b${idxNxt}`],
|
|
117
|
+
compiled.points,
|
|
118
|
+
compiled.edgeNodes || [],
|
|
119
|
+
compiled.centroid_point,
|
|
120
|
+
compiled.vertices_points,
|
|
121
|
+
bakw,
|
|
122
|
+
FORMAT_VERSION
|
|
123
|
+
);
|
|
124
|
+
return featureCollection([tri]);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function buildCentroid(compiled: Compiled) {
|
|
129
|
+
return {
|
|
130
|
+
forw: point(compiled.centroid_point[0], {
|
|
131
|
+
target: {
|
|
132
|
+
geom: compiled.centroid_point[1],
|
|
133
|
+
index: "c"
|
|
134
|
+
}
|
|
135
|
+
}),
|
|
136
|
+
bakw: point(compiled.centroid_point[1], {
|
|
137
|
+
target: {
|
|
138
|
+
geom: compiled.centroid_point[0],
|
|
139
|
+
index: "c"
|
|
140
|
+
}
|
|
141
|
+
})
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function buildTins(compiled: Compiled) {
|
|
146
|
+
const bakwIndex = compiled.tins_points.length === 1 ? 0 : 1;
|
|
147
|
+
return {
|
|
148
|
+
forw: featureCollection(
|
|
149
|
+
compiled.tins_points[0].map(idxes =>
|
|
150
|
+
indexesToTri(
|
|
151
|
+
idxes,
|
|
152
|
+
compiled.points,
|
|
153
|
+
compiled.edgeNodes || [],
|
|
154
|
+
compiled.centroid_point,
|
|
155
|
+
compiled.vertices_points,
|
|
156
|
+
false,
|
|
157
|
+
compiled.version
|
|
158
|
+
)
|
|
159
|
+
)
|
|
160
|
+
),
|
|
161
|
+
bakw: featureCollection(
|
|
162
|
+
compiled.tins_points[bakwIndex].map(idxes =>
|
|
163
|
+
indexesToTri(
|
|
164
|
+
idxes,
|
|
165
|
+
compiled.points,
|
|
166
|
+
compiled.edgeNodes || [],
|
|
167
|
+
compiled.centroid_point,
|
|
168
|
+
compiled.vertices_points,
|
|
169
|
+
true,
|
|
170
|
+
compiled.version
|
|
171
|
+
)
|
|
172
|
+
)
|
|
173
|
+
)
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function buildKinks(kinksPoints?: Position[]) {
|
|
178
|
+
if (!kinksPoints) return undefined;
|
|
179
|
+
return {
|
|
180
|
+
bakw: featureCollection(
|
|
181
|
+
kinksPoints.map((coord: Position) => point(coord))
|
|
182
|
+
)
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function normalizeLegacyStructure(
|
|
187
|
+
compiled: CompiledLegacy
|
|
188
|
+
): CompiledLegacy {
|
|
189
|
+
return JSON.parse(
|
|
190
|
+
JSON.stringify(compiled)
|
|
191
|
+
.replace('"cent"', '"c"')
|
|
192
|
+
.replace(/"bbox(\d+)"/g, '"b$1"')
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function rebuildLegacyPoints(tins: TinsBD): PointSet[] {
|
|
197
|
+
const points: PointSet[] = [];
|
|
198
|
+
const features = tins.forw!.features;
|
|
199
|
+
for (let i = 0; i < features.length; i++) {
|
|
200
|
+
const tri = features[i];
|
|
201
|
+
(["a", "b", "c"] as PropertyTriKey[]).map((key, idx) => {
|
|
202
|
+
const forw = tri.geometry!.coordinates[0][idx];
|
|
203
|
+
const bakw = tri.properties![key].geom;
|
|
204
|
+
const pIdx = tri.properties![key].index;
|
|
205
|
+
if (typeof pIdx === "number") {
|
|
206
|
+
points[pIdx] = [forw, bakw];
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
return points;
|
|
211
|
+
}
|
package/src/edgeutils.ts
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import type { Position } from "geojson";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* エッジの終点を表す型
|
|
5
|
-
* [始点のインデックス, 終点のインデックス]
|
|
6
|
-
*/
|
|
7
|
-
export type Edge = [number, number];
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* エッジセットの型
|
|
11
|
-
* [始点と終点の間の中間点群(地図座標系),
|
|
12
|
-
* 始点と終点の間の中間点群(変換先座標系),
|
|
13
|
-
* 始点と終点のインデックスペア]
|
|
14
|
-
*/
|
|
15
|
-
export type EdgeSet = [Position[], Position[], Edge];
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 古いバージョンのエッジセット型
|
|
19
|
-
* @deprecated 2.00703以降は EdgeSet を使用
|
|
20
|
-
*/
|
|
21
|
-
export type EdgeSetLegacy = {
|
|
22
|
-
illstNodes: Position[];
|
|
23
|
-
mercNodes: Position[];
|
|
24
|
-
startEnd: Edge;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* エッジセットを正規化する
|
|
29
|
-
* 古いバージョンのフォーマットを新しいフォーマットに変換する
|
|
30
|
-
*
|
|
31
|
-
* @param edges エッジセット配列
|
|
32
|
-
* @param version バージョン番号(オプション)
|
|
33
|
-
* @returns 正規化されたエッジセット配列
|
|
34
|
-
*/
|
|
35
|
-
function normalizeEdges(
|
|
36
|
-
edges: EdgeSet[] | EdgeSetLegacy[],
|
|
37
|
-
version?: number
|
|
38
|
-
): EdgeSet[] {
|
|
39
|
-
if ((version && version >= 2.00703) || Array.isArray(edges[0]))
|
|
40
|
-
return edges as EdgeSet[];
|
|
41
|
-
return (edges as EdgeSetLegacy[]).map(edge => [
|
|
42
|
-
edge.illstNodes,
|
|
43
|
-
edge.mercNodes,
|
|
44
|
-
edge.startEnd
|
|
45
|
-
]);
|
|
46
|
-
}
|
|
47
|
-
|
|
1
|
+
import type { Position } from "geojson";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* エッジの終点を表す型
|
|
5
|
+
* [始点のインデックス, 終点のインデックス]
|
|
6
|
+
*/
|
|
7
|
+
export type Edge = [number, number];
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* エッジセットの型
|
|
11
|
+
* [始点と終点の間の中間点群(地図座標系),
|
|
12
|
+
* 始点と終点の間の中間点群(変換先座標系),
|
|
13
|
+
* 始点と終点のインデックスペア]
|
|
14
|
+
*/
|
|
15
|
+
export type EdgeSet = [Position[], Position[], Edge];
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 古いバージョンのエッジセット型
|
|
19
|
+
* @deprecated 2.00703以降は EdgeSet を使用
|
|
20
|
+
*/
|
|
21
|
+
export type EdgeSetLegacy = {
|
|
22
|
+
illstNodes: Position[];
|
|
23
|
+
mercNodes: Position[];
|
|
24
|
+
startEnd: Edge;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* エッジセットを正規化する
|
|
29
|
+
* 古いバージョンのフォーマットを新しいフォーマットに変換する
|
|
30
|
+
*
|
|
31
|
+
* @param edges エッジセット配列
|
|
32
|
+
* @param version バージョン番号(オプション)
|
|
33
|
+
* @returns 正規化されたエッジセット配列
|
|
34
|
+
*/
|
|
35
|
+
function normalizeEdges(
|
|
36
|
+
edges: EdgeSet[] | EdgeSetLegacy[],
|
|
37
|
+
version?: number
|
|
38
|
+
): EdgeSet[] {
|
|
39
|
+
if ((version && version >= 2.00703) || Array.isArray(edges[0]))
|
|
40
|
+
return edges as EdgeSet[];
|
|
41
|
+
return (edges as EdgeSetLegacy[]).map(edge => [
|
|
42
|
+
edge.illstNodes,
|
|
43
|
+
edge.mercNodes,
|
|
44
|
+
edge.startEnd
|
|
45
|
+
]);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
48
|
export { normalizeEdges };
|