Views:

Question
How can I obtain the grid coordinates for a dfs1 file with non-equidistant grid spacing?

Answer
You can use MIKE Core SDK to create a small tool that reads the information from the dfs1 file and saves it to an ascii file. In the same process the chainage length can be calculated.

(MIKE Core SDK is a free software development kit that allows you to easily write code that accesses and produces files in the main MIKE data formats.)

 

The CSharp source code for the tool may look as follows in ExtractNonEqDfs1Coordinates.cs:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using DHI.Generic.MikeZero.DFS;

using System.IO;

 

namespace extractCoordinatesNonEqDfs1

{

    class ExtractNonEqDfs1Coordinates

    {

        /// <summary>

        /// Static constructor, setting up search paths for MIKE assemblies

        /// </summary>

        static ExtractNonEqDfs1Coordinates()

        {

            // 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");

        }

 

        static void Main(string[] args)

        {

            var infile = args[0];

            var outfile = args[1];

 

            // Open dfs1 file

            var dfs1 = DfsFileFactory.Dfs1FileOpen(infile);

            // Extract information from spatial axis

            var Coords = dfs1.SpatialAxis.CreateAllCoordinates();

            var NCoord = Coords.Length;

            dfs1.Close();

            

            // Calculate chainage/distance

            double[] Chain = new double[NCoord];

            string[] Coords_str = new string[NCoord+1];

            Coords_str[0] = "E (m) ; N (m) ; Chainage (m)";

            Coords_str[1] = Coords[0].X.ToString() + " ; " + Coords[0].Y.ToString() + " ; 0.00";

            for (int k = 1; k < NCoord; k++)

            {

                Chain[k] = Chain[k - 1] + Math.Sqrt(Math.Pow(Coords[k].X - Coords[k - 1].X, 2) + Math.Pow(Coords[k].Y - Coords[k - 1].Y, 2));

                Coords_str[k + 1] = Coords[k].X.ToString() + " ; " + Coords[k].Y.ToString() + " ; " + Chain[k].ToString();

            }

           

            // Save coordinates and chainage to ascii-file

            File.WriteAllLines(outfile, Coords_str);

            Console.Write("Done writing coordinates of file: {0} to file: {1}", infile, outfile);

        }

    }s

}

 

The build statement to create the executionable ‘ExtractNonEqDfs1Coordinates.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.Mike.Install.dll" /r:"netstandard.dll" ExtractNonEqDfs1Coordinates.cs
pause

 

Example
Assume that you have extracted data from a curved line using the functionality in the Data Manager and saved the data in a file ‘CurvedLine.dfs1’. You can use this tool to extract the coordinates from the dfs1 file and save them to an ascii file.

Figure 1 shows the extracted line in the domain (left) and the resulting coordinate points from entered via a xyz-file.

 

The command would be:

ExtractNonEqDfs1Coordinates.exe CurvedLine.dfs1 OutputCoor.csv

 

 

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

 

ExtractXYCoor_NonEqdfs1.png

Fig. 1 -  Example of converted points from dfs1 file with non-equidistant grid steps. Left: position of extraction line in domain. Right: position of extracted grid points.   

FURTHER INFORMATION & USEFUL LINKS

Release Note
MIKE Core SDK Release Note

Others
MIKE Core SDK Documentation Index

 

Related Products: MIKE 21/3