0%

Converting C# Data Value to ArcObject Raster Pixel Value

The length of remote sensing pixel value is constrained by a predefined pixel value type. It is an unchangeable property for each remote sensing image and saving an unsupported value to a image may cause unpredictable errors like application crash or information loss resulted from implicit data type conversion.

If you are developing application based on ArcObject, you could know the pixel type of a raster layer simply with following code.

1
2
3
4
5
IRasterLayer rasterLayer = (IRasterLayer)layer;
IRasterProps rasterProps = (IRasterProps)rasterLayer.Raster;

// Get the pixel type of you raster layer
rstPixelType pixelType = rasterProps.PixelType;

You can find a full list and detailed explanation of ArcObject raster pixel types in this page.

Not every raster pixel types has a corresponding C# value type. So I make a list for those pixels types having relative in C# so that we can make correct conversion while developing.

ArcMap Pixel TypeDescriptionCorresponding C# Data TypeSupported
PT_UNKNOWNunknownNo
PT_U11 bitNo
PT_U22 bitNo
PT_U44 bitNo
PT_UCHARunsigned 8 bit integerByteYes
PT_CHAR8 bit integerSByteYes
PT_USHORTunsigned 16 bit integerUInt16Yes
PT_SHORT16 bit integerInt16Yes
PT_ULONGunsigned 32 bit integerUInt32Yes
PT_LONG32 bit integerInt32Yes
PT_FLOATsingle precision floating pointSingleYes
PT_DOUBLEdouble precision floating pointDoubleYes
PT_COMPLEXsingle precision complexNo
PT_DCOMPLEXdouble precision complexNo
PT_CSHORTshort integer complexNo
PT_CLONGlong integer complexNo

Based on this table, I write a function to wrap the conversion from C# data type to valid ArcObject pixel value type. I use it in my project ArcMap Raster Edit Suite and it work pretty well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/// <summary>
/// Convert the csharp value to the ArcObject pixel value.
/// </summary>
/// <param name="csharpValue">Cshapr value</param>
/// <param name="pixelValueType">The pixel type of ouput value</param>
/// <param name="pixelValue">Output pixel value</param>
/// <returns>A value indicating whether the convention is successful</returns>
public static bool CSharpValue2PixelValue(object csharpValue, rstPixelType pixelValueType, out object pixelValue)
{
try
{
switch (pixelValueType)
{
case rstPixelType.PT_UCHAR:
pixelValue = (object)Convert.ToByte(csharpValue);
return true;
case rstPixelType.PT_CHAR:
pixelValue = (object)Convert.ToSByte(csharpValue);
return true;
case rstPixelType.PT_SHORT:
pixelValue = (object)Convert.ToInt16(csharpValue);
return true;
case rstPixelType.PT_USHORT:
pixelValue = (object)Convert.ToUInt16(csharpValue);
return true;
case rstPixelType.PT_CLONG:
pixelValue = (object)Convert.ToInt32(csharpValue);
return true;
case rstPixelType.PT_ULONG:
pixelValue = (object)Convert.ToUInt32(csharpValue);
return true;
case rstPixelType.PT_FLOAT:
pixelValue = (object)Convert.ToSingle(csharpValue);
return true;
case rstPixelType.PT_DOUBLE:
pixelValue = (object)Convert.ToDouble(csharpValue);
return true;
default:
pixelValue = null;
return false;
}
}
catch (Exception)
{
pixelValue = null;
return false;
}
}