Last active
March 2, 2017 19:58
-
-
Save stephencoe/d8b6c0bdd793e0d21548efb543350e9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"encoding/json" | |
"time" | |
"log" | |
"os" | |
"path/filepath" | |
"flag" | |
"github.com/rwcarlsen/goexif/exif" | |
) | |
type LocationEntry struct { | |
Date time.Time | |
Lat, Lng float64 | |
} | |
func main() { | |
flag.Parse() | |
root := flag.Arg(0) | |
list := make([]LocationEntry, 0, 10) | |
walk := func (path string, info os.FileInfo, err error) error { | |
if err != nil { | |
fmt.Println(err) | |
return nil | |
} | |
if info.IsDir() || filepath.Ext(path) != ".jpg" { | |
return nil | |
} | |
f, err := os.Open(path) | |
if err != nil { | |
log.Fatal(err) | |
} | |
x, err := exif.Decode(f) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Two convenience functions exist for date/time taken and GPS coords: | |
tm, _ := x.DateTime() | |
lat, long, _ := x.LatLong() | |
f.Close() | |
if lat == 0 || long == 0 { | |
return nil | |
} | |
c := LocationEntry{tm, lat, long} | |
list = append(list, c) | |
return nil | |
} | |
err := filepath.Walk(root, walk) | |
if err != nil { | |
fmt.Printf("walk error [%v]\n", err) | |
} | |
jsonString, err := json.Marshal(list) | |
if err != nil { | |
fmt.Printf("json error [%v]\n", err) | |
} | |
fmt.Print(string(jsonString)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment