Home > database >  How to get GPS Coordinates out of String (Python)
How to get GPS Coordinates out of String (Python)

Time:03-31

for my new pyhton project I need to get coordinates out of a string.

my actual code looks like this:

import exifread
from selenium import webdriver

with open("pictures/img-1.jpg", "rb") as file:
    tags = exifread.process_file(file)

    for key, value in tags.items():
               
        gesamtzeile = str(key)   ": "   str(value)
        print(gesamtzeile)

in the variable "gesamtzeile" there are outputs like:

Image YResolution: 72

OR LIKE:

GPS GPSLatitude: [28, 12, 1857/50]

etc.....

Now i want to check in the loop if the string(key) contains the word "GPS". If it contains "GPS", I want to filter the coordinates.

Can somebody please help me? I am new to python and don't get it at the moment;/

Thanks a lot

I tried to use "split" and tried to split it at ":" but I only want to split if the string contains "GPS".I don't know how to do this in python

CodePudding user response:

The exifread module returns these values in custom types. You just need to fetch the values:

import exifread

with open("pictures/img-1.jpg", "rb") as file:
    tags = exifread.process_file(file)
    if "GPS GPSLatitude" in tags:
        l1 = tags["GPS GPSLatitude"].values
        # Now l1[0] is degrees, l1[1]is minutes, and
        # l1[2] is seconds as a ratio.
        print( "lat", li[0], "degrees", li[1], "minutes", float(li[2]), "seconds" )
    elif "GPS GPSLongitude" in tags:
        l1 = tags["GPS GPSLongitude"].values
        print( "long", li[0], "degrees", li[1], "minutes", float(li[2]), "seconds" )

CodePudding user response:

It's unclear what the type (class) of the key (I would assume str) or the value (could be str or list?) but in either case there is no need to join them to split them afterwards. The key in a dict must be immutable, the value can literally be any type: mutable or immutable. Do you only wish to print out the lines with co-ords? If so, perhaps you are not extracting the co-ords from a str, but identifying the GPS key-value sets in the dict:

import exifread
from selenium import webdriver

with open("pictures/img-1.jpg", "rb") as file:
    tags = exifread.process_file(file)

    for key, value in tags.items():
        if "GPS" in key:
            print(f'{key}: {value}')
  • Related