-
Notifications
You must be signed in to change notification settings - Fork 2
/
image.go
215 lines (191 loc) · 4.88 KB
/
image.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"fmt"
"github.com/rwcarlsen/goexif/exif"
"log"
"math"
"os"
"strings"
"time"
)
type imagePlacemark struct {
path string // location of the file relative to the root dir (should be normalized) (empty if pure external image)
iconPath string // location of the thumbnail (or the actual image) relative to the root dir (empty if pure external image)
rootDir string // actual location of the root dir (should be normalized) (empty if pure external image)
isInternal bool
isIconInternal bool
externalPath string
iconExternalPath string
pathInKml string // path used in KML file
iconPathInKml string // ~
origExif *exif.Exif
customData dataObj
name string
description string
dateTime time.Time
latitude float64
longitude float64
hasLocation bool
hasDateTime bool
width int64
length int64
}
/*
Decodes and returns the EXIF of the file.
*/
func (i *imagePlacemark) loadOrigExif(filepath string) error {
file, err := os.Open(filepath)
if err != nil {
return err
}
defer file.Close()
i.origExif, err = exif.Decode(file)
return err
}
/*
Sets image properties according to the EXIF of the image.
*/
func (i *imagePlacemark) applyDataFromExif() {
if i.origExif == nil {
return
}
// dateTime
t, err := i.origExif.DateTime()
if err == nil {
i.dateTime = t
i.hasDateTime = true
}
// latitude & longitude
lat, lon, err := i.origExif.LatLong()
if err == nil {
i.latitude = lat
i.longitude = lon
i.hasLocation = true
}
// width & height
if w, err := i.origExif.Get(exif.ImageWidth); err == nil {
i.width, _ = w.Int64(0)
}
if l, err := i.origExif.Get(exif.ImageLength); err == nil {
i.length, _ = l.Int64(0)
}
}
/*
Sets the field customData to the parameter
*/
func (i *imagePlacemark) setCustomData(data dataObj) {
i.customData = data
}
/*
Sets image properties according to the customData object for the image
Used JSON/YAML fields/keys: "external" string, "dateTime" string, "timeZone" string, "latitude" float64, "longitude" float64
*/
func (i *imagePlacemark) applyCustomData() {
if i.customData == nil {
return
}
var err error
// external path
if ext, ok := i.customData["external"]; ok {
i.externalPath = ext.(string)
i.iconExternalPath = ext.(string)
}
// dateTime (+ timeZone)
if dt, ok := i.customData["dateTime"]; ok {
exifTimeLayout := "2006:01:02 15:04:05"
dateStr := strings.Trim(dt.(string), "\x00 ")
location := time.Local
if tz, ok := i.customData["timeZone"]; ok {
if loc, err := time.LoadLocation(tz.(string)); err == nil {
location = loc
} else {
log.Println(err)
}
}
i.dateTime, err = time.ParseInLocation(exifTimeLayout, dateStr, location)
printIfErr(err)
if err == nil {
i.hasDateTime = true
}
}
// change timeZone only
if tz, ok := i.customData["timeZone"]; ok {
if _, dtExists := i.customData["dateTime"]; dtExists == false {
if newLoc, err := time.LoadLocation(tz.(string)); err == nil {
// calculate the difference between the new and the old timezone
oldLoc := i.dateTime.Location()
someTimeOldLoc := time.Date(2000,1,1,0,0,0,0, oldLoc)
someTimeNewLoc := time.Date(2000,1,1,0,0,0,0, newLoc)
timeZonesDiff := someTimeOldLoc.Sub(someTimeNewLoc)
// add the difference to the dateTime to fix the time zone (change the timestamp)
i.dateTime = i.dateTime.Add(timeZonesDiff)
} else {
log.Println(err)
}
}
}
// latitude & longitude
if lat, ok := i.customData["latitude"]; ok {
float, err := getFloat64(lat)
if err == nil {
i.latitude = float
i.hasLocation = true
} else {
i.latitude = 0
i.hasLocation = false
}
}
if lon, ok := i.customData["longitude"]; ok {
float, err := getFloat64(lon)
if err == nil {
i.longitude = float
i.hasLocation = true
} else {
i.longitude = 0
i.hasLocation = false
}
}
}
/*
Sets paths of image and icon used in KML doc based on whether external or internal path is preferable.
*/
func (i *imagePlacemark) setKmlPaths(preferExternal, preferExternalIcon bool) {
if preferExternal && i.externalPath != "" || i.path == "" {
i.pathInKml = i.externalPath
} else {
i.pathInKml = joinPaths("files", i.path)
i.isInternal = true
}
// icon
if preferExternalIcon && i.iconExternalPath != "" || i.iconPath == "" {
i.iconPathInKml = i.iconExternalPath
} else {
i.iconPathInKml = joinPaths("files", i.iconPath)
i.isIconInternal = true
}
}
/*
Tries to convert the interface{} to float64.
*/
func getFloat64(unk interface{}) (float64, error) {
switch i := unk.(type) {
case float64:
return i, nil
case float32:
return float64(i), nil
case int64:
return float64(i), nil
case int32:
return float64(i), nil
case int:
return float64(i), nil
case uint64:
return float64(i), nil
case uint32:
return float64(i), nil
case uint:
return float64(i), nil
default:
return math.NaN(), fmt.Errorf("non-numeric type could not be converted to float")
}
}