Views:

Question
How can I manually change the map projection for a mesh file using a command?

Answer
Use MIKE Core SDK to create a tool that creates a new similar mesh file where the only difference is the new map projection and converted node point coordinates.
The input to the command would typically be the filename of the input mesh, the filename of the new output mesh and the file name for the file describing the new map projection.  An example is given below.  The CSharp source code for the tool may look as follows in ReprojectMesh.cs:

using System;
using System.Collections.Generic;
using DHI.Generic.MikeZero.DFS;
using DHI.Generic.MikeZero.DFS.mesh;
using DHI.Projections;

namespace DHI.Generic.MikeZero.MeshTool
{
  class Program
  {

        static Program()
        {
            // The setup method will make your application find the MIKE assemblies at runtime.
            if (!DHI.Mike.Install.MikeImport.Setup(22, DHI.Mike.Install.MikeProducts.Mike1D))
                throw new Exception("Could not find a MIKE installation");
        }


    /// <summary>
    /// Convert mesh file from one map projection to another (simple conversion, no Datum shift)
    /// </summary>

    public static void Main(string[] args)
    {
      string sourceFilename  =  args[0];
      string targetFilename  =  args[1];
      string targetProjFile  =  args[2];

      MeshFile MeshSource = DfsFileFactory.MeshFileRead(sourceFilename);

      string sourceProj = MeshSource.ProjectionString;
      string targetProj = System.IO.File.ReadAllText(targetProjFile);

      Reprojector reproj = new Reprojector(sourceProj, targetProj);

      //-----------------------------------------
      // New mesh nodes
      List<double> X = new List<double>();
      List<double> Y = new List<double>();
      List<double> Z = new List<double>();
      List<int> Code = new List<int>();

      // Loop over all nodes in the mesh file
      int NoNodes = MeshSource.NumberOfNodes;
      for (int l = 0; l < NoNodes; l++)
            {
              double xx = MeshSource.X[l];
              double yy = MeshSource.Y[l];
              double zz = MeshSource.Z[l];
              int cc = MeshSource.Code[l];

                                   reproj.Convert(ref xx, ref yy);

                                   X.Add(xx);
                                   Y.Add(yy);
                                   Z.Add(zz);
                                   Code.Add(cc);

            }

      //-----------------------------------------
      // New mesh elements
       List<int[]> elmttable2 = new List<int[]>();

      // Loop over all elements in the mesh file
      int NoElements = MeshSource.NumberOfElements;
      for (int l = 0; l < NoElements; l++)
           {
              int[] newElements = new int[5];
              newElements = MeshSource.ElementTable[l];

              elmttable2.Add(newElements);

           }

      //-----------------------------------------
      // Create mesh
      {
          MeshBuilder builder = new MeshBuilder();

          // Setup header and geometry
          builder.SetNodes(X.ToArray(), Y.ToArray(), Z.ToArray(), Code.ToArray());
          builder.SetElements(elmttable2.ToArray());
          builder.SetProjection(targetProj);

          // Create and write new file
          MeshFile mesh = builder.CreateMesh();
          mesh.Write(targetFilename);

      }
    }

  }
}

 

The build statement to create the executionable ‘ReprojectMesh.exe’ looks as follows:

 

set csc=C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
set sdkBin=C:\Program Files (x86)\DHI\MIKE Core SDK\2024\bin\x64
%csc% /r:"%sdkBin%\DHI.Generic.MikeZero.DFS.dll" /r:"%sdkBin%\DHI.Generic.MikeZero.EUM.dll" /r:"%sdkBin%\DHI.Projections.dll" /r:"%sdkBin%\DHI.Mike.Install.dll" /r:"netstandard.dll" ReprojectMesh.cs
pause

 

Assume that you have an old mesh file defined in UTM-29 in a subfolder called ‘MeshData’.  You want to re-project this to use the open GIS projection WGS 1984 UTM Zone 30N which is described in the file ‘WGS 1984 UTM Zone 30N.prj’ contained in a subfolder called ‘Proj’.

 

InputMesh.png

Fig. 1 - Input mesh defined in UTM-29

                   

I.e., to reproject the mesh as shown above to a new file the command would be: 


ReprojectMesh.exe ./MeshData/Funningsfjord.mesh ./MeshData/Output.mesh ./Proj/”WGS 1984 UTM Zone 30N.prj"

 

The left figure below shows the original mesh and the right figure below shows the new converted mesh.

Fig. 2 - Left: Mesh node coordinates in original .mesh file defined in UTM-29, Right: Mesh node coordinates in converted .mesh file defined in WGS 1984 UTM Zone 30N.

Note: The MIKE Core SDK installation contains the source code for several more examples. Compiled versions of various tools are also available from GitHub at https://github.com/DHI/MIKECore-Examples/releases

FURTHER INFORMATION & USEFUL LINKS

Manuals and User Guides
MIKE Core SDK Documentation Index

Release Notes
MIKE Core SDK Release Note

 

Related Products: MIKE 21/3