/// <summary> /// Builds a probabilistic raster (PROB) of all the categories (1st raster is the baseline category) and a maximum likelihood raster (MLC) depicting the /// most probable raster band from the PROB raster. MCL raster also has the category names appended to the Raster's VAT. Required properties to run this /// procedure include; SASoutput file, InRater, OutWorkspace, and the DependentField Name. /// </summary> /// <returns>a IRaster Array with the first Raster being the probability raster and the second raster being the maximum likelihood raster</returns> public IRaster createModelRaster(IRaster seedRaster) { Dictionary <string, float[]> slopesDic = new Dictionary <string, float[]>(); foreach (string s in Categories) { slopesDic.Add(s, betasdic[s].Values.ToArray()); } return(rsUtil.calcPolytomousLogisticRegressFunction(InRaster, slopesDic, seedRaster)); #region oldcode //IRaster2 inRs2 = (IRaster2)InRaster; //IPnt pntSize = new PntClass(); //pntSize.SetCoords(512, 512); //IRasterCursor inRsCur = inRs2.CreateCursorEx(pntSize); //int outBndCnt = Categories.Length + 1; //int betasCnt = betasdic.Count; //IRaster outRst = rsUtil.createNewRaster(InRaster, OutWorkspace, "PROB", outBndCnt, rstPixelType.PT_UCHAR); //IRaster outRstML = rsUtil.createNewRaster(InRaster, OutWorkspace, "MLC", 1, rstPixelType.PT_UCHAR); //IRasterEdit outRstE = (IRasterEdit)outRst; //IRasterEdit outRstMLE = (IRasterEdit)outRstML; //int inBndCnt = ((IRasterBandCollection)InRaster).Count; //System.Array[] inBndArr = new System.Array[inBndCnt]; //System.Array[] outBndArr = new System.Array[outBndCnt]; //double[] xArr = new double[inBndCnt]; //double[] regArr = new double[betasCnt]; ////int curCnt = 1; //while (inRsCur.Next() == true) //{ // //curCnt++; // IPixelBlock3 inPb = (IPixelBlock3)inRsCur.PixelBlock; // pntSize.SetCoords(inPb.Width, inPb.Height); // IPixelBlock3 outPb = (IPixelBlock3)outRst.CreatePixelBlock(pntSize); // IPixelBlock3 outPb2 = (IPixelBlock3)outRstML.CreatePixelBlock(pntSize); // #region set system arraies // System.Array mlArr = (System.Array)outPb2.get_PixelData(0); // for (int i = 0; i < inBndCnt; i++) // { // inBndArr[i] = (System.Array)inPb.get_PixelData(i); // } // for (int i = 0; i < outBndCnt; i++) // { // outBndArr[i] = (System.Array)outPb.get_PixelData(i); // } // #endregion // #region update pixel values in outBndArr // for (int c = 0; c < inPb.Width; c++) // { // for (int r = 0; r < inPb.Height; r++) // { // for (int i = 0; i < inBndCnt; i++) // { // double xArrVl = System.Convert.ToDouble(inBndArr[i].GetValue(c, r)); // xArr[i] = xArrVl; // } // for (int i = 0; i < betasCnt; i++) // { // double[] vls = betasdic[Categories[i]].Values.ToArray(); // double sumReg = vls[0]; // for (int j = 1; j < vls.Length; j++) // { // sumReg += vls[j] * xArr[j-1]; // } // regArr[i] = Math.Exp(sumReg); // } // double sumExp1 = 1 + regArr.Sum(); // double sumProb = 0; // double maxProbBnd = 0; // double maxProb = 0; // double vl = 0; // byte vlbyte = 0; // for (int i = 0; i < regArr.Length; i++) // { // vl = regArr[i] / sumExp1; // try // { // if (Double.IsNaN(vl)) // { // vl = 0; // } // vlbyte = System.Convert.ToByte(System.Math.Round(vl * 100)); // } // catch (Exception e) // { // Console.WriteLine("Error: vl = " + vl.ToString()); // vl = 0; // vlbyte = System.Convert.ToByte(0); // Console.WriteLine(e.ToString()); // } // int otBnd = i + 1; // outBndArr[otBnd].SetValue(vlbyte, c, r); // sumProb += vl; // if (vl > maxProb) // { // maxProb = vl; // maxProbBnd = otBnd; // } // } // vl = 1 - sumProb; // vlbyte = System.Convert.ToByte(System.Math.Round(vl * 100)); // if (vl > maxProb) // { // maxProbBnd = 0; // } // outBndArr[0].SetValue(vlbyte, c, r); // mlArr.SetValue(System.Convert.ToByte(maxProbBnd+1), c, r); // } // } // #endregion // #region store pixel values in out raster // for (int i = 0; i < outBndCnt; i++) // { // outPb.set_PixelData(i, outBndArr[i]); // } // outRstE.Write(inRsCur.TopLeft, (IPixelBlock)outPb); // outPb2.set_PixelData(0, mlArr); // outRstMLE.Write(inRsCur.TopLeft, (IPixelBlock)outPb2); // #endregion //} //outRstE.Refresh(); //outRstMLE.Refresh(); //rsUtil.calcStatsAndHist(((IRasterBandCollection)outRst).Item(0).RasterDataset); //IRasterDataset mlDset = ((IRasterBandCollection)outRstML).Item(0).RasterDataset; //rsUtil.calcStatsAndHist(mlDset); //#region update MLC attribute table //IRasterDatasetEdit2 mlDsetE = (IRasterDatasetEdit2)((IRasterDataset2)mlDset); //mlDsetE.BuildAttributeTable(); //ITable rTbl = ((IRasterBandCollection)mlDset).Item(0).AttributeTable; //IField fld = new FieldClass(); //IFieldEdit fldE = (IFieldEdit)fld; //fldE.Name_2 = Dependentfield; //fldE.Type_2 = esriFieldType.esriFieldTypeString; //rTbl.AddField(fld); //ICursor scur = rTbl.Search(null,false); //int vlIndex = scur.FindField("Value"); //int uIndex = scur.FindField(dependentfield); //IRow srow = scur.NextRow(); //while(srow!=null) //{ // int vl = System.Convert.ToInt32(srow.get_Value(vlIndex)); // if (vl > 1) // { // srow.set_Value(uIndex, Categories[vl - 2]); // } // else // { // srow.set_Value(uIndex, "BASE"); // } // srow.Store(); // srow= scur.NextRow(); //} //#endregion //return new IRaster[]{outRst,outRstML}; #endregion }