Convert Raster to Raster<int>
A Raster works in two separate capacities. Firstly it works like a factory that enables you to open a file, even if you don’t know what datatype is in the file yet. It also works like a base class that is responsible for providing default
implementations of some code. It even provides the Value property, allowing access to values, so long as the raster is small enough to fit into memory and you don’t mind converting the values to doubles. But what if you want to work directly
with the values, say in their integer form.
Raster encapsulates an internal IRaster, in this case a BgdRaster<int>.
The Raster class is also the base class for our existing raster libraries, though the IRaster interface is used to allow for the chance that someone might not inherit from the same base class. The RasterEM provides extension methods that are valid
for all rasters. The Raster<T> class gives a generic version of the Raster that has more specific data accessors. This can work as an in memory data object, but in most cases, this is the base class that will be used by raster classes that
match a specific file format or underlying data access library (like GDAL).
Since the easiest way to start working with any raster is to simply open a new raster, the question is how can you get access to the strong typed data objects underneath for better performance. In the following code, we don’t yet know what file
accessor we are using, and we really don’t care, as long as there is one. This means that you must have used the Raster.New or Raster.Create methods in order to start up the new raster. Just instantiating a “New” Raster<int>,
for instance would produce a valid data element that could not be used to open or read values from a file yet, since it doesn’t actually have any code for file access by itself. However, it was provided at the Raster<T> level so that you
could write a single block of code, regardless of what file access class was actually used. This allows for open ended inheritance for file access, while still allowing access to the ReadRaster method.
Public Sub OpenIntegerRaster()
Dim r As New Raster()
' this is valid even with no filename. It will launch a dialog.
If r.DataType = GetType(Integer) Then
Dim intRaster As Raster(Of Integer) = r.ToIntRaster()
If intRaster.IsInRam Then
' Access data values on a raster small enough to fit in memory
For row As Integer = 0 To intRaster.NumRows - 1
For col As Integer = 0 To intRaster.NumColumns - 1
Dim value As Integer = intRaster.Data(row)(col)
' Read data values from a huge raster
Dim startX As Integer = 0
Dim startY As Integer = 0
Dim sizeX As Integer = 1000
Dim sizeY As Integer = 1000
Dim data As Integer()() = intRaster.ReadRaster(startX, startY, sizeX, sizeY)
For row As Integer = 0 To 999
For col As Integer = 0 To 999
Dim value As Integer = data(row)(col)