Attribute table of grid data obtained by C + ArcEngine

This is the function of the attribute table of the grid data of or. In my attribute table, there are count and value fields, where value is the elevation value of the grid data, count is the number of times value appears in the image, and the function of this program is to get the maximum count, so as to obtain the corresponding value, that is, the mode of the elevation value of the grid image, as follows:

///

    ///Get property sheet
    /// </summary>
    ///< param name = "mastertifpath" > path of raster < / param >
    private int GetAttributeTable(string maskPath)
    {
       int elevation=0;//Altitude
        //Get raster (see another blog post for grid data)
       IRaster raster=GetRaster(maskPath);
      if (raster != null)
       {
           try
            {
             //Get a grating band
               IRasterBandCollection rasterBandCollection = (IRasterBandCollection)raster;
              int bandCount = rasterBandCollection.Count;//bandCount=1
               for (int j = 0; j < bandCount; j++)
               {
                    IRasterBand rasterBand = rasterBandCollection.Item(j);
                  //Get the attribute table of this raster band and save it to table
                    ITable table = rasterBand.AttributeTable;
                  //Create a query (SQL statement)
                  IQueryFilter queryFilter = new QueryFilterClass();
                   queryFilter.WhereClause = "";
                   //Query the table with queryFilter and save the result to the cursor pointer 
                   ICursor cursorCount = table.Search(queryFilter, false);
                    IRow rowCount = cursorCount.NextRow();
                    //  count field set
                   List<int> countCol = new List<int>();
                   int MaxValue = 0;
                   for (int i = 0; i < table.Fields.FieldCount; i++)//field.count=4
                    {
                      //Determine whether the field name is count
                       if (table.Fields.get_Field(i).Name == "Count")
                       {
                         while (rowCount != null)
                          {
                                //The following shows the value of the count field and gets a set of count values
                              countCol.Add(Convert.ToInt32(rowCount.get_Value(table.FindField("Count"))));
                               rowCount = cursorCount.NextRow();
                           }
                            int count = countCol.Count;
                            //Get the maximum value of field Count
                         MaxValue = countCol[0];
                           for (int w = 1; w < count; w++)
                           {
                              if (countCol[w] > MaxValue)
                               {
                                  MaxValue = countCol[w];
                              }
                           }
                           //Get the value when Count is maximum, that is, the mode of elevation
                          ICursor cursorBinvalues = table.Search(queryFilter, false);
                        IRow rowBinvalues = cursorBinvalues.NextRow();
                         while (rowBinvalues != null)
                          {
                             int value = Convert.ToInt32(rowBinvalues.get_Value(table.FindField("Count")));
                               if (value == MaxValue)
                           {
                                 elevation = Convert.ToInt32(rowBinvalues.get_Value(table.FindField("Value")));
                             }
                            rowBinvalues = cursorBinvalues.NextRow();
                           }
                    }
                       else
                      {
                           //The field name is not count. It is useless. Go to the next step.
                      }
                   }
               }
              return elevation;
         }
       catch (Exception ex)
           {
              Console.WriteLine(ex.Message);
              return 0;
           }
        }
       else
     {
           MessageBox.Show("Failed to extract grid data! ");
            return 0; 
       }

   }

This article refers to http://blog.csdn.net/gisorace/article/details/4297650. The bloggers are very powerful. Thank you.

Reprinted from: https://blog.csdn.net/chhdxzq/article/details/43453569

Keywords: Attribute SQL

Added by anatak on Mon, 28 Oct 2019 17:22:43 +0200