Save/convert a grid from .bgd into .asc (ASCII)

Aug 2, 2013 at 3:46 PM
Hi list,
I am using following dotspatial function to convert a shape file into a grid:
var demRaster2 = DotSpatial.Analysis.VectorToRaster.ToRaster(shap2, 100, "LU", "Map2.bgd");

Does anyone know how I can convert/save the grid file into .asc? I need to read the cell details from the asci file.

Aug 5, 2013 at 9:09 AM
Hi Arvin

I have a couple of subs that does that (I didn't test it with large rasters).

Here is the code of my subs. Basically you need to select the raster to convert from the layer's tree and call DoExport which checks for a raster layer being selected by the user, asks for output filename and call ExportAscii sub.
 Private Sub DoExport
        Dim savefiledialog1 As New SaveFileDialog
        Dim ras As MapRasterLayer
        Dim iras As IRaster
        If Map1.Layers.SelectedLayer Is Nothing Then
            MsgBox("Please select raster layer to export")
            ras = TryCast(Map1.Layers.SelectedLayer, MapRasterLayer)
            If ras Is Nothing Then
                MsgBox("Please select raster layer to export")

                savefiledialog1.Filter = "ESRI Ascii Raster (*.asc)|*.asc"
                savefiledialog1.OverwritePrompt = True
                exportAscii(Map1.Layers.SelectedLayer, savefiledialog1.FileName)

             End If
        End If

    End Sub

    Private Sub exportAscii(layer As DotSpatial.Controls.IMapLayer, fname As String)

        Dim ras As MapRasterLayer
        ras = layer
        Map1.Cursor = Cursors.WaitCursor

        FileOpen(64, fname, OpenMode.Output)

        PrintLine(64, "ncols " & ras.NumColumns)
        PrintLine(64, "nrows " & ras.NumRows)
        PrintLine(64, "xllcorner " & ras.DataSet.Xllcenter.ToString)
        PrintLine(64, "yllcorner " & ras.DataSet.Yllcenter.ToString)
        PrintLine(64, "cellsize " & ras.CellWidth)
        PrintLine(64, "nodata_value " & ras.NoDataValue)
        Dim dt As New DataType
        Dim outstr As String
        Dim grid1(ras.NumColumns - 1) As Double
        For i As Integer = 0 To ras.NumRows - 1

            For j As Integer = 0 To ras.NumColumns - 1
                grid1(j) = (ras.DataSet.Value(i, j))
            outstr = String.Join(" ", grid1)
            PrintLine(64, outstr)



        Map1.Cursor = Cursors.Default

    End Sub
Hope it serves