+ Reply to Thread
Results 1 to 4 of 4

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

  1. #1
    Grace Cai
    Join Date
    Jun 2011
    Posts
    15
    Points
    0
    Answers Provided
    0


    0

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

    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!

    Code:
                
                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;
                }
    Last edited by GraceCai; 08-02-2012 at 08:16 AM.

  2. #2
    Neil Clemmons

    Join Date
    Apr 2010
    Posts
    895
    Points
    571
    Answers Provided
    94


    0

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

    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.
    Neil Clemmons
    Geographic Information Services, Inc.
    http://www.gisinc.com

    Check out our blog:
    http://blog.gisinc.com

  3. #3
    Grace Cai
    Join Date
    Jun 2011
    Posts
    15
    Points
    0
    Answers Provided
    0


    0

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

    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
    Last edited by GraceCai; 08-02-2012 at 08:22 AM.

  4. #4
    Neil Clemmons

    Join Date
    Apr 2010
    Posts
    895
    Points
    571
    Answers Provided
    94


    0

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

    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:

    Code:
            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()
    Neil Clemmons
    Geographic Information Services, Inc.
    http://www.gisinc.com

    Check out our blog:
    http://blog.gisinc.com

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts