public zonalHelper(rasterUtil rasterUtility,esriUtil.Forms.RunningProcess.frmRunningProcessDialog runningdialog)
 {
     rsUtil = rasterUtility;
     tempWksStr = rsUtil.TempConvDir;
     rd = runningdialog;
 }
        public void summarizeAcrossFields(ITable ftrCls, string[] fieldNames, esriUtil.rasterUtil.localType[] stats, string qWhere="", string prefix="")
        {
            int[] fldIndex = new int[fieldNames.Length];
            int[] updateFldsIndex =  new int[stats.Length];
            string[] updateNames = new string[stats.Length];
            for (int i = 0; i < fieldNames.Length; i++)
            {
                fldIndex[i]=ftrCls.FindField(fieldNames[i]);
            }
            for (int i = 0; i < stats.Length; i++)
            {
                string nm = stats[i].ToString();
                if(prefix!="" && prefix!=null) nm = prefix+"_"+nm;
                updateNames[i] = geoUtil.createField(ftrCls,nm,esriFieldType.esriFieldTypeDouble,false);
                updateFldsIndex[i] = ftrCls.FindField(updateNames[i]);
            }
            bool catStat = false;
            if(stats.Contains(rasterUtil.localType.ASM)||stats.Contains(rasterUtil.localType.ENTROPY)||stats.Contains(rasterUtil.localType.UNIQUE)||stats.Contains(rasterUtil.localType.MEDIAN)||stats.Contains(rasterUtil.localType.MODE)) catStat = true;
            //Console.WriteLine("Updating Categories = " + catStat.ToString());
            IQueryFilter qf = new QueryFilterClass();
            if (!String.IsNullOrEmpty(qWhere)) qf.WhereClause = qWhere;
            //qf.SubFields = String.Join(",", fieldNames)+","+String.Join(",",updateNames);
            ICursor uCur = ftrCls.Update(qf, true);
            IRow ftr = uCur.NextRow();
            while (ftr != null)
            {
                double[] vlArr = new double[10];//cnt, min, max, sum, sum2, minfield, maxfield, subtract, multiply, divide
                Dictionary<string, int> dic = new Dictionary<string,int>();
                vlArr[1]=double.MaxValue;
                vlArr[2]=double.MinValue;
                for (int i = 0; i < fldIndex.Length; i++)
                {
                    object vlObj = ftr.get_Value(fldIndex[i]);
                    if(vlObj!=null)
                    {
                        double vl = System.Convert.ToDouble(vlObj);
                        vlArr[0] = vlArr[0] + 1;
                        if (vl < vlArr[1])
                        {
                            vlArr[1] = vl;
                            vlArr[5]=i;
                        }
                        if (vl > vlArr[2])
                        {
                            vlArr[2] = vl;
                            vlArr[6] = i;
                        }
                        vlArr[3] = vlArr[3] + vl;
                        vlArr[4] = vlArr[4] + (vl * vl);
                        vlArr[7] = vlArr[7] - vl;
                        vlArr[8] = vlArr[8] * vl;
                        vlArr[9] = vlArr[9] / vl;
                        if(catStat)
                        {
                            int cntVl;
                            string vlStr = vl.ToString();
                            if(dic.TryGetValue(vlStr,out cntVl))
                            {
                                dic[vlStr] = cntVl+1;
                            }
                            else
                            {
                                dic.Add(vlStr,1);
                            }
                        }
                    }
                }
                for (int i = 0; i < stats.Length; i++)
                {
                    rasterUtil.localType st = stats[i];
                    double sVl = 0;
                    switch (st)
                    {
                        case rasterUtil.localType.MAX:
                            sVl = vlArr[2];
                         break;
                        case rasterUtil.localType.MIN:
                            sVl = vlArr[1];
                         break;
                        case rasterUtil.localType.MAXBAND:
                            sVl = vlArr[6];
                         break;
                        case rasterUtil.localType.MINBAND:
                            sVl = vlArr[5];
                         break;
                        case rasterUtil.localType.SUM:
                            sVl = vlArr[3];
                         break;
                        case rasterUtil.localType.MULTIPLY:
                            sVl = vlArr[8];
                         break;
                        case rasterUtil.localType.DIVIDE:
                            sVl = vlArr[9];
                         break;
                        case rasterUtil.localType.SUBTRACT:
                            sVl = vlArr[7];
                         break;
                        case rasterUtil.localType.MEAN:
                            sVl = vlArr[3]/vlArr[0];
                         break;
                        case rasterUtil.localType.VARIANCE:
                            sVl = (vlArr[4] - (Math.Pow(vlArr[3], 2) / vlArr[0])) / (vlArr[0] - 1);
                         break;
                        case rasterUtil.localType.STD:
                            sVl = Math.Sqrt((vlArr[4] - (Math.Pow(vlArr[3], 2) / vlArr[0])) / (vlArr[0] - 1));
                         break;
                        case rasterUtil.localType.MODE:
                            sVl = getMode(dic);
                         break;
                        case rasterUtil.localType.MEDIAN:
                            sVl = getMedian(dic);
                         break;
                        case rasterUtil.localType.UNIQUE:
                            sVl = dic.Keys.Count;
                         break;
                        case rasterUtil.localType.ENTROPY:
                            sVl = getEntropy(dic);
                         break;
                        case rasterUtil.localType.ASM:
                            sVl = getASM(dic);
                         break;
                        default:
                         break;
                    }
                    //Console.WriteLine("Setting value " + updateNames[i] + " to " + sVl.ToString());
                    ftr.set_Value(updateFldsIndex[i],sVl);

                }
                uCur.UpdateRow(ftr);
                ftr = uCur.NextRow();
            }
            System.Runtime.InteropServices.Marshal.ReleaseComObject(uCur);
        }
 public batchCalculations(rasterUtil rasterUtility, esriUtil.Forms.RunningProcess.frmRunningProcessDialog runningDialog)
 {
     rsUtil = rasterUtility;
     if (rp != null) rp = runningDialog;
 }
 public ITable zonalStats(object inZoneRaster, object inValueRaster, string outTableName, zoneType[] zoneTypes, esriUtil.Forms.RunningProcess.frmRunningProcessDialog rd,bool classCounts=false)
 {
     FunctionRasters.zonalHelper zH = new FunctionRasters.zonalHelper(this,rd);
     zH.InValueRaster = createIdentityRaster(inValueRaster);
     zH.InZoneRaster =createIdentityRaster(inZoneRaster);
     zH.ZoneTypes = zoneTypes;
     zH.OutTableName = outTableName;
     zH.ZoneClassCount = classCounts;
     zH.setZoneValues();
     return zH.OutTable;
 }