aboutsummaryrefslogtreecommitdiff
path: root/exif.go
blob: 1684d28102cb914381ca770d1bccdb570ca2ed0b (plain)
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
package main

import (
	"encoding/json"
	"os/exec"
	"strconv"

	"github.com/pkg/errors"
)

type ExifData struct {
	Latitude  float64
	Longitude float64
}

func exiftool(path string) (*ExifData, error) {
	cmd := exec.Command("exiftool", "-c", "%+.24f", "-j", path)
	output, err := cmd.Output()
	if err != nil {
		return nil, errors.Wrapf(err, "running exiftool")
	}

	type Output struct {
		GPSLatitude  string
		GPSLongitude string
	}

	os := []Output{}
	if err := json.Unmarshal(output, &os); err != nil || len(os) != 1 {
		return nil, errors.Wrapf(err, "parsing exiftool output")
	}
	o := os[0]

	latitude, err := strconv.ParseFloat(o.GPSLatitude, 64)
	if err != nil {
		return nil, errors.Wrapf(err, "parsing latitude '%v'", o.GPSLatitude)
	}

	longitude, err := strconv.ParseFloat(o.GPSLongitude, 64)
	if err != nil {
		return nil, errors.Wrapf(err, "parsing longitude '%v'", o.GPSLongitude)
	}

	return &ExifData{Latitude: latitude, Longitude: longitude}, nil
}