UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 2.51 kB
import{getLength as n,getMidpoint as t,getRingArea as l}from"./coordsUtils.js";function e(n){return n?n.hasZ?[n.xmax-n.xmin/2,n.ymax-n.ymin/2,n.zmax-n.zmin/2]:[n.xmax-n.xmin/2,n.ymax-n.ymin/2]:null}function r(n){return n?o(n.rings,n.hasZ??!1):null}function o(n,t){if(!n||!n.length)return null;const l=[],e=[],r=t?[1/0,-1/0,1/0,-1/0,1/0,-1/0]:[1/0,-1/0,1/0,-1/0];for(let o=0,i=n.length;o<i;o++){const l=u(n[o],t,r);l&&e.push(l)}if(e.sort(((n,l)=>{let e=n[2]-l[2];return 0===e&&t&&(e=n[4]-l[4]),e})),e.length&&(l[0]=e[0][0],l[1]=e[0][1],t&&(l[2]=e[0][3]),(l[0]<r[0]||l[0]>r[1]||l[1]<r[2]||l[1]>r[3]||t&&(l[2]<r[4]||l[2]>r[5]))&&(l.length=0)),!l.length){const e=n[0]&&n[0].length?i(n[0],t):null;if(!e)return null;l[0]=e[0],l[1]=e[1],t&&e.length>2&&(l[2]=e[2])}return l}function u(n,t,l){let e=0,r=0,o=0,u=0,i=0;const s=n.length?n[0][0]:0,g=n.length?n[0][1]:0,h=n.length&&t?n[0][2]:0;for(let f=0;f<n.length;f++){const c=n[f],m=n[(f+1)%n.length],[x,a,y]=c,p=x-s,z=a-g,[Z,d,j]=m,U=Z-s,b=d-g,k=p*b-U*z;if(u+=k,e+=(p+U)*k,r+=(z+b)*k,t&&c.length>2&&m.length>2){const n=y-h,t=j-h,l=p*t-U*n;o+=(n+t)*l,i+=l}x<l[0]&&(l[0]=x),x>l[1]&&(l[1]=x),a<l[2]&&(l[2]=a),a>l[3]&&(l[3]=a),t&&(y<l[4]&&(l[4]=y),y>l[5]&&(l[5]=y))}if(u>0&&(u*=-1),i>0&&(i*=-1),!u)return null;u*=.5,i*=.5;const c=[e/(6*u)+s,r/(6*u)+g,u];return t&&(l[4]===l[5]||0===i?(c[3]=(l[4]+l[5])/2,c[4]=0):(c[3]=o/(6*i)+h,c[4]=i)),c}function i(l,e){const r=e?[0,0,0]:[0,0],o=e?[0,0,0]:[0,0];let u=0,i=0,s=0,g=0;for(let h=0,c=l.length;h<c-1;h++){const c=l[h],f=l[h+1];if(c&&f){r[0]=c[0],r[1]=c[1],o[0]=f[0],o[1]=f[1],e&&c.length>2&&f.length>2&&(r[2]=c[2],o[2]=f[2]);const l=n(r,o);if(l){u+=l;const n=t(c,f);i+=l*n[0],s+=l*n[1],e&&n.length>2&&(g+=l*n[2])}}}return u>0?e?[i/u,s/u,g/u]:[i/u,s/u]:l.length?l[0]:null}const s=1e-6;function g(n){if(!n||!n.rings)return null;const{rings:t}=n;let e=0;for(let o=0;o<t.length;o++)e+=l(t[o]);if(e<s)return o(t,!1);const r=[0,0],u=t[0][0];for(let l=0;l<t.length;l++)c(r,u,t[l]);return r[0]*=1/e,r[1]*=1/e,r[0]+=u[0],r[1]+=u[1],r}const h=1/3;function c(n,t,e){if(!n||!e||e.length<3)return null;const r=e[0],o=[0,0],u=[e[1][0]-r[0],e[1][1]-r[1]];let i;for(let l=2;l<e.length;l++)o[0]=e[l][0]-r[0],o[1]=e[l][1]-r[1],i=.5*h*(o[0]*u[1]-o[1]*u[0]),n[0]+=i*(u[0]+o[0]),n[1]+=i*(u[1]+o[1]),u[0]=o[0],u[1]=o[1];const s=l(e),g=[r[0],r[1]];return g[0]-=t[0],g[1]-=t[1],g[0]*=s,g[1]*=s,n[0]+=g[0],n[1]+=g[1],n}export{e as extentCentroid,i as lineCentroid,r as polygonCentroid,u as ringCentroid,o as ringsCentroid,g as weightedAreaCentroid};