Merge branch 'zserg-gh-44'
This commit is contained in:
		
						commit
						91a7622967
					
				@ -105,7 +105,7 @@ def decimal_to_dms(decimal, signed=True):
 | 
			
		||||
 | 
			
		||||
def dms_to_decimal(degrees, minutes, seconds, direction=' '):
 | 
			
		||||
    sign = 1
 | 
			
		||||
    if(direction[0] in 'NEne'):
 | 
			
		||||
    if(direction[0] in 'WSws'):
 | 
			
		||||
        sign = -1
 | 
			
		||||
    return (
 | 
			
		||||
        float(degrees) + float(minutes) / 60 + float(seconds) / 3600
 | 
			
		||||
 | 
			
		||||
@ -29,6 +29,11 @@ class Media(object):
 | 
			
		||||
 | 
			
		||||
    __name__ = 'Media'
 | 
			
		||||
 | 
			
		||||
    d_coordinates = {
 | 
			
		||||
            'latitude' : 'latitude_ref',
 | 
			
		||||
            'longitude': 'longitude_ref'
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
    def __init__(self, source=None):
 | 
			
		||||
        self.source = source
 | 
			
		||||
        self.exif_map = {
 | 
			
		||||
 | 
			
		||||
@ -6,7 +6,6 @@ image objects (JPG, DNG, etc.).
 | 
			
		||||
"""
 | 
			
		||||
 | 
			
		||||
import imghdr
 | 
			
		||||
import LatLon
 | 
			
		||||
import os
 | 
			
		||||
import pyexiv2
 | 
			
		||||
import re
 | 
			
		||||
@ -68,9 +67,7 @@ class Photo(Media):
 | 
			
		||||
        if(not self.is_valid()):
 | 
			
		||||
            return None
 | 
			
		||||
 | 
			
		||||
        key = self.exif_map['latitude']
 | 
			
		||||
        if(type == 'longitude'):
 | 
			
		||||
            key = self.exif_map['longitude']
 | 
			
		||||
        key = self.exif_map[type]
 | 
			
		||||
        exif = self.get_exif()
 | 
			
		||||
 | 
			
		||||
        if(key not in exif):
 | 
			
		||||
@ -79,29 +76,12 @@ class Photo(Media):
 | 
			
		||||
        try:
 | 
			
		||||
            # this is a hack to get the proper direction by negating the
 | 
			
		||||
            #   values for S and W
 | 
			
		||||
            latdir = 1
 | 
			
		||||
            if(type == 'latitude' and str(exif[self.exif_map['latitude_ref']].value) == 'S'):  # noqa
 | 
			
		||||
                latdir = -1
 | 
			
		||||
 | 
			
		||||
            londir = 1
 | 
			
		||||
            if(type == 'longitude' and str(exif[self.exif_map['longitude_ref']].value) == 'W'):  # noqa
 | 
			
		||||
                londir = -1
 | 
			
		||||
 | 
			
		||||
            coords = exif[key].value
 | 
			
		||||
            if(type == 'latitude'):
 | 
			
		||||
                lat_val = LatLon.Latitude(
 | 
			
		||||
                    degree=coords[0],
 | 
			
		||||
                    minute=coords[1],
 | 
			
		||||
                    second=coords[2]
 | 
			
		||||
            return geolocation.dms_to_decimal(
 | 
			
		||||
                    *coords,
 | 
			
		||||
                    direction = exif[self.exif_map[self.d_coordinates[type]]].value
 | 
			
		||||
                    )
 | 
			
		||||
                return float(str(lat_val)) * latdir
 | 
			
		||||
            else:
 | 
			
		||||
                lon_val = LatLon.Longitude(
 | 
			
		||||
                    degree=coords[0],
 | 
			
		||||
                    minute=coords[1],
 | 
			
		||||
                    second=coords[2]
 | 
			
		||||
                )
 | 
			
		||||
                return float(str(lon_val)) * londir
 | 
			
		||||
 | 
			
		||||
        except KeyError:
 | 
			
		||||
            return None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -92,3 +92,13 @@ def time_convert(s_time):
 | 
			
		||||
        return time.gmtime((time.mktime(s_time)))
 | 
			
		||||
    else:
 | 
			
		||||
        return s_time
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# isclose(a,b,rel_tol)
 | 
			
		||||
# To compare float coordinates a and b
 | 
			
		||||
# with relative tolerance c
 | 
			
		||||
 | 
			
		||||
def isclose(a, b, rel_tol = 1e-8):
 | 
			
		||||
    diff = abs(a - b)
 | 
			
		||||
    return (diff <= abs(rel_tol * a) and
 | 
			
		||||
            diff <= abs(rel_tol * b))
 | 
			
		||||
 | 
			
		||||
@ -57,31 +57,31 @@ def test_get_coordinate_default():
 | 
			
		||||
    photo = Photo(helper.get_file('with-location.jpg'))
 | 
			
		||||
    coordinate = photo.get_coordinate()
 | 
			
		||||
 | 
			
		||||
    assert coordinate == 37.3667027222, coordinate
 | 
			
		||||
    assert helper.isclose(coordinate,37.3667027222), coordinate
 | 
			
		||||
 | 
			
		||||
def test_get_coordinate_latitude():
 | 
			
		||||
    photo = Photo(helper.get_file('with-location.jpg'))
 | 
			
		||||
    coordinate = photo.get_coordinate('latitude')
 | 
			
		||||
 | 
			
		||||
    assert coordinate == 37.3667027222, coordinate
 | 
			
		||||
    assert helper.isclose(coordinate,37.3667027222), coordinate
 | 
			
		||||
 | 
			
		||||
def test_get_coordinate_latitude_minus():
 | 
			
		||||
    photo = Photo(helper.get_file('with-location-inv.jpg'))
 | 
			
		||||
    coordinate = photo.get_coordinate('latitude')
 | 
			
		||||
 | 
			
		||||
    assert coordinate == -37.3667027222, coordinate
 | 
			
		||||
    assert helper.isclose(coordinate,-37.3667027222), coordinate
 | 
			
		||||
 | 
			
		||||
def test_get_coordinate_longitude():
 | 
			
		||||
    photo = Photo(helper.get_file('with-location.jpg'))
 | 
			
		||||
    coordinate = photo.get_coordinate('longitude')
 | 
			
		||||
 | 
			
		||||
    assert coordinate == -122.033383611, coordinate
 | 
			
		||||
    assert helper.isclose(coordinate,-122.033383611), coordinate
 | 
			
		||||
 | 
			
		||||
def test_get_coordinate_longitude_plus():
 | 
			
		||||
    photo = Photo(helper.get_file('with-location-inv.jpg'))
 | 
			
		||||
    coordinate = photo.get_coordinate('longitude')
 | 
			
		||||
 | 
			
		||||
    assert coordinate == 122.033383611, coordinate
 | 
			
		||||
    assert helper.isclose(coordinate,122.033383611), coordinate
 | 
			
		||||
 | 
			
		||||
def test_get_coordinates_without_exif():
 | 
			
		||||
    photo = Photo(helper.get_file('no-exif.jpg'))
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,3 @@
 | 
			
		||||
click>=6.2,<7.0
 | 
			
		||||
LatLon>=1.0.2,<2.0
 | 
			
		||||
requests>=2.9.1,<3.0
 | 
			
		||||
send2trash>=1.3.0,<2.0
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user