Example #1
0
        private void populateComboBox()
        {
            if (mp != null)
            {
                IEnumLayer rstLyrs = vUtil.getActiveViewLayers(viewUtility.esriIRasterLayer);
                ILayer     lyr     = rstLyrs.Next();
                while (lyr != null)
                {
                    string       lyrNm  = lyr.Name;
                    IRasterLayer rstLyr = (IRasterLayer)lyr;
                    IRaster      rst    = rsUtil.createRaster(((IRaster2)rstLyr.Raster).RasterDataset);
                    if (!rstDic.ContainsKey(lyrNm))
                    {
                        rstDic.Add(lyrNm, rst);
                        cmbInRaster1.Items.Add(lyrNm);
                        cmbInRaster2.Items.Add(lyrNm);
                    }
                    lyr = rstLyrs.Next();
                }
            }
            foreach (string s in new string[] { "+", "-", "*", "/", "POW", "MOD" })
            {
                esriRasterArithmeticOperation opVl = esriRasterArithmeticOperation.esriRasterMode;
                switch (s)
                {
                case "+":
                    opVl = esriRasterArithmeticOperation.esriRasterPlus;
                    break;

                case "-":
                    opVl = esriRasterArithmeticOperation.esriRasterMinus;
                    break;

                case "*":
                    opVl = esriRasterArithmeticOperation.esriRasterMultiply;
                    break;

                case "/":
                    opVl = esriRasterArithmeticOperation.esriRasterDivide;
                    break;

                case "POW":
                    opVl = esriRasterArithmeticOperation.esriRasterPower;
                    break;

                default:
                    opVl = esriRasterArithmeticOperation.esriRasterMode;
                    break;
                }
                cmbProcess.Items.Add(s);
                oprDic.Add(s, opVl);
            }
        }
 public static void estimateStatistics(object inRaster1, object inRaster2, IRaster outRaster, esriRasterArithmeticOperation arithmeticType)
 {
     if (inRaster1 is Raster&&inRaster2 is IScalar)
     {
         IRasterBandCollection rsbci = (IRasterBandCollection)inRaster1;
         IRasterBandCollection rsbco = (IRasterBandCollection)outRaster;
         double[] sc = (double[])((IScalar)inRaster2).Value;
         for (int i = 0; i < rsbci.Count; i++)
         {
             IRasterBand rsBi = rsbci.Item(i);
             IRasterBand rsBo = rsbco.Item(i);
             IRasterStatistics rsBStatsi = rsBi.Statistics;
             IRasterStatistics rsBStatso = rsBo.Statistics;
             if (rsBStatsi == null)
             {
                 continue;
             }
             if (rsBStatso == null)
             {
                 rsBStatso = new RasterStatistics();
             }
             double vl = sc[i];
             switch (arithmeticType)
             {
                 case esriRasterArithmeticOperation.esriRasterDivide:
                     rsBStatso.Minimum = rsBStatsi.Minimum / vl;
                     rsBStatso.Maximum = rsBStatsi.Maximum / vl;
                     rsBStatso.Mean = rsBStatsi.Mean / vl;
                     rsBStatso.StandardDeviation = rsBStatsi.StandardDeviation / vl;
                     break;
                 case esriRasterArithmeticOperation.esriRasterMinus:
                     rsBStatso.Minimum = rsBStatsi.Minimum - vl;
                     rsBStatso.Maximum = rsBStatsi.Maximum - vl;
                     rsBStatso.Mean = rsBStatsi.Mean - vl;
                     rsBStatso.StandardDeviation = rsBStatsi.StandardDeviation - vl;
                     break;
                 case esriRasterArithmeticOperation.esriRasterMode:
                     rsBStatso.Minimum = rsBStatsi.Minimum % vl;
                     rsBStatso.Maximum = rsBStatsi.Maximum % vl;
                     rsBStatso.Mean = rsBStatsi.Mean % vl;
                     rsBStatso.StandardDeviation = rsBStatsi.StandardDeviation % vl;
                     break;
                 case esriRasterArithmeticOperation.esriRasterMultiply:
                     rsBStatso.Minimum = rsBStatsi.Minimum * vl;
                     rsBStatso.Maximum = rsBStatsi.Maximum * vl;
                     rsBStatso.Mean = rsBStatsi.Mean * vl;
                     rsBStatso.StandardDeviation = rsBStatsi.StandardDeviation * vl;
                     break;
                 case esriRasterArithmeticOperation.esriRasterPlus:
                     rsBStatso.Minimum = rsBStatsi.Minimum + vl;
                     rsBStatso.Maximum = rsBStatsi.Maximum + vl;
                     rsBStatso.Mean = rsBStatsi.Mean + vl;
                     rsBStatso.StandardDeviation = rsBStatsi.StandardDeviation + vl;
                     break;
                 case esriRasterArithmeticOperation.esriRasterPower:
                     rsBStatso.Minimum = Math.Pow(rsBStatsi.Minimum,vl);
                     rsBStatso.Maximum = Math.Pow(rsBStatsi.Maximum,vl);
                     rsBStatso.Mean = Math.Pow(rsBStatsi.Mean,vl);
                     rsBStatso.StandardDeviation = Math.Pow(rsBStatsi.StandardDeviation, vl);
                     break;
                  default:
                     break;
             }
         }
     }
     else if(inRaster1 is Scalar&&inRaster2 is Raster)
     {
         IRasterBandCollection rsbci = (IRasterBandCollection)inRaster2;
         IRasterBandCollection rsbco = (IRasterBandCollection)outRaster;
         double[] sc = (double[])((IScalar)inRaster1).Value;
         for (int i = 0; i < rsbci.Count; i++)
         {
             IRasterBand rsBi = rsbci.Item(i);
             IRasterBand rsBo = rsbco.Item(i);
             IRasterStatistics rsBStatsi = rsBi.Statistics;
             IRasterStatistics rsBStatso = rsBo.Statistics;
             if (rsBStatsi == null)
             {
                 continue;
             }
             if (rsBStatso == null)
             {
                 rsBStatso = new RasterStatistics();
             }
             double vl = sc[i];
             switch (arithmeticType)
             {
                 case esriRasterArithmeticOperation.esriRasterDivide:
                     rsBStatso.Minimum = rsBStatsi.Minimum / vl;
                     rsBStatso.Maximum = rsBStatsi.Maximum / vl;
                     rsBStatso.Mean = rsBStatsi.Mean / vl;
                     rsBStatso.StandardDeviation = rsBStatsi.StandardDeviation / vl;
                     break;
                 case esriRasterArithmeticOperation.esriRasterMinus:
                     rsBStatso.Minimum = rsBStatsi.Minimum - vl;
                     rsBStatso.Maximum = rsBStatsi.Maximum - vl;
                     rsBStatso.Mean = rsBStatsi.Mean - vl;
                     rsBStatso.StandardDeviation = rsBStatsi.StandardDeviation - vl;
                     break;
                 case esriRasterArithmeticOperation.esriRasterMode:
                     rsBStatso.Minimum = rsBStatsi.Minimum % vl;
                     rsBStatso.Maximum = rsBStatsi.Maximum % vl;
                     rsBStatso.Mean = rsBStatsi.Mean % vl;
                     rsBStatso.StandardDeviation = rsBStatsi.StandardDeviation % vl;
                     break;
                 case esriRasterArithmeticOperation.esriRasterMultiply:
                     rsBStatso.Minimum = rsBStatsi.Minimum * vl;
                     rsBStatso.Maximum = rsBStatsi.Maximum * vl;
                     rsBStatso.Mean = rsBStatsi.Mean * vl;
                     rsBStatso.StandardDeviation = rsBStatsi.StandardDeviation * vl;
                     break;
                 case esriRasterArithmeticOperation.esriRasterPlus:
                     rsBStatso.Minimum = rsBStatsi.Minimum + vl;
                     rsBStatso.Maximum = rsBStatsi.Maximum + vl;
                     rsBStatso.Mean = rsBStatsi.Mean + vl;
                     rsBStatso.StandardDeviation = rsBStatsi.StandardDeviation + vl;
                     break;
                 case esriRasterArithmeticOperation.esriRasterPower:
                     rsBStatso.Minimum = Math.Pow(rsBStatsi.Minimum, vl);
                     rsBStatso.Maximum = Math.Pow(rsBStatsi.Maximum, vl);
                     rsBStatso.Mean = Math.Pow(rsBStatsi.Mean, vl);
                     rsBStatso.StandardDeviation = Math.Pow(rsBStatsi.StandardDeviation, vl);
                     break;
                 default:
                     break;
             }
         }
     }
     else
     {
         IRasterBandCollection rsbci1 = (IRasterBandCollection)inRaster1;
         IRasterBandCollection rsbco = (IRasterBandCollection)outRaster;
         IRasterBandCollection rsbci2 = (IRasterBandCollection)inRaster1;
         for (int i = 0; i < rsbci1.Count; i++)
         {
             IRasterBand rsBi1 = rsbci1.Item(i);
             IRasterBand rsBi2 = rsbci2.Item(i);
             IRasterBand rsBo = rsbco.Item(i);
             IRasterStatistics rsBStatsi1 = rsBi1.Statistics;
             IRasterStatistics rsBStatsi2 = rsBi2.Statistics;
             IRasterStatistics rsBStatso = rsBo.Statistics;
             if (rsBStatsi1 == null||rsBStatsi2==null)
             {
                 continue;
             }
             if (rsBStatso == null)
             {
                 rsBStatso = new RasterStatistics();
             }
             switch (arithmeticType)
             {
                 case esriRasterArithmeticOperation.esriRasterDivide:
                     rsBStatso.Minimum = rsBStatsi1.Minimum / rsBStatsi2.Minimum;
                     rsBStatso.Maximum = rsBStatsi1.Maximum / rsBStatsi2.Maximum;
                     rsBStatso.Mean = rsBStatsi1.Mean / rsBStatsi2.Mean;
                     rsBStatso.StandardDeviation = rsBStatsi1.StandardDeviation / rsBStatsi2.StandardDeviation;
                     break;
                 case esriRasterArithmeticOperation.esriRasterMinus:
                     rsBStatso.Minimum = rsBStatsi1.Minimum - rsBStatsi2.Maximum;
                     rsBStatso.Maximum = rsBStatsi1.Maximum - rsBStatsi2.Minimum;
                     rsBStatso.Mean = (rsBStatsi1.Mean + rsBStatsi2.Mean)/2;
                     rsBStatso.StandardDeviation = (rsBStatsi1.StandardDeviation + rsBStatsi2.Minimum)/2;
                     break;
                 case esriRasterArithmeticOperation.esriRasterMode:
                     rsBStatso.Minimum = rsBStatsi1.Minimum % rsBStatsi2.Minimum;
                     rsBStatso.Maximum = rsBStatsi1.Maximum % rsBStatsi2.Maximum;
                     rsBStatso.Mean = rsBStatsi1.Mean % rsBStatsi2.Mean;
                     rsBStatso.StandardDeviation = rsBStatsi1.StandardDeviation % rsBStatsi2.StandardDeviation;
                     break;
                 case esriRasterArithmeticOperation.esriRasterMultiply:
                     rsBStatso.Minimum = rsBStatsi1.Minimum / rsBStatsi2.Minimum;
                     rsBStatso.Maximum = rsBStatsi1.Maximum / rsBStatsi2.Maximum;
                     rsBStatso.Mean = rsBStatsi1.Mean / rsBStatsi2.Mean;
                     rsBStatso.StandardDeviation = rsBStatsi1.StandardDeviation / rsBStatsi2.StandardDeviation;
                     break;
                 case esriRasterArithmeticOperation.esriRasterPlus:
                     rsBStatso.Minimum = rsBStatsi1.Minimum + rsBStatsi2.Maximum;
                     rsBStatso.Maximum = rsBStatsi1.Maximum + rsBStatsi2.Minimum;
                     rsBStatso.Mean = (rsBStatsi1.Mean + rsBStatsi2.Mean)/2;
                     rsBStatso.StandardDeviation = (rsBStatsi1.StandardDeviation + rsBStatsi2.Minimum)/2;
                     break;
                 case esriRasterArithmeticOperation.esriRasterPower:
                     rsBStatso.Minimum = Math.Pow(rsBStatsi1.Minimum, rsBStatsi2.Minimum);
                     rsBStatso.Maximum = Math.Pow(rsBStatsi1.Maximum, rsBStatsi2.Maximum);
                     rsBStatso.Mean = Math.Pow(rsBStatsi1.Mean, rsBStatsi2.Mean);
                     rsBStatso.StandardDeviation = Math.Pow(rsBStatsi1.StandardDeviation, rsBStatsi2.StandardDeviation);
                     break;
                 default:
                     break;
             }
         }
     }
 }
Example #3
0
        private void btnExecute_Click(object sender, EventArgs e)
        {
            string inRst1Nm = cmbInRaster1.Text;
            string inRst2Nm = cmbInRaster2.Text;
            string opNm     = cmbProcess.Text;
            string outNmRst = txtOutName.Text;

            if (inRst1Nm == "" || inRst1Nm == null)
            {
                MessageBox.Show("You must specify an input raster for In Raster 1 or type in a number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (inRst2Nm == "" || inRst2Nm == null)
            {
                MessageBox.Show("You must specify an input raster for In Raster 2 or type in a number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (opNm == "" || opNm == null)
            {
                MessageBox.Show("You must select a process from the dropdown menu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (outNmRst == "" || outNmRst == null)
            {
                MessageBox.Show("You must specify an output raster name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            object rs1, rs2;

            if (rstDic.ContainsKey(inRst1Nm))
            {
                rs1 = rstDic[inRst1Nm];
            }
            else if (rsUtil.isNumeric(inRst1Nm))
            {
                rs1 = inRst1Nm;
            }
            else
            {
                MessageBox.Show("You must specify an input raster for In Raster 1 or type in a number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (rstDic.ContainsKey(inRst2Nm))
            {
                rs2 = rstDic[inRst2Nm];
            }
            else if (rsUtil.isNumeric(inRst2Nm))
            {
                rs2 = inRst2Nm;
            }
            else
            {
                MessageBox.Show("You must specify an input raster for In Raster 2 or type in a number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            esriRasterArithmeticOperation op = oprDic[opNm];

            this.Visible = false;
            esriUtil.Forms.RunningProcess.frmRunningProcessDialog rp = new RunningProcess.frmRunningProcessDialog(false);
            DateTime dt = DateTime.Now;

            rp.addMessage("Creating Raster. This may take a while...");
            rp.stepPGBar(10);
            rp.TopMost = true;
            try
            {
                IRaster outRs = rsUtil.returnRaster(rsUtil.calcArithmaticFunction(rs1, rs2, op));
                outraster     = outRs;
                outrastername = outNmRst;
                if (mp != null && aM)
                {
                    rp.addMessage("Calculating Statistics...");
                    rp.Show();
                    rp.Refresh();
                    //rsUtil.calcStatsAndHist(((IRaster2)outRs).RasterDataset);
                    IRasterLayer rsLyr = new RasterLayerClass();
                    rsLyr.CreateFromRaster(outraster);
                    rsLyr.Name    = outrastername;
                    rsLyr.Visible = false;
                    mp.AddLayer((ILayer)rsLyr);
                }
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                rp.addMessage(ex.ToString());
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                DateTime dt2 = DateTime.Now;
                TimeSpan ts  = dt2.Subtract(dt);
                string   t   = " in " + ts.Days.ToString() + " days " + ts.Hours.ToString() + " hours " + ts.Minutes.ToString() + " minutes and " + ts.Seconds.ToString() + " seconds .";
                rp.stepPGBar(100);
                rp.addMessage("Finished Arithmetic Analysis" + t);
                rp.enableClose();
                this.Close();
            }
        }
 //, esriCellsizeType outCellSize = esriCellsizeType.esriCellsizeMinOf, esriExtentType outExtent = esriExtentType.esriExtentIntersectionOf )
 /// <summary>
 /// Will perform an arithmeticOperation on an input raster all bands
 /// </summary>
 /// <param name="inRaster1">either IRaster, IRasterDataset, or a valid path pointing to a raster</param>
 /// <param name="inRaster2">either IRaster, IRasterDataset, a numeric value, or a valid path pointing to a raster</param>
 /// <param name="op">the type of operation</param>
 /// <returns>a IRaster that can be used for further analysis</returns>
 public IFunctionRasterDataset calcArithmaticFunction(object inRaster1, object inRaster2, esriRasterArithmeticOperation op, rstPixelType outRasterType = rstPixelType.PT_FLOAT)
 {
     string tempAr = funcDir + "\\" + FuncCnt + ".afr";
     IFunctionRasterDataset frDset = new FunctionRasterDatasetClass();
     IFunctionRasterDatasetName frDsetName = new FunctionRasterDatasetNameClass();
     frDsetName.FullName = tempAr;
     frDset.FullName = (IName)frDsetName;
     IRasterFunction rsFunc = new ArithmeticFunctionClass();
     rsFunc.PixelType = outRasterType;
     //IArithmeticFunctionArguments2 args = new ArithmeticFunctionArgumentsClass();
     IArithmeticFunctionArguments args = new ArithmeticFunctionArgumentsClass();
     if(isNumeric(inRaster1.ToString())&&isNumeric(inRaster2.ToString()))
     {
         Console.WriteLine("Must have at least one raster");
         return null;
     }
     args.Operation = op;
     object iR1, iR2;
     if (isNumeric(inRaster1.ToString())&&!isNumeric(inRaster2.ToString()))
     {
         iR2 = createIdentityRaster(inRaster2,rstPixelType.PT_FLOAT);
         IScalar sc = new ScalarClass();
         int bCnt = ((IRasterBandCollection)iR2).Count;
         float[] d = new float[bCnt];
         for (int i = 0; i < bCnt; i++)
         {
             d[i] = System.Convert.ToSingle(inRaster1);
         }
         sc.Value = d;
         iR1 = sc;
     }
     else if (isNumeric(inRaster2.ToString()) && !isNumeric(inRaster1.ToString()))
     {
         iR1 = createIdentityRaster(inRaster1, rstPixelType.PT_FLOAT);
         IScalar sc = new ScalarClass();
         int bCnt = ((IRasterBandCollection)iR1).Count;
         float[] d = new float[bCnt];
         for (int i = 0; i < bCnt; i++)
         {
             d[i] = System.Convert.ToSingle(inRaster2);
         }
         sc.Value = d;
         iR2 = sc;
     }
     else
     {
         iR1 = createIdentityRaster(inRaster1, rstPixelType.PT_FLOAT);
         iR2 = createIdentityRaster(inRaster2, rstPixelType.PT_FLOAT);
         IRasterBandCollection rsBc1 = (IRasterBandCollection)iR1;
         IRasterBandCollection rsBc2 = (IRasterBandCollection)iR2;
         int bCnt1,bCnt2;
         bCnt1 = rsBc1.Count;
         bCnt2 = rsBc2.Count;
         if (bCnt1 != rsBc2.Count)
         {
             int dif = bCnt1-bCnt2;
             int absDif = Math.Abs(dif);
             if (dif > 0)
             {
                 IRaster rsB = createRaster(getBand(iR2, 0));
                 IRaster[] rsArr = new IRaster[absDif];
                 for (int i = 0; i < absDif; i++)
                 {
                     rsArr[i] = rsB;
                 }
                 iR2 = compositeBandFunction(rsArr);
             }
             else
             {
                 IRaster rsB = createRaster(getBand(iR1, 0));
                 IRaster[] rsArr = new IRaster[absDif];
                 for (int i = 0; i < absDif; i++)
                 {
                     rsArr[i] = rsB;
                 }
                 iR1 = compositeBandFunction(rsArr);
             }
         }
     }
     args.Raster = iR1;
     args.Raster2 = iR2;
     //args.CellsizeType = esriCellsizeType.esriCellsizeMinOf;
     //args.ExtentType = esriExtentType.esriExtentIntersectionOf;
     frDset.Init(rsFunc, args);
     return frDset;
 }