private static Dictionary<string, double> getStrataProportion(IFunctionRasterDataset strataRaster,rasterUtil rsUtil)
        {
            IRaster2 rs2 = (IRaster2)rsUtil.createRaster(strataRaster);
            Dictionary<string, double> outDic = new Dictionary<string, double>();
            IRasterCursor rsCur = rs2.CreateCursorEx(null);
            //Console.WriteLine(((IRasterProps)rs2).Height.ToString() + ((IRasterProps)rs2).Height.ToString());
            int n = 0;
            do
            {
                IPixelBlock pb = rsCur.PixelBlock;
                //Console.WriteLine("PixelBLock w_h = " + pb.Width.ToString() + "_" + pb.Height.ToString());
                for (int r = 0; r < pb.Height; r++)
                {
                    for (int c = 0; c < pb.Width; c++)
                    {
                        object vlObj = pb.GetVal(0, c, r);
                        if (vlObj != null)
                        {
                            string vl = vlObj.ToString();
                            double vlCnt;
                            if (outDic.TryGetValue(vl, out vlCnt))
                            {
                                outDic[vl] = vlCnt + 1;
                            }
                            else
                            {
                                outDic.Add(vl, 1);
                            }
                            n += 1;
                        }
                        else
                        {
                            //Console.WriteLine("VL Null");
                        }

                    }
                }
            } while (rsCur.Next() == true);
            //Console.WriteLine("OutDic Count = " + outDic.Count.ToString());
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rsCur);
            foreach (string s in outDic.Keys.ToArray())
            {
                double vl = outDic[s];
                outDic[s] = vl / n;
            }
            return outDic;
        }
 private static Dictionary<string, double[][]> getDictionaryValues(ESRI.ArcGIS.Geodatabase.IFeatureClass pointFtr, ESRI.ArcGIS.Geodatabase.IField[] fldsToSummarize, IFunctionRasterDataset strataRaster, geoDatabaseUtility geoUtil, rasterUtil rsUtil)
 {
     IRaster2 rs2 = (IRaster2)rsUtil.createRaster(strataRaster);
     int[] ptfldIndex = new int[fldsToSummarize.Length];
     for (int i = 0; i < ptfldIndex.Length; i++)
     {
         ptfldIndex[i] = pointFtr.FindField(fldsToSummarize[i].Name);
     }
     Dictionary<string, double[][]> outDic = new Dictionary<string, double[][]>();
     IFeatureCursor sCur = pointFtr.Search(null, true);
     IFeature sFtr = sCur.NextFeature();
     while (sFtr != null)
     {
         IGeometry geo = sFtr.Shape;
         IPoint pnt = (IPoint)geo;
         int clm, rw;
         rs2.MapToPixel(pnt.X, pnt.Y,out clm, out rw);
         object strataVlObj = rs2.GetPixelValue(0, clm, rw);
         if(strataVlObj!=null)
         {
             string strataVl = strataVlObj.ToString();
             double[][] vlArr;
             if (outDic.TryGetValue(strataVl, out vlArr))
             {
                 for (int i = 0; i < ptfldIndex.Length; i++)
                 {
                     object vlObj = sFtr.get_Value(ptfldIndex[i]);
                     if (vlObj != null)
                     {
                         double vl = System.Convert.ToDouble(vlObj);
                         vlArr[i][0] += vl;
                         vlArr[i][1] += (vl * vl);
                         vlArr[i][2] += 1;
                     }
                 }
             }
             else
             {
                 vlArr = new double[fldsToSummarize.Length][];
                 for (int i = 0; i < ptfldIndex.Length; i++)
                 {
                     double[] vlSumArr = new double[3];
                     object vlObj =sFtr.get_Value(ptfldIndex[i]);
                     if (vlObj != null)
                     {
                         double vl =  System.Convert.ToDouble(vlObj);
                         vlSumArr[0] = vl;
                         vlSumArr[1] = (vl * vl);
                         vlSumArr[2] = 1;
                     }
                     vlArr[i] = vlSumArr;
                 }
                 outDic[strataVl] = vlArr;
             }
         }
         sFtr = sCur.NextFeature();
     }
     System.Runtime.InteropServices.Marshal.ReleaseComObject(sCur);
     return outDic;
 }
 private static void runRawBlock(IFunctionRasterDataset fDset, IPnt pSize, IPnt pLoc, rasterUtil rsUtil)
 {
     IRaster2 rs2 = (IRaster2)rsUtil.createRaster(fDset);
     for (int b = 0; b < 1; b++)
     {
         object vl = rs2.GetPixelValue(b, 100, 100);
     }
     //IRawBlocks rPb = (IRawBlocks)fDset;
     //IPixelBlock pb = rPb.CreatePixelBlock();
     //int bEndc = (int)Math.Ceiling(pSize.X / pb.Width);
     //int bEndr = (int)Math.Ceiling(pSize.Y / pb.Height);
     //for (int rb = 0; rb < bEndr; rb += (int)pSize.Y)
     //{
     //    for (int cb = 0; cb < bEndc; cb += (int)pSize.X)
     //    {
     //        for (int b = 0; b < rPb.RasterInfo.BandCount; b++)
     //        {
     //            Console.WriteLine("Band " + b.ToString());
     //            rPb.ReadBlock(100, 100, b, pb);
     //            for (int r = 0; r < pb.Height; r++)
     //            {
     //                for (int c = 0; c < pb.Width; c++)
     //                {
     //                    object vl = pb.GetVal(0, c, r);
     //                }
     //            }
     //        }
     //    }
     //}
 }
        public string fillDbRaster(IImageServerLayer imSvLyr, IWorkspace wks,ESRI.ArcGIS.Geometry.IEnvelope ext,ISpatialReference sr, out IRaster outrs)
        {
            if (ext.SpatialReference.FactoryCode != sr.FactoryCode)
            {
                ext.Project(sr);
            }
            StringBuilder msg = new StringBuilder();
            if (wks == null)
            {
                wks = servWks;
            }
            outrs = null;
            Forms.RunningProcess.frmRunningProcessDialog rp = new Forms.RunningProcess.frmRunningProcessDialog(false);
            rp.addMessage("Downloading images please be patient...");
            rp.Show();
            //rp.showInSepperateProcess();
            rp.TopMost = true;
            rp.stepPGBar(10);
            rp.Refresh();
            DateTime dtS = DateTime.Now;
            try
            {
                rasterUtil rsUtil = new rasterUtil();
                int minX = System.Convert.ToInt32(ext.XMin);
                int maxX = System.Convert.ToInt32(ext.XMax);
                int minY = System.Convert.ToInt32(ext.YMin);
                int maxY = System.Convert.ToInt32(ext.YMax);
                int xDiff = System.Convert.ToInt32(ext.Width);
                int yDiff = System.Convert.ToInt32(ext.Height);
                int tile = 1;
                IRaster rast = rsUtil.createRaster(((IRaster2)imSvLyr.Raster).RasterDataset);
                ISaveAs saveas = (ISaveAs)rast;
                IRasterProps rasterProps = (IRasterProps)rast;
                imSvLyr.SpatialReference = sr;
                string nm = "T" + System.Guid.NewGuid().ToString().Substring(0, 3);
                string svImgNm = imSvLyr.ServiceInfo.Name;
                string rNm = nm;
                int mCols = System.Convert.ToInt32(imSvLyr.ServiceInfo.MaxNCols * .90);
                int mRows = System.Convert.ToInt32(imSvLyr.ServiceInfo.MaxNRows * .90);
                if (xDiff < mCols) mCols = xDiff;
                if (yDiff < mRows) mRows = yDiff;
                List<IRaster> tileLst = new List<IRaster>();
                for (int i = minX; i < maxX; i += mRows)
                {
                    for (int j = minY; j < maxY; j += mCols)
                    {

                        IEnvelope clipEnvelope = new EnvelopeClass();
                        clipEnvelope.PutCoords(i, j, i + mRows, j + mCols);
                        rasterProps.Extent = clipEnvelope;
                        rasterProps.Width = mRows;
                        rasterProps.Height = mCols;
                        rasterProps.SpatialReference = sr;
                        string r = rNm + tile.ToString();
                        if (r.Length > 12)
                        {
                            rp.addMessage("Too many tiles. Ending at Tile: " + tile);
                            msg.AppendLine("Too many tiles. Ending at Tile: " + tile);
                            //outrs = rsUtil.mosaicRastersFunction(wks, rNm, tileLst.ToArray(),esriMosaicMethod.esriMosaicNone,rstMosaicOperatorType.MT_FIRST,true,true,false,true);
                            return msg.ToString();
                        }
                        if (((IWorkspace2)wks).get_NameExists(esriDatasetType.esriDTRasterDataset, r))
                        {
                            r = rsUtil.getSafeOutputName(wks, r);
                            //Console.WriteLine("Deleting Raster " + r);
                            //((IRasterWorkspaceEx)wks).DeleteRasterDataset(r);
                        }
                        rp.addMessage("Creating tile " + r);
                        rp.stepPGBar(5);
                        rp.Refresh();
                        //Console.WriteLine("TestLength  = " + testLng.ToString());
                        tileLst.Add(rsUtil.returnRaster((IRasterDataset)saveas.SaveAs(r, wks, "GDB")));
                        msg.AppendLine("Added Tile " + r);
                        tile++;
                    }
                }
                rp.addMessage("Merging rasters...");
                rp.Refresh();
                //outrs = rsUtil.mosaicRastersFunction(wks, rNm, tileLst.ToArray(), esriMosaicMethod.esriMosaicNone, rstMosaicOperatorType.MT_FIRST, true, true, false, true);
            }
            catch (Exception e)
            {
                string x = e.ToString();
                msg.AppendLine(x);
                Console.WriteLine("Error: " + x);
            }
            finally
            {
                DateTime dtE = DateTime.Now;
                TimeSpan ts = dtE.Subtract(dtS);
                msg.AppendLine("Finished process in " + ts.TotalMinutes + " minutes.");
                rp.addMessage("Finished process in " + ts.TotalMinutes + " minutes.");
                rp.stepPGBar(100);
                rp.enableClose();
                rp.TopMost = false;
                rp.Close();
            }
            return msg.ToString();
        }
        private static void runRasterPb(IFunctionRasterDataset fDset, IPnt pSize, IPnt pLoc, rasterUtil rsUtil)
        {
            IRaster rs = rsUtil.createRaster(fDset);
            IPixelBlock pb = rs.CreatePixelBlock(pSize);
            rs.Read(pLoc, pb);
            for (int b = 0; b < 1; b++)
            {
                Console.WriteLine("PixelBlock type = " + pb.PixelType[b].ToString());
                Console.WriteLine("fDset type = " + fDset.RasterInfo.PixelType.ToString());
                for (int r = 0; r < pb.Height; r++)
                {
                    for (int c = 0; c < pb.Width; c++)
                    {
                        object vl = pb.GetVal(b, c, r);
                        //if (vl == null) Console.WriteLine("VL = null for r,c " + r.ToString() + ", " + c.ToString());
                        //else Console.WriteLine("VL = " + vl.ToString());
                    }
                }

            }
        }