@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 2.64 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.33/esri/copyright.txt for details.
*/
import e from"../../core/Error.js";import{px2pt as t}from"../../core/screenUtils.js";import{getGeometryExtent as i}from"../../geometry/support/extentUtils.js";import r from"../statistics/spatialStatistics.js";import{verifyBinningParams as n}from"../support/binningUtils.js";import{binningCapableLayerTypes as s,featureCapableLayerTypes as o,createLayerAdapter as a,getLayerTypeLabels as f}from"../support/adapters/support/layerUtils.js";const c=.1,u=.25,p=.1,l=.1;async function m(t){const{view:i}=t;if(!(t&&i&&t.layer))throw new e("reference-size:missing-parameters","'view' and 'layer' parameters are required");t.forBinning&&n(t,"reference-size");const{layer:r,...c}=t,u=t.forBinning?s:o,p=a(r,u,t.forBinning);if(!p)throw new e("reference-size:invalid-parameters","'reference-size' must be one of these types: "+f(u).join(", "));const l={layerAdapter:p,...c,view:i};await i.when();const m=null!=l.signal?{signal:l.signal}:null;if(await p.load(m),!t.forBinning&&"polygon"!==p.geometryType)throw new e("reference-size:not-supported",`reference-size is not supported for geometryType: ${p.geometryType}`);return l}function y(e){let t=0;for(const i of e)t+=i;return t/e.length}function g(e,t){const i=t??y(e),r=e.reduce(((e,t)=>e+(t-i)**2),0);return Math.sqrt(r/e.length)}async function h(t,n){const s=await r({features:t,geometryType:n});if(!(s&&"avgSize"in s&&s.avgSize))throw new e("reference-size:insufficient-info","average polygon size is invalid");const o=s.avgSize,a=[],f=[];for(const e of t){const t=i(e.geometry);if(!t){a.push(0),f.push(0);continue}const r=Math.abs(t.xmax-t.xmin),n=Math.abs(t.ymax-t.ymin);a.push(r/n),f.push(Math.sqrt(r*n))}const c=Number(y(a).toFixed(2)),m=Number(g(a,c).toFixed(2)),h=g(f),w=Number((h/o).toFixed(2));return{isGrid:m<=p&&c>=1-u&&c<=1+u&&w<=l,avgSize:o}}async function w(r,n){const{view:s}=n;if(n.forBinning){const n=r[Math.floor(r.length/2)].geometry,o=i(n);if(!o)throw new e("reference-size:insufficient-info","Extent is invalid");const a=Math.abs(o.xmax-o.xmin),f=Math.abs(o.ymax-o.ymin),c=Math.min(a,f);return{size:t(c/s.resolution),isGrid:!0}}const{isGrid:o,avgSize:a}=await h(r,n.layerAdapter.geometryType);return{size:t((o?a*(1-c):.75*a)/s.resolution),isGrid:o}}async function d(t){const i=await m(t),r=-1,n=await i.layerAdapter.getSampleFeatures({sampleSize:r,returnGeometry:!0,...i},"json");if(!n?.length)throw new e("reference-size:insufficient-info","No features are available to calculate statistics");return w(n,i)}export{d as default};