1

I've been using python for a while but I still run into elementary problems. I'm currently using exifread library in python to get latitude and longitude. I need to convert the DMS values to DD.

So far I have written the following:

    #!/bin/python
import math
import exifread
path_name = './IMAG0658.jpg'
f = open(path_name, 'rb')

tags = exifread.process_file(f)

GPSLat = tags['GPS GPSLatitude']
print tags['GPS GPSLatitudeRef']
print GPSLat.values[0]
print GPSLat.values[1]
print GPSLat.values[2]
GPSDDLat = GPSLat.values[0] + ((GPSLat.values[1] / 60) + (GPSLat.values[2]/3600))
print GPSDDLat

However it crashes after printing the following:

$python tempexif.py
N
33
52
433621/10000
Traceback (most recent call last):
  File "tempexif.py", line 14, in <module>
    GPSDDLat = GPSLat.values[0] + ((GPSLat.values[1] / 60) + (GPSLat.values[2]/3600))
TypeError: unsupported operand type(s) for /: 'instance' and 'int'

How do I do math against the list values rather than the object itself?

1
  • Sample image you can find here Commented Oct 7, 2018 at 21:31

2 Answers 2

1

Your code is not complete since you do not provide the input file IMAG0658.jpg. But some things can still be said and we can still answer your question.

From your print statements it looks like GPSLat.values[0] and GPSLat.values[1] are both int values. But we see that GPSLat.values[2] looks like a fraction and thus is not an int value. From your error traceback we see that either GPSLat.values[1] or GPSLat.values[2] is actually an object instance. So at least one of those values prints like a numeric value but is actually an object instance.

The solution to your problem is to take those apparent numeric values, which are shown in print functions and thus are accessible by the str function as well, and convert them to numeric values before further computation. Since GPSLat.values[2] does not print like an int but does print like a fraction, we can use the Fraction type in the fractions module to do an easy conversion to float. So for each value, we use str to get the numeric-looking value then convert that to an actual numeric float value.

So we can do

from fractions import Fraction

GPSDDLat = ( float(str(GPSLat.values[0])) 
           + float(str(GPSLat.values[1])) / 60
           + float(Fraction(str(GPSLat.values[2]))) / 3600
           )

But without that file I cannot do an actual check of that code.

Sign up to request clarification or add additional context in comments.

6 Comments

@EirNym: Are you sure? Fraction values should not cause that traceback since that kind of value can be used in the / operation.
the proposed solution worked. I'm don't understand why each one needed to be cast as a str then as a float rather than just casting as a float but it works. The last one being converted to a fraction then to float makes sense though... it's the casting as a string first that gets me.
Yes, I introspected objects and source speaks by itself. They modified it, unfortunately.
@StevenLutz: I do not know if each one needs all that work to become a float value. But I could see from your code and the printout that my technique would work and had no assurance that another one would also work.
I did try directly casting it as float prior to posting and it didn't work
|
0

GPSLat.values[x] is an object (an instance, as it says in the Traceback).

In order to obtain the degree you need to do:

def val_2_deg(value):
    return float(value.num) / float(value.den)

and then change your formula to:

GPSDDLat = val_2_deg(GPSLat.values[0]) + val_2_deg(GPSLat.values[1])/60 + val_2_deg(GPSLat.values[2])/3600

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.