[Gdal-dev] RE: Any tips for VB .NET?

Paul, Michael michael.paul at tao.es
Wed Mar 22 02:54:58 EST 2006


Hi,

I only managed to get this GDALRasterIO to function with types Byte and
Float32. You have passed a data type 6 to this function, which is equivalent
to GDalDataType.Float32 (see http://gdal.maptools.org/gdal_8h.html). But
your code is passing a string, this shouldn't work it think. 
You may do something like this:

Public Sub RunExtract()
        Try
            
            GDALAllRegister()
            Dim raster As IntPtr = GDALOpen("c:\FrogTech\seabed_g.img", 0)
		If (raster = IntPtr.Zero) Then ' throw exception or similar
            Dim rasBand As IntPtr = GDALGetRasterBand(raster, 1)
		If (rasBand = IntPtr.Zero) Then ' throw exception or similar
            Dim xSize As Integer = GDALGetRasterXSize(raster)
            Dim ySize As Integer = GDALGetRasterYSize(raster)
            MsgBox(xSize & "  " & ySize)
            ' allocate some memory and get its address
		Dim dataPointer As IntPtr =   Marshal.AllocHGlobal(xSize *
ySize) 
            Dim errResult As IntPtr = GDALRasterIO(rasBand, 0, 0, 0, xSize,
ySize, New IntPtr(rasData.GetHashCode), xSize, ySize, 1, 0, 0)
		' use Marshal.ReadByte(dataPointer, offset) to read data
		Marshal.FreeHGlobal(dataPointer)
            ' MsgBox(rBand)
            GDALClose(raster)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try


    End Sub


Hope this helps.

Regards,
 
Michael.


-----Mensaje original-----
De: gdal-dev-bounces at lists.maptools.org
[mailto:gdal-dev-bounces at lists.maptools.org] En nombre de Holywhippet
Enviado el: miércoles, 22 de marzo de 2006 1:51
Para: gdal-dev at lists.maptools.org
Asunto: [Gdal-dev] RE: Any tips for VB .NET?


 Thanks, this comes very close to solving my problem. I basically just want
to extract the data from a Raster for the time being. At the end of my
message is the code I've written in VB .NET. It all works ok up to the
GDALRasterIO part. At that point everything falls apart. I'm not sure what
kind of variable to send in for the data or how to initialise it. I'm not
100% proficient in .NET so I'm not sure if I'm overlooking something
obvious. When it fails it tells me : "Object reference not set to an
instance of an object." so I'm guessing I'm not initialising the data
variable correctly or something.  Any ideas?




Imports System.IO
Imports System.Runtime.InteropServices

Module mainModule
    Sub main()
        Dim tool As New ExtractRaster
        tool.RunExtract()
        ' tool.Convert()
    End Sub
End Module
Public Class ExtractRaster
    Declare Function SetWindowLong Lib "USER32.DLL" Alias "SetWindowLongA"
(ByVal hwnd As Integer, ByVal attr As Integer, ByVal lVal As Integer) As
Integer
    '  Declare Function GDALOpen Lib "C:\Program
Files\FWTools1.0.0a7\bin\gdal_fw.dll" ()
    '  Declare Function GDALGetRasterBand Lib "C:\Program
Files\FWTools1.0.0a7\bin\gdal_fw.dll" ()
    '  Declare Sub GDALAllRegister Lib "C:\Program
Files\FWTools1.0.0a7\bin\gdal_fw.dll" Alias "GDALGetRasterBand" ()
    '  Declare Function GDALGetRasterXSize Lib "C:\Program
Files\FWTools1.0.0a7\bin\gdal_fw.dll" ()
    '  Declare Function GDALGetRasterYSize Lib "C:\Program
Files\FWTools1.0.0a7\bin\gdal_fw.dll" ()
    ' Declare Function GDALReadRaster Lib "C:\Program
Files\FWTools1.0.0a7\bin\gdal_fw.dll" ()
    <DllImport("gdal_fw.dll", EntryPoint:="CPLGetLastErrorMsg")> _
          Private Shared Function CPLGetLastErrorMsgExt() As IntPtr
    End Function

    <DllImport("gdal_fw.dll")> _
    Friend Shared Function GDALOpen(<MarshalAs(UnmanagedType.LPStr)> ByVal
pszFilename As String, ByVal eAccess As Integer) As IntPtr
    End Function

    <DllImport("gdal_fw.dll")> _
    Friend Shared Function GDALGetRasterXSize(ByVal hDataset As IntPtr) As
Integer

    End Function
    <DllImport("gdal_fw.dll")> _
    Friend Shared Function GDALGetRasterYSize(ByVal hDataset As IntPtr) As
Integer

    End Function

    <DllImport("gdal_fw.dll")> _
    Friend Shared Sub GDALAllRegister()

    End Sub
    <DllImport("gdal_fw.dll")> _
    Friend Shared Sub GDALClose(ByVal hDriver As IntPtr)

    End Sub

    <DllImport("gdal_fw.dll")> _
    Friend Shared Function GDALGetRasterBand(ByVal hDataset As IntPtr, ByVal
index As Integer) As IntPtr

    End Function

    <DllImport("gdal_fw.dll")> _
    Friend Shared Function GDALRasterIO(ByVal hBand As IntPtr, ByVal _
    flag As Integer, ByVal xOff As Integer, ByVal yOff As Integer, ByVal
xSize As _
    Integer, ByVal ySize As Integer, ByVal data As IntPtr, ByVal bufXSize As
_
    Integer, ByVal bufYSize As Integer, ByVal type As Integer, ByVal _
    pixelStride As Integer, ByVal lineStride As Integer) As IntPtr

    End Function

    ' Fields
    Private Const gdalDll As String = "gdal_fw.dll"

    Public Sub RunExtract()
        Try
            
            GDALAllRegister()
            Dim raster As IntPtr = GDALOpen("c:\FrogTech\seabed_g.img", 0)
            Dim rasBand As IntPtr = GDALGetRasterBand(raster, 1)
            Dim xSize As Integer = GDALGetRasterXSize(raster)
            Dim ySize As Integer = GDALGetRasterYSize(raster)
            MsgBox(xSize & "  " & ySize)
            ' Dim rBand As String = GDALReadRaster(rasBand, 0, 0, xSize,
ySize, xSize, ySize, 6)
            Dim rasData As String = New String(" "c, xSize & ySize) 'IntPtr
' 
            Dim errResult As IntPtr = GDALRasterIO(rasBand, 0, 0, 0, xSize,
ySize, New IntPtr(rasData.GetHashCode), xSize, ySize, 6, 0, 0)
            ' MsgBox(rBand)
            GDALClose(raster)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try


    End Sub
End Class
--
View this message in context:
http://www.nabble.com/Any-tips-for-VB-.NET--t1315468.html#a3524371
Sent from the GDAL - Dev forum at Nabble.com.

_______________________________________________
Gdal-dev mailing list
Gdal-dev at lists.maptools.org
http://lists.maptools.org/mailman/listinfo/gdal-dev




More information about the Gdal-dev mailing list