1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use crate::errors::*;

use exif;
use std::io;


#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Location {
    latitude: f64,
    longitude: f64,
}

impl Location {
    pub fn try_from(fields: &[exif::Field]) -> Result<Location> {
        let mut builder = LocationBuilder::default();
        fields.iter()
            .map(|f| builder.add_one(f))
            .collect::<Result<()>>()?;
        builder.build()
    }
}

#[derive(Debug, Default)]
struct LocationBuilder {
    latitude: Option<f64>,
    latitude_ref: Option<f64>,
    longitude: Option<f64>,
    longitude_ref: Option<f64>,
}

impl LocationBuilder {
    fn add_one(&mut self, f: &exif::Field) -> Result<()> {
        debug!("Exif tag: {:?}, {}", f.tag, f.value.display_as(f.tag));
        match f.tag {
            exif::Tag::GPSLatitudeRef => self.latitude_ref = Some(cardinal_direction_modifier(&f.value)?),
            exif::Tag::GPSLongitudeRef => self.longitude_ref = Some(cardinal_direction_modifier(&f.value)?),
            exif::Tag::GPSLatitude => self.latitude = Some(dms_to_float(&f.value)?),
            exif::Tag::GPSLongitude => self.longitude = Some(dms_to_float(&f.value)?),
            _ => (),
        };
        Ok(())
    }

    fn build(self) -> Result<Location> {
        let latitude = self.latitude
            .ok_or_else(|| format_err!("Missing latitude field"))?;
        let latitude_ref = self.latitude_ref
            .ok_or_else(|| format_err!("Missing latitude field"))?;

        let longitude = self.longitude
            .ok_or_else(|| format_err!("Missing latitude field"))?;
        let longitude_ref = self.longitude_ref
            .ok_or_else(|| format_err!("Missing latitude field"))?;

        Ok(Location {
            latitude: latitude * latitude_ref,
            longitude: longitude * longitude_ref,
        })
    }
}

pub fn gps(img: &[u8]) -> Result<Option<Location>> {
    let mut buf = io::BufReader::new(img);
    let reader = exif::Reader::new(&mut buf)?;
    let fields = reader.fields();
    debug!("Exif fields: {:?}", fields);

    let location = Location::try_from(fields).ok();
    Ok(location)
}

pub fn dms_to_float(dms: &exif::Value) -> Result<f64> {
    let dms = match dms {
        exif::Value::Rational(dms) => dms,
        _ => panic!("Unexpected exif value for dms"),
    };

    if dms.len() != 3 {
        bail!("Incorrect numbers for floats for dms");
    }

    let degrees = dms[0].to_f64();
    let minutes = dms[1].to_f64();
    let seconds = dms[2].to_f64();

    let float = degrees + minutes/60.0 + seconds/3600.0;
    let float = (float * 1000000.0).round() / 1000000.0;
    Ok(float)
}

pub fn cardinal_direction_modifier(value: &exif::Value) -> Result<f64> {
    match value {
        exif::Value::Ascii(s) => {
            let s = s.get(0)
                .ok_or_else(|| format_err!("Cardinal direction value is empty"))?;

            match s.get(0) {
                Some(b'N') => Ok(1.0),
                Some(b'S') => Ok(-1.0),
                Some(b'E') => Ok(1.0),
                Some(b'W') => Ok(-1.0),
                _ => bail!("Unexpected cardinal direction"),
            }
        },
        _ => bail!("Unexpected exif value"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_init;

    #[test]
    fn verify_exif_location() {
        test_init();

        let location = Location::try_from(&[
            exif::Field {
                tag: exif::Tag::GPSLatitudeRef,
                thumbnail: false,
                value: exif::Value::Ascii(vec![&[b'N']]),
            }, exif::Field {
                tag: exif::Tag::GPSLongitudeRef,
                thumbnail: false,
                value: exif::Value::Ascii(vec![&[b'E']]),
            }, exif::Field {
                tag: exif::Tag::GPSLatitude,
                thumbnail: false,
                value: exif::Value::Rational(vec![exif::Rational {
                    num: 43,
                    denom: 1,
                }, exif::Rational {
                    num: 28,
                    denom: 1,
                }, exif::Rational {
                    num: 176399999,
                    denom: 100000000,
                }]),
            }, exif::Field {
                tag: exif::Tag::GPSLongitude,
                thumbnail: false,
                value: exif::Value::Rational(vec![exif::Rational {
                    num: 11,
                    denom: 1,
                }, exif::Rational {
                    num: 53,
                    denom: 1,
                }, exif::Rational {
                    num: 742199999,
                    denom: 100000000,
                }]),
            },
        ]).unwrap();
        println!("{:?}", location);

        assert_eq!(location, Location {
            latitude: 43.467157,
            longitude: 11.885395
        });
    }

    #[test]
    fn verify_dms() {
        test_init();

        let latitude = dms_to_float(&exif::Value::Rational(vec![exif::Rational {
            num: 43,
            denom: 1,
        }, exif::Rational {
            num: 28,
            denom: 1,
        }, exif::Rational {
            num: 176399999,
            denom: 100000000,
        }])).unwrap();

        assert_eq!(latitude, 43.467157);
    }
}