Read a raster ASCII GRID

Nov 11, 2014 at 10:37 AM
Hello, I have a problem because I have a raster (asc) ascii GRID in the following format:

ncols 2049
nrows 2049
xllcorner 418192.768
yllcorner 2418087.439
xdim 25.263525390625
ydim 20.121337890625
nodata_value -9999

is a rectangular raster, not square, which instead of having cellsize, has the pair XDIM, YDIM.

Is there any way to read this format in dotspatial since gdal is not recognized as valid.

Thanks a lot, Claudio
Nov 11, 2014 at 1:37 PM
Hi Claudio,

a long ago I made myself a sub to import an ascii raster.

Here is the code
  Public Sub ImportAsciiRaster()
        
        Dim linein As String
        Dim slin() As String

        Dim ncols, nrows As Integer
        Dim xll, yll, nodata As Double
        Dim cell As Double
        'ask the user to specify the file to import
        Dim openfiledialog1 As New OpenFileDialog
        openfiledialog1.ShowDialog()
        Dim frasinput As String
        frasinput = openfiledialog1.FileName

        'Create a BGD imported raster
        Dim frasout As String = System.IO.Path.ChangeExtension(frasinput, "BGD")
        

        'Read the ascii raster file
        FileOpen(64, frasinput, OpenMode.Input)

        'Header
        linein = LineInput(64).Trim()
        slin = Regex.Split(linein, "\s+")
        If (slin(0).Trim().ToUpper().Contains("NCOLS") = False) Then
            MsgBox("Error import. Not a ESRI ascii raster")
            Exit Sub
        End If
        Integer.TryParse(slin(1).Trim(), ncols)
        linein = LineInput(64).Trim()
        slin = Regex.Split(linein, "\s+")
        Integer.TryParse(slin(1).Trim(), nrows)
        linein = LineInput(64).Trim()
        slin = Regex.Split(linein, "\s+")
        Double.TryParse(slin(1).Trim(), xll)
        linein = LineInput(64).Trim()
        slin = Regex.Split(linein, "\s+")
        Double.TryParse(slin(1).Trim(), yll)
        linein = LineInput(64).Trim()
        slin = Regex.Split(linein, "\s+")
        Double.TryParse(slin(1).Trim(), cell)        'you may duplicate this and read a "cellx" and "celly" dimensions
        linein = LineInput(64).Trim()
        slin = Regex.Split(linein, "\s+")
        Double.TryParse(slin(1).Trim(), nodata)



        Dim rasopt() As String
        ReDim rasopt(1)
        rasopt(0) = ""
      

                Dim datatype As System.Type
                datatype = GetType(Double)

                Dim rasinput As DotSpatial.Data.Raster
                rasinput = DotSpatial.Data.Raster.CreateRaster(frasout, Nothing, ncols, nrows, 1, datatype, rasopt)
               
                rasinput.Xllcenter = xll
                rasinput.Yllcenter = yll
                rasinput.NoDataValue = nodata
                rasinput.CellHeight = cell                 'Here you may put "celly" read from your ascii file
                rasinput.CellWidth = cell                 'Here you may put "cellx" read from your ascii file

                Dim value As Double

                'Reading raster cell values and insert them into rasinput
                For i As Integer = 0 To nrows - 1
                    linein = LineInput(64)
                    slin = Regex.Split(linein.Trim(), "\s+")

                    For j As Integer = 0 To ncols - 1
                        Double.TryParse(slin(j), value)

                        rasinput.Value(i, j) = value

                    Next
                Next
                FileClose(64)
               
                'save results
                rasinput.SaveAs(frasout)
 
        Return


    End Sub

See if you can get through it

Oscar
Nov 11, 2014 at 2:33 PM
Thanks Oscar, I will be very useful, it is a pity that dotspatial or gdal not support this format without having to convert it before.

Claudio