<< More Tutorials
How to read GPS coordinates from an image in Go using rethumb?
To read GPS coordinates from an image in Go use the following code:
// Special thanks to https://github.com/rogpeppe
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"math"
)
func main() {
resp, err := http.Get("http://api.rethumb.com/v1/exif/all/http://images.rethumb.com/image_exif_1.jpg")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var dat rethumbResponse
if err := json.Unmarshal(body, &dat); err != nil {
log.Fatal(err)
}
fmt.Println(dat.GPS)
}
type rethumbResponse struct {
GPS GPS
}
type GPS struct {
Lat degMinSec `json:"GPSLatitude"`
LatLabel string `json:"GPSLatitudeRef"`
Long degMinSec `json:"GPSLongitude"`
LongLabel string `json:"GPSLongitudeRef"`
}
func (gps GPS) String() string {
return fmt.Sprintf("%s%s %s%s", gps.Lat, gps.LatLabel, gps.Long, gps.LongLabel)
}
type degMinSec [3]precFloat
func (d degMinSec) String() string {
return fmt.Sprintf("%v° %v' %v''", d[0], d[1], d[2])
}
type precFloat struct {
n float64
prec int
}
func (f *precFloat) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
var prec string
if n, err := fmt.Sscanf(s, "%g/%s", &f.n, &prec); n != 2 || err != nil {
return fmt.Errorf("badly formed value %q", s)
}
if strings.TrimRight(prec, "0") != "1" {
return fmt.Errorf("precision is not power of 10")
}
f.prec = len(prec) - 1
return nil
}
func (f precFloat) String() string {
return fmt.Sprintf("%.*f", f.prec, f.n / math.Pow(10, float64(f.prec)))
}
Start using this example now
Use the following commands to get started:
$ git clone https://github.com/rethumb/rethumb-go-examples.git
$ cd rethumb-go-examples
$ go build read-gps-coordinates.go
$ ./read-gps-coordinates
More examples using Go
How to use rethumb in Go?
How to resize an image by width in Go using rethumb?
How to resize an image by height in Go using rethumb?
How to resize an image by width and height in Go using rethumb?
How to create a square thumbnail from an image in Go using rethumb?
How to read Exif data in json format from an image in Go using rethumb?
How to make an image responsive in Go using rethumb?
How to read GPS coordinates from an image in Go using rethumb?
How to convert an image to JPG, GIF, PNG, TIF or WebP in Go using rethumb?
How to resize an image to cover any dimensions in Go using rethumb?