|
bool | AssignNewProjection (string Projection) |
| Assign new projection to the grid. More...
|
|
void | CellToProj (int Column, int Row, out double x, out double y) |
| Uses a cell's column and row position to find the center of the cell in projected map coordinates. More...
|
|
bool | Clear (object ClearValue) |
| Clears all data in the grid, setting the value of all cells in the grid to the specified clear value. More...
|
|
bool | Close () |
| Closes the grid. More...
|
|
Image | CreateImageProxy (GridColorScheme ColorScheme) |
| Creates a proxy image file to display grid data. More...
|
|
bool | CreateNew (string Filename, GridHeader Header, GridDataType DataType, object InitialValue, bool InRam, GridFileType fileType, ICallback cBack) |
| Creates a new grid. More...
|
|
GridColorScheme | GenerateColorScheme (tkGridSchemeGeneration Method, PredefinedColorScheme Colors) |
| Generates colour scheme for the grid. More...
|
|
GdalRasterBand | get_Band (int bandIndex) |
| Gets datasource band with specified index. More...
|
|
string | get_ErrorMsg (int ErrorCode) |
| Retrieves the error message associated with the specified error code. More...
|
|
object | get_Value (int Column, int Row) |
| Returns the value stored in the grid at the specified cell. More...
|
|
bool | GetFloatWindow (int StartRow, int EndRow, int StartCol, int EndCol, ref float Vals) |
| Returns an array with grid values which lie within specified bounds. More...
|
|
bool | GetFloatWindow2 (int StartRow, int EndRow, int StartCol, int EndCol, ref double Vals) |
| Returns an array with grid values which lie within specified bounds. Double overload of Grid.GetFloatWindow method. More...
|
|
bool | GetRow (int Row, ref float Vals) |
| The faster way to read the array values that are of a specific size. More...
|
|
bool | GetRow2 (int Row, ref double Vals) |
| Provides the faster way to read the array values that are of a specific size. Double overload of Grid.GetRow. More...
|
|
bool | Open (string Filename, GridDataType DataType, bool InRam, GridFileType fileType, ICallback cBack) |
| Opens a grid. More...
|
|
Image | OpenAsImage (GridColorScheme Scheme, tkGridProxyMode proxyMode=tkGridProxyMode.gpmAuto, ICallback cBack=null) |
| Opens grid as image. More...
|
|
bool | OpenBand (int bandIndex) |
| Makes the specified band of the grid active. More...
|
|
void | ProjToCell (double x, double y, out int Column, out int Row) |
| Converts a point in projected map coordinates to a cell (column, row) in the grid. More...
|
|
bool | PutFloatWindow (int StartRow, int EndRow, int StartCol, int EndCol, ref float Vals) |
| Sets an array of values for a given region of the grid. More...
|
|
bool | PutFloatWindow2 (int StartRow, int EndRow, int StartCol, int EndCol, ref double Vals) |
| Sets an array of values for a given region of the grid. Double overload of Grid.PutFloatWindow. More...
|
|
bool | PutRow (int Row, ref float Vals) |
| Provides The faster way to write the array values that are of a specific size. More...
|
|
bool | PutRow2 (int Row, ref double Vals) |
| Provides The faster way to write the array values that are of a specific size. Double overload of Grid.PutRow. More...
|
|
bool | RemoveImageProxy () |
| Removes proxy image file created for grid display. More...
|
|
bool | Resource (string newSrcPath) |
| Changes the grid source without closing it. More...
|
|
GridColorScheme | RetrieveColorScheme (tkGridSchemeRetrieval Method) |
| Retrieves colour scheme from the grid. More...
|
|
GridColorScheme | RetrieveOrGenerateColorScheme (tkGridSchemeRetrieval retrievalMethod, tkGridSchemeGeneration generateMethod, PredefinedColorScheme Colors) |
| Tries to retrieve any existing color scheme associated with grid or generates a new one if none is found. More...
|
|
bool | Save (string Filename, GridFileType GridFileType, ICallback cBack) |
| Saves the grid. More...
|
|
void | set_Value (int Column, int Row, object pVal) |
| Sets the value of the specified cell of the grid. More...
|
|
bool | SetInvalidValuesToNodata (double MinThresholdValue, double MaxThresholdValue) |
| Sets invalid values to no data values. More...
|
|
The grid object is used to represent a grid which can be added to the map.
Grid datasource holds values describing distribution of certain characteristic over territory (e.g. elevation, population, precipitation, etc.), but usually doesn't define how these values should be rendered. In most cases grid datasource have a single band, but there may be mutiband ones as well (NetCDF format for example).
To render a grid, a color scheme must be chosen which will define how grid values will be mapped to colors. Some datasources may hold such color scheme (for examples GTiff with indexed colors). In many other cases the choice of color scheme is arbitrary. Here is an example of the same grid rendered with 2 different color schemes (SummerMountains and DesertFires).
MapWinGIS doesn't render Grid class directly, but uses Image class to do it. The distinction between these two is:
- Grid class provides access to underlying values of a stored characteristic (e.g. population, precipitation, etc.);
- Image class holds colors for each of those values mapped with specific color scheme.
By default MapWinGIS renders grid with hillshading effect which can be switched on/off and adjusted in GridColorScheme class.
Metadata about grid can be accessed through GridHeader object (Grid.Header property). It includes size, coordinate system and projection, etc.
- Attention
- Grid.Header property must not be called multiple times in a loop as it can trigger reading the data from disk on each occasion.
A. TYPES OF GRID RENDERING.
MapWinGIS can render grid datasource in 2 different ways:
- Direct rendering by Image class.
- grid datasource is opened by GDAL drivers;
- before each rendering values of grids are mapped into colors + additional hillshading effect may be applied;
- the rendering process is much more computationally intensive than rendering of regular RGB images, therefore an approach provide poor performance for large datasets.
Grid rendering may also by applied to a single band of regular RGB image, displaying it with synthetic colors. See more information on direct grid rendering in description of Image class.
- Note
- Internally grids can be handled by either GDAL drivers or MapWinGIS own drivers. There is no way to affect which one will be chosen, however it's possible to check which one is used by Grid.SourceType property. The significance of it is that grid formats not supported by GDAL can't rendered directly by Image class.
- Creation of so called proxy image to render a grid.
- all values of the grid are mapped to colors and the output is saved as a new image file with BMP or GTiff format;
- the selection of image format depends upon GlobalSettings.GridProxyFormat property;
- conversion is made by Utils.GridToImage2 method; the operation may take significant amount of time;
- Grid.OpenAsImage, Grid.CreateImageProxy, FileManager.Open can create proxy images by running Utils.GridToImage2 method internally;
- a proxy image is written with the same name as for the source grid + "_proxy.bmp" or "_proxy.tif" postfix, for example a proxy for "grid.asc" will be named "grid_proxy.tif";
- a color scheme always is written to the disk along with image (xml file with .mwleg extension, for example "grid_proxy.bmp.mwleg");
For multiband datasources proxy image will be created for the band marked as active by Grid.ActiveBandIndex. Use Grid.OpenBand to change the index.
There is no visual differences between 2 methods of grid rendering, however performance may differ substantially.
B. COLOR SCHEME FOR GRID.
Color scheme for grids can be retrieved in the following ways:
- from .mwleg XML file previously saved to the disk;
- from datasource itself, for example color table in indexed GTiff datasource (Grid.RasterColorTableColoringScheme);
- can be automatically generated using PredefinedColorScheme and values of grid (Grid.GenerateColorScheme);
- manual generation (in case some very specific coloring is required).
The first 2 approaches are covered by Grid.RetrieveColorScheme. Grid.RetrieveOrGenerateColorScheme method will look for any existing color scheme first, and if nothing is available will resort to generation. This is recommended approach in the most scenarios.
To generate color scheme manually the following code can be used:
var grid = some_code_to_open_grid;
PredefinedColorScheme
Predefined color schemes which can be used for grid visualization of for initialization of instance o...
Definition: Enumerations.cs:132
ColoringType
Defines the type of coloring for grids.
Definition: Enumerations.cs:24
A grid color scheme defines how a grid will be colored.
Definition: GridColorScheme.cs:21
See PredefinedColorScheme enumerations for available colors. Hillshading effect can be turned on/off with GridColorScheme.ApplyColoringType or by changing coloring of individual breaks (GridColorBreak.ColoringType).
C. HOW TO ADD GRID TO THE MAP.
Depending on requirements it can be done in fully automatic way with a single line or code (which is the best thing to do in most cases), or with manual coding if default behaviour isn't suitable. The approaches can be classified as:
- Attention
- The samples below assume that the following variables are defined:
string filename = @"d:\grid.asc";
A utils object provides access to a set of utility functions to perform a variety of tasks on other o...
Definition: Utils.cs:20
1. Using FileManager class.
The most generic one:
tkFileOpenStrategy
Possible open strategies for datasources.
Definition: Enumerations.cs:1799
With creation of proxy image:
int handle = axMap1.AddLayerFromFilename(filename,
tkFileOpenStrategy.fosProxyForGrid,
true);
Opening as direct grid. Along with usual reasons (invalid filename, corrupted file), the approach may fail in case:
- size of grid exceeds GlobalSettings.MaxNoProxyGridSizeMb property (this may be addressed by changing global settings like demonstrated);
- grid format isn't supported by GDAL (nothing can be done about that).
gs.MaxNoProxyGridSizeMb = 100.0;
Holds global settings for MapWinGIS. Allows to retrieve GDAL errors.
Definition: GlobalSettings.cs:29
With a check that something was actually opened:
int handle = axMap1.AddLayerFromFilename(filename,
tkFileOpenStrategy.fosAutoDetect,
true);
if (handle != -1 && axMap1.FileManager.LastOpenIsSuccess)
{
MessageBox.Show("Grid was added to the map. Open strategy: " + axMap1.FileManager.LastOpenStrategy.ToString());
}
else
{
}
string ErrorMsgFromObject(object comClass)
Displays error message for the last error that took place within object passed as parameter.
Definition: Utils.cs:631
Using custom instance of FileManager:
{
if (img == null)
{
MessageBox.Show(
"Failed to open grid: " + ut.
ErrorMsgFromObject(fm) +
"; Open strategy: " + fm.LastOpenStrategy);
}
else
{
int handle = axMap1.AddLayer(img, true);
}
}
Provides functionality for opening and examining different types of datasources.
Definition: FileManager.cs:158
2. Passing Grid to AxMap.AddLayer method.
The following logic will be executed:
The selection of rendering mode depends on:
See details in description of tkGridProxyMode enumeration.
The selection of proxy format depends on GlobalSettings.GridProxyFormat property.
A color scheme used for rendering will be written to disk with:
- datasource name + .mwleg extension (for direct rendering);
- proxy name + .mwleg extension (for proxy rendering);
For direct rendering saving of color scheme to disk can be disabled with GlobalSettings.SaveGridColorSchemeToFile property.
The selection of color scheme depends on:
{
int handle = axMap1.AddLayer(grid, true);
var img = axMap1.get_Image(handle);
if (img != null)
{
MessageBox.Show("Grid was opened. Rendering: " + (img.IsGridProxy ? "using proxy" : "direct"));
}
else
{
MessageBox.Show("To add grid to map: " + axMap1.get_ErrorMsg(axMap1.LastErrorCode));
}
grid.Close();
}
else
{
}
GridDataType
The data type which represents a single cell of a grid.
Definition: Enumerations.cs:56
tkGridProxyMode
Possible behaviours for displaying grid datasource. The behaviours will be used in AxMap....
Definition: Enumerations.cs:1519
GridFileType
The type of grid supported by MapWinGIS.
Definition: Enumerations.cs:70
The grid object is used to represent a grid which can be added to the map.
Definition: Grid.cs:412
3. Manual opening/creation of Image for Grid.
With automatic selection of rendering mode:
{
if (img != null)
{
int handle = axMap1.AddLayer(img, true);
}
else
{
MessageBox.Show(
"Failed to create image representation for the grid: " + ut.
ErrorMsgFromObject(grid));
}
grid.Close();
}
else
{
}
tkGridSchemeGeneration
Methods of generation of a new color scheme for grid.
Definition: Enumerations.cs:1640
tkGridSchemeRetrieval
Methods for retrieval of existing color schemes from grid.
Definition: Enumerations.cs:1612
Opening for direct rendering without using Grid class:
if (image.Open(filename,
ImageType.USE_FILE_EXTENSION,
false,
null))
{
if (image.IsRgb)
{
MessageBox.Show("It's not a grid.");
}
else
{
int handle = axMap1.AddLayer(image, true);
if (handle == -1)
{
MessageBox.Show("Failed to open image: " + axMap1.get_ErrorMsg(axMap1.LastErrorCode));
}
else
{
MessageBox.Show("Data source is rendered as: " + (image.GridRendering ? "grid" : "image"));
}
}
}
else
{
}
ImageType
The type of images supported by MapWinGIS.
Definition: Enumerations.cs:92
Represents an raster image of particular format which may be added to the map.
Definition: Image.cs:66
Manual creation of image proxy and color scheme:
{
if (img == null)
{
}
else
{
int handle = axMap1.AddLayer(img, true);
}
}
else
{
}
tkGridProxyFormat
Possible formats for images acting as visualization proxies for grid
Definition: Enumerations.cs:1502
Image GridToImage2(Grid Grid, GridColorScheme ci, tkGridProxyFormat imageFormat, bool InRam, ICallback cBack)
Creates an image proxy for grid visualization.
Definition: Utils.cs:566
bool Close()
Closes the image.
Definition: Image.cs:313
To work with image proxies directly the following API members can be used: Grid.CreateImageProxy, Grid.HasValidImageProxy, Grid.RemoveImageProxy, Image.IsGridProxy.
D. Reading and editing the values.
Grids can be opened in either in-memory or disk based mode. Grid values can be retrieved and edited using:
For in-memory mode to save the changes to disk Grid.Save methods must be called. In disk-based mode the changes are saved automatically.
The size of grid can be obtained from its header (GridHeader.NumberRows, GridHeader.NumberCols).
For multiband datasource to read or edit values in the band other than first one use Grid.OpenBand.
bool inRam = true;
{
var header = grid.Header;
double noData = (double)header.NodataValue;
for (int i =0; i < header.NumberCols; i++)
{
for (int j = 0; j < header.NumberCols; j++)
{
double val = grid.get_Value(i, j)
if (val != noData && val > 1000)
{
grid.set_Value(i, j, noData);
}
}
}
if (!grid.Save(grid.Filename,
GridFileType.UseExtension,
null ))
{
}
grid.Close();
}
else
{
}
E. Coordinate system and projection.
Information about geographic coordinate system and projection of grid can be retrieved using GridHeader.GeoProjection property. It is used by AxMap control when AxMap.GrabProjectionFromData is true. However grid can also be rendered fine if no coordinate system is specified, although projection aware functionality of MapWinGIS (tiles, scalebar) will be unavailable or won't function properly.
The location and scaling of grid in world coordinates is defined by GridHeader.XllCenter, GridHeader.YllCenter, GridHeader.dX, GridHeader.dY. While Grid.CellToProj, Grid.ProjToCell can be used to convert from cell coordinates to world coordinates.
Information about coordinates system and projection will be copied to output image when a proxy for the grid is created by Utils.GridToImage2 and all other API members which are using this method.
{
var gp = grid.Header.GeoProjection;
MessageBox.Show(gp.IsEmpty() ? "No coordinates system for grid is specified." : "Coordinate system for grid: " + grid.Header.GeoProjection.ExportToWKT());
if (gp.IsEmpty)
{
double x, y;
grid.CellToProj(0, 0, out x, out y);
MessageBox.Show(string.Format("World coordinates of the first cell: x={0}; y={1}", x, y));
}
grid.Close();
}
F. Support of GDAL overviews (version 4.9.2):
When opening grids MapWinGIS will recoginze existing GDAL overviews and will automatically create new overviews if needed (GlobalSettings.MinOverviewWidth). By default (GlobalSettings.GridProxyMode = gpmAuto) overviews option will always be chosen over proxy image creation, as it takes significantly less time. For original grids external overviews will be created (.ovr file), for proxy images - built-in overviews.
The use of proxy images for grid can still occur under following conditions:
- proxy image already exists while there is no overviews;
- grid datasource isn't supported by GDAL, so can't be opened directly;
- proxy image option was chosen explicitly with GlobalSettings.GridProxyMode = gpmUseProxy or AxMap.AddLayerFromFilename (filename, fosProxyForGrid, true).