UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) • 12.1 kB
/* 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"../../Color.js";import{getMetersPerUnitForSR as t}from"../../core/unitUtils.js";import n from"../../layers/support/Field.js";import{isFunctionRaster as a}from"../../layers/support/rasterDatasets/datasetUtils.js";import r from"../ClassBreaksRenderer.js";import l from"../FlowRenderer.js";import s from"../RasterColormapRenderer.js";import i from"../RasterShadedReliefRenderer.js";import o from"../RasterStretchRenderer.js";import u from"../UniqueValueRenderer.js";import m from"../VectorFieldRenderer.js";import c from"./AuthoringInfo.js";import f from"./ClassBreakInfo.js";import{convertColorRampToColormap as p,predefinedJsonColorRamps as d}from"./colorRampUtils.js";import{isVectorFieldRendererSupported as b,isColormapRendererSupported as h,isUVRendererSupported as g,isClassBreaksSupported as v,isShadedReliefRendererSupported as y,isFlowRendererSupported as w}from"./rasterRendererChecks.js";import C from"./UniqueValueInfo.js";import x from"../visualVariables/ColorVariable.js";import M from"../visualVariables/SizeVariable.js";import R from"../visualVariables/support/ColorStop.js";import j from"../../rest/support/ClassBreaksDefinition.js";import{createGenerateRendererClassBreaks as V}from"../../rest/support/generateRendererUtils.js";import T from"../../rest/support/MultipartColorRamp.js";import I from"../../symbols/SimpleFillSymbol.js";const S=.25,k=T.fromJSON({type:"multipart",colorRamps:[{fromColor:[0,0,255],toColor:[0,255,255]},{fromColor:[0,255,255],toColor:[255,255,0]},{fromColor:[255,255,0],toColor:[255,0,0]}]}),F=T.fromJSON(d[0]),L=new Set(["scientific","standard-time","vector-uv","vector-magdir","vector-u","vector-v","vector-magnitude","vector-direction"]);function q(e){return e.presetRenderers?.find((({method:t,value:n})=>"raster-function-template"===t?n===e.rasterFunctionName:"variable"===t&&n===e.variableName))}function B(e,t){const{attributeTable:n,colormap:a}=e;if(b(e)){const t=$(e);if(null!=t)return t}if(t?.rasterFunctionColorRamp){const n=U(e,t);return n.colorRamp=t.rasterFunctionColorRamp,n}if(null!=a){const t=K(e);if(null!=t)return t}if(null!=n){const t=P(e);if(null!=t)return t}return U(e,t)}function E(e,t=!1){const n=["raster-stretch"];return h(e,t)&&n.push("raster-colormap"),g(e)&&n.push("unique-value"),v(e,t)&&n.push("class-breaks"),y(e)&&n.push("raster-shaded-relief"),b(e)&&n.push("vector-field"),w(e)&&n.push("flow"),n}function z(e){return a(e)&&"Colormap"===e.rasterFunction.functionName?e.rasterFunction.colorRamp:void 0}function N(e,t,n){const a=["nearest","bilinear","cubic","majority"],r=n?.toLowerCase().replace("bicubic","cubic"),l=a.find((e=>e===r));if("Map"===t)return l??"bilinear";if("standard-time"===e.dataType)return l??"nearest";return"thematic"===e.dataType||e.attributeTable||e.colormap?"nearest"===l||"majority"===l?l:"nearest":l??"bilinear"}function U(e,t){e=W(e,t?.variableName);const{bandCount:n}=e;let{bandIds:a,stretchType:r}=t||{};a?.some((e=>e>=n))&&(a=null);let l=e.statistics,s=e.histograms;n>1?(a=a?.length?a:D(e),l=null==l?null:a?.map((e=>l[e])),s=null==s?null:a?.map((e=>s[e]))):a=[0],null==r&&(r=J(e)),t?.includeStatisticsInStretch&&("percent-clip"===r||"histogram-equalization"===r)&&e.statistics?.length&&(r="min-max");let i=!1;switch(r){case"none":i=!1;break;case"percent-clip":i=!s?.length;break;default:i=!l?.length}const{dataType:u}=e,m=1===a?.length&&L.has(u)?k:null,c=new o({stretchType:r,dynamicRangeAdjustment:i,colorRamp:m,outputMin:0,outputMax:255,gamma:1===a?.length?[1]:[1,1,1],useGamma:!1});return"percent-clip"===r?c.maxPercent=c.minPercent=S:"standard-deviation"===r&&(c.numberOfStandardDeviations=2),i||null==e.multidimensionalInfo&&!t?.includeStatisticsInStretch||("percent-clip"===r?c.histograms=s:"min-max"!==r&&"standard-deviation"!==r||(c.statistics=l)),c}function W(e,t){const{multidimensionalInfo:n}=e;if(!t||!n)return e;const a=n.variables.find((e=>e.name===t));if(!a)return e;if(e=e.clone(),a){const{statistics:t,histograms:n}=a;t?.length&&(e.statistics=t.map((e=>({min:e.min,max:e.max,avg:e.avg,stddev:e.stddev})))),n?.length&&(e.histograms=n)}return e}function D(e){const t=e.bandCount;if(1===t)return null;if(2===t)return[0];const{bandInfos:n}=e;let a;if(n.length===t){const{red:e,green:t,blue:r,nir:l}=A(n);null!=e&&null!=t&&null!=r?a=[e,t,r]:null!=l&&null!=e&&null!=t&&(a=[l,e,t])}return!a&&t>=3&&(a=[0,1,2]),a}function O(e,t){const n=e.bandInfos.map((({name:e})=>e));return t?.length?t.map((e=>n[e])):n}function A(e){const t={};for(let n=0;n<e.length;n++){const a=e[n],r=a.name.toLowerCase();if("red"===r)t.red=n;else if("green"===r)t.green=n;else if("blue"===r)t.blue=n;else if("nearinfrared"===r||"nearinfrared_1"===r||"nir"===r)t.nir=n;else if(a.maxWavelength&&a.minWavelength){const e=a.minWavelength,r=a.maxWavelength;null==t.blue&&e>=410&&e<=480&&r>=480&&r<=540?t.blue=n:null==t.green&&e>=490&&e<=560&&r>=560&&r<=610?t.green=n:null==t.red&&e>=595&&e<=670&&r>=660&&r<=730?t.red=n:null==t.nir&&e>=700&&e<=860&&r>=800&&r<=950&&(t.nir=n)}}return t}function J(e){let t="percent-clip";const{pixelType:n,dataType:a,histograms:r,statistics:l,multidimensionalInfo:s}=e,i=L.has(a)||"generic"===a&&null!=s;return"u8"!==n||"processed"!==a&&null!=r&&null!=l?"u8"===n||"elevation"===a||i?t="min-max":null!=r?t="percent-clip":null!=l&&(t="min-max"):t="none",t}function P(t,n,a,r){if(!g(t,n))return null;const{attributeTable:l,statistics:s}=t,i=G(l,n),o=H(l,"red"),m=H(l,"green"),f=H(l,"blue"),d=new c,b=[],h=new Set,v=!!(o&&m&&f);if(null!=l)l.features.forEach((t=>{const n=t.attributes[i.name];if(!h.has(t.attributes[i.name])&&null!=n){h.add(n);const a=v&&("single"===o.type||"double"===o.type)&&("single"===m.type||"double"===m.type)&&("single"===f.type||"double"===f.type)&&!l.features.some((e=>e.attributes[o.name]>1||e.attributes[m.name]>1||e.attributes[f.name]>1)),r=a?255:1;b.push(new C({value:t.attributes[i.name],label:t.attributes[i.name]+"",symbol:new I({style:"solid",outline:null,color:new e(v?[t.attributes[o.name]*r,t.attributes[m.name]*r,t.attributes[f.name]*r,1]:[0,0,0,0])})}))}}));else if(s?.[0])for(let u=s[0].min;u<=s[0].max;u++)b.push(new C({value:u,label:u.toString(),symbol:new I({style:"solid",outline:null,color:new e([0,0,0,0])})}));if(b.sort(((e,t)=>e.value&&"string"==typeof e.value.valueOf()?0:e.value>t.value?1:-1)),!v){const t=p(F,{numColors:b.length});b.forEach(((n,a)=>n.symbol.color=new e(t[a].slice(1,4)))),d.colorRamp=F}if(a||r){const t=a||p(r,{numColors:b.length}).map((e=>e.slice(1)));b.forEach(((n,a)=>n.symbol.color=new e(t[a]))),d.colorRamp=r}return new u({field:i.name,uniqueValueInfos:b,authoringInfo:d})}function G(e,t,a){let r;return null!=e?(r=t?e.fields.find((e=>t.toLowerCase()===e.name.toLowerCase())):_(e.fields),r||(a||(r=e.fields.find((e=>"string"===e.type))),r||(r=H(e,"value")))):r=new n({name:"value"}),r}function _(e){let t;for(let n=0;n<e.length;n++){const a=e[n].name.toLowerCase();if("string"===e[n].type){if(a.startsWith("class")){t=e[n];break}null==t&&(a.endsWith("name")||a.endsWith("type"))&&(t=e[n])}}return t}function H(e,t){return null==e?null:e.fields.find((e=>e.name.toLowerCase()===t))}function K(e){if(!h(e))return null;let t;const{attributeTable:n,colormap:a}=e;if(null!=n){const e=H(n,"value"),a=G(n,null,!0);"string"===a.type&&(t={},n.features.forEach((n=>{const r=n.attributes;t[r[e.name]]=a?r[a.name]:r[e.name]})))}return s.createFromColormap(a,t)}function Q(e,n="traditional"){if(!y(e))return null;const{extent:a}=e,r=a.width*t(a.spatialReference);return new i({hillshadeType:n,scalingType:r>5e6?"adjusted":"none"})}function X(e,t){e=W(e,t?.variableName);const{attributeTable:n}=e;if(!v(e))return null;const a=null!=e.histograms?e.histograms[0]:null,l=null!=t?.numClasses&&isFinite(t?.numClasses)?t.numClasses:5,s=new c({classificationMethod:t?.classificationMethod,colorRamp:t?.colorRamp});let i=t?.field||"value";const o=[],u=[],m=1e3,d=null!=n,b=d&&n.fields.find((e=>"count"===e.name.toLowerCase())),h=d?n.fields.find((e=>e.name.toLowerCase()===i.toLowerCase())):void 0;if(h&&d){i=h.name;const e=n.features.length;let t=0;n.features.forEach((n=>t+=(b?n.attributes[b.name]:50)/e)),n.features.forEach((n=>{const a=n.attributes[h.name],r=b?n.attributes[b.name]:50;if(r>0){u.push(r);const n=Math.max(1,Math.round(r/e/t*m));for(let e=0;e<n;e++)o.push(a)}}))}else{const{pixelType:t}=e,r=(a.max-a.min)/a.size,l=t.includes("s")||t.includes("u"),s=l&&1===r?Math.floor(a.min+.5):a.min,i=l&&1===r?Math.floor(a.max-.5):a.max,c=a.size;let f,p=0;a.counts.forEach((e=>p+=e/c)),a.counts.forEach(((e,t)=>{if(e>0){u.push(e);const l=Math.max(1,Math.round(e/c/p*m));f=d?n.features[t].attributes[h.name]:0===t?s:t===c-1?i:a.min+r*(t+.5);for(let e=0;e<l;e++)o.push(f)}}))}const g=t?.classificationMethod||"natural-breaks";let y=t?.definedInterval;"defined-interval"!==g||y||(y=Y(e,h,l));const w=V({values:o,valueFrequency:u,normalizationTotal:null,definition:new j({classificationMethod:g,breakCount:l,definedInterval:y})});let C=t?.colors;if(!C){const e=t?.colorRamp||k;s.colorRamp=e;const n=p(e,{numColors:w.classBreaks.length,interpolateAlpha:!0});C=n.map((e=>e.slice(1)))}const x=w.classBreaks.map(((e,t)=>new f({minValue:e.minValue,maxValue:e.maxValue,label:e.label,symbol:new I({color:C[t]})})));return new r({field:i,classBreakInfos:x,authoringInfo:s})}function Y(e,t,n){let a=0,r=0;if(null!=e.attributeTable){const n=e.attributeTable;a=r=n.features[0].attributes[t.name],n.features.forEach((e=>{const n=e.attributes[t.name];n<a&&(a=n),n>r&&(r=n)}))}else if(null!=e.histograms){const t=e.histograms;a=t[0].min,r=t[0].max}return(r-a)/n}const Z=new Map([["m/s","meter-per-second"],["km/h","kilometer-per-hour"],["knots","knots"],["ft/s","feet-per-second"],["mph","mile-per-hour"]]);function $(e){if(!b(e))return null;let t;if(null!=e.statistics&&e.statistics.length&&("vector-magdir"===e.dataType||"vector-uv"===e.dataType)){const{minMagnitude:n,maxMagnitude:a}=ae(e.dataType,e.statistics);t=[new M({field:"Magnitude",minSize:10,maxSize:40,minDataValue:n,maxDataValue:a})]}const n=null!=e.multidimensionalInfo?Z.get(e.multidimensionalInfo.variables[0].unit):void 0,a=new m({visualVariables:t,inputUnit:n,rotationType:"geographic"});return a.visualVariables=[...a.sizeVariables,...a.rotationVariables],a}function ee(e){return{color:e.symbolLayers[0].material?.color,type:"esriSFS",style:"esriSFSSolid"}}function te(e){if("uniqueValue"===e.type){const t=e.uniqueValueInfos,n=t?.[0].symbol;return n?.symbolLayers?.length&&(e.uniqueValueInfos=t?.map((e=>({value:e.value,label:e.label,symbol:e.symbol?ee(e.symbol):null})))),e}if("classBreaks"===e.type){const t=e.classBreakInfos,n=t[0].symbol;return n?.symbolLayers?.length&&(e.classBreakInfos=t.map((e=>({classMinValue:e.classMinValue,classMaxValue:e.classMaxValue,label:e.label,symbol:e.symbol?ee(e.symbol):null})))),e}return e}function ne(t){if(!w(t))return null;let n;if(null!=t.statistics&&t.statistics.length>0&&("vector-magdir"===t.dataType||"vector-uv"===t.dataType)){const{minMagnitude:a,maxMagnitude:r}=ae(t.dataType,t.statistics);n=[new x({field:"Magnitude",stops:[new R({value:a,color:new e("#1020c0")}),new R({value:r,color:new e("#c02010")})]})]}return new l({visualVariables:n})}function ae(e,t){let n,a;if("vector-magdir"===e)n=t[0].min,a=t[0].max;else{const e=t[0].min,r=t[0].max,l=t[1].min,s=t[1].max;n=0,a=Math.max(Math.abs(e),Math.abs(l),Math.abs(r),Math.abs(s))}return{minMagnitude:n,maxMagnitude:a}}export{X as createClassBreaksRenderer,K as createColormapRenderer,B as createDefaultRenderer,ne as createFlowRenderer,Q as createShadedReliefRenderer,U as createStretchRenderer,P as createUVRenderer,$ as createVectorFieldRenderer,ae as estimateMagnitudeRange,O as getBandNames,G as getClassField,D as getDefaultBandCombination,N as getDefaultInterpolation,Y as getDefinedInterval,H as getField,z as getFunctionColorRamp,E as getSupportedRendererTypes,W as getVariableRasterInfo,A as getWellKnownBandIndexes,q as matchPresetRenderer,te as normalizeRendererJSON};