// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://@sbaseurl@/jsapi/jsapi/esri/copyright.txt and http://www.arcgis.com/apps/webappbuilder/copyright.txt for details.

//>>built
define("dojo/_base/declare dojo/_base/lang dojo/_base/array dojo/string dojo/number esri/request esri/layers/RasterFunction ./rasterFunctions".split(" "),function(y,p,m,r,z,u,v,A){var B=function(a,b){if(!a||!b)return!1;var c=a.length;if(c!==b.length)return!1;for(var d=0;d<c;d++)if(a[d]instanceof Array&&b[d]instanceof Array){if(!a[d].compare(b[d]))return!1}else if(a[d]!==b[d])return!1;return!0},C=function(a){try{if(a.attributes&&"ForestFragmentation_US_USFS_2002"===a.attributes.Name&&"Forest Fragmentation"===
a.attributes.Title&&49===a.attributes.OBJECTID)return null}catch(g){}a=a.attributes;var b,c;if(a){var d={id:a.OBJECTID,name:a.Name,title:a.Title,url:a.Url,metadata:a.Metadata};if(a.InputRanges&&a.InputRanges.split&&"undefined"!==typeof a.OutputValues&&a.OutputValues.split){var e=m.map(a.InputRanges.split(","),function(g){return parseFloat(g.trim())});var f=m.map(a.OutputValues.split(","),function(g){return parseInt(g.trim(),10)});a.RangeLabels&&a.RangeLabels.split&&(b=m.map(a.RangeLabels.split(","),
function(g){return g.trim()}))}if(a.NoDataRanges&&a.NoDataRanges.split){var k=m.map(a.NoDataRanges.split(","),function(g){return parseFloat(g.trim())});a.NoDataRangeLabels&&a.NoDataRangeLabels.split&&(c=m.map(a.NoDataRangeLabels.split(","),function(g){return g.trim()}))}d.remapRanges=q.createRemapRanges(e,f,k,{labels:b,noDataLabels:c})}return d},w=function(a){return m.map(a.colors,function(b){return[b.value].concat(b.rgb)})},t=function(a,b){var c=[],d=0;a&&a.overlayLayers&&a.overlayLayers.push?0<
a.overlayLayers.length?(m.forEach(a.overlayLayers,function(e,f){d+=e.weight;e.remapRanges&&e.remapRanges.push&&0<e.remapRanges.length?m.forEach(e.remapRanges,function(k,g){(isNaN(k.outputValue)||null===k.outputValue)&&c.push(r.substitute(b.wro.validation.overlayLayerOutputInvalid,[f,g]));(isNaN(k.inputMin)||null===k.inputMin||isNaN(k.inputMax)||null===k.inputMax||k.inputMin>k.inputMax)&&c.push(r.substitute(b.wro.validation.overlayLayerInputInvalid,[f,g]))}):c.push(r.substitute(b.wro.validation.overlayLayerRangesMissing,
[f]))}),100!==d&&c.push(b.wro.validation.overlayLayerWeight)):c.push(b.wro.validation.overlayLayerRequired):c.push(b.wro.validation.overlayLayerNotDefined);return c},D=function(a,b,c){a+="/computeHistograms";var d={geometryType:"esriGeometryPolygon",f:"json"};d.geometry=window.JSON.stringify(b);c&&(c.renderingRule&&(d.renderingRule=window.JSON.stringify(c.renderingRule)),c.pixelSize&&(d.pixelSize=window.JSON.stringify(c.pixelSize)));return u({url:a,content:d,handleAs:"json",callbackParamName:"callback"})},
q={rasterFunctionArgumentsToRemapRanges:function(a,b){if(a){var c=p.mixin({inputRangesArgumentName:"InputRanges",outputValuesArgumentName:"OutputValues",noDataRangesArgumentName:"NoDataRanges",labelsArgumentName:"Labels",noDataLabelsArgumentName:"NoDataLabels"},b);b=a[c.inputRangesArgumentName];var d=a[c.outputValuesArgumentName];var e=a[c.noDataRangesArgumentName];var f=a[c.labelsArgumentName];a=a[c.noDataLabelsArgumentName];d=this.createRemapRanges(b,d,e,{labels:f,noDataLabels:a,sortBy:"inputMin"})}return d},
createRemapRanges:function(a,b,c,d){var e=[],f=!1;if(d){var k=d.labels;var g=d.noDataLabels;var l=d.sortBy}a&&a.push&&b&&b.push&&a.length===2*b.length&&(f=k&&k.push&&k.length===b.length,m.forEach(b,function(h,n){var x=2*n;h={inputMin:a[x],inputMax:a[x+1],outputValue:h,originalOutputValue:h};h.label=f?k[n]:this.getRangeString(h.inputMin,h.inputMax);e.push(h)},this));c&&c.push&&0<c.length&&(f=g&&g.push&&g.length===c.length/2,m.forEach(c,function(h,n){0===n%2&&(h={inputMin:h,inputMax:c[n+1],outputValue:0,
originalOutputValue:0},h.label=f?g[n]:this.getRangeString(h.inputMin,h.inputMax),e.push(h))},this));if(0<e.length)return l&&e.sort(function(h,n){return h[l]<n[l]?-1:h[l]>n[l]?1:0}),e},getRangeString:function(a,b){return a===b?a+"":a+" - "+b},remapRangesToRasterFunctionArguments:function(a,b){if(a){var c={};b=p.mixin({includeLabels:!0,inputRangesArgumentName:"InputRanges",outputValuesArgumentName:"OutputValues",noDataRangesArgumentName:"NoDataRanges",labelsArgumentName:"Labels",noDataLabelsArgumentName:"NoDataLabels"},
b);var d=[];var e=[];var f=[];var k=[];var g=[];m.forEach(a,function(l){var h=l.label||this.getRangeString(l.inputMin,l.inputMax);l.outputValue?(d.push(l.inputMin),d.push(l.inputMax),e.push(l.outputValue),k.push(h)):(f.push(l.inputMin),f.push(l.inputMax),g.push(h))},this);0<e.length&&(c[b.inputRangesArgumentName]=d,c[b.outputValuesArgumentName]=e,b.includeLabels&&(c[b.labelsArgumentName]=k));0<f.length&&(c[b.noDataRangesArgumentName]=f,b.includeLabels&&(c[b.noDataLabelsArgumentName]=g))}return c},
getRemapRangeByValue:function(a,b){var c;(b||0===b)&&m.some(a,function(d){return b===d.inputMin&&b===d.inputMax||b>=d.inputMin&&b<d.inputMax?(c=d,!0):!1});return c},findRemapRange:function(a,b){var c,d;if(a&&b)return m.some(a,function(e){if((b.inputMin||0===b.inputMin)&&(b.inputMax||0===b.inputMax))return(c=e.inputMin===b.inputMin&&e.inputMax===b.inputMax)&&(d=e),c}),d}};return y([],{context:null,i18n:null,constructor:function(a,b){b=b||{};this.imageServiceLayer=a;this.rasterFunctionName=b.rasterFunctionName||
"";this.histogramRasterFunctionName=b.histogramRasterFunctionName||"";this.rastersInFunction=b.rastersInFunction||0;this.variableName=b.variableName||"Raster";this.colorMapArgName=b.colorMapArgName||"Colormap";this.dummyRasterId=b.dummyRasterId||1;this.colormapDefinitions=b.colormapDefinitions||[];this.argumentNamePrefixes=p.mixin({id:"Raster",weight:"Weight_Raster",inputRanges:"InputRanges_Raster",outputValues:"OutputValues_Raster",noDataRanges:"NoDataRanges_Raster",labels:"Labels_Raster",noDataLabels:"NoDataLabels"},
b.argumentNamePrefixes);this.queryParameters=p.mixin({where:"1\x3d1",outFields:["*"],returnGeometry:!1},b.queryParameters);this.rasterLayers=b.rasterLayers||[]},initRasterLayers:function(a){var b=this;return this.queryRasters().then(function(c){return b._initRasterLayers(c.features,a)})},queryRasters:function(a){var b=this.imageServiceLayer.url+"/query";a=p.mixin(p.mixin({f:"json"},a),this.queryParameters);a.outFields&&a.outFields.join&&(a.outFields=a.outFields.join(","));return u({url:b,content:a,
handleAs:"json",callbackParamName:"callback"})},_initRasterLayers:function(a,b){var c=this.context;try{this.dummyRasterId=1,this.imageServiceLayer&&this.imageServiceLayer.url&&-1!==this.imageServiceLayer.url.indexOf("//landscape3.arcgis.com")&&(this.dummyRasterId=53)}catch(f){}var d=this,e;b=(b||{}).featureCompareFunc||function(f,k){return f.attributes.Title<k.attributes.Title?-1:f.attributes.Title>k.attributes.Title?1:0};a&&a.length&&0<a.length&&a[0].attributes&&(d.rasterLayers&&d.rasterLayers.push?
d.rasterLayers.length=0:d.rasterLayers=[],b&&a.sort(b),m.forEach(a,function(f){if(e=C(f))e.url=c.checkMixedContent(e.url),d.rasterLayers.push(e)}));return d.rasterLayers},createNewModel:function(){var a={overlayLayers:[]};this.colormapDefinitions&&this.colormapDefinitions instanceof Array&&0<this.colormapDefinitions.length&&(a.colormapDefinition=this.colormapDefinitions[0]);return a},getRasterLayer:function(a){var b;m.some(this.rasterLayers,function(c){if(c.id===a)return b=c,!1});return b},setOverlayLayerDefaults:function(a,
b){b&&(b.remapRanges&&a.remapRanges&&m.forEach(b.remapRanges,function(c){var d;c.label&&(d=q.findRemapRange(a.remapRanges,{inputMin:c.inputMin,inputMax:c.inputMax}))&&(d.label=c.label)},this),a.title=b.title)},operationalLayersToModel:function(a){var b,c=this.rasterFunctionName;c&&m.some(a,function(d){return d.renderingRule&&d.renderingRule&&d.renderingRule.rasterFunction===c?(b=d,!0):!1},this);return this.imageServiceLayerToModel(b)},imageServiceLayerToModel:function(a){var b=this;var c=a.renderingRule;
if(a&&c){var d={overlayLayers:[]};if(this.rasterFunctionName===c.rasterFunction){var e=new RegExp("^"+this.argumentNamePrefixes.id+"[1-9]+$");c=c.rasterFunctionArguments;a.remapRangeLabels&&p.mixin(c,a.remapRangeLabels);a.noDataRangeLabels&&p.mixin(c,a.noDataRangeLabels);for(var f in c)if(f.match(e)){var k=f.replace(this.argumentNamePrefixes.id,"");var g={id:parseInt(c[f].replace("$",""),10)};a=this.argumentNamePrefixes.weight+k;g.weight=c[a];g.weight&&0<g.weight&&(g.weight*=100,g=p.mixin(this.getRasterLayer(g.id)||
{},g),g.remapRanges=q.rasterFunctionArgumentsToRemapRanges(c,{inputRangesArgumentName:this.argumentNamePrefixes.inputRanges+k,outputValuesArgumentName:this.argumentNamePrefixes.outputValues+k,noDataRangesArgumentName:this.argumentNamePrefixes.noDataRanges+k,labelsArgumentName:this.argumentNamePrefixes.labels+k,noDataLabelsArgumentName:this.argumentNamePrefixes.noDataLabels+k}),d.overlayLayers.push(g))}else f===this.colorMapArgName&&(d.colormapDefinition=this.findColormapDefinition(c[f]))}else e=c,
c=e.rasterFunctionArguments,"Colormap"===e.rasterFunction&&(d.colormapDefinition=this.findColormapDefinition(c.Colormap),e=c.Raster,c=e.rasterFunctionArguments),"Local"===e.rasterFunction&&49===c.Operation?(e=c.Rasters[0],c=e.rasterFunctionArguments):"Local"===e.rasterFunction&&48===c.Operation&&(c.Operation=49,e=c.Rasters[0],c=e.rasterFunctionArguments),"Local"===e.rasterFunction&&55===c.Operation&&(d.overlayLayers=m.map(c.Rasters,function(l){var h={};m.forEach(l.rasterFunctionArguments.Rasters,
function(n){isNaN(n)?(h.id=parseInt(n.rasterFunctionArguments.Raster.substr(1),10),h=p.mixin(b.getRasterLayer(h.id)||{},h),h.remapRanges=q.rasterFunctionArgumentsToRemapRanges(n.rasterFunctionArguments)):h.weight=100*n});return h}));return d}},findColormapDefinition:function(a){var b;m.some(this.colormapDefinitions,function(c){var d=!1;return(d=m.every(c.colors,function(e,f){return B([e.value].concat(e.rgb),a[f])}))?(b=c,!0):!1});return b},modelToOperationalLayers:function(a,b){return[this.modelToImageServiceLayer(a,
b)]},modelToImageServiceLayer:function(a,b){var c={id:this.imageServiceLayer.id,url:this.imageServiceLayer.url,opacity:this.imageServiceLayer.opacity,title:"Weighted Overlay Model"};a=this.getRasterFunction(a.overlayLayers,a.colormapDefinition,{includeLabels:!0});var d={},e={},f;for(f in a.arguments)0===f.indexOf(this.argumentNamePrefixes.labels)?(d[f]=a.arguments[f],delete a.arguments[f]):0===f.indexOf(this.argumentNamePrefixes.noDataLabels)&&(e[f]=a.arguments[f],delete a.arguments[f]);c.renderingRule=
a.toJson();c.remapRangeLabels=d;c.noDataRangeLabels=e;b&&b.modelTitle&&(c.title=b.modelTitle);return c},getRasterFunction:function(a,b,c){var d=[];m.forEach(a,function(h){0<h.weight&&d.push(h)});if(10.3<=this.imageServiceLayer.version){var e=m.map(d,function(h){var n=q.remapRangesToRasterFunctionArguments(h.remapRanges);n.Raster="$"+h.id;n.weight=h.weight/100;return n}),f;b&&(f=w(b));b=A.createWeightedSumParams(e,f);return new v(b)}f=new v;a=d.length;var k={};c=p.mixin({rasterFunctionName:this.rasterFunctionName,
rastersInFunction:this.rastersInFunction,argumentNamePrefixes:this.argumentNamePrefixes,variableName:this.variableName,dummyRasterId:this.dummyRasterId,includeLabels:!1},c);f.functionName=c.rasterFunctionName;c.variableName&&(f.variableName=c.variableName);for(var g=1;g<=c.rastersInFunction;g++){var l=g<=a?d[g-1]:{id:c.dummyRasterId,weight:0};k[c.argumentNamePrefixes.id+g]="$"+l.id;k[c.argumentNamePrefixes.weight+g]=z.round(l.weight/100,2);0<l.weight&&l.remapRanges&&(l=q.remapRangesToRasterFunctionArguments(l.remapRanges,
{includeLabels:c.includeLabels,inputRangesArgumentName:c.argumentNamePrefixes.inputRanges+g,outputValuesArgumentName:c.argumentNamePrefixes.outputValues+g,noDataRangesArgumentName:c.argumentNamePrefixes.noDataRanges+g,labelsArgumentName:c.argumentNamePrefixes.labels+g,noDataLabelsArgumentName:c.argumentNamePrefixes.noDataLabels+g}),p.mixin(k,l))}if(this.colorMapArgName){b&&(e=w(b));if(!e)throw this.i18n.wro.validation.requiresColormap;k[this.colorMapArgName]=e}f.arguments=k;return f},validateModel:function(a){a=
t(a,this.i18n);return{isValid:1>a.length,modelErrors:a}},runModel:function(a){if(this.imageServiceLayer){var b=t(a,this.i18n);if(1>b.length)this.imageServiceLayer.setRenderingRule(this.getRasterFunction(a.overlayLayers,a.colormapDefinition));else throw this.context.showMessages(this.i18n.wro.validation.createModelError,"",b),this.wro.validation.invalidModel+":\n\n"+b.join("\n");}else throw this.i18n.wro.validation.imageServiceNotDefined;},clearModel:function(){this.imageServiceLayer&&null!==this.imageServiceLayer.renderingRule&&
this.imageServiceLayer.setRenderingRule(null)},getHistogram:function(a,b,c){var d=p.mixin({},c);if(this.histogramRasterFunctionName){if(this.imageServiceLayer){c=t(a,this.i18n);if(1>c.length)return d.renderingRule=this.getRasterFunction(a.overlayLayers,a.colormapDefinition,{rasterFunctionName:this.histogramRasterFunctionName}).toJson(),D(this.imageServiceLayer.url,b,d).then(function(e){return e.histograms[0]});throw this.i18n.wro.validation.invalidModel+":\n\n"+c.join("\n");}throw this.i18n.wro.validation.imageLayerNotDefined;
}throw this.i18n.wro.validation.histogramNotDefined;},getModelPixelSize:function(a,b){var c=this.imageServiceLayer;b=p.mixin({width:c.maxImageWidth,height:c.maxImageHeight},b);var d=Math.max(c.pixelSizeX,Math.ceil(a.getWidth()/b.width));a=Math.max(c.pixelSizeY,Math.ceil(a.getHeight()/b.height));b.forceSquare?(a={x:Math.max(d,a)},a.y=a.x):a={x:d,y:a};return a},getColormapDefinition:function(a){var b;m.some(this.colormapDefinitions,function(c){if(c.id===a)return b=c,!1});return b}})});