Reading and displaying digital elevation modells (ddm files)

DDM is a binary format of the Hungarian digital elevation modell. There are two files: *.hdr is a header file, which contains meta data about binaries. Meta data are the followings: number of rows, number of columns, grid size, georeferencing corner points (xmin, xmax, ymin,ymax), coordinate system id, section number. The other file format is ddm, which is a binary. One elevation data is stored on two bytes (one byte can represent a number, which changes from 0 to 255). That's why one elevation point can represented in two bytes.

Parsing meta data


 private void readParameters(string fname)
        {
            string[] s = File.ReadAllLines(Path.ChangeExtension(fname, "hdr"));

            _secName = s[0];
            _spatialSystem = s[1];
            _gridsize = Convert.ToInt16(s[12]);
            _nrow = Convert.ToInt16(s[10].Split(' ')[0]);
            _ncol = Convert.ToInt16(s[10].Split(' ')[1]);

            string xmn = (s[2].Split(' ')[0]).Replace('.',',');
            _xmin = Convert.ToSingle(xmn);
            string ymn = s[2].Split(' ')[1].Replace('.', ',');
            _ymin = Convert.ToSingle(ymn);

            string xmx = (s[4].Split(' ')[0]).Replace('.', ',');
            _xmax = Convert.ToSingle(xmx);
            string ymx = s[4].Split(' ')[1].Replace('.', ',');
            _ymax = Convert.ToSingle(ymx);
        }

Read binary elevation data


        private void readDDM()
        {
            _demBitmap = new Bitmap(_ncol,_nrow);
            _dem = new float[_ncol, _nrow];

            using (FileStream fs = new FileStream(_filename, FileMode.Open, FileAccess.Read))
            {

                BinaryReader br = new BinaryReader(fs);
                float min = 255F;
                float max = 0F;
                for (int i = 0; i < _nrow; i++) // min max search
                {
                    for (int j = 0; j < _ncol; j++ )
                    {
                        int b = br.ReadInt16();
                        _dem[j, i] = b;
                        if (b < min) { min = b; }
                        if (b > max) { max = b; }
                    }
                }
                float minmax=max-min;
                fs.Position = 0;
                
                for (int i = 0; i < _nrow; i++)  //convert to 0 - 255 interval
                {
                    for (int j = 0; j < _ncol; j++)
                    {
                        float b = br.ReadInt16();
                        int c = (int)((b - min) / minmax * 255F);
                        Color col=Color.FromArgb(c,c,c);
                        _demBitmap.SetPixel(j, i, col);
                    }
                }
            }
        }