0
Answer

Convert an ASCII Grid file to an ASCII XYZ file

Gerben Schmidt

Gerben Schmidt

14y
4.8k
1
Hi,

I want to convert an ASCII Grid file to an ASCII XYZ file.
These files can be very large >3gb

The ASCII Grid file example:

ncols         768 (number of colums)
nrows         736 (number of rows)
xllcorner     47472.00 (lower left corner X of the grid)
yllcorner     418933.00 (lower left corner Y of the grid)
cellsize      1.00 (Grid Size)
nodata_value  -9999
 -9999 -9999 -9999 -9999 -9999
 -9999 -9999 -9999 -9999 -9999
 -9999 -9999 -9999 -9999 -9999
 -9999 -9999 -9999 -9999 -9999
 -9999 -9999 -9999 -9999 -9999
 -9999 -9999 -9999 -9999 -9999
 -9999 -9999 -9999 -9999 -9999
 -9999 -9999 -9999 -9999 -213.20

The ASCII XYZ file example:

47472.00, 418933.00,-213.20

Now I have a code works (I tested it with 1gb file)
However I want to know 2 things:

1. is my code efficient enough
2. I want to split the Output file when the filesize is larger then 500mb

Here is my Code:

              using (StreamReader sr = new StreamReader(filename))
                {
                    // Read the Header of the file (6 lines)

                    //ncols
                    string[] line1 = sr.ReadLine().Replace(',', '.').Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    int ncols = (Convert.ToInt32(line1[1]));

                    //nrows
                    string[] line2 = sr.ReadLine().Replace(',', '.').Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    int nrows = (Convert.ToInt32(line2[1]));

                    //xllcorner
                    string[] line3 = sr.ReadLine().Replace(',', '.').Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    double xllcorner = Convert.ToDouble((line3[1]), CultureInfo.InvariantCulture);

                    //xllcorner
                    string[] line4 = sr.ReadLine().Replace(',', '.').Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    double yllcorner = Convert.ToDouble((line4[1]), CultureInfo.InvariantCulture);

                    //cellsize
                    string[] line5 = sr.ReadLine().Replace(',', '.').Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    double cellsize = Convert.ToDouble((line5[1]), CultureInfo.InvariantCulture);

                    //nodata
                    string[] line6 = sr.ReadLine().Replace(',', '.').Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    double nodata = Convert.ToDouble((line6[1]), CultureInfo.InvariantCulture);

                    //pre calculate the X and Y coordinates

                    ArrayList xcoords = new ArrayList();
                    IEnumerable<int> x = Enumerable.Range(0, ncols);

                    foreach (int col in x)
                    {
                        xcoords.Add(Convert.ToString(xllcorner + (0.5 * cellsize) + (col * cellsize)));
                    }

                    ArrayList ycoords = new ArrayList();
                    IEnumerable<int> y = Enumerable.Range(0, nrows);

                    foreach (int row in y)
                    {
                        ycoords.Add(Convert.ToString(yllcorner - (0.5 * cellsize) + (nrows - row) * cellsize));
                    }

                    //Read Depth and Write to XYZ file

                    using (StreamWriter xyz = new StreamWriter(textBox2.Text))
                    {
                        foreach (int row in y)
                        {
                            string[] line = sr.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                            foreach (int col in x)
                            {
                                if (line[col] != Convert.ToString(nodata))
                                {
                                    xyz.WriteLine(xcoords[col].ToString().Replace(',', '.') + "," + ycoords[row].ToString().Replace(',', '.') + "," + line[col].Replace(',', '.'));
                                }
                            }
                        }
                    }
                }