This content has been marked as final. Show 3 replies
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.
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!
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()