3 Replies Latest reply: Aug 2, 2012 9:21 AM by Neil

    Using Try/Catch to resolve error thrown when reclassifying a non-existent value

    gracecai
      Hi,

      When I try to reclassify a value using IReclassOp.ReclassByRemap and if none of the values/ranges in the reclass table exist in the input raster I get this error:

      ERROR 010067: Error in executing grid expression. Unable to initialize reclass table...

      So instead of checking if a certain value exists I've put the reclass code in a try/catch block to handle the problem. I know this isn't really the ideal way of using try/catch but I can't find any efficient solution to this problem. I'd love to hear your opinion. If anyone knows of a better solution please let me know!

                  
                  try
                  {
                      IReclassOp reclassOp = new RasterReclassOpClass();
                      INumberRemap remapHardZero = new NumberRemapClass();
                      remapHardZero.MapValueToNoData(0.0);
                      inputDataset = reclassOp.ReclassByRemap(inputDataset, remapHardZero as IRemap, true);
                      return inputDataset;
                  }

                  catch (Exception err)
                  {
                      if (!Regex.IsMatch(err.ToString(), "ERROR 010067: Error in executing grid expression. Unable to initialize reclass table"))
                      { MessageBox.Show(err.ToString()); }
                      return null;
                  }
        • Re: Using Try/Catch to resolve error thrown when reclassifying a non-existent value
          Neil
          That is one of the reasons why we have exception handling - to catch expected errors and handle them appropriately.  I don't see anything wrong with what you're doing.  Look at it from another perspective - what if you wanted to reclassify thousands of values?  If you added code to query the raster attribute table to see if the value was present prior to calling the method, then your code would be executing time consuming operations each and every time for the sole purpose of catching a condition that should occur rarely.  To me, that sounds very inefficient.  I would prefer to simply handle the exception on the rare occasions that it is thrown.  On the other hand, if you expect this exception to be thrown regularly, then you may want to consider writing the test to see if the value is present.  The process of throwing and handling an exception takes a fair amount of time, so at some point simply catching the exception becomes less efficient than testing for the value on average.
          • Re: Using Try/Catch to resolve error thrown when reclassifying a non-existent value
            gracecai
            Hi Neil,

            Ideally I would create a check for the value, because I expect this event to occur semi-regularly. But as I'm still new to ArcObjects and C# I'm not sure what is the best way to go about doing this. Getting a cursor and running through the grid seems like an extremely roundabout way of tackling it..

            also I've amended my original post. This only happens when the original raster contains none of the values/ranges in the reclass table. For me this happens when I try to nullify cells based on user inputs. If I try to MapValueToNoData(0.0) and the raster does not contain zeros, then that exception is thrown. So theoretically it wouldn't be less efficient to look for the value (even if I check every cell value, I'd be only running through it once). Currently I'm looking into getting access to the Count/Value table >.< it seems like a simple thing to do but I'm a bit lost!

            Thanks
            • Re: Using Try/Catch to resolve error thrown when reclassifying a non-existent value
              Neil
              The RasterLayer coclass implements IAttributeTable so you can QI directly to it from your layer reference.  Something like this:

              Dim attributeTable As IAttributeTable = DirectCast(rasterLayer, IAttributeTable) ' where rasterLayer is an IRasterLayer reference

              IAttributeTable.AttributeTable will return an ITable reference that you can then query for specific values.  One thing to keep in mind is that not all raster layers will have an attribute table.  The following code should build the attribute table for a raster dataset if it doesn't exist:

                      Dim raster As IRaster = rasterDataset.CreateDefaultRaster        
                      Dim rasterBandCollection As IRasterBandCollection = DirectCast(raster, IRasterBandCollection)
                      Dim rasterBand As IRasterBand = rasterBandCollection.Item(0)
                      rasterBand.ComputeStatsAndHist()
                      DirectCast(rasterDataset, IRasterDatasetEdit2).BuildAttributeTable()