public void start()
        {
            try
            {
                geoDatabaseUtility geoUtil = new geoDatabaseUtility();
                System.Diagnostics.Process pr = new System.Diagnostics.Process();
                geoUtil.check_dir(rmrsDir);
                string hFl = rmrsDir + "\\" + HelpFileName;
                pr.StartInfo.FileName = hFl;

                if (System.IO.File.Exists(hFl))
                {
                    pr.Start();
                }
                else
                {
                    update up = new update();
                    try
                    {
                        System.Windows.Forms.MessageBox.Show("Can't find help files. Trying to download from the internet.");
                        string cuSet = up.UpdateCheck;
                        if (cuSet.ToLower() != "yes")
                        {
                            up.UpdateCheck = "yes";

                        }
                        Properties.Settings.Default.HelpVersion = "unknown";
                        Properties.Settings.Default.Save();
                        if (up.updateHelp())
                        {
                            pr.Start();
                        }
                        else
                        {
                            System.Windows.Forms.MessageBox.Show("Can't find help files on the internet. Try again later.");
                        }
                        up.UpdateCheck = cuSet;
                    }
                    catch
                    {
                        System.Windows.Forms.MessageBox.Show("Error in updating help. Try again later.");
                    }
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {

            }
        }
 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 calcStandMeans(IFeatureClass strataFtr, IFeatureClass standsFtr, int[] meanStrataFldIndex, int[] varStrataFldIndex, int[] countFldStrataIndex, IField[] fldsToSummarize, geoDatabaseUtility geoUtil)
 {
     int cnt = 0;
     int[] ptFldIndex = new int[fldsToSummarize.Length];
     int[] meanFldIndex = new int[fldsToSummarize.Length];
     int[] varFldIndex = new int[fldsToSummarize.Length];
     int[] cntFldIndex = new int[fldsToSummarize.Length];
     foreach (IField fld in fldsToSummarize)
     {
         string sName = geoUtil.createField(standsFtr, "v_" + fld.Name, esriFieldType.esriFieldTypeDouble, false);
         varFldIndex[cnt] = standsFtr.FindField(sName);
         string mName = geoUtil.createField(standsFtr, "m_" + fld.Name, esriFieldType.esriFieldTypeDouble, false);
         meanFldIndex[cnt] = standsFtr.FindField(mName);
         string cName = geoUtil.createField(standsFtr, "n_" + fld.Name, esriFieldType.esriFieldTypeDouble, false);
         cntFldIndex[cnt] = standsFtr.FindField(cName);
         cnt++;
     }
     IFeatureCursor uCur = standsFtr.Update(null, true);
     IFeature uFtr = uCur.NextFeature();
     while (uFtr != null)
     {
         ESRI.ArcGIS.Geometry.IGeometry geo = uFtr.Shape;
         ISpatialFilter spFlt = new SpatialFilter();
         spFlt.Geometry = geo;
         spFlt.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
         double totalArea = 0;
         IFeatureCursor sCur = strataFtr.Search(spFlt, true);
         IFeature sFtr = sCur.NextFeature();
         double[][] vlArr = new double[meanFldIndex.Length][];
         for (int i = 0; i < meanFldIndex.Length; i++)
         {
             vlArr[i] = new double[3];
         }
         while (sFtr != null)
         {
             ESRI.ArcGIS.Geometry.IGeometry sgeo = sFtr.Shape;
             ESRI.ArcGIS.Geometry.ITopologicalOperator4 topo = (ESRI.ArcGIS.Geometry.ITopologicalOperator4)sgeo;
             ESRI.ArcGIS.Geometry.IGeometry sgeo2 = topo.Intersect(geo, ESRI.ArcGIS.Geometry.esriGeometryDimension.esriGeometry2Dimension);
             double subArea = (((ESRI.ArcGIS.Geometry.IArea)sgeo2).Area);
             totalArea += subArea;
             for (int i = 0; i < meanFldIndex.Length; i++)
             {
                 vlArr[i][0] += System.Convert.ToDouble(sFtr.get_Value(meanStrataFldIndex[i])) * subArea;
                 vlArr[i][1] += System.Convert.ToDouble(sFtr.get_Value(varStrataFldIndex[i])) * subArea;
                 vlArr[i][2] += System.Convert.ToDouble(sFtr.get_Value(countFldStrataIndex[i]));
             }
             sFtr = sCur.NextFeature();
         }
         System.Runtime.InteropServices.Marshal.ReleaseComObject(sCur);
         if (totalArea != 0)
         {
             for (int i = 0; i < meanFldIndex.Length; i++)
             {
                 uFtr.set_Value(meanFldIndex[i], vlArr[i][0]/totalArea);
                 uFtr.set_Value(varFldIndex[i], vlArr[i][1]/totalArea);
                 uFtr.set_Value(cntFldIndex[i], vlArr[i][2]);
             }
             uCur.UpdateFeature(uFtr);
         }
         uFtr = uCur.NextFeature();
     }
     System.Runtime.InteropServices.Marshal.ReleaseComObject(uCur);
 }
        public static void summarizeBiomassPolygon(IFeatureClass pointFtr, IField[] fldsToSummarize, IFeatureClass strataFtr, IFeatureClass standsFtr = null, geoDatabaseUtility geoUtil=null )
        {
            if(geoUtil==null) geoUtil = new geoDatabaseUtility();
            int cnt = 0;
            int[] ptFldIndex = new int[fldsToSummarize.Length];
            int[] meanFldIndex = new int[fldsToSummarize.Length];
            int[] varFldIndex = new int[fldsToSummarize.Length];
            int[] cntFldIndex = new int[fldsToSummarize.Length];
            foreach (IField fld in fldsToSummarize)
            {

                ptFldIndex[cnt] = pointFtr.FindField(fld.Name);
                string sName = geoUtil.createField(strataFtr, "v_" + fld.Name, esriFieldType.esriFieldTypeDouble, false);
                varFldIndex[cnt] = strataFtr.FindField(sName);
                string mName = geoUtil.createField(strataFtr, "m_" + fld.Name, esriFieldType.esriFieldTypeDouble, false);
                meanFldIndex[cnt] = strataFtr.FindField(mName);
                string cntName = geoUtil.createField(strataFtr, "n_" + fld.Name, esriFieldType.esriFieldTypeInteger, false);
                cntFldIndex[cnt] = strataFtr.FindField(cntName);
                cnt++;
            }
            IFeatureCursor uCur = strataFtr.Update(null, true);
            IFeature uFtr = uCur.NextFeature();
            while (uFtr != null)
            {
                ISpatialFilter sFilt = new SpatialFilter();
                sFilt.Geometry = uFtr.Shape;
                sFilt.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                double[][] fldVlsArr = new double[fldsToSummarize.Length][];
                for (int i = 0; i < fldsToSummarize.Length; i++)
                {
                    fldVlsArr[i] = new double[3];
                }
                IFeatureCursor psCur = pointFtr.Search(sFilt, true);
                IFeature psFtr = psCur.NextFeature();

                while (psFtr != null)
                {
                    for (int i = 0; i < ptFldIndex.Length; i++)
                    {
                        int indexVl = ptFldIndex[i];
                        object objVl = psFtr.get_Value(indexVl);
                        if (objVl != null)
                        {
                            double vl = System.Convert.ToDouble(objVl);
                            double vl2 = vl*vl;
                            fldVlsArr[i][0] += vl;
                            fldVlsArr[i][1] += vl2;
                            fldVlsArr[i][2] += 1;
                        }
                    }
                    psFtr = psCur.NextFeature();
                }

                System.Runtime.InteropServices.Marshal.ReleaseComObject(psCur);
                for (int i = 0; i < ptFldIndex.Length; i++)
                {
                    double s = fldVlsArr[i][0];
                    double s2 = fldVlsArr[i][1];
                    double n = fldVlsArr[i][2];
                    double mean = s / n;//mean
                    double var = (s2 - (Math.Pow(s, 2) / n)) / (n - 1);//variance
                    int mIndex = meanFldIndex[i];
                    int vIndex = varFldIndex[i];
                    int cntIndex = cntFldIndex[i];
                    uFtr.set_Value(mIndex, mean);
                    uFtr.set_Value(vIndex, var);
                    uFtr.set_Value(cntIndex, n);
                }
                uCur.UpdateFeature(uFtr);
                uFtr = uCur.NextFeature();
            }
            System.Runtime.InteropServices.Marshal.ReleaseComObject(uCur);
            if (standsFtr != null)
            {
                calcStandMeans(strataFtr, standsFtr, meanFldIndex, varFldIndex, cntFldIndex, fldsToSummarize,geoUtil);
            }
        }
        public static void summarizeBiomassPolygon(IFeatureClass pointFtr, IField[] fldsToSummarize, IFunctionRasterDataset strataRaster, IFeatureClass standsFtr, geoDatabaseUtility geoUtil = null, rasterUtil rsUtil = null)
        {
            if (geoUtil == null) geoUtil = new geoDatabaseUtility();
            if(rsUtil == null) rsUtil = new rasterUtil();
            int cnt = 0;
            //need to work on calculating N
            Dictionary<string,double[][]> vlDic = getDictionaryValues(pointFtr, fldsToSummarize, strataRaster, geoUtil, rsUtil); //Strata: SummaryFields [{sum,sum2,cnt},...]
            int[] meanFldIndex = new int[fldsToSummarize.Length];
            int[] varFldIndex = new int[fldsToSummarize.Length];
            int[] cntFldIndex = new int[fldsToSummarize.Length];
            //string cntName = geoUtil.createField(standsFtr, "n", esriFieldType.esriFieldTypeInteger, false);
            //int cntIndex = standsFtr.FindField(cntName);
            foreach (IField fld in fldsToSummarize)
            {
                string sName = geoUtil.createField(standsFtr, "v_" + fld.Name, esriFieldType.esriFieldTypeDouble, false);
                varFldIndex[cnt] = standsFtr.FindField(sName);
                string mName = geoUtil.createField(standsFtr, "m_" + fld.Name, esriFieldType.esriFieldTypeDouble, false);
                meanFldIndex[cnt] = standsFtr.FindField(mName);
                string cntName = geoUtil.createField(standsFtr, "n_" + fld.Name, esriFieldType.esriFieldTypeDouble, false);
                cntFldIndex[cnt] = standsFtr.FindField(cntName);
                cnt++;
            }
            IFeatureCursor uCur = standsFtr.Update(null, true);
            IFeature uFtr = uCur.NextFeature();
            while (uFtr != null)
            {
                ESRI.ArcGIS.Geometry.IGeometry geo = uFtr.Shape;
                IFunctionRasterDataset cRs = rsUtil.clipRasterFunction(strataRaster, geo, esriRasterClippingType.esriRasterClippingOutside);
                //Console.WriteLine("Clipping raster");
                Dictionary<string, double> rsStrataPropDic = getStrataProportion(cRs,rsUtil); //Strata: proportion of area
                //int tn = 0;
                //double[] tn = new double[meanFldIndex.Length];
                double[][] updateValuesArr = new double[meanFldIndex.Length][];
                for (int i = 0; i < meanFldIndex.Length; i++)
                {
                    updateValuesArr[i] = new double[3];
                }
                foreach (KeyValuePair<string, double> kvp in rsStrataPropDic)
                {
                    string stratum = kvp.Key;
                    double proportion = kvp.Value;
                    //Console.WriteLine(stratum + " = " + proportion.ToString());
                    double[][] vlDicArr;
                    if (vlDic.TryGetValue(stratum, out vlDicArr))
                    {
                        //double n = vlDicArr[0][2];
                        //tn += System.Convert.ToInt32(n);
                        for (int i = 0; i < meanFldIndex.Length; i++)
                        {
                            double[] dArr = vlDicArr[i];
                            double n = dArr[2];
                            //tn[i] += n;
                            double s=dArr[0];
                            double s2=dArr[1];
                            updateValuesArr[i][0] += (s/n) * proportion;//mean
                            updateValuesArr[i][1] += (s2-Math.Pow(s,2)/n)/(n-1) * proportion;//variance
                            updateValuesArr[i][2] += n;
                        }

                    }
                }
                //uFtr.set_Value(cntIndex, tn);
                for (int i = 0; i < meanFldIndex.Length; i++)
                {
                    uFtr.set_Value(meanFldIndex[i], updateValuesArr[i][0]);
                    uFtr.set_Value(varFldIndex[i], updateValuesArr[i][1]);
                    uFtr.set_Value(cntFldIndex[i], updateValuesArr[i][2]);
                }
                uCur.UpdateFeature(uFtr);
                uFtr = uCur.NextFeature();
            }
            System.Runtime.InteropServices.Marshal.ReleaseComObject(uCur);
        }
 /// <summary>
 /// adds a layer to the active view
 /// </summary>
 /// <param name="path">full path name</param>
 /// <returns>Ilayer</returns>
 public ILayer addLayer(string path)
 {
     IMap map = (IMap)acView;
     geoDatabaseUtility geoUtil = new geoDatabaseUtility();
     esriDatasetType dType = geoUtil.getDataType(path);
     ILayer lyr = null;
     switch(dType)
     {
         case esriDatasetType.esriDTFeatureClass:
             IFeatureLayer ftrLayer = new FeatureLayerClass();
             ftrLayer.FeatureClass = geoUtil.getFeatureClass(path);
             lyr = (ILayer)ftrLayer;
             lyr.Name = ftrLayer.FeatureClass.AliasName;
             map.AddLayer(lyr);
             break;
         case esriDatasetType.esriDTRasterBand:
         case esriDatasetType.esriDTRasterCatalog:
         case esriDatasetType.esriDTRasterDataset:
             IRasterLayer rasterLayer = new RasterLayerClass();
             rasterLayer.CreateFromDataset(geoUtil.getRasterDataset(path));
             rasterLayer.Name = rasterLayer.Name;
             map.AddLayer((ILayer)rasterLayer);
             break;
         case esriDatasetType.esriDTTable:
             ITable tbl = geoUtil.getTable(path);
             ITableCollection tableCollection = (ITableCollection)map;
             tableCollection.AddTable(tbl);
             break;
         default:
             break;
     }
     acView.Refresh();
     return lyr;
 }
        static void Main(string[] args)
        {
            //ESRI License Initializer generated code.
            m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeAdvanced}, new esriLicenseExtensionCode[] {});
            System.DateTime dt = System.DateTime.Now;

            System.DateTime dt2;
            TimeSpan ts;
            rasterUtil rsUtil = new rasterUtil();
            geoDatabaseUtility geoUtil = new geoDatabaseUtility();
            featureUtil ftrUtil = new featureUtil();
            string zones = @"C:\Users\jshogland\Documents\JOHN\Requests\TaceyFrescino\forFIESTA\test.gdb\utcobnd";
            string values = @"C:\Users\jshogland\Documents\JOHN\Requests\TaceyFrescino\forFIESTA\test.gdb\AdminBoundary";
            string[] flds = { "m_BAA_all_1" };
            IFeatureClass zonesFtr = geoUtil.getFeatureClass(zones);
            IFeatureClass valuesFtr = geoUtil.getFeatureClass(values);
            ftrUtil.weightFieldValuesByAreaLength(valuesFtr, flds, zonesFtr);

            //string ftpSite = @"ftp://rockyftp.cr.usgs.gov/vdelivery/Datasets/Staged/NAIP/mt_2013/";
            //List<string> fNames = getFileNames(ftpSite);
            //string outDir = @"E:\Helena\NAIP\Bulk Order 421750\NAIP JPG2000";
            //List<string> exNames = getExistingNames(outDir);
            //List<string> tiles = getTiles();
            //for (int i = 0; i < fNames.Count; i++)
            //{
            //    string fName = fNames[i];
            //    string lfn = fName.Substring(0, 7);
            //    if (tiles.Contains(lfn.ToLower()))
            //    {
            //        if (!exNames.Contains(fName))
            //        {
            //            Console.WriteLine("Total number of files = " + fNames.Count.ToString());
            //            Console.WriteLine("Downloading " + fName);
            //            bool gotFile = Download(ftpSite, outDir, fName);
            //            Console.WriteLine("Got File " + fName + " " + gotFile.ToString());
            //        }
            //    }
            //}
            dt2 = System.DateTime.Now;
            ts = dt2.Subtract(dt);
            Console.WriteLine("Total Seconds RawBlock= " + ts.TotalSeconds.ToString());

            m_AOLicenseInitializer.ShutdownApplication();
        }