Skip to content

MzChart - DHI.Chart.Map

MzChart is a native library that has been developed and optimised over many years, and today it constitutes an important foundation of the map and chart controls in the MIKE product portfolio, including the Grid Editor, Plot Composer and many others. The MzChart component is developed in C++, and the API is extensive. Some of the basic parts for 2D plots have been made available through a .NET wrapper named DHI.Chart.Map, and at this stage this mainly includes the functionality related to the plotting and styling of maps based on data from mesh and grid files.

The APIs for plotting the following data types are now available: - 2D orthogonal grid plots - dfs2. - 2D rotated grid plots - dfs2. - Mesh plots - values at mesh nodes. - dfsu plots - values at element centers.

Moreover, the API for defining the map palette, vector overlays etc is exposed through the .NET API. MzChart also supports querying for specific geographical areas. The API is structured such that the reading data (e.g. from a dfsu file) is separated from the data objects, which means that the plotting functionality is largely independent of the data storage format. The pattern for generating a map plot is as follows:

  1. Read the data from source (e.g. dfs), by using the appropriate APIs (e.g. the dfs API).
  2. Populate the map data object for the appropriate map type (e.g. FEM Grid data class)
  3. Create a new map overlay for the appropriate map type (FEM grid plot).
  4. Set the grid data object on the overlay, and configure the overlay settings (e.g. isolines, line colors, palette, feathering etc.).
  5. Create a new Chart object, and add add the overlay to the map.
  6. Repeat step 1-5 for each map layer.
  7. Set the bounding box, and generate the map image.

DHI.Chart.Map API

The API is available through the namespace:

DHI.Chart.Map

The assembly is available as a NuGet package at www.nuget.org/packages/DHI.Chart.Map/

The DHI.Chart.Map NuGet package contains all components required to build and run applications for reading and writing PFS files on Windows. It contains a mix of .NET assemblies and native libraries. It is utilizing GDI/GDI+ and requires an enviroment where this is available. That unfortunately rules out some Cloud services, where GDI/GDI+ are not available.

Example

A simple example illustrating the generation of a 2D Orthogonal grid map from a dfs2 file is provided below.

public static void GridDfs2Test(bool makeBmp)
{   
    // Intitalize MzChart    
    Map.Chart.Init();

    // Open a dfs2 file
    string pathName = Path.Combine(UnitTestHelper.TestDataRoot, @"OresundHD.dfs2");
    Dfs2File dfs2 = DfsFileFactory.Dfs2FileOpen(pathName);

    // Read the values of the first item and first time step.
    float[] values = (float[])dfs2.ReadItemTimeStep(1, 1).Data;

    // Get axis and projection
    IDfsAxisEqD2 axis = dfs2.SpatialAxis as IDfsAxisEqD2;
    IDfsProjection proj = dfs2.FileInfo.Projection;

    MapProjection mapProj = new MapProjection(proj.WKTString);
    mapProj.Geo2Proj(proj.Longitude, proj.Latitude, out east, out north);

    // Create the chart object
    Map.Chart chart = new Map.Chart(1024, 1024);

    // Create the grid data object
    Grid2DOrthoNeqData gridData = new Grid2DOrthoNeqData();

    gridData.CreateGridAndGeometry(axis.XCount, axis.YCount, axis.AxisUnit, axis.X0, axis.Y0, axis.Dx, axis.Dy);
    gridData.SetEumItem(dfs2.ItemInfo[0].Quantity.Item);
    gridData.SetEumUnit(dfs2.ItemInfo[0].Quantity.Unit);
    gridData.DeleteValue = dfs2.FileInfo.DeleteValueFloat;
    gridData.SetGridValues(values);

    // Create the grid overlay
    Grid2DOrthoNeqOverlay overlay = new Grid2DOrthoNeqOverlay();

    overlay.SetGridData(gridData);
    overlay.EnableNiceValue = true;
    overlay.CreateAutoScaledRainbowPalette();
    overlay.IsolineType = MapOverlay.EIsolineType.IsolineSmooth;
    overlay.EnableIsoline = true;
    overlay.SetFeathering(true, 0.5f);
    overlay.EnableIsolineLabel = true;
    overlay.ShadingType = MapOverlay.EShadingType.ShadedContour;
    overlay.Palette.LandValue = (float)dfs2.FileInfo.CustomBlocks[0][3];
    overlay.DeleteValueDraw = true;

    chart.AddOverlay(overlay);

    // Set the bounding box
    MzRectangle wrect = new MzRectangle();
    data.GetDataArea(wrect);
    chart.SetDataArea(wrect);
    chart.SetView(wrect);

    // Generate the image and save to file
    Bitmap bmp = chart.DrawBitmap();
    bmp.Save(@"c:\temp\oresunddfs2.png);

    // Dispose the chart
    chart.Dispose();
}