General: How can I change the map projection in a mesh file using a command?



Use MIKE 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(18, 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);



      // 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];



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

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

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




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\2020\MIKE SDK\bin
%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


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



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.


Note: The MIKE SDK installation contains source code for several more examples.