-
Notifications
You must be signed in to change notification settings - Fork 0
/
location.go
66 lines (56 loc) · 1.97 KB
/
location.go
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
// functions for reading EXIF data from images to capture the location photos were taken
package main
import (
"log"
"strconv"
"strings"
"github.com/gosexy/exif"
"github.com/x1um1n/checkerr"
)
// getLocation opens a local file, extracts the lat/long from EXIF data & returns it as a coordinate struct
func getLocation(path string) (c Coordinate, e error) {
log.Println("Reading EXIF data from " + path)
parser := exif.New()
e = parser.Open(path)
if !checkerr.Check(e, "Error parsing EXIF data from ", path) {
if parser.Tags["Latitude"] != "" {
if parser.Tags["North or South Latitude"] != "" {
if parser.Tags["Longitude"] != "" {
if parser.Tags["East or West Longitude"] != "" {
c = parseCoord(parser.Tags["Latitude"], parser.Tags["North or South Latitude"], parser.Tags["Longitude"], parser.Tags["East or West Longitude"])
} else {
log.Println("Location either missing or incomplete in EXIF data")
}
} else {
log.Println("Location either missing or incomplete in EXIF data")
}
} else {
log.Println("Location either missing or incomplete in EXIF data")
}
} else {
log.Println("Location either missing or incomplete in EXIF data")
}
}
return
}
// parseCoordString takes an EXIF lat/long string and converts it to a coordinate
func parseCoordString(val string) float64 {
chunks := strings.Split(val, ",")
hours, _ := strconv.ParseFloat(strings.TrimSpace(chunks[0]), 64)
minutes, _ := strconv.ParseFloat(strings.TrimSpace(chunks[1]), 64)
seconds, _ := strconv.ParseFloat(strings.TrimSpace(chunks[2]), 64)
return hours + (minutes / 60) + (seconds / 3600)
}
// parseCoord takes location values from EXIF data and returns a Coordinate struct
func parseCoord(latVal, latRef, lngVal, lngRef string) (c Coordinate) {
lat := parseCoordString(latVal)
lng := parseCoordString(lngVal)
if latRef == "S" { // N is "+", S is "-"
lat *= -1
}
if lngRef == "W" { // E is "+", W is "-"
lng *= -1
}
c = Coordinate{lat, lng}
return
}