Skip to content

ToIntRasterCS

sindizzy edited this page Dec 30, 2020 · 1 revision

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>.

RasterEncapsulation 

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). 

RasterInheritance

 

 

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 void OpenIntegerRaster()
        {
            Raster r = new Raster();
            r.Open(); // this is valid even with no filename.  It will launch a dialog.
            if(r.DataType == typeof(int))
            {
                Raster<int> intRaster = r.ToIntRaster();
                if(intRaster.IsInRam)
                {
                    // Access data values on a raster small enough to fit in memory
                    for (int row = 0; row < intRaster.NumRows; row++)
                    {
                        for (int col = 0; col < intRaster.NumColumns; col++)
                        {
                            int value = intRaster.Data[row][col];
                        }
                    }
                }
            <span class=rem>// Read data values from a huge raster</span>
            <span class=kwrd>int</span> startX = 0;
            <span class=kwrd>int</span> startY = 0;
            <span class=kwrd>int</span> sizeX = 1000;
            <span class=kwrd>int</span> sizeY = 1000;
            <span class=kwrd>int</span>[][] data = intRaster.ReadRaster(startX, startY, sizeX, sizeY);
            <span class=kwrd>for</span>(<span class=kwrd>int</span> row = 0; row &lt; 1000; row++)
            {
                <span class=kwrd>for</span>(<span class=kwrd>int</span> col = 0; col &lt; 1000; col++)
                {
                    <span class=kwrd>int</span> <span class=kwrd>value</span> = data[row][col];
                }
            }

        }
    }</pre></div><div class="ClearBoth"></div>
Clone this wiki locally