How to parse ogc geometry(wkb) from mysql with copymemory method (visual basic)

Posted on 2004-11-29
Last Modified: 2012-08-14
I need to parse a wkb with copymemory. The wkb is described here:

rs.Open "SELECT AsBinary(address_loc) as address_loc from address where (address = 'street a')", conn

The data is then in rs.Fields(0).Value.

How can I parse the data to get x,y into doubles? (I just tried it without copymemory and converting the stuff into hex but that's not really fast)

thx red

Question by:redtiger889
    LVL 32

    Expert Comment

    I cannot test this because I do not have MySql, but this should get you started.  
    Using CopyMemory will only only work if the ByteOrder is little-endian (reverse byte order).
    Save your work before using CopyMemory!

    Not sure if you can pass  rs.Fields(0).Value to CopyMemory, you may need to copy it to a byte array as in the example below or try:
    ByVal VarPtr(rs.Fields(0).Value)

    Option Explicit

    'The WKB type to split out our array
    Private Type WellKnownBinary
        'ByteOrder As Byte '(cant use single bytes because CopyMemory wants to be word aligned)
        Type As Long
        X As Double
        Y As Double
    End Type

    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

    Private Sub Command1_Click()

        Dim a(20) As Byte 'This is the WKB array
        Dim i As Long 'just a counter
        Dim wkb As WellKnownBinary
        For i = 0 To 20
            a(i) = i 'Fill in dummy data
        'Start reading from a(1) because we are skipping the ByteOrder byte
        Call CopyMemory(wkb, ByVal VarPtr(a(1)), LenB(wkb))
        Debug.Print Hex(wkb.Type)

    End Sub

    Author Comment

    Unfortunately it's not working - the output is not correct  -  my code looks like that (without copymemory):

    'strStream = rs.fields(0).value

    Dim k As Long
    Dim hexString As String

        For k = 1 To Len(strStream)
            hexString = hexString & Hex(Asc(Mid(strStream, k, 1)))
        Next k

        Debug.Print hexString
    Dim X As Double
    Dim Y As Double

    'Debug.Print Left(hexString, 1) & "/" & Mid(hexString, 2, 4) 'order and type
    'Debug.Print Mid(hexString, 6, 10) & "/" & Mid(hexString, 16, 10) 'x and y


    Author Comment

    Here's what I tried : I get 100 instead of 1000 (near). But the lenght of b() after b = rs.Fields(0).Value is 42. I think there's just a problem with the postion of x,y .... (???)

       Dim b() As Byte
        Dim wkb As WellKnownBinary
        Dim rs As New ADODB.Recordset
        rs.Open "SELECT AsBinary(address_loc) as address_loc from address where (address = 'lindauergasse')", conn

        b = rs.Fields(0).Value
        Call CopyMemory(wkb, ByVal VarPtr(b(1)), LenB(wkb))

        Debug.Print Hex(wkb.Type)

    thx and greetings red
    LVL 32

    Accepted Solution

    If rs.Fields(0).Value is a string value, then when you assign to a byte array it will be twice as long because strings in VB are unicode (1 character = 2 bytes).  If you cannot get the data directly from the database as binary data, then you can convert the string to a byte array like this:

    b = StrConv(rs.Fields(0).Value, vbFromUnicode)

    Author Comment

    Thx a lot - you saved my evening.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now