Results 1 to 10 of 10

Thread: [RESOLVED] Getting GPS Latitide and Long from an imagem file: Negative Second?!

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2022
    Posts
    5

    Resolved [RESOLVED] Getting GPS Latitide and Long from an imagem file: Negative Second?!

    Hello!

    I'm with a problem in a code getting GPS Latitude an Longitude from an image file. The problem is that I'm receiving a negative second, like this:

    174'-19.7154024''S 4042'-4.7759284''W

    But the correct value is:

    174'41.64127325''S 4042'56.5807472''W

    In the file properties, I see the correct lat and long:

    Name:  PE3vXp2.jpg
Views: 308
Size:  49.0 KB

    Someone can help me? How can I convert the negative second to the correct value?

    My code:

    Code:
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            ' Image.GetPropertyItem : http://msdn.microsoft.com/en-us/library/system.drawing.imaging.propertyitem.id(v=vs.110).aspx
    
            Dim Bytes() As Byte
            Dim Dir As String
            Dim Deg As Integer
            Dim Min As Integer
            Dim Sec As Single
            Dim Ref As Integer
            Dim Value As Single
    
            Dim img As Image = Image.FromFile("C:\Users\Charles\Downloads\DJI_0684.jpg")
            ListBox1.Items.Add("Image width : " & CStr(img.Width))
            ListBox1.Items.Add("Image height : " & CStr(img.Height))
    
            ' Get latitude
            Dir = Chr(img.GetPropertyItem(1).Value(0))
            Bytes = img.GetPropertyItem(2).Value
            Deg = GetRational(Bytes, 0)
            Min = GetRational(Bytes, 8)
            Sec = Math.Round(GetRational(Bytes, 16), 1)
            Value = Deg + (Min * 60 + Sec) / 3600
            If Dir = "S" Then Value = -Value
            ListBox1.Items.Add("Latitude : " & Dir & " " & CStr(Deg) & "" & CStr(Min) & "'" & CStr(Sec) & """ = " & CStr(Value))
    
            ' Get longitude
            Dir = Chr(img.GetPropertyItem(3).Value(0))
            Bytes = img.GetPropertyItem(4).Value
            Deg = GetRational(Bytes, 0)
            Min = GetRational(Bytes, 8)
            Sec = Math.Round(GetRational(Bytes, 16), 1)
            Value = Deg + (Min * 60 + Sec) / 3600
            If Dir = "W" Then Value = -Value
            ListBox1.Items.Add("Longitude : " & Dir & " " & CStr(Deg) & "" & CStr(Min) & "'" & CStr(Sec) & """ = " & CStr(Value))
    
            ' Get altitude
            Ref = img.GetPropertyItem(5).Value(0)
            Bytes = img.GetPropertyItem(6).Value
            Value = Math.Round(Ref + GetRational(Bytes, 0), 1)
            ListBox1.Items.Add("Altitude : " & CStr(Value) & " meters")
        End Sub
    
    
    Public Function GetRational(ByVal Bytes() As Byte, ByVal Offset As Integer) As Double '''' talvez apagar depois (teste)
    
            Return BitConverter.ToInt32(Bytes, Offset) / BitConverter.ToInt32(Bytes, Offset + 4)
    
    End Function

  2. #2
    Addicted Member
    Join Date
    Jan 2013
    Posts
    142

    Re: Getting GPS Latitide and Long from an imagem file: Negative Second?!

    Hi there,

    Your code works fine, it is giving you the correct coordinates, The picture has South/West coordinates which are being translated into negative numbers by your code. It is Windows Explorer that is misleading you. Windows Explorer has the habit of showing latitude/ longitude with no north/south/east/west specifier, It doesn't even show negative numbers instead of south and west. If you rely on Windows Explorer alone you wouldn't know whether the picture has been taken in North America or in Indonesia.

    Hope that helps.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2022
    Posts
    5

    Re: Getting GPS Latitide and Long from an imagem file: Negative Second?!

    Hello my friend. Thank you for your help! But it doesn't work on Google Maps, for example. If I do a search using these negative coordinates, Google Maps will give me an error.

    I need to get the correct coordinates because my application opens a browser window and uses Google Maps search.

    Do you know how to turn those negative coordinates into correct coordinates?

  4. #4
    Addicted Member
    Join Date
    Jan 2013
    Posts
    142

    Re: Getting GPS Latitide and Long from an imagem file: Negative Second?!

    First, replace the listbox with a textbox, then use the following code.
    Run the project and give me the output you see in the textbox.

    Code:
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    
            Try
                Dim Bytes() As Byte
                Dim Dir As String
                Dim Deg As Integer
                Dim Min As Integer
                Dim Sec As Single
                Dim latitude As String
                Dim longitude As String
    
                Dim img As Image = Image.FromFile("C:\Users\Charles\Downloads\DJI_0684.jpg")
    
                ' Get latitude
                Dir = Chr(img.GetPropertyItem(1).Value(0))
                Bytes = img.GetPropertyItem(2).Value
                Deg = GetRational(Bytes, 0)
                Min = GetRational(Bytes, 8)
                Sec = Math.Round(GetRational(Bytes, 16), 1)
                latitude = Dir & CStr(Deg) & "" & CStr(Min) & "'" & CStr(Sec) & Chr(34)
    
                ' Get longitude
                Dir = Chr(img.GetPropertyItem(3).Value(0))
                Bytes = img.GetPropertyItem(4).Value
                Deg = GetRational(Bytes, 0)
                Min = GetRational(Bytes, 8)
                Sec = Math.Round(GetRational(Bytes, 16), 1)
                longitude = Dir & CStr(Deg) & "" & CStr(Min) & "'" & CStr(Sec) & Chr(34)
    
                'Show the coordinates in Sexagesimal
                TextBox1.Text = latitude & "," & longitude
    
            Catch ex As Exception
    
            End Try
        End Sub
    
    
        Public Function GetRational(ByVal Bytes() As Byte, ByVal Offset As Integer) As Double '''' talvez apagar depois (teste)
    
            Return BitConverter.ToInt32(Bytes, Offset) / BitConverter.ToInt32(Bytes, Offset + 4)
    
        End Function
    End Class

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2022
    Posts
    5

    Re: Getting GPS Latitide and Long from an imagem file: Negative Second?!

    Quote Originally Posted by cPubis View Post
    First, replace the listbox with a textbox, then use the following code.
    Run the project and give me the output you see in the textbox.
    The output in Textbox is:

    S174'-19,7",W4042'-4,8"

    Remains negative...

    If you want to test, the link of image that shows the negative coordinates is:

    https://drive.google.com/file/d/1cTg...ew?usp=sharing

    By way of comparison, here is an image that shows the coordinate correctly:

    https://drive.google.com/file/d/1ZfI...ew?usp=sharing
    Last edited by ctrindade; Aug 8th, 2022 at 09:58 AM.

  6. #6
    Addicted Member
    Join Date
    Jan 2013
    Posts
    142

    Re: Getting GPS Latitide and Long from an imagem file: Negative Second?!

    Hi there,

    The code is fine, I have several gps readers and each of them gives different reading. This picture is weird!

    Anyway, if the rest of your pictures are the same you can use the following code to read their "raw" gps info directly from the image.

    Make a new form with one textbox and copy the code. The textbox will give you negative output, but it's correct negative.

    Code:
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim Image As String = File.ReadAllText("C:\Users\Charles\Downloads\DJI_0684.jpg")
            Dim Latitude As String
            Dim Longitude As String
            Dim indexLat As Integer = Image.IndexOf("Latitude=")
            If indexLat >= 0 Then
                For i = indexLat + 10 To indexLat + 20
                    Latitude += Image(i)
                Next
            End If
            Dim indexLong As Integer = Image.IndexOf("Longitude=")
            If indexLong >= 0 Then
                For i = indexLong + 11 To indexLong + 21
                    Longitude += Image(i)
                Next
            End If
            TextBox1.Text = Latitude & "," & Longitude
        End Sub
    Hope that helps.
    Last edited by cPubis; Aug 8th, 2022 at 01:31 PM.

  7. #7
    Addicted Member
    Join Date
    Jan 2013
    Posts
    142

    Re: Getting GPS Latitide and Long from an imagem file: Negative Second?!

    Hello there,

    Please note that the code above was specifically created for your "weird" picture, so it could give you wrong readings with "not-so-wired" pictures. If you want a code that works correctly with all pictures then use the following code:

    Code:
    Imports System.Drawing.Imaging
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim img As Image = Image.FromFile("C:\Users\Charles\Downloads\DJI_0684.jpg")
            Dim LatRef As PropertyItem = img.GetPropertyItem(1)
            Dim Lat As PropertyItem = img.GetPropertyItem(2)
            Dim LngRef As PropertyItem = img.GetPropertyItem(3)
            Dim Lng As PropertyItem = img.GetPropertyItem(4)
            TextBox1.Text = gps(LatRef, Lat) & "," & gps(LngRef, Lng)
        End Sub
        Private Function gps(ByVal Ref As PropertyItem, ByVal Prop As PropertyItem) As Single
            Dim deg1 As UInteger = BitConverter.ToUInt32(Prop.Value, 0)
            Dim deg2 As UInteger = BitConverter.ToUInt32(Prop.Value, 4)
            Dim deg As Single = deg1 / CSng(deg2)
            Dim min1 As UInteger = BitConverter.ToUInt32(Prop.Value, 8)
            Dim min2 As UInteger = BitConverter.ToUInt32(Prop.Value, 12)
            Dim min As Single = min1 / CSng(min2)
            Dim sec1 As UInteger = BitConverter.ToUInt32(Prop.Value, 16)
            Dim sec2 As UInteger = BitConverter.ToUInt32(Prop.Value, 20)
            Dim sec As Single = sec1 / CSng(sec2)
            Dim coor As Single = deg + (min / 60.0F) + (sec / 3600.0F)
            Dim Specifier As String = System.Text.Encoding.ASCII.GetString(New Byte(0) {Ref.Value(0)})
            If Specifier = "S" OrElse Specifier = "W" Then coor = 0 - coor
            Return coor
        End Function
    End Class
    Hope that helps.

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2022
    Posts
    5

    Re: Getting GPS Latitide and Long from an imagem file: Negative Second?!

    Quote Originally Posted by cPubis View Post
    Hi there,

    The code is fine, I have several gps readers and each of them gives different reading. This picture is weird!

    Anyway, if the rest of your pictures are the same you can use the following code to read their "raw" gps info directly from the image.

    Make a new form with one textbox and copy the code. The textbox will give you negative output, but it's correct negative.

    Code:
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim Image As String = File.ReadAllText("C:\Users\Charles\Downloads\DJI_0684.jpg")
            Dim Latitude As String
            Dim Longitude As String
            Dim indexLat As Integer = Image.IndexOf("Latitude=")
            If indexLat >= 0 Then
                For i = indexLat + 10 To indexLat + 20
                    Latitude += Image(i)
                Next
            End If
            Dim indexLong As Integer = Image.IndexOf("Longitude=")
            If indexLong >= 0 Then
                For i = indexLong + 11 To indexLong + 21
                    Longitude += Image(i)
                Next
            End If
            TextBox1.Text = Latitude & "," & Longitude
        End Sub
    Hope that helps.
    Ihuuullll! It worked out!! Thank you so much, you saved my week!

    The coordinate is correct. Now I just need to convert to DMS, but that's easy.

    Thanks again for spending your time on this solution. God bless you!

  9. #9
    Addicted Member
    Join Date
    Jan 2013
    Posts
    142

    Re: Getting GPS Latitide and Long from an imagem file: Negative Second?!

    Happy to help my friend.
    If you believe that your issue is resolved, please mark the thread Resolved. Thanks.

  10. #10

    Thread Starter
    New Member
    Join Date
    Aug 2022
    Posts
    5

    Re: Getting GPS Latitide and Long from an imagem file: Negative Second?!

    Quote Originally Posted by cPubis View Post
    Hello there,

    Please note that the code above was specifically created for your "weird" picture, so it could give you wrong readings with "not-so-wired" pictures. If you want a code that works correctly with all pictures then use the following code:

    Code:
    Imports System.Drawing.Imaging
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim img As Image = Image.FromFile("C:\Users\Charles\Downloads\DJI_0684.jpg")
            Dim LatRef As PropertyItem = img.GetPropertyItem(1)
            Dim Lat As PropertyItem = img.GetPropertyItem(2)
            Dim LngRef As PropertyItem = img.GetPropertyItem(3)
            Dim Lng As PropertyItem = img.GetPropertyItem(4)
            TextBox1.Text = gps(LatRef, Lat) & "," & gps(LngRef, Lng)
        End Sub
        Private Function gps(ByVal Ref As PropertyItem, ByVal Prop As PropertyItem) As Single
            Dim deg1 As UInteger = BitConverter.ToUInt32(Prop.Value, 0)
            Dim deg2 As UInteger = BitConverter.ToUInt32(Prop.Value, 4)
            Dim deg As Single = deg1 / CSng(deg2)
            Dim min1 As UInteger = BitConverter.ToUInt32(Prop.Value, 8)
            Dim min2 As UInteger = BitConverter.ToUInt32(Prop.Value, 12)
            Dim min As Single = min1 / CSng(min2)
            Dim sec1 As UInteger = BitConverter.ToUInt32(Prop.Value, 16)
            Dim sec2 As UInteger = BitConverter.ToUInt32(Prop.Value, 20)
            Dim sec As Single = sec1 / CSng(sec2)
            Dim coor As Single = deg + (min / 60.0F) + (sec / 3600.0F)
            Dim Specifier As String = System.Text.Encoding.ASCII.GetString(New Byte(0) {Ref.Value(0)})
            If Specifier = "S" OrElse Specifier = "W" Then coor = 0 - coor
            Return coor
        End Function
    End Class
    Hope that helps.
    This solution is even better! Thank you so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width