@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 10.7 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import e from"../../../core/Error.js";import{throwIfAborted as t}from"../../../core/promiseUtils.js";import i from"../../../geometry/Extent.js";import n from"../../../geometry/Point.js";import{getByteCount as o}from"../formats/pixelRangeUtils.js";import{clip as a,snapToRaster as r}from"../functions/clipUtils.js";import{transformPixels as s}from"../functions/pixelTransformUtils.js";import{mapPixelValueToAttribute as l,mosaic as c,pixelIdFieldName as p,convertPixelBlockToFeatures as f,getValidPixels as u,countCategoricalPixels as m}from"../functions/pixelUtils.js";import{load as x,projectPolygon as h}from"../functions/rasterProjectionHelper.js";import{computeBoxStatistics as d,computeStatisticsHistograms as y}from"../functions/stretchUtils.js";import w from"../../support/Field.js";import g from"../../support/PixelBlock.js";import{getBandNames as k}from"../../../renderers/support/rasterRendererHelper.js";import b from"../../../rest/support/ImageIdentifyParameters.js";const B=2**30,S=2048,P=2**20,j=20,I=100;async function R(e,t){return"imagery"===e.type?await e.generateRasterInfo(e.rasterFunction,t):e.raster.rasterInfo}function T(e){const t=[new w({name:p,alias:"Pixel ID",type:"oid"})],{pixelType:i}=e,n="f32"===i?"single":"f64"===i?"double":"integer";return k(e).forEach(e=>{t.push(new w({name:e,alias:e,type:n}))}),t.map(e=>e.toJSON())}async function v(t,i){if(t.spatialReference.equals(i))return t;if("polyline"===t.type){const n=(await import("../../../geometry/operators/projectOperator.js")).execute(t,i);if(null==n)throw new e("read-pixels","failed to project the geometry into the layer's spatial reference");return n}if("extent"===t.type||t.rings[0].length<j){const e=(await import("../../../geometry/operators/lengthOperator.js")).execute(t,{unit:"meters"})/I;t=(await import("../../../geometry/operators/densifyOperator.js")).execute(t,e,{unit:"meters"}),await x(),t=h(t,i)}return t}async function z(e,t,i=!1){const{spatialReference:o}=e,a=t?.geometry,s=await v(a??e.extent,o),l="extent"===s.type?s:s.extent,c=null==a||"extent"===a.type&&a.spatialReference.equals(o)?void 0:s,p=new n({x:e.pixelSize.x,y:e.pixelSize.y,spatialReference:o}),f=!!t?.autoResample,u=f?p:t?.pixelSize??p;let{extent:m,width:x,height:h}=r(e,l,u);if(!f)return{extent:m,clipGeometry:c,width:x,height:h,pixelSize:u};const d=e.storageInfo?.pyramidScalingFactor??2;let{x:y,y:w}=u;const g=t?.maxPixelCount??P;for(;x*h>g&&(!i||Math.max(x,h)>1024);)y*=d,w*=d,x/=d,h/=d;return x=Math.round(x),h=Math.round(h),{extent:m,clipGeometry:c,width:x,height:h,pixelSize:new n({x:y,y:w,spatialReference:o})}}async function*M(e,n,o=!1,r){const{extent:s,clipGeometry:l,width:c,height:p}=n,f=s.width/c,u=s.height/p,m=Math.ceil(c/S),x=Math.ceil(p/S),h=f*S,d=u*S,{xmin:y,xmax:w,ymin:g,ymax:k}=s,b=x*m,B={bandIds:n.bandIds?.length?n.bandIds:void 0,interpolation:"nearest",...r};for(let P=0;P<x;P++)for(let n=0;n<m;n++){t(B);const r=new i({xmin:y+n*h,xmax:o?Math.min(w,y+(n+1)*h):y+(n+1)*h,ymax:k-P*d,ymin:o?Math.max(g,k-(P+1)*d):k-(P+1)*d,spatialReference:s.spatialReference}),c={row:P,col:n},p=P*m+n+1;if(l&&!r.intersects(l)){yield{extent:r,pixelBlock:null,id:c,total:b,current:p};continue}const x=o?Math.round(r.width/f):S,j=o?Math.round(r.height/u):S;let{pixelBlock:I}=await e.fetchPixels(r,x,j,B).catch(()=>({pixelBlock:void 0}));I&&l&&(I=await a(I,r,l)),yield{extent:r,pixelBlock:I,id:c,total:b,current:p}}}async function*F(e,i,n){const o=await R(e,n),a=T(o),r={...i,geometry:i?.geometry??o.extent},s=M(e,await z(o,r),!0,n),l=o.width;for await(const c of s){if(!c.pixelBlock){yield{...c};continue}const r=V(e),{pixelBlock:s}=c,{statistics:p,histograms:f}=await K({pixelBlock:s,rasterJobHandler:r},n),u=c;u.statistics=p,u.histograms=f;const m=c.id.row*o.width*S+c.id.col*S;t(n),u.featureSet=await _({pixelBlock:s,extent:c.extent,fields:a,skipFactor:i?.skipFactor??1,rasterJobHandler:r,pixelIdOffset:m,imageRowSize:l},n),t(n),yield u}}function H(t,i,n){const a=o(i),{width:r,height:s}=t;if(r*s*a>B)throw new e("fetch-pixels","failed to fetch pixels as pixel byte count exceeds the 1GB limit");if(n&&r*s>n)throw new e("fetch-pixels",`failed to fetch pixels as pixel count exceeds the ${n} limit`)}async function C(t,i,n){if(!i.geometry)throw new e("fetch-pixels","geometry is required to fetch pixel feature set");const o=await R(t),a=await z(o,i,!0);H(a,o.pixelType);const{extent:r,pixelSize:s}=a,l=Math.round(o.extent.width/s.x),c=T(o),p={fields:c,features:[]},f=p.features,{maxPixelCount:u}=i,m=!!i.autoResample&&null!=u&&a.width*a.height>u,x=m?1:i.skipFactor??1,h=a.width<=S||a.height<=S,d=M(t,a,1===x||h,n);for await(const e of d){if(!e.pixelBlock)continue;const i=V(t),a=Math.floor((o.extent.ymax-e.extent.ymax)/s.y)*l+Math.floor((e.extent.xmin-o.extent.xmin)/s.x),r=await _({pixelBlock:e.pixelBlock,extent:e.extent,fields:c,skipFactor:x,rasterJobHandler:i,pixelIdOffset:a,imageRowSize:l},n);if(r?.features?.length){const{features:e}=r,t=e.length;for(let i=0;i<t;i++){const t=e.pop();f.push(t)}}}if(m&&u){const e=u/(i.skipFactor??1)**2;if(f.length>e){const t=f.length/e,i=[...f],n=f.length-1;f.length=0;for(let o=0,a=0;o<e-1;o++){a+=t;const e=Math.round(a);e<n&&f.push(i[e])}}}return{extent:r,featureSet:p,pixelSize:s}}async function O(e,t,i){const{geometry:n}=t;if("multipoint"===n.type)return J(e,n,i);if("polyline"===n.type)return U(e,n,i);const o=await $(e,{geometry:n},i);if(!o)return null;return d(o)}async function J(e,t,i){const n=await R(e,i),o=t.points.map((e,i)=>t.getPoint(i)).map(t=>q(e,t,i)),a=(await Promise.all(o)).filter(e=>null!=e);if(a.length<=1)return N(a[0]);const r=a[0].map((e,t)=>a.map(e=>e[t]));return d(new g({pixels:r,pixelType:n.pixelType}))}async function q(e,t,i){if("imagery"===e.type){const n=await e.identify(new b({geometry:t,returnCatalogItems:!1}),i).catch(()=>null);return null==n?.value||n.value.includes("NoData")?null:n.value.replaceAll(", "," ").split(" ").map(e=>Number(e))}const n=await e.identify(t,i).catch(()=>null);return n?.value}function N(e){return e?.map(e=>({min:e,max:e,avg:e,median:e,quartile1:e,quartile3:e}))}async function U(e,t,i){const n=await D(e,{geometry:t},i),o=await R(e,i),{bandCount:a,pixelType:r}=o,s=new Array(a).fill(0).map(()=>g.createEmptyBand(r,n.length));let l=0;for(let c=0;c<n.length;c++){const e=n[c];if(null!=e.value){l++;for(let t=0;t<a;t++)s[t][c]=e.value[t]}}l!==n.length&&s.forEach((e,t)=>{s[t]=e.subarray(0,l)});return d(new g({pixels:s,pixelType:r}))}async function A(t,i,n){const o=await R(t,n);if(!o.attributeTable||!o.pixelType.startsWith("u"))return null;const a=o.attributeTable.clone(),r=a.fields.find(e=>"value"===e.name.toLowerCase()),s=a.fields.find(e=>"count"===e.name.toLowerCase());if(!r||!s)throw new e("compute-attribute-table","Value or count field not found in attribute table");const l=2**Number(o.pixelType.slice(1)),c=new Uint32Array(l),{geometry:p}=i;return await L(t,p,(e,t)=>m(e,c),n),a.features.forEach(e=>{const t=e.attributes[r.name];e.attributes[s.name]=c[t]}),a}async function E(e,t,i){const{geometry:n}=t;if("point"===n.type)return q(e,n,i);if("multipoint"===n.type){const t=n.points.map((e,t)=>n.getPoint(t)).map(t=>q(e,t,i)),o=(await Promise.all(t)).filter(e=>null!=e);if(0===o.length)return null;return G(o).map(e=>e/o.length)}const o=await L(e,n,(e,t)=>W(e),i),a=o?.filter(e=>e.count>0);if(!a?.length)return null;const r=G(a.map(e=>e.bandSum)),s=a.map(e=>e.count).reduce((e,t)=>e+t);return r.map(e=>e/s)}function G(e){return e.reduce((e,t)=>e.map((e,i)=>e+t[i]))}function W(e){const{width:t,height:i,pixels:n,mask:o}=e,a=new Array(n.length).fill(0);let r=0;for(let s=0;s<i;s++)for(let e=0;e<t;e++){const i=s*t+e;if(!o||o[i]){for(let e=0;e<n.length;e++)a[e]+=n[e][i];r++}}return{bandSum:a,count:r}}async function D(e,t,i){const n=await L(e,t.geometry,u,i);return n?.flat()??[]}async function L(i,n,o,r){const s=i.spatialReference;if("polyline"===n.type&&!n.spatialReference.equals(s)){const t=(await import("../../../geometry/operators/projectOperator.js")).execute(n,s);if(null==t)throw new e("read-pixels","failed to fetch pixels");n=t}const l="polyline"===n.type?n.extent:n,c=await v(l,s),p="polyline"===n.type?n:c,f=await R(i,r),u={geometry:c,autoResample:!1},m=[],x=M(i,await z(f,u),!0,r);for await(const e of x){if(!e.pixelBlock)continue;const{extent:i}=e,n=o(await a(e.pixelBlock,i,p),i);m.push(n),t(r)}return m}async function K(e,t){const{rasterJobHandler:i}=e,{pixelBlock:n}=e,{statistics:o,histograms:a}=i?await i.computeStatisticsHistograms({pixelBlock:n},t):y(n);return{statistics:o,histograms:a}}async function _(e,t){const{extent:i,fields:n,skipFactor:o,rasterJobHandler:a,pixelIdOffset:r,imageRowSize:s}=e,{pixelBlock:l}=e,c=n.map(({name:e})=>e),p=a?await a.convertPixelBlockToFeatures({pixelBlock:l,extent:i,fieldNames:c,skipFactor:o,pixelIdOffset:r,imageRowSize:s},t):f({pixelBlock:l,extent:i,fieldNames:c,skipFactor:o,pixelIdOffset:r,imageRowSize:s});return p?{fields:n,features:p}:void 0}function V(e){return"_rasterJobHandler"in e?e._rasterJobHandler:null}async function $(e,t,i){const n=await R(e);t={...t,maxPixelCount:t.maxPixelCount??2**28};const o=await z(n,t);o.bandIds=t.bandIds?.length?t.bandIds:void 0,H(o,n.pixelType,t.maxPixelCount);const{width:a,height:r}=o;if(a<=S&&r<=S){const t=o.bandIds?.length?o.bandIds:void 0,{pixelBlock:n}=await e.fetchPixels(o.extent,a,r,{bandIds:t,interpolation:"nearest",...i});return n}const s=[],l=M(e,o,!1,i);for await(const c of l)s.push(c.pixelBlock);if(!s.some(e=>null!=e))return null;const p=Math.ceil(a/S),f=Math.ceil(r/S),u={width:p*S,height:f*S};let m;const x=V(e);if(x){const e=await x.mosaicAndTransform({srcPixelBlocks:s,srcMosaicSize:u,destDimension:u},{...i,transferPixelsToWorker:!0});m=e?.pixelBlock}else m=c(s,u);return m}async function Q(t,i,n){if(!i.geometry)throw new e("compute-statistics-histograms","geometry is required to fetch pixel feature set");let o=await $(t,i,n);if(!o)throw new e("compute-statistics-histograms","failed to get intersecting pixels");const{valueAttributeFieldName:a}=i;if(a&&"value"!==a.toLowerCase()){const e=await R(t,n);o=l(o,e,a)}const r=V(t),{transformType:c}=i;if(c){const e={lambda:i.lambda,shift:i.shift};o=r?await r.transformPixels({pixelBlock:o,transformType:c,transformParameters:e},{...n,transferPixelsToWorker:!0}):s(o,c,e)}const p=i.histogramSize??void 0;return r?await r.computeStatisticsHistograms({pixelBlock:o,histogramSize:p,includeSkewnessKurtosis:!0},{...n,transferPixelsToWorker:!0}):y(o,{histogramSize:p,includeSkewnessKurtosis:!0})}export{O as computeBoxStatistics,A as computeRasterAttributeTable,Q as computeStatisticsHistograms,F as createPixelFeatureSetCursor,C as fetchPixelFeatureSet,E as identifyAveragePixelValue,D as readIntersectingPixels};