UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) 4.67 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import{getMetersPerUnitForSR as t}from"../../../core/unitUtils.js";import e from"../../../geometry/Extent.js";import n from"../../../geometry/Point.js";import i from"../PixelBlock.js";function a(e,n){if(e.spatialReference.equals(n))return e;const i=t(e.spatialReference),a=t(n);if(i===a)return e;const r=i/a;return{x:e.x*r,y:e.y*r}}async function r(t,e,n){if("extent"===n.type)return m(t,e,n);const{width:a,height:r}=t,x=new Uint8Array(a*r);if(!(await import("../../../geometry/operators/intersectsOperator.js")).execute(e,n))return new i({pixelType:t.pixelType,width:a,height:r,mask:x,maskIsAlpha:!1,pixels:[...t.pixels]});if("polyline"===n.type)return s(t,e,n);return(await import("../../../geometry/operators/containsOperator.js")).execute(n,e)?t:o(t,e,n)}function o(t,e,n){if(!t)return t;const{width:a,height:r}=t,o=x({geometry:n,size:[a,r],srcExtent:e,srcMask:t.mask});return new i({pixelType:t.pixelType,width:a,height:r,mask:o,maskIsAlpha:!1,pixels:[...t.pixels]})}function x(t){const{geometry:e,size:n,srcExtent:i,srcMask:a}=t,[r,o]=n;let x;const m=i.width/r,s=i.height/o,{xmin:l,ymax:p}=i;if("extent"===e.type){const t=(e.xmin-l)/m,n=(e.xmax-l)/m,i=(p-e.ymax)/s,a=(p-e.ymin)/s;x=[[[t,i],[t,a],[n,a],[n,i],[t,i]]]}else x=e.rings.map((t=>t.map((([t,e])=>[(t-l)/m,(p-e)/s]))));return h(x,n,a)}function h(t,e,n){const[i,a]=e,r=new OffscreenCanvas(i,a).getContext("2d");r.fillStyle="#f00",r.beginPath(),t.forEach((t=>{r.moveTo(t[0][0],t[0][1]);for(let e=0;e<t.length;e++)r.lineTo(t[e][0],t[e][1]);r.closePath()})),r.fill();const o=r.getImageData(0,0,i,a).data,x=i*a,h=new Uint8Array(x);let m=!1;for(let s=0;s<x;s++)n&&!n[s]||(o[4*s+3]>127?h[s]=255:m=!0);return m||n?h:void 0}function m(t,e,n){const{width:a,height:r}=t,o=new Uint8Array(a*r),x=e.width/a,h=e.height/r;if(n.width/x<.5||n.height/h<.5)return new i({pixelType:t.pixelType,width:a,height:r,mask:o,pixels:[...t.pixels]});const{xmin:m,xmax:s,ymin:l,ymax:p}=e,{xmin:f,xmax:c,ymin:y,ymax:u}=n,M=Math.max(m,f),w=Math.min(s,c),d=Math.max(l,y),g=Math.min(p,u),T=.5*x,k=.5*h;if(w-M<T||g-d<k||w<m+T||M>s-T||d>p-k||g<l+k)return new i({pixelType:t.pixelType,width:a,height:r,mask:o,pixels:[...t.pixels]});const R=Math.max(0,(M-m)/x),j=Math.min(a,Math.max(0,(w-m)/x)),A=Math.max(0,(p-g)/h),z=Math.min(r,Math.max(0,(p-d)/h)),U=Math.round(R),E=Math.round(j)-1,P=Math.round(A),S=Math.round(z)-1;if(U===E&&R%1>.5&&j%1<.5||P===S&&A%1>.5&&z%1<.5)return new i({pixelType:t.pixelType,width:a,height:r,mask:o,pixels:[...t.pixels]});if(0===U&&0===P&&E===a&&S===r)return t;const v=t.mask;for(let i=P;i<=S;i++)for(let t=U;t<=E;t++){const e=i*a+t;o[e]=v?v[e]:255}return new i({pixelType:t.pixelType,width:a,height:r,mask:o,pixels:[...t.pixels]})}function s(t,e,n){const{width:a,height:r}=t,o=new Uint8Array(a*r),x=e.width/a,h=e.height/r,{xmin:m,ymax:s}=e,{paths:l}=n,p=t.mask;for(let i=0;i<l.length;i++){const t=l[i];for(let e=0;e<t.length-1;e++){const[n,i]=t[e],[l,f]=t[e+1];let c=Math.floor((s-i)/h),y=Math.floor((s-f)/h);if(y<c){const t=c;c=y,y=t}c=Math.max(0,c),y=Math.min(r-1,y);const u=(l-n)/(f-i);for(let t=c;t<=y;t++){const e=t===c?Math.max(i,f):(r+1-t)*h,s=t===y?Math.min(i,f):e-h;let M=f===i?Math.floor((n-m)/x):Math.floor((u*(e-i)+n-m)/x),w=f===i?Math.floor((l-m)/x):Math.floor((u*(s-i)+n-m)/x);if(w<M){const t=M;M=w,w=t}const d=t*a;M=Math.max(0,M),w=Math.min(a-1,w);for(let t=d+M;t<=d+w;t++)o[t]=p?p[t]:255}}}return new i({pixelType:t.pixelType,width:a,height:r,mask:o,pixels:[...t.pixels]})}function l(t,e){const{extent:i}=p(t,e,new n({x:t.pixelSize.x,y:t.pixelSize.y,spatialReference:t.spatialReference})),{extent:a}=t.extent;if(i.xmax=Math.min(i.xmax,a.xmax),i.ymax=Math.min(i.ymax,a.ymax),i.xmin<i.xmax&&i.ymin<i.ymax){const{x:e,y:n}=t.pixelSize,a=Math.round(i.width/e),r=Math.round(i.height/n);t.extent=i,t.width=a,t.height=r}}function p(t,n,i,r=!0){const{spatialReference:o}=t,{x,y:h}=a(i,o);let m,s,l;const p="extent"===n.type?n:n.extent;let{xmin:f,xmax:c,ymax:y,ymin:u}=p;const{xmin:M,ymax:w}=t.extent;return r?(f=M+(f>M?x*Math.round((f-M)/x):0),y=w-(y<w?h*Math.round((w-y)/h):0),c=M+(c>M?x*Math.round((c-M)/x):0),u=w-(u<w?h*Math.round((w-u)/h):0),m=new e({xmin:f,ymax:y,xmax:c,ymin:u,spatialReference:o}),s=Math.round(m.width/x),l=Math.round(m.height/h)):(s=Math.floor((c-f)/x+.8),l=Math.floor((y-u)/h+.8),f=M+(f>M?x*Math.floor((f-M)/x+.1):0),y=w-(y<w?h*Math.floor((w-y)/h+.1):0),c=f+s*x,u=y-l*h,m=new e({xmin:f,ymax:y,xmax:c,ymin:u,spatialReference:o})),{extent:m,width:s,height:l}}export{r as clip,l as clipRasterInfo,x as convertGeometryToMask,p as snapToRaster};