-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathumap.go
248 lines (213 loc) · 6.18 KB
/
umap.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// umap functions
//
// FIX THIS - add support for tracks
//
// Combine all *.gpx > tracks.gx ( multiple <trk> tags )
package main
import (
_ "embed"
"fmt"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"text/template"
)
type PhotoData struct {
Photo string
}
type UmapData struct {
Name string
WebURL string
East float64
West float64
North float64
South float64
CenterLat float64
CenterLong float64
Has360Photos bool
HasPhotos bool
HasTracks bool
}
//go:embed photo360-html.template
var photo360htmlTemplate string
//go:embed umap.template
var umapTemplate string
func createUmapFiles(outputDirectory *string, webURL *string, filenames []string) error {
_, err := os.Stat(*outputDirectory)
if !os.IsNotExist(err) {
return fmt.Errorf("output directory %s already exists, refusing to overwrite", *outputDirectory)
}
err = os.Mkdir(*outputDirectory, 0755)
if err != nil {
return fmt.Errorf("unable to create output directory - %v", err)
}
// csv files for 360 and non-360 images
//
csvPlain, err := os.Create(path.Join(*outputDirectory, "photos.csv"))
if err != nil {
return fmt.Errorf("unable to create output file - %v", err)
}
defer csvPlain.Close()
csvPlain.WriteString("photo,lat,lon\n")
csv360, err := os.Create(path.Join(*outputDirectory, "photos360.csv"))
if err != nil {
return fmt.Errorf("unable to create output file - %v", err)
}
defer csv360.Close()
csv360.WriteString("photo,lat,lon\n")
east := -90.0
west := 90.0
north := -90.0
south := 90.0
totalLat := 0.0
totalLong := 0.0
totalCount := 0
has360Photos := false
hasPhotos := false
hasTracks := false
var gpxFiles []string
// process gpx files first
//
for _, imageFilename := range filenames {
if filepath.Ext(imageFilename) == ".gpx" {
gpxFiles = append(gpxFiles, imageFilename)
hasTracks = true
}
}
err = mergeGPX(gpxFiles, path.Join(*outputDirectory, "tracks.gpx"))
if err != nil {
return fmt.Errorf("unable to create tracks.gpx file - %v", err)
}
// process jpgs
for _, imageFilename := range filenames {
if filepath.Ext(imageFilename) == ".jpg" || filepath.Ext(imageFilename) == ".JPG" {
timestamp, lat, long, altitude, err := getMetadata(imageFilename)
if err != nil || lat != lat || long != long {
if hasTracks {
lat, long, _, err = getMetadataFromGPX(timestamp, path.Join(*outputDirectory, "tracks.gpx"))
if err != nil {
log.Printf("%s: Unable to get metadata from gpx: %v, skipping picture\n", imageFilename, err)
continue
}
} else {
log.Printf("%s: Unable to get metadata: %v, skipping picture\n", imageFilename, err)
continue
}
}
log.Printf("%s: Timestamp %s\n", imageFilename, timestamp)
log.Printf("%s: Latitude %f, Longitude %f\n", imageFilename, lat, long)
log.Printf("%s: Altitude %f\n", imageFilename, altitude)
if long > east {
east = long
}
if long < west {
west = long
}
if lat > north {
north = lat
}
if lat < south {
south = lat
}
totalLat = totalLat + lat
totalLong = totalLong + long
totalCount = totalCount + 1
if is360(imageFilename) {
has360Photos = true
// update 360 csv
//
csv360.WriteString(fmt.Sprintf("%s,%f,%f\n", path.Base(imageFilename), lat, long))
// write 360 phto html
//
td := PhotoData{Photo: path.Base(imageFilename)}
t, err := template.New("umap").Parse(photo360htmlTemplate)
if err != nil {
if err != nil {
log.Printf("%s: Unable to get photo360-html.template: %v, skipping picture\n", imageFilename, err)
continue
}
}
html, err := os.Create(path.Join(*outputDirectory, path.Base(imageFilename)+".html"))
if err != nil {
return fmt.Errorf("unable to create output file - %v", err)
}
defer html.Close()
err = t.Execute(html, td)
if err != nil {
if err != nil {
log.Printf("%s: Unable to process photo360-html.template: %v, skipping picture\n", imageFilename, err)
continue
}
}
} else {
hasPhotos = true
// update plain csv
//
csvPlain.WriteString(fmt.Sprintf("%s,%f,%f\n", path.Base(imageFilename), lat, long))
// create thumbnail
//
thumb := path.Join(*outputDirectory, path.Base(imageFilename)+"-thumb.jpg")
err := exec.Command("convert", imageFilename, "-resize", "450", thumb).Run()
if err != nil {
log.Printf("%s: Unable to create thumbnail: %v, skipping picture\n", imageFilename, err)
continue
}
}
// copy original photo
//
r, err := os.Open(imageFilename)
if err != nil {
log.Printf("%s: Unable to copy photo: %v\n", imageFilename, err)
continue
}
defer r.Close()
w, err := os.Create(path.Join(*outputDirectory, path.Base(imageFilename)))
if err != nil {
log.Printf("%s: Unable to copy photo: %v\n", imageFilename, err)
continue
}
defer w.Close()
w.ReadFrom(r)
}
}
// umap file
//
td := UmapData{Name: path.Base(*webURL),
WebURL: *webURL,
East: east,
West: west,
North: north,
South: south,
CenterLat: totalLat / float64(totalCount),
CenterLong: totalLong / float64(totalCount),
Has360Photos: has360Photos,
HasPhotos: hasPhotos,
HasTracks: hasTracks}
t, err := template.New("umap").Parse(umapTemplate)
if err != nil {
if err != nil {
return fmt.Errorf("unable to get umap.template: %v", err)
}
}
umap, err := os.Create(path.Join(*outputDirectory, "photos.umap"))
if err != nil {
return fmt.Errorf("unable to create output file - %v", err)
}
defer umap.Close()
err = t.Execute(umap, td)
if err != nil {
if err != nil {
return fmt.Errorf("unable to process umap.template: %v", err)
}
}
log.Printf("uMap files have been generated in %s directory\n", *outputDirectory)
log.Printf("To use in uMap :\n")
log.Printf("1. Copy photos, html pages and csv files to %s\n", *webURL)
log.Printf("2. On uMap server, click \"Create a map\"\n")
log.Printf("3. Click \"Edit map settings\" ( cog wheel ), \"Advanced actions\" then \"Empty\"\n")
log.Printf("4. Click \"Import data\" ( up arrow ), browse and upload photos.umap then \"Import\"\n")
log.Printf("5. Click \"Save\" and \"Disable editing\"\n")
return nil
}