UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 5.92 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import t from"../PixelBlock.js";import{SimpleBandStatistics as e}from"../SimpleBandStatistics.js";import{isValidPixelBlock as i}from"./pixelUtils.js";const s=1,n=1/111e3;function a(t){let{altitude:e,azimuth:i}=t;const{hillshadeType:s,pixelSizePower:a=1,pixelSizeFactor:o=1,scalingType:l,isGCS:r,resolution:c}=t,h="multi-directional"===s?2*t.zFactor:t.zFactor,{x:u,y:f}=c;let d=h/(8*u),p=h/(8*f);if(r&&h>.001&&(d/=n,p/=n),"adjusted"===l)if(r){const t=u*n,e=f*n;d=(h+t**a*o)/(8*t),p=(h+e**a*o)/(8*e)}else d=(h+u**a*o)/(8*u),p=(h+f**a*o)/(8*f);let x=(90-e)*Math.PI/180,w=Math.cos(x),y=(360-i+90)*Math.PI/180,m=Math.sin(x)*Math.cos(y),M=Math.sin(x)*Math.sin(y);const z=[315,270,225,360,180,0],A=[60,60,60,60,60,90],g=new Float32Array([3,5,3,2,1,4]),F=g.reduce(((t,e)=>t+e)),k=g.map((t=>t/F)),P="multi-directional"===s?z.length:1,S=new Float32Array(6),C=new Float32Array(6),T=new Float32Array(6);if("multi-directional"===s)for(let n=0;n<P;n++)e=A[n],i=z[n],x=(90-e)*Math.PI/180,w=Math.cos(x),y=(360-i+90)*Math.PI/180,m=Math.sin(x)*Math.cos(y),M=Math.sin(x)*Math.sin(y),S[n]=w,C[n]=m,T[n]=M;else S.fill(w),C.fill(m),T.fill(M);return{resolution:c,factor:[d,p],sinZcosA:m,sinZsinA:M,cosZ:w,sinZcosAs:[...C],sinZsinAs:[...T],cosZs:[...S],weights:[...k],hillshadeType:["traditional","multi-directional"].indexOf(s)}}function o(t,e){const{width:i,height:n}=t,a=i*n,o=new Uint8Array(a),l=null!=t.mask;let r;if(l){r=new Uint8Array(t.mask);for(let t=0;t<r.length;t++)r[t]&&(o[t]=1,r[t]=1)}const c=t.pixels[0],h=new Float32Array(a),u=new Float32Array(a);let f,d=0;if(e){const{resolution:t}=e,i=t?(t.x+t.y)/2:1;d=200*e.zFactor/(i*i),f=new Float32Array(a)}const p=e?.curvatureType;let x,w,y,m,M,z,A,g;for(let F=s;F<n-s;F++){const t=F*i;for(let e=s;e<i-s;e++){const s=t+e;if(!r||0!==r[s]){if(r){if(r[s-i-1]+r[s-i]+r[s-i+1]+r[s-1]+r[s+1]+r[s+i-1]+r[s+i]+r[s+i+1]<7){o[s]=0;continue}x=r[s-i-1]?c[s-i-1]:c[s],w=r[s-i]?c[s-i]:c[s],y=r[s-i+1]?c[s-i+1]:c[s],m=r[s-1]?c[s-1]:c[s],M=r[s+1]?c[s+1]:c[s],z=r[s+i-1]?c[s+i-1]:c[s],A=r[s+i]?c[s+i]:c[s],g=r[s+i+1]?c[s+i+1]:c[s]}else x=c[s-i-1],w=c[s-i],y=c[s-i+1],m=c[s-1],M=c[s+1],z=c[s+i-1],A=c[s+i],g=c[s+i+1];if(h[s]=y+M+M+g-(x+m+m+z),u[s]=z+A+A+g-(x+w+w+y),null!=f){const t=c[s],e=.5*(m+M)-t,i=.5*(w+A)-t;if("standard"===p)f[s]=-d*(e+i);else{const t=(-x+y+z-g)/4,n=(-m+M)/2,a=(w-A)/2,o=n*n,l=a*a,r=o+l;r&&(f[s]="profile"===p?d*(e*o+i*l+t*n*a)/r:-d*(e*l+i*o-t*n*a)/r)}}}}}return{outMask:l?o:null,dzxs:h,dzys:u,curvatures:f}}function l(t,e,i){for(let s=0;s<i;s++)t[s*e]=t[s*e+1],t[(s+1)*e-1]=t[(s+1)*e-2];for(let s=1;s<e-1;s++)t[s]=t[s+e],t[s+(i-1)*e]=t[s+(i-2)*e]}function r(n,r){if(!i(n))return n;const{factor:c,sinZcosA:h,sinZsinA:u,cosZ:f,sinZcosAs:d,sinZsinAs:p,cosZs:x,weights:w}=a(r),[y,m]=c,M="traditional"===r.hillshadeType,{width:z,height:A}=n,g=new Uint8Array(z*A),{dzxs:F,dzys:k,outMask:P}=o(n);for(let t=s;t<A-s;t++){const e=t*z;for(let t=s;t<z-s;t++){const i=e+t;if(!P||P[i]){const t=F[i]*y,e=k[i]*m,s=Math.sqrt(1+t*t+e*e);let n=0;if(M){let i=255*(f+u*e-h*t)/s;i<0&&(i=0),n=i}else{const i=p.length;for(let a=0;a<i;a++){let i=255*(x[a]+p[a]*e-d[a]*t)/s;i<0&&(i=0),n+=i*w[a]}}g[i]=255&n}}}l(g,z,A);return new t({width:z,height:A,pixels:[g],mask:P,pixelType:"u8",validPixelCount:n.validPixelCount,statistics:[new e(0,255)]})}function c(t,e,s,n){if(!i(t)||!i(e))return;const{min:a,max:o}=n,l=t.pixels[0],{pixels:r,mask:c}=e,h=r[0],u=255.00001/(o-a),f=new Uint8ClampedArray(h.length),d=new Uint8ClampedArray(h.length),p=new Uint8ClampedArray(h.length),x=s.length-1;for(let i=0;i<h.length;i++){if(c&&0===c[i])continue;const t=Math.floor((h[i]-a)*u),[e,n]=s[t<0?0:t>x?x:t],o=l[i],r=o*n,w=r*(1-Math.abs(e%2-1)),y=o-r;switch(Math.floor(e)){case 0:f[i]=r+y,d[i]=w+y,p[i]=y;break;case 1:f[i]=w+y,d[i]=r+y,p[i]=y;break;case 2:f[i]=y,d[i]=r+y,p[i]=w+y;break;case 3:f[i]=y,d[i]=w+y,p[i]=r+y;break;case 4:f[i]=w+y,d[i]=y,p[i]=r+y;break;case 5:case 6:f[i]=r+y,d[i]=y,p[i]=w+y}}t.pixels=[f,d,p],t.updateStatistics()}function h(e,a){if(!i(e))return e;const r=a.zFactor,c=a.pixelSizePower??1,h=a.pixelSizeFactor??1,u=a.slopeType,f=a.isGCS,{x:d,y:p}=a.resolution;let x=r/(8*d),w=r/(8*p);f&&Math.abs(r-1)<1e-4&&(x/=n,w/=n),"adjusted"===u&&(x=(r+d**c*h)/(8*d),w=(r+p**c*h)/(8*p));const{dzxs:y,dzys:m,outMask:M}=o(e),{width:z,height:A}=e,g=new Float32Array(z*A);for(let t=s;t<A-s;t++){const e=t*z;for(let t=s;t<z-s;t++){const i=e+t;if(!M||M[i]){const s=y[i]*x,n=m[i]*w,a=Math.sqrt(s*s+n*n);g[e+t]="percent-rise"===u?100*a:57.2957795*Math.atan(a)}}}l(g,z,A);const F=new t({width:z,height:A,pixels:[g],mask:M,pixelType:"f32",validPixelCount:e.validPixelCount});return F.updateStatistics(),F}function u(n,a={}){if(!i(n))return n;const{resolution:r}=a,c=r?1/r.x:1,h=r?1/r.y:1,{dzxs:u,dzys:f,outMask:d}=o(n),{width:p,height:x}=n,w=new Float32Array(p*x);for(let t=s;t<x-s;t++){const e=t*p;for(let t=s;t<p-s;t++){const i=e+t;if(!d||d[i]){const s=u[i]*c,n=f[i]*h;let a=-1;0===s&&0===n||(a=90-57.29578*Math.atan2(n,-s),a<0&&(a+=360),360===a?a=0:a>360&&(a%=360)),w[e+t]=a}}}l(w,p,x);return new t({width:p,height:x,pixels:[w],mask:d,pixelType:"f32",validPixelCount:n.validPixelCount,statistics:[new e(-1,360)]})}function f(e,s){if(!i(e))return e;const{curvatures:n,outMask:a}=o(e,s),{width:r,height:c}=e;l(n,r,c);const h=new t({width:r,height:c,pixels:[n],mask:a,pixelType:"f32",validPixelCount:e.validPixelCount});return h.updateStatistics(),h}function d(t,e,i){const{hillshadeType:s,altitude:n,azimuth:a,zFactor:o,pixelSizeFactor:l,pixelSizePower:r,slopeType:c}=t;return{hillshadeType:s,altitude:n,azimuth:a,zFactor:o,pixelSizePower:r,pixelSizeFactor:l,scalingType:"scaled"===c?"adjusted":"none",resolution:e,isGCS:i}}export{u as aspect,a as calculateHillshadeParams,f as curvature,n as gcsFactor,d as getHillshadeOptions,r as hillshade,h as slope,c as tintHillshade};